Which function returns a pair after division ? (integer,frac)
Ki Rill
rill.ki at yahoo.com
Tue Sep 19 03:52:11 UTC 2023
On Tuesday, 19 September 2023 at 03:44:18 UTC, Vitaliy Fadeev
wrote:
> What D function or D operator does this?
>
> ```asm
> IDIV EAX, r/m32
> ```
>
> ```
> IDIV 5, 2
> EAX = 2
> EDX = 1
> ```
>
> and returns (2,1) at once?
You can either use function `out` parameters with return value or
`tuples`:
```D
import std.typecons;
// with tuples
auto getVal(int a, int b) {
// ...
return tuple(integer, frac);
}
// out params
int getVal2(int a, int b, out int frac) {
// ...
frac = _fraq;
return integer;
}
// USAGE
{
// with tuples
auto v = getVal(5, 2);
assert(v[0] == 2);
assert(v[1] == 1);
// with out params
int frac;
int integer = getVal2(5, 2, frac);
assert(integer == 2);
assert(frac == 1);
}
```
More information about the Digitalmars-d-learn
mailing list