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.

  1. Unqual!F quantize(F val, F unit)
    quantize
    (
    alias rfunc = rint
    F
    )
    (
    const F val
    ,
    const F unit
    )
    if (
    is(typeof(rfunc(F.init)) : F) &&
    )
  2. Unqual!F quantize(F val, E exp)
  3. Unqual!F quantize(F val)

Examples

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));
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);
assert(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);

Meta