英語版
このページの英語版を見る

std.math.rounding

これは std.math.
浮動小数点数を丸めるためのいくつかの関数が含まれている。
Authors:
Walter Bright, Don Clugston, Conversion of CEPHES math library to D by Iain Buclaw and David Nadlinger
pure nothrow @nogc @trusted real ceil(real x);

pure nothrow @nogc @trusted double ceil(double x);

pure nothrow @nogc @trusted float ceil(float x);
は、x の値を次の整数に四捨五入して返す。 (を返す。)
Examples:
import std.math.traits : isNaN;

writeln(ceil(+123.456L)); // +124
writeln(ceil(-123.456L)); // -123
writeln(ceil(-1.234L)); // -1
writeln(ceil(-0.123L)); // 0
writeln(ceil(0.0L)); // 0
writeln(ceil(+0.123L)); // 1
writeln(ceil(+1.234L)); // 2
writeln(ceil(real.infinity)); // real.infinity
assert(isNaN(ceil(real.nan)));
assert(isNaN(ceil(real.init)));
pure nothrow @nogc @trusted real floor(real x);

pure nothrow @nogc @trusted double floor(double x);

pure nothrow @nogc @trusted float floor(float x);
x関数は、xの値を次の整数に四捨五入して返す。 (を返す。)
Examples:
import std.math.traits : isNaN;

writeln(floor(+123.456L)); // +123
writeln(floor(-123.456L)); // -124
writeln(floor(+123.0L)); // +123
writeln(floor(-124.0L)); // -124
writeln(floor(-1.234L)); // -2
writeln(floor(-0.123L)); // -1
writeln(floor(0.0L)); // 0
writeln(floor(+0.123L)); // 0
writeln(floor(+1.234L)); // 1
writeln(floor(real.infinity)); // real.infinity
assert(isNaN(floor(real.nan)));
assert(isNaN(floor(real.init)));
Unqual!F quantize(alias rfunc = rint, F)(const F val, const F unit)
if (is(typeof(rfunc(F.init)) : F) && isFloatingPoint!F);
の倍数に丸める。 valの倍数に丸める。 unitrfunc の倍数に丸める。 を指定する。デフォルトはrint で、現在の丸めモードを使用する。 である。
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.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);
Unqual!F quantize(real base, alias rfunc = rint, 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 = rint, F)(const F val)
if (is(typeof(rfunc(F.init)) : F) && isFloatingPoint!F);
丸める valの倍数に丸める。 pow(base, exp)rfunc は、使用する丸め関数を指定する。 デフォルトはrint で、現在の丸めモードを使用する。 である。
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 real nearbyint(real x);
現在の丸めモードを使用して、x を最も近い整数値に丸める。 モードを使用する。
rint関数と異なり、nearlyintはFE_INEXACT例外を発生させない。 例外を発生させない。
Examples:
import std.math.traits : isNaN;

writeln(nearbyint(0.4)); // 0
writeln(nearbyint(0.5)); // 0
writeln(nearbyint(0.6)); // 1
writeln(nearbyint(100.0)); // 100

assert(isNaN(nearbyint(real.nan)));
writeln(nearbyint(real.infinity)); // real.infinity
writeln(nearbyint(-real.infinity)); // -real.infinity
pure nothrow @nogc @safe real rint(real x);

pure nothrow @nogc @safe double rint(double x);

pure nothrow @nogc @safe float rint(float x);
現在の丸めモードを使用して、xを最も近い整数値に丸める。 モードを使用する。
戻り値がxと等しくない場合、FE_IFXACT例外が発生する。 例外が発生する。
nearbyintは同じ操作を行うが は FE_INEXACT 例外をセットしない。
Examples:
import std.math.traits : isNaN;

version (IeeeFlagsSupport) resetIeeeFlags();
writeln(rint(0.4)); // 0
version (IeeeFlagsSupport) assert(ieeeFlags.inexact);

writeln(rint(0.5)); // 0
writeln(rint(0.6)); // 1
writeln(rint(100.0)); // 100

assert(isNaN(rint(real.nan)));
writeln(rint(real.infinity)); // real.infinity
writeln(rint(-real.infinity)); // -real.infinity
pure nothrow @nogc @trusted long lrint(real x);
現在の丸めモードを使用して、xを最も近い整数値に丸める。 モードを使用する。
これは一般に、浮動小数点数を整数に変換する最も速い方法である。 を整数に変換する最も速い方法である。この関数の結果は丸めモードに依存する。 の結果は丸めモードに依存する。 デフォルトの丸めモード(偶数整数に丸める)を使用する場合 lrint(4.5) == 4, lrint(5.5)==6.
Examples:
writeln(lrint(4.5)); // 4
writeln(lrint(5.5)); // 6
writeln(lrint(-4.5)); // -4
writeln(lrint(-5.5)); // -6

writeln(lrint(int.max - 0.5)); // 2147483646L
writeln(lrint(int.max + 0.5)); // 2147483648L
writeln(lrint(int.min - 0.5)); // -2147483648L
writeln(lrint(int.min + 0.5)); // -2147483648L
nothrow @nogc @trusted auto round(real x);
x の値を整数に丸めたものを返す。 xの小数部がちょうど0.5の場合、戻り値は0から切り捨てられる。 はゼロから切り捨てられる。
Returns:
Areal
Examples:
writeln(round(4.5)); // 5
writeln(round(5.4)); // 5
writeln(round(-4.5)); // -5
writeln(round(-5.1)); // -5
nothrow @nogc @trusted long lround(real x);
最も近い整数に丸めた x の値を返す。
xの小数部がちょうど0.5の場合、戻り値はゼロから四捨五入される。 される。
この関数はDigital Mars Cランタイムには実装されていない。
Examples:
version (CRuntime_DigitalMars) {}
else
{
    writeln(lround(0.49)); // 0
    writeln(lround(0.5)); // 1
    writeln(lround(1.5)); // 2
}
pure nothrow @nogc @trusted real trunc(real x);
x の整数部分を返し、小数部分を取り除く。 これは「チョップ」丸めとしても知られている。 pure すべてのプラットフォームで使用できる。
Examples:
writeln(trunc(0.01)); // 0
writeln(trunc(0.49)); // 0
writeln(trunc(0.5)); // 0
writeln(trunc(1.5)); // 1
pure nothrow @nogc @safe long rndtol(real x);

pure nothrow @nogc @safe long rndtol(double x);

pure nothrow @nogc @safe long rndtol(float x);
x を現在の丸めモードで long 値に丸めたものを返す。 もし x の整数値が がlong.maxより大きい場合、結果は 不定である。
Examples:
writeln(rndtol(1.0)); // 1L
writeln(rndtol(1.2)); // 1L
writeln(rndtol(1.7)); // 2L
writeln(rndtol(1.0001)); // 1L