Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

std.math.remainder

This is a submodule of std.math.
It contains several versions of remainder calculation.
Authors:
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger
nothrow @nogc @trusted real fmod(real x, real y);
Calculates the remainder from the calculation x/y.
Returns:
The value of x - i * y, where i is the number of times that y can be completely subtracted from x. The result has the same sign as x.
Special Values
x y fmod(x, y) invalid?
±0.0 not 0.0 ±0.0 no
±∞ anything NAN yes
anything ±0.0 NAN yes
!=±∞ ±∞ x no
Examples:
import std.math.operations : feqrel;
import std.math.traits : isIdentical, isNaN;

assert(isIdentical(fmod(0.0, 1.0), 0.0));
assert(fmod(5.0, 3.0).feqrel(2.0) > 16);
assert(isNaN(fmod(5.0, 0.0)));
nothrow @nogc @trusted real modf(real x, ref real i);
Breaks x into an integral part and a fractional part, each of which has the same sign as x. The integral part is stored in i.
Returns:
The fractional part of x.
Special Values
x i (on input) modf(x, i) i (on return)
±∞ anything ±0.0 ±∞
Examples:
import std.math.operations : feqrel;

real frac;
real intpart;

frac = modf(3.14159, intpart);
assert(intpart.feqrel(3.0) > 16);
assert(frac.feqrel(0.14159) > 16);
nothrow @nogc @trusted real remainder(real x, real y);

nothrow @nogc @trusted real remquo(real x, real y, out int n);
Calculate the remainder x REM y, following IEC 60559.
REM is the value of x - y * n, where n is the integer nearest the exact value of x / y. If |n - x / y| == 0.5, n is even. If the result is zero, it has the same sign as x. Otherwise, the sign of the result is the sign of x / y. Precision mode has no effect on the remainder functions.
remquo returns n in the parameter n.
Special Values
x y remainder(x, y) n invalid?
±0.0 not 0.0 ±0.0 0.0 no
±∞ anything -NAN ? yes
anything ±0.0 ±NAN ? yes
!= ±∞ ±∞ x ? no
Examples:
import std.math.operations : feqrel;
import std.math.traits : isNaN;

assert(remainder(5.1, 3.0).feqrel(-0.9) > 16);
assert(remainder(-5.1, 3.0).feqrel(0.9) > 16);
writeln(remainder(0.0, 3.0)); // 0.0

assert(isNaN(remainder(1.0, 0.0)));
assert(isNaN(remainder(-1.0, 0.0)));
Examples:
import std.math.operations : feqrel;

int n;

assert(remquo(5.1, 3.0, n).feqrel(-0.9) > 16 && n == 2);
assert(remquo(-5.1, 3.0, n).feqrel(0.9) > 16 && n == -2);
assert(remquo(0.0, 3.0, n) == 0.0 && n == 0);