Report a bug
If you spot a problem with this page, click here to create a GitHub 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.
mir.bignum.decimal
Stack-allocated decimal type.
Note The module doesn't provide full arithmetic API for now.
- struct
Decimal
(size_t maxSize64) if (maxSize64 && (maxSize64 <= (ushort).max)); - Stack-allocated decimal type.Parameters:
maxSize64 count of 64bit words in coefficient Examples:import mir.conv: to; Decimal!3 decimal; DecimalExponentKey key; import mir.math.constant: PI; assert(decimal.fromStringImpl("3.141592653589793378e-10", key)); assert(cast(double) decimal.view == double(PI) / 1e10); assert(key == DecimalExponentKey.e);
Examples:import mir.conv: to; Decimal!3 decimal; DecimalExponentKey key; assert(decimal.fromStringImpl("0", key)); assert(key == DecimalExponentKey.none); assert(decimal.exponent == 0); assert(decimal.coefficient.length == 0); assert(!decimal.coefficient.sign); assert(cast(double) decimal.coefficient == 0); assert(decimal.fromStringImpl("-0.0", key)); assert(key == DecimalExponentKey.dot); assert(decimal.exponent == -1); assert(decimal.coefficient.length == 0); assert(decimal.coefficient.sign); assert(cast(double) decimal.coefficient == 0); assert(decimal.fromStringImpl("0e0", key)); assert(key == DecimalExponentKey.e); assert(decimal.exponent == 0); assert(decimal.coefficient.length == 0); assert(!decimal.coefficient.sign); assert(cast(double) decimal.coefficient == 0);
- sizediff_t
exponent
; - BigInt!maxSize64
coefficient
; - DecimalView!size_t
view
();
const DecimalView!(const(size_t))view
(); - pure @nogc @safe this(C)(scope const(C)[]
str
)
if (isSomeChar!C); - Examples:
import mir.math.constant: PI; Decimal!2 decimal = "3.141592653589793378e-40"; // constructor assert(cast(double) decimal.view == double(PI) / 1e40);
- this(T)(const T
x
)
if (isFloatingPoint!T && (maxSize64 >= 1 + (T.mant_dig >= 64))); - Constructs Decimal from the floating point number using the Ryu algorithm.The number is the shortest decimal representation that being converted back would result the same floating-point number.Examples:
// float and double can be used to construct Decimal of any length auto decimal64 = Decimal!1(-1.235e-7); assert(decimal64.exponent == -10); assert(decimal64.coefficient == -1235); // real number may need Decimal at least length of 2 auto decimal128 = Decimal!2(-1.235e-7L); assert(decimal128.exponent == -10); assert(decimal128.coefficient == -1235); decimal128 = Decimal!2(1234e3f); assert(decimal128.exponent == 3); assert(decimal128.coefficient == 1234);
- ref auto
opAssign
(size_t rhsMaxSize64)(auto ref scope const Decimal!rhsMaxSize64rhs
) return
if (rhsMaxSize64 < maxSize64); - pure nothrow @nogc @safe bool
fromStringImpl
(C)(scope const(C)[]str
, out DecimalExponentKeykey
)
if (isSomeChar!C); - Returns:false in case of overflow or incorrect string.
Precondition non-empty coefficients.
Examples:import mir.conv: to; Decimal!3 decimal; DecimalExponentKey key; assert(decimal.fromStringImpl("1.334", key)); assert(cast(double) decimal.view == 1.334); assert(key == DecimalExponentKey.dot); assert(decimal.fromStringImpl("+0.334e-5"w, key)); assert(cast(double) decimal.view == 0.334e-5); assert(key == DecimalExponentKey.e); assert(decimal.fromStringImpl("-334D-5"d, key)); assert(cast(double) decimal.view == -334e-5); assert(key == DecimalExponentKey.D); assert(decimal.fromStringImpl("2482734692817364218734682973648217364981273648923423", key)); assert(cast(double) decimal.view == 2482734692817364218734682973648217364981273648923423.0); assert(key == DecimalExponentKey.none); assert(decimal.fromStringImpl(".023", key)); assert(cast(double) decimal.view == .023); assert(key == DecimalExponentKey.dot); assert(decimal.fromStringImpl("0E100", key)); assert(cast(double) decimal.view == 0); assert(key == DecimalExponentKey.E); assert(decimal.fromStringImpl("-nan", key)); assert(decimal.coefficient.length == 0); assert(cast(double) decimal.view == 0); assert(decimal.coefficient.sign); assert(key == DecimalExponentKey.nan); assert(decimal.fromStringImpl("inf", key)); assert(cast(double) decimal.view == 0); assert(key == DecimalExponentKey.infinity); assert(!decimal.fromStringImpl("3.3.4", key)); assert(!decimal.fromStringImpl("3.4.", key)); assert(!decimal.fromStringImpl("4.", key)); assert(!decimal.fromStringImpl(".", key)); assert(!decimal.fromStringImpl("0.", key)); assert(!decimal.fromStringImpl("00", key)); assert(!decimal.fromStringImpl("0d", key));
- const pure nothrow @safe immutable(C)[]
toString
(C = char)(NumericSpecspec
= NumericSpec.init)
if (isSomeChar!C && isMutable!C); - Examples:
auto str = "-3.4010447314490204552169750449563978034784726557588085989975288830070948234680e-13245"; auto decimal = Decimal!4(str); assert(decimal.toString == str, decimal.toString); decimal = Decimal!4.init; assert(decimal.toString == "0.0");
- const void
toString
(C = char, W)(ref scope Ww
, NumericSpecspec
= NumericSpec.init)
if (isSomeChar!C && isMutable!C); - Examples:Check @nogc toString impl
import mir.format: stringBuf; auto str = "5.28238923728e-876543210"; auto decimal = Decimal!1(str); stringBuf buffer; buffer << decimal; assert(buffer.data == str, buffer.data);
- const T
opCast
(T, bool wordNormalized = false, bool nonZero = false)()
if (isFloatingPoint!T && isMutable!T); - Mir parsing supports up-to quadruple precision. The conversion error is 0 ULP for normal numbers. Subnormal numbers with an exponent greater than or equal to -512 have upper error bound equal to 1 ULP.
- const @property bool
isNaN
(); - const @property bool
isInfinity
();
Copyright © 2016-2021 by Ilya Yaroshenko | Page generated by
Ddoc on Wed Jan 20 14:16:08 2021