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
int exponent;
BigInt!maxSize64 coefficient;
DecimalView!size_t view();

const DecimalView!(const(size_t)) view();
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.
pure nothrow @nogc @trusted bool parseDecimal(size_t maxSize64, C)(scope const(C)[] str, ref Decimal!maxSize64 decimal, out DecimalExponentKey key)
if (isSomeChar!C);
Returns:
false in case of integer overflow or incorrect input.
enum DecimalExponentKey: int;
Examples:
Decimal!3 decimal;
DecimalExponentKey key;

assert("1.334".parseDecimal(decimal, key));
assert(cast(double) decimal.view == 1.334);
assert(key == DecimalExponentKey.dot);

assert("+0.334e-5"w.parseDecimal(decimal, key));
assert(cast(double) decimal.view == 0.334e-5);
assert(key == DecimalExponentKey.e);

assert("-334D-5"d.parseDecimal(decimal, key));
assert(cast(double) decimal.view == -334e-5);
assert(key == DecimalExponentKey.D);

assert("2482734692817364218734682973648217364981273648923423".parseDecimal(decimal, key));
assert(cast(double) decimal.view == 2482734692817364218734682973648217364981273648923423.0);
assert(key == DecimalExponentKey.none);

assert(".023".parseDecimal(decimal, key));
assert(cast(double) decimal.view == .023);
assert(key == DecimalExponentKey.dot);

assert("0E100".parseDecimal(decimal, key));
assert(cast(double) decimal.view == 0);
assert(key == DecimalExponentKey.E);

assert("-nan".parseDecimal(decimal, key));
assert(cast(double) decimal.view == 0);
assert(decimal.coefficient.sign);
assert(key == DecimalExponentKey.nan);

assert("inf".parseDecimal(decimal, key));
assert(cast(double) decimal.view == 0);
assert(key == DecimalExponentKey.infinity);

assert(!"3.3.4".parseDecimal(decimal, key));
assert(!"3.4.".parseDecimal(decimal, key));
assert(!"4.".parseDecimal(decimal, key));
assert(!".".parseDecimal(decimal, key));
assert(!"0.".parseDecimal(decimal, key));
assert(!"00".parseDecimal(decimal, key));
assert(!"0d".parseDecimal(decimal, key));
none
dot
d
e
D
E
infinity
nan