View source code
Display the source code in std/math/rounding.d from which this page was generated on github.
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 local clone.

std.math.rounding.quantize - multiple declarations

Function quantize

Round val to a multiple of unit. rfunc specifies the rounding function to use; by default this is rint, which uses the current rounding mode.

Unqual!F quantize(alias rfunc, F) (
  const F val,
  const F unit
)
if (is(typeof(rfunc(F.init)) : F) && isFloatingPoint!F);

Example

import std.math.operations : isClose;

assert(isClose(12345.6789L.quantize(0.01L), 12345.68L));
assert(isClose(12345.6789L.quantize!floor(0.01L), 12345.67L));
assert(isClose(12345.6789L.quantize(22.0L), 12342.0L));

Example

import std.math.operations : isClose;
import std.math.traits : isNaN;

assert(isClose(12345.6789L.quantize(0), 12345.6789L));
assert(12345.6789L.quantize(real.infinity).isNaN);
assert(12345.6789L.quantize(real.nan).isNaN);
writeln(real.infinity.quantize(0.01L)); // real.infinity
assert(real.infinity.quantize(real.nan).isNaN);
assert(real.nan.quantize(0.01L).isNaN);
assert(real.nan.quantize(real.infinity).isNaN);
assert(real.nan.quantize(real.nan).isNaN);

Function quantize

Round val to a multiple of pow(base, exp). rfunc specifies the rounding function to use; by default this is rint, which uses the current rounding mode.

Unqual!F quantize(real base, alias rfunc, F, E) (
  const F val,
  const E exp
)
if (is(typeof(rfunc(F.init)) : F) && isFloatingPoint!F && isIntegral!E);

Unqual!F quantize(real base, long exp = 1, alias rfunc, F) (
  const F val
)
if (is(typeof(rfunc(F.init)) : F) && isFloatingPoint!F);

Example

import std.math.operations : isClose;

assert(isClose(12345.6789L.quantize!10(-2), 12345.68L));
assert(isClose(12345.6789L.quantize!(10, -2), 12345.68L));
assert(isClose(12345.6789L.quantize!(10, floor)(-2), 12345.67L));
assert(isClose(12345.6789L.quantize!(10, -2, floor), 12345.67L));

assert(isClose(12345.6789L.quantize!22(1), 12342.0L));
assert(isClose(12345.6789L.quantize!22, 12342.0L));

Authors

Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

License

Boost License 1.0.