Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

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 usinga local clone.

std.math.rounding

This is a submodule ofstd.math.
It contains several functions for rounding floating point numbers.
License:
Boost License 1.0.
Authors:
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger

Sourcestd/math/rounding.d

pure nothrow @nogc @trusted realceil(realx);

pure nothrow @nogc @trusted doubleceil(doublex);

pure nothrow @nogc @trusted floatceil(floatx);
Returns the value of x rounded upward to the next integer (toward positive infinity).
Examples:
import std.math.traits : isNaN;writeln(ceil(+123.456L));// +124writeln(ceil(-123.456L));// -123writeln(ceil(-1.234L));// -1writeln(ceil(-0.123L));// 0writeln(ceil(0.0L));// 0writeln(ceil(+0.123L));// 1writeln(ceil(+1.234L));// 2writeln(ceil(real.infinity));// real.infinityassert(isNaN(ceil(real.nan)));assert(isNaN(ceil(real.init)));
pure nothrow @nogc @trusted realfloor(realx);

pure nothrow @nogc @trusted doublefloor(doublex);

pure nothrow @nogc @trusted floatfloor(floatx);
Returns the value of x rounded downward to the next integer (toward negative infinity).
Examples:
import std.math.traits : isNaN;writeln(floor(+123.456L));// +123writeln(floor(-123.456L));// -124writeln(floor(+123.0L));// +123writeln(floor(-124.0L));// -124writeln(floor(-1.234L));// -2writeln(floor(-0.123L));// -1writeln(floor(0.0L));// 0writeln(floor(+0.123L));// 0writeln(floor(+1.234L));// 1writeln(floor(real.infinity));// real.infinityassert(isNaN(floor(real.nan)));assert(isNaN(floor(real.init)));
Unqual!Fquantize(alias rfunc = rint, F)(const Fval, const Funit)
if (is(typeof(rfunc(F.init)) : F) && isFloatingPoint!F);
Roundval to a multiple ofunit.rfunc specifies the rounding function to use; by default this isrint, which uses the current rounding mode.
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));
Examples:
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.infinityassert(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);
Unqual!Fquantize(real base, alias rfunc = rint, F, E)(const Fval, const Eexp)
if (is(typeof(rfunc(F.init)) : F) && isFloatingPoint!F && isIntegral!E);

Unqual!Fquantize(real base, long exp = 1, alias rfunc = rint, F)(const Fval)
if (is(typeof(rfunc(F.init)) : F) && isFloatingPoint!F);
Roundval to a multiple ofpow(base,exp).rfunc specifies the rounding function to use; by default this isrint, which uses the current rounding mode.
Examples:
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));
pure nothrow @nogc @safe realnearbyint(realx);
Rounds x to the nearest integer value, using the current rounding mode.
Unlike the rint functions, nearbyint does not raise the FE_INEXACT exception.
Examples:
import std.math.traits : isNaN;writeln(nearbyint(0.4));// 0writeln(nearbyint(0.5));// 0writeln(nearbyint(0.6));// 1writeln(nearbyint(100.0));// 100assert(isNaN(nearbyint(real.nan)));writeln(nearbyint(real.infinity));// real.infinitywriteln(nearbyint(-real.infinity));// -real.infinity
pure nothrow @nogc @safe realrint(realx);

pure nothrow @nogc @safe doublerint(doublex);

pure nothrow @nogc @safe floatrint(floatx);
Rounds x to the nearest integer value, using the current rounding mode.
If the return value is not equal to x, the FE_INEXACT exception is raised.
nearbyint performs the same operation, but does not set the FE_INEXACT exception.
Examples:
import std.math.traits : isNaN;version (IeeeFlagsSupport) resetIeeeFlags();writeln(rint(0.4));// 0version (IeeeFlagsSupport)assert(ieeeFlags.inexact);writeln(rint(0.5));// 0writeln(rint(0.6));// 1writeln(rint(100.0));// 100assert(isNaN(rint(real.nan)));writeln(rint(real.infinity));// real.infinitywriteln(rint(-real.infinity));// -real.infinity
pure nothrow @nogc @trusted longlrint(realx);
Rounds x to the nearest integer value, using the current rounding mode.
This is generally the fastest method to convert a floating-point number to an integer. Note that the results from this function depend on the rounding mode, if the fractional part of x is exactly 0.5. If using the default rounding mode (ties round to even integers) lrint(4.5) == 4, lrint(5.5)==6.
Examples:
writeln(lrint(4.5));// 4writeln(lrint(5.5));// 6writeln(lrint(-4.5));// -4writeln(lrint(-5.5));// -6writeln(lrint(int.max - 0.5));// 2147483646Lwriteln(lrint(int.max + 0.5));// 2147483648Lwriteln(lrint(int.min - 0.5));// -2147483648Lwriteln(lrint(int.min + 0.5));// -2147483648L
nothrow @nogc @trusted autoround(realx);
Return the value of x rounded to the nearest integer. If the fractional part of x is exactly 0.5, the return value is rounded away from zero.
Returns:
Areal.
Examples:
writeln(round(4.5));// 5writeln(round(5.4));// 5writeln(round(-4.5));// -5writeln(round(-5.1));// -5
nothrow @nogc @trusted longlround(realx);
Return the value of x rounded to the nearest integer.
If the fractional part of x is exactly 0.5, the return value is rounded away from zero.
Examples:
writeln(lround(0.49));// 0writeln(lround(0.5));// 1writeln(lround(1.5));// 2
pure nothrow @nogc @trusted realtrunc(realx);
Returns the integer portion of x, dropping the fractional portion. This is also known as "chop" rounding.pure on all platforms.
Examples:
writeln(trunc(0.01));// 0writeln(trunc(0.49));// 0writeln(trunc(0.5));// 0writeln(trunc(1.5));// 1
pure nothrow @nogc @safe longrndtol(realx);

pure nothrow @nogc @safe longrndtol(doublex);

pure nothrow @nogc @safe longrndtol(floatx);
Returns x rounded to a long value using the current rounding mode. If the integer value of x is greater than long.max, the result is indeterminate.
Examples:
writeln(rndtol(1.0));// 1Lwriteln(rndtol(1.2));// 1Lwriteln(rndtol(1.7));// 2Lwriteln(rndtol(1.0001));// 1L
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 00:54:02 2026

[8]ページ先頭

©2009-2026 Movatter.jp