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 using a local clone.

decimal.decimal

IEEE 754-2008 implementation of decimal floating point data types. Decimal values are represented in memory using an integral coefficient and a 10-based exponent. Implementation is based on binary integer decimal encoding, supported by Intel.

Decimal data types use the same semantics as the built-in floating point data type (NaNs, infinities, etc.), the main difference being that they use internally a 10 exponent instead of a 2 exponent.
The current implementation supports three decimal data types, as specified by IEEE 754-2008 standard. The supported types are: decimal32 , decimal64  and decimal128 , but they can be easily extended to other bit widths if a underlying unsigned integral type is provided.
Decimal data types are best used in financial applications because arithmetic operation results are exact.
Category Members
Classics copysign  fabs  fdim  fmod  fma  getNaNPayload  modf  NaN  nextAfter  nextDown  nextToward  nextUp  remainder  sgn 
Comparison approxEqual  cmp  fmax  fmaxAbs  fmin  fminAbs  isEqual  isGreater  isGreaterOrEqual  isGreaterOrUnordered  isIdentical  isLess  isLessOrEqual  isLessOrUnordered  isNotEqual  isUnordered  sameQuantum  totalOrder  totalOrderAbs 
Conversion fromDPD  fromMsCurrency  fromMsDecimal  to  toDPD  toExact  toMsCurrency  toMsDecimal 
Data types Decimal  decimal32  decimal64  decimal128  DecimalClass  DecimalControl  ExceptionFlags  Precision  RoundingMode 
Exceptions DecimalException  DivisionByZeroException  InexactException  InvalidOperationException  OverflowException  UnderflowException 
Exponentiations & logarithms cbrt  compound  exp  exp10  exp10m1  exp2  exp2m1  expm1  frexp  ilogb  ldexp  log  log10  log10p1  log2  log2p1  logp1  nextPow10  pow  quantexp  root  rsqrt  scalbn  sqrt  truncPow10 
Introspection decimalClass  isCanonical  isFinite  isInfinity  isNaN  isNormal  isPowerOf10  isSignaling  isSubnormal  isZero  signbit 
Reduction dot  poly  scaledProd  scaledProdSum  scaledProdDiff  sum  sumAbs  sumSquare 
Rounding ceil  floor  lrint  lround  nearbyint  quantize  rint  rndtonl  round  trunc 
Trigonometry acos  acosh  asin  asinh  atan  atan2  atan2pi  atanh  atanpi  cos  cosh  cospi  hypot  sin  sinh  sinpi  tan  tanh 


Context

All arithmetic operations are performed using a thread local context. The context is setting various environment options:

Operators

All floating point operators are implemented. Binary operators accept as left or right side argument any decimal, integral, character or binary floating point value.

Initialization

Creating decimal floating point values can be done in several ways:

Error handling

Errors occuring in arithmetic operations using decimal values can be handled in two ways. By default, the thread local context will throw exceptions for errors considered severe (InvalidOperationException , DivisionByZeroException  or OverflowException ). Any other error is considered silent and the context will only set corresponding error flags (ExceptionFlags.inexact  or ExceptionFlags.underflow )
Most of the operations will throw InvalidOperationException  if a signaling NaN is encountered, if not stated otherwise in the documentation. This behaviour is intended in order to avoid usage of unitialized variables (decimal values being by default always initialized to signaling NaN)
//these will throw:
auto a = decimal32() + 12;    //InvalidOperationException
auto b = decimal32.min / 0;   //DivisionByZeroException
auto c = decimal32.max * 2;   //OverflowException

//these will not throw:
auto d = decimal32(123456789);                  //inexact
auto e = decimal32.min_normal / decimal32.max;  //underflow
Default behaviour can be altered using DecimalControl  by setting or clearing corresponding traps:
DecimalControl.disableExceptions(ExceptionFlags.overflow)
//from now on OverflowException will not be thrown;

DecimalControl.enableExceptions(ExceptionFlags.inexact)
//from now on InexactException will be thrown


Exceptions and results

Values returned after an exception is thrown or after an error flag is set, depend on the current RoundingMode .
Exception tiesToEven tiesToAway towardPositive towardNegative towardZero
OverflowException  +∞ +∞ +∞ +max +max
OverflowException  -∞ -∞ -max -∞ -max
UnderflowException  ±0.0 ±0.0 +min_normal * epsilon -min_normal * epsilon ±0.0
DivisionByZeroException  ±∞ ±∞ ±∞ ±∞ ±∞
InvalidOperationException  NaN NaN NaN NaN NaN
InexactException  does not have a specific value associated.
The subnormal exception is not implemented because it is not part of the IEEE-754-2008 standard. If an operation results in a subnormal value (absolute value is smaller than min_normal), UnderflowException  is always thrown or ExceptionFlag.underflow  is always set. It's better to avoid subnormal values when performing calculations, the results of the operations involving such values are not exact.


Properties

The following properties are defined for each decimal type:
Constant Name decimal32 decimal64 decimal128
init initial value signaling NaN signaling NaN signaling NaN
nan Not a Number NaN NaN NaN
infinity positive infinity +∞ +∞ +∞
dig precision 7 16 34
epsilon smallest increment to the value 1 10-6 10-15 10-33
mant_dig number of bits in mantissa 24 54 114
max_10_exp maximum int value such that 10max_10_exp is representable 96 384 6144
min_10_exp minimum int value such that 10min_10_exp is representable and normalized -95 -383 -6143
max_2_exp maximum int value such that 2max_2_exp is representable 318 1275 20409
min_2_exp minimum int value such that 2min_2_exp is representable and normalized -315 -1272 -20406
max largest representable value that's not infinity 9.(9) * 1096 9.(9) * 10384 9.(9) * 106144
min_normal smallest normalized value that's not 0 10-95 10-383 10-6143


Useful constants

There are common constants defined for each type. Values int the tablebelow have 34 digits of precision corresponding to decimal128 data type; for decimal64 and decimal32, they are rounded away from 0 according to their respecive precision.
auto a = decimal32.PI;
auto b = decimal64.LN2;
auto c = decimal128.E;
Constant Formula Value
E e 2.7182818284590452353602874713526625
PI π 3.1415926535897932384626433832795029
PI_2 π/2 1.5707963267948966192313216916397514
PI_4 π/4 0.7853981633974483096156608458198757
M_1_PI 1/π 0.3183098861837906715377675267450287
M_2_PI 2/π 0.6366197723675813430755350534900574
M_2_SQRTPI 2/√π 1.1283791670955125738961589031215452
SQRT2 √2 1.4142135623730950488016887242096981
SQRT1_2 √½ 0.7071067811865475244008443621048490
LN10 loge10 2.3025850929940456840179914546843642
LOG2T log210 3.3219280948873623478703194294893902
LOG2E log2e 1.4426950408889634073599246810018921
LOG2 log102 0.3010299956639811952137388947244930
LOG10E log10e 0.4342944819032518276511289189166051
LN2 loge2 0.6931471805599453094172321214581766


Interaction with binary floating point

Even all decimal operations allows the usage of binary floating point values, such mixing must be avoided; Internally, binary floating point values are converted to decimal counterparts before any operation:
float f = 1.1;
decimal32 d = "2.5";
decimal32 e = d + f;
//behind the scene this is roughly equivalent with e = d + decimal32(f);
It is impossible to represent binary floating point values in full decimal precision. By default, float values are converted using 9 digits of precision, double values using 17 digits of precision and real values using 21 digits of precision;
float f = 1.1; //internal representation is 1.10000002384185791015625;
decimal32 d1 = d;  //1.100000, 9 digits from float, but decimal32 has a 7 digits precision
decimal64 d2 = d;  //1.10000002000000, 9 digits from float
decimal128 d3 = d; //1.10000002000000000000000000000000, 9 digits from float;
An exact conversion is possible only if the binary floating point value is an exact power of 2 and fits in the destination type precision or if it's a power of 5.
float f = 4.0;   //internally represented as 1.0 * 2^^2
decimal32 d = f; //internally represented as 0.4 * 10^^1

float f = 25.0;  //internally represented as 1.5625 * 2^^4
decimal32 d = f; //internally represented as 0.25 * 10^^2

float f = 2147483648; //internally represented as 1.0 * 2^^31
decimal32 d = f;      //inexact, internally represented as 0.2147484 * 10^^7
Binary floating point conversion is dependent on the RoundingMode :
double d = 2.7; //internal representation is 2.7000000476837158203125;
DecimalControl.rounding = RoundingMode.tiesToAway;
decimal64 d1 = d;  //d1 will be 2.700000047683716;
DecimalControl.rounding = RoundingMode.towardZero;
decimal64 d2 = d;  //d2 will be 2.700000047683715;
Only Intel 80-bit reals are supported. Any other real type is cast to double before any conversion.

Special remarks



Performance tips



License:
Boost License 1.0

Authors:
Răzvan Ștefănescu

Source

decimal.d

struct Decimal(int bits) if (bits == 32 || bits == 64 || bits == 128);
Decimal floating-point computer numbering format that occupies 4, 8 or 16 bytes in computer memory.

enum auto radix;
always 10 for decimal data types

this(T)(auto ref const T value);
Constructs a Decimal data type using the specified value

Params:
T value any integral, char, bool, floating point, decimal, string or character range value

Exceptions

Data type Invalid Overflow Underflow Inexact
integral
char
float
bool
decimal
string
range


Using integral values

auto a = decimal32(112);       //represented as 112 x 10^^0;
auto b = decimal32(123456789); //inexact, represented as 1234568 * x 10^^2


Using floating point values

auto a = decimal32(1.23);
//inexact, represented as 123 x 10^^-2,
//because floating point data cannot exactly represent 1.23
//in fact 1.23 as float is 1.230000019073486328125
auto b = decimal64(float.nan);


Using other decimal values

auto a = decimal32(decimal64(10));
auto b = decimal64(a);
auto c = decimal64(decimal128.nan);


Using strings or ranges

A decimal value can be defined based on decimal, scientific or hexadecimal representation:
  • values are rounded away from zero in case of precision overflow;
  • auto d = decimal32("2.3456789")
    //internal representation will be 2.345679
    //because decimal32 has a 7-digit precision
    
  • the exponent in hexadecimal notation is 10-based;
  • auto d1 = decimal64("0x00003p+21");
    auto d2 = decimal64("3e+21");
    assert (d1 == d2);
    
  • the hexadecimal notation doesn't have any decimal point, because there is no leading 1 as for binary floating point values;
  • there is no octal notation, any leading zero before the decimal point is ignored;
  • digits can be grouped using underscores;
  • case insensitive special values are accepted: nan, qnan, snan, inf, infinity;
  • there is no digit count limit for decimal representation, very large values are rounded and adjusted by increasing the 10-exponent;
  • auto d1 = decimal32("123_456_789_123_456_789_123_456_789_123"); //30 digits
    //internal representation will be 1.234568 x 10^^30
    
  • NaN payloads can be defined betwen optional brackets ([], (), {}, <>). The payload is unsigned and is accepted in decimal or hexadecimal format;
auto d = decimal32("10");              //integral
auto e = decimal64("125.43")           //floating point
auto f = decimal128("123.456E-32");    //scientific
auto g = decimal32("0xABCDEp+21");     //hexadecimal 0xABCD * 10^^21
auto h = decimal64("NaN1234");         //$(B NaN) with 1234 payload
auto i = decimal128("sNaN<0xABCD>")    //signaling $(B NaN) with a 0xABCD payload
auto j = decimal32("inf");             //infinity


Using char or bool values

These constructors are provided only from convenience, and to offer support for conversion function to. Char values are cast to unsigned int. Bool values are converted to 0.0 (false) or 1.0 (true)
auto a = decimal32(true); //1.0
auto b = decimal32('a');  //'a' ascii code (97)

auto c = to!decimal32(false); //phobos to!(bool, decimal32)
auto d = to!decimal128('Z');  //phobos to!(char, decimal128)


ref auto opAssign(T)(auto ref const T value);
Implementation of assignnment operator. It supports the same semantics as the constructor.

const T opCast(T)();
Implementation of cast operator. Supported casts: integral, floating point, decimal, char, bool

Exceptions

Data type Invalid Overflow Underflow Inexact
integral
char
float
bool
decimal


const pure nothrow @nogc @safe auto opUnary(string op : "+")();
const pure nothrow @nogc @safe auto opUnary(string op : "-")();
Implementation of +/- unary operators. These operations are silent, no exceptions are thrown

ref @safe auto opUnary(string op : "++")();
ref @safe auto opUnary(string op : "--")();
Implementation of ++/-- unary operators.

Exceptions

Value ++/-- Invalid Overflow Inexact
NaN NaN
±∞ ±∞
any any


const bool opEquals(T)(auto ref const T value);
Implementation of == operator. This operation is silent, no exceptions are thrown. Supported types : decimal, floating point, integral, char

const float opCmp(T)(auto ref const T value);
Implementation of comparison operator. Supported types : decimal, floating point, integral, char
this Value Result Invalid
NaN any NaN
any NaN NaN
any any ±1.0, 0.0


const auto opBinary(string op, T)(auto ref const T value) if (op == "+" || op == "-" || op == "*" || op == "/" || op == "%" || op == "^^");
const auto opBinaryRight(string op, T)(auto ref const T value) if (op == "+" || op == "-" || op == "*" || op == "/" || op == "%" || op == "^^");
auto opOpAssign(string op, T)(auto ref const T value) if (op == "+" || op == "-" || op == "*" || op == "/" || op == "%" || op == "^^");
Implementation of binary and assignment operators (+, -, *, /, %, ^^).

Returns:
the widest decimal value as result of the operation

Supported types

decimal, floating point, integral, char

Exceptions

Left Op Right Result Invalid Div0 Overflow Underflow Inexact
NaN any any NaN
any any NaN NaN
+∞ + -∞ NaN
+∞ + any +∞
any + +∞ +∞
-∞ + +∞ NaN
-∞ + any -∞
any + -∞ -∞
any + any any
+∞ - +∞ NaN
+∞ - any +∞
any - +∞ -∞
-∞ - -∞ NaN
-∞ - any -∞
any - -∞ -∞
any - any any
±∞ * 0.0 NaN
±∞ * any ±∞
any * any any
±∞ / ±∞ NaN
0.0 / 0.0 NaN
±∞ / any ±∞
any / 0.0 ±∞
any / any any
±∞ % any NaN
any % ±∞ NaN
any % 0.0 NaN
any % any any


const void toString(C)(scope void delegate(const(C)[]) sink, FormatSpec!C fmt) if (isSomeChar!C);
const void toString(C)(scope void delegate(const(C)[]) sink) if (isSomeChar!C);
Converts current value to string, passing it to the given sink using the specified format.

Params:
void delegate(const(C)[]) sink a delegate used to sink character arrays;
FormatSpec!C fmt a format specification;

Notes

This function is not intended to be used directly, it is used by the format, output or conversion family of functions from Phobos. All standard format options are supported, except digit grouping.

Supported formats

  • f, F - floating point notation
  • e, E - scientific notation
  • a, A - hexadecimal floating point notation
  • g, G - shortest representation between floating point and scientific notation
  • s, S - same as g, G


Throws:
FormatException if the format specifier is not supported

See Also:
FormatSpec format to writef writefln

const string toString();
Converts current value to string in floating point or scientific notation, which one is shorter.

const string toString(C)(FormatSpec!C fmt);
const string toString(C)(const(C)[] fmt);
Converts current value to string according to the format specification

pure nothrow @nogc @safe size_t toHash();
Returns a unique hash of the decimal value suitable for use in a hash table.

Notes

This function is not intended for direct use, it's provided as support for associative arrays.

alias decimal32 = Decimal!32.Decimal;
alias decimal64 = Decimal!64.Decimal;
alias decimal128 = Decimal!128.Decimal;
Shorthand notations for Decimal  types

template isDecimal(D...)
Returns true if all specified types are decimal types.

Examples:
static assert(isDecimal!decimal32);
static assert(isDecimal!(decimal32, decimal64));
static assert(!isDecimal!int);
static assert(!isDecimal!(decimal128, byte));


template CommonDecimal(T...) if (isDecimal!T)
Returns the most wide decimal type among the specified types

Examples:
static assert(is(CommonDecimal!(decimal32, decimal64) == decimal64));
static assert(is(CommonDecimal!(decimal32, decimal128) == decimal128));
static assert(is(CommonDecimal!(decimal64, decimal128) == decimal128));


abstract class DecimalException: object.Exception;
Root object for all decimal exceptions

class InvalidOperationException: decimal.decimal.DecimalException;
Thrown if any operand of a decimal operation is not a number or si not finite

class DivisionByZeroException: decimal.decimal.DecimalException;
Thrown if the denominator of a decimal division operation is zero.

class OverflowException: decimal.decimal.DecimalException;
Thrown if the result of a decimal operation exceeds the largest finite number of the destination format.

class UnderflowException: decimal.decimal.DecimalException;
Thrown if the result of a decimal operation is smaller the smallest finite number of the destination format.

class InexactException: decimal.decimal.DecimalException;
Thrown if the result of a decimal operation was rounded to fit in the destination format.

enum ExceptionFlags: uint;
These flags indicate that an error has occurred. They indicate that a 0, NaN or an infinity value has been generated, that a result is inexact, or that a signalling NaN has been encountered. If the corresponding traps are set using DecimalControl , an exception will be thrown after setting these error flags.

By default the context will have all error flags lowered and exceptions are thrown only for severe errors.

none
no error

invalidOperation
InvalidOperationException  is thrown if trap is set

divisionByZero
DivisionByZeroException  is thrown if trap is set

overflow
OverflowException  is thrown if trap is set

underflow
UnderflowException  is thrown if trap is set

inexact
InexactException  is thrown if trap is set

severe
group of errors considered severe: invalidOperation, divisionByZero, overflow

all
all errors

enum RoundingMode: int;
Rounding modes. To better understand how rounding is performed, consult the table below.

Value tiesToEven tiesToAway towardPositive towardNegative towardZero
+1.3 +1 +1 +2 +1 +1
+1.5 +2 +2 +2 +1 +1
+1.8 +2 +2 +2 +1 +1
-1.3 -1 -1 -1 -2 -1
-1.5 -2 -2 -1 -2 -1
-1.8 -2 -2 -1 -2 -1
+2.3 +2 +2 +3 +2 +2
+2.5 +2 +3 +3 +2 +2
+2.8 +3 +3 +3 +2 +2
-2.3 -2 -2 -2 -3 -2
-2.5 -2 -3 -2 -3 -2
-2.8 -3 -3 -2 -3 -2


tiesToEven
rounded away from zero; halfs are rounded to the nearest even number

tiesToAway
rounded away from zero

towardPositive
truncated toward positive infinity

towardNegative
truncated toward negative infinity

towardZero
truncated toward zero

alias Precision = uint;
Precision used to round decimal operation results. Every result will be adjusted to fit the specified precision. Use DecimalControl  to query or set the context precision

precisionDefault
use the default precision of the current type (7 digits for decimal32, 16 digits for decimal64 or 34 digits for decimal128)

precision32
use 32 bits precision (7 digits)

precision64
use 64 bits precision (16 digits)

precision128
use 128 bits precision (34 digits)

struct DecimalControl;
Container for decimal context control, provides methods to alter exception handling, manually edit error flags, adjust arithmetic precision and rounding mode

static RoundingMode rounding;
Gets or sets the rounding mode used when the result of an operation exceeds the decimal precision. See RoundingMode  for details.
DecimalControl.rounding = RoundingMode.tiesToEven;
decimal32 d1 = 123456789;
assert(d1 == 123456800);

DecimalControl.rounding = RoundingMode.towardNegative;
decimal32 d2 = 123456789;
assert(d2 == 123456700);


static Precision precision;
Gets or sets the precision applied to peration results. See Precision  for details.
DecimalControl.precision = precisionDefault;
decimal32 d1 = 12345;
assert(d1 == 12345);

DecimalControl.precision = 4;
decimal32 d2 = 12345;
assert(d2 == 12350);


static @safe void raiseFlags(const ExceptionFlags group);
Sets specified error flags. Multiple errors may be ORed together.
DecimalControl.raiseFlags(ExceptionFlags.overflow | ExceptionFlags.underflow);
assert (DecimalControl.overflow);
assert (DecimalControl.underflow);


static nothrow @nogc @safe void resetFlags(const ExceptionFlags group);
static nothrow @nogc @safe void resetFlags();
Unsets specified error flags. Multiple errors may be ORed together.
DecimalControl.resetFlags(ExceptionFlags.inexact);
assert(!DecimalControl.inexact);


static nothrow @nogc @safe void restoreFlags(const ExceptionFlags group);
Enables specified error flags (group) without throwing corresponding exceptions.
DecimalControl.restoreFlags(ExceptionFlags.underflow | ExceptionsFlags.inexact);
assert (DecimalControl.testFlags(ExceptionFlags.underflow | ExceptionFlags.inexact));


static nothrow @nogc @safe bool hasFlags(const ExceptionFlags group);
Checks if the specified error flags are set. Multiple exceptions may be ORed together.
DecimalControl.raiseFlags(ExceptionFlags.overflow | ExceptionFlags.underflow | ExceptionFlags.inexact);
assert (DecimalControl.hasFlags(ExceptionFlags.overflow | ExceptionFlags.inexact));


static nothrow @nogc @safe ExceptionFlags saveFlags();
Returns the current set flags.
DecimalControl.restoreFlags(ExceptionFlags.inexact);
assert (DecimalControl.saveFlags() & ExceptionFlags.inexact);


static nothrow @nogc @safe void disableExceptions(const ExceptionFlags group);
static nothrow @nogc @safe void disableExceptions();
Disables specified exceptions. Multiple exceptions may be ORed together.
DecimalControl.disableExceptions(ExceptionFlags.overflow);
auto d = decimal64.max * decimal64.max;
assert (DecimalControl.overflow);
assert (isInfinity(d));


static nothrow @nogc @safe void enableExceptions(const ExceptionFlags group);
Enables specified exceptions. Multiple exceptions may be ORed together.
DecimalControl.enableExceptions(ExceptionFlags.overflow);
try
{
    auto d = decimal64.max * 2;
}
catch (OverflowException)
{
    writeln("Overflow error")
}


static nothrow @nogc @property @safe ExceptionFlags enabledExceptions();
Extracts current enabled exceptions.
auto saved = DecimalControl.enabledExceptions;
DecimalControl.disableExceptions(ExceptionFlags.all);
DecimalControl.enableExceptions(saved);


static nothrow @nogc @property @safe bool invalidOperation();
static nothrow @nogc @property @safe bool divisionByZero();
static nothrow @nogc @property @safe bool overflow();
static nothrow @nogc @property @safe bool underflow();
static nothrow @nogc @property @safe bool inexact();
IEEE decimal context errors. By default, no error is set.
DecimalControl.disableExceptions(ExceptionFlags.all);
decimal32 uninitialized;
decimal64 d = decimal64.max * 2;
decimal32 e = uninitialized + 5.0;
assert(DecimalControl.overflow);
assert(DecimalControl.invalidOperation);


enum bool is754version1985;
true if this programming environment conforms to IEEE 754-1985

enum bool is754version2008;
true if this programming environment conforms to IEEE 754-2008

D acos(D)(auto ref const D x) if (isDecimal!D);
Calculates the arc cosine of x, returning a value ranging from 0 to π.

Exceptions

InvalidOperationException  x is signaling NaN or |x| > 1.0
InexactException  the result is inexact


Special values

x acos(x)
NaN NaN
-1.0 π
+1.0 +0.0
< -1.0 NaN
> +1.0 NaN


Examples:
decimal32 x = 0;
assert(acos(x) == decimal32.PI_2);


D acosh(D)(auto ref const D x) if (isDecimal!D);
Calculates the inverse hyperbolic cosine of x

Exceptions

InvalidOperationException  x is signaling NaN or x < 1.0
InexactException  the result is inexact


Special values

x acosh(x)
NaN NaN
+1.0 +0.0
+∞ +∞
< 1.0 NaN


Examples:
decimal32 x = 1;
assert (acosh(x) == 0);


bool approxEqual(D1, D2, D3, D4)(auto ref const D1 x, auto ref const D2 y, auto ref const D3 maxRelDiff, auto ref const D4 maxAbsDiff) if (isDecimal!(D1, D2, D3, D4));
bool approxEqual(D1, D2, D3)(auto ref const D1 x, auto ref const D2 y, auto ref const D3 maxRelDiff) if (isDecimal!(D1, D2, D3));
bool approxEqual(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
Computes whether two values are approximately equal, admitting a maximum relative difference, or a maximum absolute difference.

Params:
D1 x First item to compare
D2 y Second item to compare
D3 maxRelDiff Maximum allowable relative difference (defaults to 1e-5)
D4 maxAbsDiff Maximum allowable absolute difference (defaults to 1e-2)

Returns:
true if the two items are approximately equal under either criterium.

Notes

This operation is silent, does not throw any exceptions and it doesn't set any error flags.

D asin(D)(auto ref const D x) if (isDecimal!D);
Calculates the arc sine of x, returning a value ranging from -π/2 to +π/2.

Exceptions

InvalidOperationException  x is signaling NaN or |x| > 1.0
InexactException  the result is inexact


Special values

x asin(x)
NaN NaN
-1.0 -π/2
+1.0 +π/2
< -1.0 NaN
> +1.0 NaN


Examples:
decimal32 x = 1;
assert(asin(x) == decimal32.PI_2);
assert(asin(-x) == -decimal32.PI_2);


D asinh(D)(auto ref const D x) if (isDecimal!D);
Calculates the inverse hyperbolic sine of x

Exceptions

InvalidOperationException  x is signaling NaN
UnderflowException  the result is too small to be represented
InexactException  the result is inexact


Special values

x asinh(x)
NaN NaN
±0.0 ±0.0
±∞ ±∞


Examples:
decimal32 x = 0;
assert (asinh(x) == 0);


D atan(D)(auto ref const D x) if (isDecimal!D);
Calculates the arc tangent of x, returning a value ranging from -π/2 to π/2.

Exceptions

InvalidOperationException  x is signaling NaN
InexactException  the result is inexact
UnderflowException  the result is too small to be represented


Special values

x atan(x)
NaN NaN
±0.0 ±0.0
±∞ ±π/2


Examples:
decimal32 radians = 1;
assert(atan(radians) == decimal32.PI_4);


auto atan2(D1, D2)(auto ref const D1 y, auto ref const D2 x) if (isDecimal!(D1, D2));
Calculates the arc tangent of y / x, returning a value ranging from -π to π.

Exceptions

InvalidOperationException  x or y is signaling NaN
InexactException  the result is inexact
UnderflowException  the result is too small to be represented


Special values

y x atan2(y, x)
NaN any NaN
any NaN NaN
±0.0 -0.0 ±π
±0.0 +0.0 ±0.0
±0.0 <0.0 ±π
±0.0 >0.0 ±0.0
±∞ -∞ ±3π/4
±∞ +∞ ±π/4
±∞ any ±π/2
any -∞ ±π
any +∞ ±0.0


Examples:
decimal32 y = 10;
decimal32 x = 0;
assert (atan2(y, x) == decimal32.PI_2);


auto atan2pi(D1, D2)(auto ref const D1 y, auto ref const D2 x) if (isDecimal!(D1, D2));
Calculates the arc tangent of y / x divided by π, returning a value ranging from -1 to 1.

Exceptions

InvalidOperationException  x or y is signaling NaN
InexactException  the result is inexact
UnderflowException  the result is too small to be represented


Special values

y x atan2pi(y, x)
NaN any NaN
any NaN NaN
±0.0 -0.0 ±1.0
±0.0 +0.0 ±0.0
±0.0 <0.0 ±1.0
±0.0 >0.0 ±0.0
±∞ -∞ ±3/4
±∞ +∞ ±1/4
±∞ any ±1/2
any -∞ ±1.0
any +∞ ±0.0


Examples:
decimal32 y = 10;
decimal32 x = 0;
assert (atan2pi(y, x) == decimal32("0.5"));


D atanh(D)(auto ref const D x) if (isDecimal!D);
Calculates the inverse hyperbolic tangent of x

Exceptions

InvalidOperationException  x is signaling NaN or |x| > 1.0
DivisionByZeroException  |x| = 1.0
UnderflowException  the result is too small to be represented
InexactException  the result is inexact


Special values

x atanh(x)
NaN NaN
±0.0 ±0.0
±1.0 ±∞
>1.0 NaN
<1.0 NaN


Examples:
decimal32 x = 0;
assert (atanh(x) == 0);


D atanpi(D)(auto ref const D x) if (isDecimal!D);
Calculates the arc tangent of x divided by π, returning a value ranging from -1/2 to 1/2.

Exceptions

InvalidOperationException  x is signaling NaN
InexactException  the result is inexact
UnderflowException  the result is too small to be represented


Special values

x atan(x)
NaN NaN
±0.0 ±0.0
±∞ ±1/2


Examples:
decimal32 radians = 1;
assert (atanpi(radians) == decimal32("0.25"));


D cbrt(D)(auto ref const D x) if (isDecimal!D);
Computes the cubic root of x

Throws:
InvalidOperationException  x is signaling NaN
UnderflowException  cubic root of x is too small to be represented
InexactException  the result is inexact


Special values

x cbrt(x)
NaN NaN
±0.0 ±0.0
±∞ ±∞


Examples:
decimal32 x = 27;
assert (cbrt(x) == 3);


D ceil(D)(auto ref const D x) if (isDecimal!D);
Returns the value of x rounded upward to the next integer (toward positive infinity). This operation is silent, doesn't throw any exception.

Special values

x ceil(x)
NaN NaN
±0.0 ±0.0
±∞ ±∞


Examples:
assert (ceil(decimal32("123.456")) == 124);
assert (ceil(decimal32("-123.456")) == -123);


int cmp(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
Defines a total order on all decimal values.

Params:
D1 x a decimal value
D2 y a decimal value

Returns:
-1 if x precedes y, 0 if x is equal to y, +1 if x follows y

Notes

The total order is defined as:
- -sNaN < -NaN < -infinity < -finite < -0.0 < +0.0 < +finite < +infinity < +NaN < +sNaN
- for two NaN values the total order is defined based on the payload

Examples:
assert (cmp(-decimal32.nan, decimal64.max) == -1);
assert (cmp(decimal32.max, decimal128.min_normal) == 1);
assert (cmp(decimal64(0), -decimal64(0)) == 1);


auto compound(D)(auto ref const D x, const int n) if (isDecimal!D);
Computes (1 + x)n where n is an integer

Throws:
InvalidOperationException  x is signaling NaN or x < -1.0
DivisionByZeroException  x = -1.0 and n < 0
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  the result is inexact


Special values

x n compound(x, n)
sNaN any NaN
any 0 +1.0
-1.0 <0 +∞
-1.0 >0 +0.0
+∞ any +∞


Examples:
decimal32 x = "0.2";
assert (compound(x, 2) == decimal32("1.44"));


D1 copysign(D1, D2)(auto ref const D1 to, auto ref const D2 from) if (isDecimal!(D1, D2));
Copies the sign of a decimal value to another. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D1 to a decimal value to copy
D2 from a decimal value from which the sign is copied

Returns:
to with the sign of from

Examples:
decimal32 negative = -decimal32.min_normal;
decimal64 test = decimal64.max;
assert(copysign(test, negative) == -decimal64.max);


D cos(D)(auto ref const D x) if (isDecimal!D);
Returns cosine of x.

Throws:
InvalidOperationException  x is signaling NaN or ±∞
UnderflowException  result is too small to be represented
InexactException  the result is inexact


Special values

x cos(x)
NaN NaN
±∞ NaN
±0.0 +1.0
π/6 +√3/2
π/4 +√2/2
π/3 +0.5
π/2 +0.0
2π/3 -0.5
3π/4 -√2/2
5π/6 -√3/2
π -1.0


D cosh(D)(auto ref const D x) if (isDecimal!D);
Calculates the hyperbolic cosine of x.

Throws:
InvalidOperationException  x is signaling NaN
OverflowException  result is too big to be represented
InexactException  the result is inexact


Special values

x cosh(x)
NaN NaN
±∞ +∞
±0.0 +1.0


D cospi(D)(auto ref const D x) if (isDecimal!D);
Returns cosine of xπ.

Throws:
InvalidOperationException  x is signaling NaN or ±∞
UnderflowException  result is too small to be represented
InexactException  the result is inexact


Special values

x cospi(x)
NaN NaN
±∞ NaN
±0.0 +1.0
1/6 +√3/2
1/4 +√2/2
1/3 +0.5
1/2 +0.0
2/3 -0.5
3/4 -√2/2
5/6 -√3/2
1.0 -1.0


enum DecimalClass: int;
IEEE-754-2008 floating point categories

signalingNaN
quietNaN
a signalling NaN represents most of the time an uninitialized variable; a quiet NaN represents the result of an invalid operation

negativeInfinity
positiveInfinity
value represents infinity

negativeNormal
positiveNormal
value represents a normalized decimal value

negativeSubnormal
positiveSubnormal
value represents a subnormal decimal value

negativeZero
positiveZero
value is 0

DecimalClass decimalClass(D)(auto ref const D x) if (isDecimal!D);
Returns the decimal class where x falls into. This operation is silent, no exception flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
One of the members of DecimalClass  enumeration

Examples:
assert(decimalClass(decimal32.nan) == DecimalClass.quietNaN);
assert(decimalClass(decimal64.infinity) == DecimalClass.positiveInfinity);
assert(decimalClass(decimal128.max) == DecimalClass.positiveNormal);
assert(decimalClass(-decimal32.max) == DecimalClass.negativeNormal);
assert(decimalClass(decimal128.epsilon) == DecimalClass.positiveNormal);


D dot(D)(const(D)[] x, const(D)[] y) if (isDecimal!D);
Sums xi * yi using a higher precision, rounding only once at the end.

Returns:
x0 * y0 + x1 * y1 + ... + xn * yn

Notes

If x and y arrays are not of the same length, operation is performed for min(x.length, y.length);

Throws:
InvalidOperationException  any x is signaling NaN
InvalidOperationException  any combination of elements is (±∞, ±0.0) or (±0.0, ±∞)
InvalidOperationException  there are two products resulting in infinities of different sign
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


D exp(D)(auto ref const D x) if (isDecimal!D);
Calculates ex

Throws:
InvalidOperationException  x is signaling NaN
UnderflowException  ex is too small to be represented
OverflowException  ex is too big to be represented
InexactException  the result is inexact


Special values

x exp(x)
NaN NaN
±0.0 +1.0
-∞ 0
+∞ +∞


Examples:
decimal32 power = 1;
assert (exp(power) == decimal32.E);


D exp10(D)(auto ref const D x) if (isDecimal!D);
Calculates 10x

Throws:
InvalidOperationException  x is signaling NaN
UnderflowException  10x is too small to be represented
OverflowException  10x is too big to be represented
InexactException  the result is inexact


Special values

x exp10(x)
NaN NaN
±0.0 +1.0
-∞ +0.0
+∞ +∞


Examples:
decimal32 x = 3;
assert(exp10(x) == 1000);


D exp10m1(D)(auto ref const D x) if (isDecimal!D);
Calculates 10x - 1

Throws:
InvalidOperationException  x is signaling NaN
UnderflowException  10x - 1 is too small to be represented
OverflowException  10x - 1 is too big to be represented
InexactException  the result is inexact


Special values

x exp10m1(x)
NaN NaN
±0.0 ±0.0
-∞ -1.0
+∞ +∞


Examples:
decimal32 x = 3;
assert(exp10m1(x) == 999);


D exp2(D)(auto ref const D x) if (isDecimal!D);
Calculates 2x

Throws:
InvalidOperationException  x is signaling NaN
UnderflowException  2x is too small to be represented
OverflowException  2x is too big to be represented
InexactException  the result is inexact


Special values

x exp2(x)
NaN NaN
±0.0 +1.0
-∞ +0.0
+∞ +∞


Examples:
decimal32 x = 3;
assert(exp2(x) == 8);


D exp2m1(D)(auto ref const D x) if (isDecimal!D);
Calculates 2x - 1

Throws:
InvalidOperationException  x is signaling NaN
UnderflowException  2x - 1 is too small to be represented
OverflowException  2x - 1 is too big to be represented
InexactException  the result is inexact


Special values

x exp2m1(x)
NaN NaN
±0.0 ±0.0
-∞ -1.0
+∞ +∞


Examples:
decimal32 x = 3;
assert(exp2m1(x) == 7);


D expm1(D)(auto ref const D x) if (isDecimal!D);
Calculates ex - 1

Throws:
InvalidOperationException  x is signaling NaN
UnderflowException  ex - 1 is too small to be represented
OverflowException  ex - 1 is too big to be represented
InexactException  the result is inexact


Special values

x expm1(x)
NaN NaN
±0.0 ±0.0
-∞ -1.0
+∞ +∞


D fabs(D)(auto ref const D x) if (isDecimal!D);
Calculates |x|. This operation is silent, no error flags are set and no exceptions are thrown.

Examples:
assert(fabs(-decimal32.max) == decimal32.max);
assert(fabs(decimal64.infinity) == decimal64.infinity);


auto fdim(D1, D2)(auto ref const D1 x, auto ref const D2 y);
Returns the positive difference between x and y. If xy, retuns 0.0

Throws:
InvalidOperationException  either x or y is signaling NaN
InvalidOperationException  x and y are infinities with same sign
UnderflowException  result is subnormal
InexactException  result is inexact


Special values

x y fdim(x, y)
NaN any NaN
any NaN NaN
+∞ +∞ NaN
-∞ -∞ NaN
x > y x - y
xy 0.0


Examples:
decimal32 x = "10.4";
decimal32 y = "7.3";

assert (fdim(x, y) == decimal32("3.1"));
assert (fdim(y, x) == 0);


D floor(D)(auto ref const D x) if (isDecimal!D);
Returns the value of x rounded downward to the previous integer (toward negative infinity). This operation is silent, doesn't throw any exception.

Special values

x floor(x)
NaN NaN
±0.0 ±0.0
±∞ ±∞


Examples:
assert (floor(decimal32("123.456")) == 123);
assert (floor(decimal32("-123.456")) == -124);


auto fma(D1, D2, D3)(auto ref const D1 x, auto ref const D2 y, auto ref const D3 z) if (isDecimal!(D1, D2, D3));
Returns (x * y) + z, rounding only once according to the current precision and rounding mode

Throws:
InvalidOperationException  x, y or z is signaling NaN
InvalidOperationException  (x, y) = (±∞, ±0.0) or (±0.0, ±∞)
InvalidOperationException  x or y is infinite, z is infinite but has opposing sign
UnderflowException  result is too small to be represented
OverflowException  result is too big to be represented
InexactException  the result is inexact


Special values

x y z fma(x, y, z)
NaN any any NaN
any NaN any NaN
any any NaN NaN
±∞ ±0.0 any NaN
±0.0 ±∞ any NaN
+∞ >0.0 -∞ NaN
-∞ <0.0 -∞ NaN
-∞ <0.0 -∞ NaN
+∞ >0.0 -∞ NaN
-∞ >0.0 +∞ NaN
+∞ <0.0 +∞ NaN
+∞ <0.0 +∞ NaN
-∞ >0.0 +∞ NaN
>0.0 +∞ -∞ NaN
<0.0 -∞ -∞ NaN
<0.0 -∞ -∞ NaN
>0.0 +∞ -∞ NaN
>0.0 -∞ +∞ NaN
<0.0 +∞ +∞ NaN
<0.0 +∞ +∞ NaN
>0.0 -∞ +∞ NaN
+∞ >0.0 +∞ +∞
-∞ <0.0 +∞ +∞
+∞ <0.0 -∞ -∞
-∞ >0.0 -∞ -∞
>0.0 +∞ +∞ +∞
<0.0 -∞ +∞ +∞
<0.0 +∞ -∞ -∞
>0.0 -∞ -∞ -∞
+∞ >0.0 any +∞
-∞ <0.0 any +∞
+∞ <0.0 any -∞
-∞ >0.0 any -∞
>0.0 +∞ any +∞
<0.0 -∞ any +∞
<0.0 +∞ any -∞
>0.0 -∞ any -∞


Examples:
decimal32 x = 2;
decimal64 y = 3;
decimal128 z = 5;
assert (fma(x, y, z) == 11);


auto fmax(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!D1 && isDecimal!D2);
Returns the larger decimal value between x and y

Throws:
InvalidOperationException  if x or y is signaling NaN

Special values

x y fmax(x, y)
NaN any y
any NaN x


Examples:
decimal32 x = 3;
decimal64 y = -4;
assert (fmax(x, y) == 3);


auto fmaxAbs(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!D1 && isDecimal!D2);
Returns the larger decimal value between absolutes of x and y

Throws:
InvalidOperationException  if x or y is signaling NaN

Special values

x y fmaxAbs(x, y)
NaN any y
any NaN x


Examples:
decimal32 x = 3;
decimal64 y = -4;
assert (fmaxAbs(x, y) == -4);


auto fmin(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!D1 && isDecimal!D2);
Returns the smaller decimal value between x and y

Throws:
InvalidOperationException  if x or y is signaling NaN

Special values

x y fmin(x, y)
NaN any y
any NaN x


Examples:
decimal32 x = 3;
decimal64 y = -4;
assert (fmin(x, y) == -4);


auto fminAbs(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!D1 && isDecimal!D2);
Returns the smaller decimal value between absolutes of x and y

Throws:
InvalidOperationException  if x or y is signaling NaN

Special values

x y fminAbs(x, y)
NaN any y
any NaN x


Examples:
decimal32 x = 3;
decimal64 y = -4;
assert (fminAbs(x, y) == 3);


auto fmod(D1, D2)(auto ref const D1 x, auto ref const D2 y);
Calculates the remainder of the division x / y

Params:
D1 x dividend
D2 y divisor

Returns:
The value of x - n * y, where n is the quotient rounded toward zero of the division x / y

Throws:
InvalidOperationException  x or y is signaling NaN, x = ±∞, y = ±0.0
UnderflowException  result is too small to be represented
DivisionByZeroException  y = 0.0
InexactException  the result is inexact


Special values

x y fmod(x, y)
NaN any NaN
any NaN NaN
±∞ any NaN
any 0.0 NaN
any ±∞ NaN


Examples:
decimal32 x = "18.5";
decimal32 y = "4.2";
assert (fmod(x, y) == decimal32("1.7"));


D frexp(D)(auto ref const D x, out int y);
Separates decimal value into coefficient and exponent. This operation is silent, doesn't throw any exception.

Returns:
a result such as x = result * 10y and |result| < 1.0

Special values

x y frexp(x, y)
NaN 0 NaN
+∞ 0 +∞
-∞ 0 -∞
±0.0 0 ±0.0


Notes

This operation is silent, doesn't throw any exceptions and doesn't set any error flags. Signaling NaNs are quieted by this operation

pure nothrow @nogc @safe uint getNaNPayload(const decimal32 x);
pure nothrow @nogc @safe ulong getNaNPayload(const decimal64 x);
pure nothrow @nogc @safe ulong getNaNPayload(const decimal128 x, out ulong payloadHi);
Extracts the current payload from a NaN value

Note

These functions do not check if x is truly a NaN value before extracting the payload. Using them on finite values will extract a part of the coefficient

Examples:
decimal32 x = decimal32("nan(123)");
decimal64 y = decimal64("nan(456)");
decimal128 z = decimal128("nan(789)");

assert (getNaNPayload(x) == 123);
assert (getNaNPayload(y) == 456);
ulong hi;
assert (getNaNPayload(z, hi) == 789 && hi == 0);


auto hypot(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!D1 && isDecimal!D2);
Calculates the length of the hypotenuse of a right-angled triangle with sides of length x and y. The hypotenuse is the value of the square root of the sums of the squares of x and y.

Throws:
InvalidOperationException  x, y is signaling NaN
OverflowException  result is too big to be represented
InexactException  the result is inexact


Special values

x y hypot(x, y)
NaN NaN nan
±∞ any +∞
any ±∞ +∞
NaN any nan
any NaN nan
0.0 any y
any 0.0 x


Examples:
decimal32 x = 3;
decimal32 y = 4;
assert (hypot(x, y) == 5);


int ilogb(D)(auto ref const D x) if (isDecimal!D);
Returns the 10-exponent of x as a signed integral value..

Throws:
InvalidOperationException  if x is NaN, infinity or 0

Special values

x ilogb(x)
NaN int.min
±∞ int min + 1
±0.0 int.min + 2
±1.0 0


Examples:
assert (ilogb(decimal32(1234)) == 3);


bool isCanonical(D)(auto ref const D x) if (isDecimal!D);
Determines if x is canonical. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
true if x is canonical, false otherwise

Notes

A decimal value is considered canonical:
- if the value is NaN, the payload must be less than 10 precision - 1;
- if the value is infinity, no trailing bits are accepted;
- if the value is finite, the coefficient must be less than 10 precision.

Examples:
assert(isCanonical(decimal32.max));
assert(isCanonical(decimal64.max));
assert(!isCanonical(decimal32("nan(0x3fffff)")));


bool isFinite(D : Decimal!bits, int bits)(auto ref const D x);
Determines if x is a finite value. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
true if x is finite, false otherwise (NaN or infinity)

Examples:
assert(isFinite(decimal32.max));
assert(!isFinite(decimal64.nan));
assert(!isFinite(decimal128.infinity));


bool isIdentical(D)(auto ref const D x, auto ref const D y) if (isDecimal!D);
Checks if two decimal values are identical

Params:
D x a decimal value
D y a decimal value

Returns:
true if x has the same internal representation as y

Notes

Even if two decimal values are equal, their internal representation can be different:
- NaN values must have the same sign and the same payload to be considered identical; NaN(12) is not identical to NaN(13)
- Zero values must have the same sign and the same exponent to be considered identical; 0 * 103 is not identical to 0 * 105
- Finite values must be represented based on same exponent to be considered identical; 123 * 10-3 is not identical to 1.23 * 10-1

Examples:
assert (isIdentical(decimal32.min_normal, decimal32.min_normal));
assert (!isIdentical(decimal64("nan"), decimal64("nan<200>")));


bool isInfinity(D)(auto ref const D x) if (isDecimal!D);
Determines if x represents infinity. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
true if x is infinite, false otherwise (NaN or any finite value)

Examples:
assert(isInfinity(decimal32.infinity));
assert(isInfinity(-decimal64.infinity));
assert(!isInfinity(decimal128.nan));


bool isNaN(D)(auto ref const D x) if (isDecimal!D);
Determines if x represents a NaN. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
true if x is NaN (quiet or signaling), false otherwise (any other value than NaN)

Examples:
assert(isNaN(decimal32()));
assert(isNaN(decimal64.nan));
assert(!isNaN(decimal128.max));


bool isNormal(D)(auto ref const D x) if (isDecimal!D);
Determines if x is normalized. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
true if x is normal, false otherwise (NaN, infinity, zero, subnormal)

Examples:
assert(isNormal(decimal32.max));
assert(!isNormal(decimal64.nan));
assert(!isNormal(decimal32("0x1p-101")));


bool isPowerOf10(D)(auto ref const D x) if (isDecimal!D);
Checks whether a decimal value is a power of ten. This operation is silent, no exception flags are set and no exceptions are thrown.

Params:
D x any decimal value

Returns:
true if x is power of ten, false otherwise (NaN, infinity, 0, negative)

Examples:
assert (isPowerOf10(decimal32("1000")));
assert (isPowerOf10(decimal32("0.001")));


bool isSignaling(D)(auto ref const D x) if (isDecimal!D);
Determines if x represents a signaling NaN. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
true if x is NaN and is signaling, false otherwise (quiet NaN, any other value)

Examples:
assert(isSignaling(decimal32()));
assert(!isSignaling(decimal64.nan));
assert(!isSignaling(decimal128.max));


bool isSubnormal(D)(auto ref const D x) if (isDecimal!D);
Determines if x is subnormal (denormalized). This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
true if x is subnormal, false otherwise (NaN, infinity, zero, normal)

Examples:
assert(isSubnormal(decimal32("0x1p-101")));
assert(!isSubnormal(decimal32.max));
assert(!isSubnormal(decimal64.nan));


bool isZero(D)(auto ref const D x) if (isDecimal!D);
Determines if x represents the value zero. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
true if x is zero, false otherwise (any other value than zero)

Standards:
If the internal representation of the decimal data type has a coefficient greater that 10precision - 1, is considered 0 according to IEEE standard.

Examples:
assert(isZero(decimal32(0)));
assert(!isZero(decimal64.nan));
assert(isZero(decimal32("0x9FFFFFp+10")));


bool isGreater(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
bool isGreaterOrEqual(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
bool isGreaterOrUnordered(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
bool isLess(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
bool isLessOrEqual(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
bool isLessOrUnordered(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
bool isUnordered(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
Compares two decimal operands. This operation is silent, no exception flags are set and no exceptions are thrown.

Returns:
true if the specified condition is satisfied

Notes

By default, comparison operators will throw InvalidOperationException  or will set the ExceptionFlags.invalidOperation  context flag if a trap is not set. The equivalent functions are silent and will not throw any exception (or will not set any flag) if a NaN value is encountered.

Examples:
assert(isUnordered(decimal32.nan, decimal64.max));
assert(isGreater(decimal32.infinity, decimal128.max));
assert(isGreaterOrEqual(decimal32.infinity, decimal64.infinity));
assert(isLess(decimal64.max, decimal128.max));
assert(isLessOrEqual(decimal32.min_normal, decimal32.min_normal));


bool isEqual(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
bool isNotEqual(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
Compares two decimal operands for equality

Returns:
true if the specified condition is satisfied, false otherwise or if any of the operands is NaN.

Notes

By default, Decimal.opEquals  is silent, returning false if a NaN value is encountered. isEqual and isNotEqual will throw InvalidOperationException  or will set the ExceptionFlags.invalidOperation  context flag if a trap is not set.

Examples:
assert (isEqual(decimal32.max, decimal32.max));
assert (isNotEqual(decimal32.max, decimal32.min_normal));


D ldexp(D)(auto ref const D x, const int n) if (isDecimal!D);
Computes x * 10n. This operation is silent, doesn't throw any exception.

Special values

x n ldexp(x, n)
NaN any NaN
±∞ any ±∞
±0 any ±0
any 0 x


D log(D)(auto ref const D x) if (isDecimal!D);
Calculates the natural logarithm of logex.

Throws:
InvalidOperationException  x is signaling NaN or x < 0
DivisionByZero  x is ±0.0
Underflow  result is too small to be represented
InexactException  the result is inexact


Special values

x log(x)
NaN NaN
±0.0 -∞
-∞ NaN
+∞ +∞
e +1.0
< 0.0 NaN


Examples:
assert (log(decimal32.E) == 1);


D log10(D)(auto ref const D x) if (isDecimal!D);
Calculates log10x.

Throws:
InvalidOperationException  x is signaling NaN or x < 0.0
DivisionByZero  x is ±0.0
Underflow  result is too small to be represented
InexactException  the result is inexact


Special values

x log(x)
NaN NaN
±0.0 -∞
-∞ NaN
+∞ +∞
+10.0 +1.0
< 0.0 NaN


D log10p1(D)(auto ref const D x) if (isDecimal!D);
Calculates log10(x + 1).

Throws:
InvalidOperationException  x is signaling NaN or x < 1.0
DivisionByZero  x is -1.0
Underflow  result is too small to be represented
InexactException  the result is inexact


Special values

x log(x)
NaN NaN
-1.0 -∞
-∞ NaN
+∞ +∞
+9.0 +1.0
< -1.0 NaN


D log2(D)(auto ref const D x) if (isDecimal!D);
Calculates log2x.

Throws:
InvalidOperationException  x is signaling NaN or x < 0
DivisionByZero  x is ±0.0
Underflow  result is too small to be represented
InexactException  the result is inexact


Special values

x log(x)
NaN NaN
±0.0 -∞
-∞ NaN
+∞ +∞
+2.0 +1.0
< 0.0 NaN


D log2p1(D)(auto ref const D x) if (isDecimal!D);
Calculates log2(x + 1).

Throws:
InvalidOperationException  x is signaling NaN or x < 0
DivisionByZero  x is -1.0
Underflow  result is too small to be represented
InexactException  the result is inexact


Special values

x log(x)
NaN NaN
±0.0 -∞
-∞ NaN
+∞ +∞
+1.0 +1.0
< -1.0 NaN


D logp1(D)(auto ref const D x) if (isDecimal!D);
Calculates loge(x + 1).

Throws:
InvalidOperationException  x is signaling NaN or x < 0
DivisionByZero  x is -1.0
Underflow  result is too small to be represented
InexactException  the result is inexact


Special values

x log(x)
NaN NaN
±0.0 -∞
-∞ NaN
+∞ +∞
e - 1 +1.0
< -1.0 NaN


long lrint(D)(auto ref const D x, const RoundingMode mode) if (isDecimal!D);
long lrint(D)(auto ref const D x) if (isDecimal!D);
Returns the value of x rounded using the specified rounding mode. If no rounding mode is specified the default context rounding mode is used instead.

Throws:
InvalidOperationException  x is NaN or ±∞
OverflowException  result is too big to be represented
InexactException  the result is inexact


Special values

x lrint(x)
NaN 0
-∞ long.min
+∞ long.max


long lround(D)(auto ref const D x);
Returns the value of x rounded away from zero.

Throws:
InvalidOperationException  x is NaN or ±∞
OverflowException  result is too big to be represented


Special values

x lround(x)
NaN 0
-∞ long.min
+∞ long.max


D modf(D)(auto ref const D x, ref D y) if (isDecimal!D);
Splits x in integral and fractional part. This operation is silent, doesn't throw any exception

Params:
D x value to split
D y fractional part

Returns:
The value of x truncated toward zero.

Special values

x modf(x) y
NaN NaN NaN
0.0 0.0 0.0
±∞ 0.0 ±∞


D NaN(D, T)(const T payload) if (isDecimal!D && isUnsigned!T);
decimal128 NaN(T)(const T payloadHi, const T payloadLo) if (isUnsigned!T);
Creates a quiet NaN value using the specified payload

Notes

Payloads are masked to fit the current representation, having a limited bit width of to mant_dig - 2;

Examples:
auto a = NaN!decimal32(12345U);
auto b = NaN!decimal64(12345UL);
decimal128 c = NaN(123U, 456U);


D nearbyint(D)(auto ref const D x, const RoundingMode mode) if (isDecimal!D);
D nearbyint(D)(auto ref const D x) if (isDecimal!D);
Returns the value of x rounded using the specified rounding mode. If no rounding mode is specified the default context rounding mode is used instead.

Throws:
InvalidOperationException  if x is signaling NaN

Special values

x nearbyint(x)
NaN NaN
±∞ ±∞
±0.0 ±0.0


Examples:
assert(nearbyint(decimal32("1.2"), RoundingMode.tiesToEven) == 1);
assert(nearbyint(decimal64("2.7"), RoundingMode.tiesToAway) == 3);
assert(nearbyint(decimal128("-7.9"), RoundingMode.towardZero) == -7);
assert(nearbyint(decimal128("6.66")) == 7);


D nextDown(D)(auto ref const D x) if (isDecimal!D);
Returns the previous decimal value before x.

Throws:
InvalidOperationException  if x is signaling NaN

Special values

x nextDown(x)
NaN NaN
-∞ -∞
-max -∞
±0.0 -min_normal * epsilon
+∞ D.max


D nextPow10(D)(auto ref const D x) if (isDecimal!D);
Gives the next power of 10 after x.

Throws:
InvalidOperationException  x is signaling NaN
OverflowException  result is too big to be represented


Special values

x nextPow10(x)
NaN NaN
±∞ ±∞
±0.0 +1.0


D1 nextAfter(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
alias nextToward = nextAfter(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
Returns the next value after or before x, toward y.

Throws:
InvalidOperationException  either x or y is signaling NaN
OverflowException  result is ±∞
UnderflowException  result is subnormal or ±0.0
InexactException  result is ±∞, subnormal or ±0.0


Special values

x y nextAfter(x, y)
NaN any NaN
any NaN NaN
x = y x
x < y nextUp (x)
x > y nextDown (x)


D nextUp(D)(auto ref const D x) if (isDecimal!D);
Returns the next representable decimal value after x.

Throws:
InvalidOperationException  if x is signaling NaN

Special values

x nextUp(x)
NaN NaN
-∞ -D.max
±0.0 D.min_normal * epsilon
D.max +∞
+∞ +∞


auto poly(D1, D2)(auto ref const D1 x, const(D2)[] a) if (isDecimal!(D1, D2));
Calculates a0 + a1x + a2x2 + .. + anxn

Throws:
InvalidOperationException  x is signaling NaN or any ai is signaling NaN
InvalidOperationException  x is ±∞ and any ai is ±0.0
InvalidOperationException  x is ±0.0 and any ai is ±∞
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


D pow(D, T)(auto ref const D x, const T n) if (isDecimal!D && isIntegral!T);
Compute the value of xn, where n is integral

Throws:
InvalidOperationException  x is signaling NaN
DivisionByZeroException  x = ±0.0 and n < 0
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


Special values

x n pow(x, n)
sNaN any NaN
any 0 +1.0
NaN any NaN
±∞ any ±∞
±0.0 odd n < 0 ±∞
±0.0 even n < 0 +∞
±0.0 odd n > 0 ±0.0
±0.0 even n > 0 +0.0


auto pow(D1, D2)(auto ref const D1 x, auto ref const D2 x);
Compute the value of xy

Throws:
InvalidOperationException  x is signaling NaN
DivisionByZeroException  x = ±0.0 and y < 0.0
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


Special values

x y pow(x, y)
sNaN any NaN
any 0 +1.0
NaN any NaN
±∞ any ±∞
±0.0 odd n < 0 ±∞
±0.0 even n < 0 +∞
±0.0 odd n > 0 ±0.0
±0.0 even n > 0 +0.0


D1 quantize(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
Express a value using another value exponent

Params:
D1 x source value
D2 y value used as exponent source

Returns:
a value with the same numerical value as x but with the exponent of y

Throws:
InvalidOperationException  x is signaling NaN
InvalidOperationException  only one of x or y is ±∞
InexactException  result is inexact


Special values

x y quantize(x, y)
NaN any NaN
any NaN NaN
±∞ ±∞ ±∞
±∞ any NaN
any ±∞ NaN


int quantexp(D)(auto ref const D x) if (isDecimal!D);
Returns the exponent encoded into the specified decimal value;

Throws:
InvalidOperationException  x is NaN or ±∞


Special values

x quantexp(x)
NaN int.min
±∞ int.min


Notes

Unlike frexp  where the exponent is calculated for a |coefficient| < 1.0, this functions returns the raw encoded exponent.

Examples:
auto d = decimal32("0x0001p+12"); //1 * 10^^12
auto z = decimal64("0x0000p-3");  //0 * 10^^-3

int calculatedExponent, rawExponent;

//d is 0.1 * 10^^13
frexp(d, calculatedExponent);
rawExponent = quantexp(d);
assert (calculatedExponent == 13  && rawExponent == 12);

//z is 0.0
frexp(z, calculatedExponent);
rawExponent = quantexp(z);
assert (calculatedExponent == 0  && rawExponent == -3);


auto remainder(D1, D2)(auto ref const D1 x, auto ref const D2 y);
Calculates the remainder of the division x / y

Params:
D1 x dividend
D2 y divisor

Returns:
The value of x - n * y, where n is the quotient rounded to nearest even of the division x / y

Throws:
InvalidOperationException  x or y is signaling NaN, x = ±∞, y = ±0.0
UnderflowException  result is too small to be represented
DivisionByZeroException  y = 0.0
InexactException  the result is inexact


Special values

x y remainder(x, y)
NaN any NaN
any NaN NaN
±∞ any NaN
any 0.0 NaN
any ±∞ NaN


D rint(D)(auto ref const D x, const RoundingMode mode) if (isDecimal!D);
D rint(D)(auto ref const D x) if (isDecimal!D);
Returns the value of x rounded using the specified rounding mode. If no rounding mode is specified the default context rounding mode is used instead. This function is similar to nearbyint , but if the rounded value is not exact it will throw InexactException 

Throws:
InvalidOperationException  x is signaling NaN
InexactException  the result is inexact


Special values

x rint(x)
NaN NaN
±∞ ±∞
±0.0 ±0.0


Examples:
DecimalControl.resetFlags(ExceptionFlags.inexact);
assert(rint(decimal32("9.9")) == 10);
assert(DecimalControl.inexact);

DecimalControl.resetFlags(ExceptionFlags.inexact);
assert(rint(decimal32("9.0")) == 9);
assert(!DecimalControl.inexact);


D rndtonl(D)(auto ref const D x, const RoundingMode mode) if (isDecimal!D);
@safe D rndtonl(D)(auto ref const D x) if (isDecimal!D);
Returns the value of x rounded using the specified rounding mode. If no rounding mode is specified the default context rounding mode is used instead. If the value doesn't fit in a long data type OverflowException  is thrown.

Throws:
InvalidOperationException  x is NaN
OverflowException  result does not fit in a long data type
InexactException  the result is inexact


Special values

x rndtonl(x)
NaN NaN
±∞ ±∞
±0.0 ±0.0


D root(D)(auto ref const D x, const T n) if (isDecimal!D & isIntegral!T);
Compute the value of x1/n, where n is an integer

Throws:
InvalidOperationException  x is signaling NaN
DivisionByZeroException  x = ±0.0 and n < 0.0
OverflowException  result is too big to be represented or n = -1
UnderflowException  result is too small to be represented or n = -1
InexactException  result is inexact


Special values

x y root(x, n)
sNaN any NaN
any 0 NaN
any -1 NaN
NaN any NaN
±∞ any ±∞
±0.0 odd n < 0 ±∞
±0.0 even n < 0 +∞
±0.0 odd n > 0 ±0.0
±0.0 even n > 0 +0.0


D round(D)(auto ref const D x) if (isDecimal!D);
Returns the value of x rounded away from zero. This operation is silent, doesn't throw any exception.

Special values

x round(x)
NaN NaN
±0.0 ±0.0
±∞ ±∞


D rsqrt(D)(auto ref const D x) if (isDecimal!D);
Computes the inverse square root of x

Throws:
InvalidOperationException  if x is signaling NaN or negative, InexactException , UnderflowException , DivisionByZeroException 

Special values

x rsqrt(x)
NaN NaN
< 0.0 NaN
±0.0 NaN
+∞ +∞


bool sameQuantum(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
Compares the exponents of two decimal values

Params:
D1 x a decimal value
D2 y a decimal value

Returns:
true if the internal representation of x and y use the same exponent, false otherwise

Notes

Returns also true if both operands are NaN or both operands are infinite.

Examples:
assert(sameQuantum(decimal32.infinity, -decimal64.infinity));

auto x = decimal32("123456e+23");
auto y = decimal64("911911e+23");
assert(sameQuantum(x, y));


D scalbn(D)(auto ref const D x, const int n) if (isDecimal!D);
Returns:
x efficiently multiplied by 10n

Throws:
InvalidOperationException  if x is signaling NaN, OverflowException , UnderflowException , InexactException 

Special values

x n scalbn(x, n)
NaN any NaN
±∞ any ±∞
±0 any ±0
any 0 x


D scaledProd(D)(const(D)[] x, out int scale) if (isDecimal!D);
Multiplies elements of x using a higher precision, rounding only once at the end.

Returns:
x0 * x1 * ... * xn

Notes

To avoid overflow, an additional scale is provided that the final result is to be multiplied py 10scale

Throws:
InvalidOperationException  any x is signaling NaN
InvalidOperationException  there is one infinite element and one 0.0 element
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


D scaledProdSum(D)(const(D)[] x, const(D)[] y, out int scale) if (isDecimal!D);
Multiplies results of xi + yi using a higher precision, rounding only once at the end.

Returns:
(x0 + y0) * (x1 + y1) * ... * (xn + yn)

Notes

To avoid overflow, an additional scale is provided that the final result is to be multiplied py 10scale.
If x and y arrays are not of the same length, operation is performed for min(x.length, y.length);

Throws:
InvalidOperationException  any x is signaling NaN
InvalidOperationException  any x[i] and y[i] are infinite and with different sign
InvalidOperationException  there is one infinite element and one xi + yi == 0.0
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


D scaledProdDiff(D)(const(D)[] x, const(D)[] y, out int scale) if (isDecimal!D);
Multiplies results of xi - yi using a higher precision, rounding only once at the end.

Returns:
(x0 - y0) * (x1 - y1) * ... * (xn - yn)

Notes

To avoid overflow, an additional scale is provided that the final result is to be multiplied py 10scale
If x and y arrays are not of the same length, operation is performed for min(x.length, y.length);

Throws:
InvalidOperationException  any x is signaling NaN
InvalidOperationException  any xi and yi are infinite and with different sign
InvalidOperationException  there is one infinite element and one xi - yi == 0.0
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


pure nothrow @nogc @safe D sgn(D : Decimal!bits, int bits)(auto ref const D x);
Determines if x is negative This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
-1.0 if x is negative, 0.0 if x is zero, 1.0 if x is positive

Examples:
assert(sgn(decimal32.max) == 1);
assert(sgn(-decimal32.max) == -1);
assert(sgn(decimal64(0)) == 0);


int signbit(D : Decimal!bits, int bits)(auto ref const D x);
Returns the sign bit of the specified value. This operation is silent, no error flags are set and no exceptions are thrown.

Params:
D x a decimal value

Returns:
1 if the sign bit is set, 0 otherwise

Examples:
assert(signbit(-decimal32.infinity) == 1);
assert(signbit(decimal64.min_normal) == 0);
assert(signbit(-decimal128.max) == 1);


D sin(D)(auto ref const D x) if (isDecimal!D);
Returns sine of x.

Throws:
InvalidOperationException  x is signaling NaN or ±∞
UnderflowException  result is too small to be represented
InexactException  the result is inexact


Special values

x sin(x)
NaN NaN
±∞ NaN
-π/2 -1.0
-π/3 -√3/2
-π/4 -√2/2
-π/6 -0.5
±0.0 +0.0
+π/6 +0.5
+π/4 +√2/2
+π/3 +√3/2
+π/2 +1.0


D sinh(D)(auto ref const D x) if (isDecimal!D);
Calculates the hyperbolic sine of x.

Throws:
InvalidOperationException  x is signaling NaN
OverflowException  result is too big to be represented
InexactException  the result is inexact


Special values

x sinh(x)
NaN NaN
±∞ +∞
±0.0 +0.0


D sinPi(D)(auto ref const D x) if (isDecimal!D);
Returns sine of x*π.

Throws:
InvalidOperationException  x is signaling NaN or ±∞
UnderflowException  result is too small to be represented
InexactException  the result is inexact


Special values

x sin(x)
NaN NaN
±∞ NaN
-1/2 -1.0
-1/3 -√3/2
-1/4 -√2/2
-1/6 -0.5
±0.0 +0.0
+1/6 +0.5
+1/4 +√2/2
+1/3 +√3/2
+1/2 +1.0


D sqrt(D)(auto ref const D x) if (isDecimal!D);
Computes the square root of x

Throws:
InvalidOperationException  if x is signaling NaN or negative, InexactException , UnderflowException 

Special values

x sqrt(x)
NaN NaN
< 0.0 NaN
±0.0 ±0.0
+∞ +∞


D sum(D)(const(D)[] x) if (isDecimal!D);
Sums elements of x using a higher precision, rounding only once at the end.


Returns:
x0 + x1 + ... + xn

Throws:
InvalidOperationException  any x is signaling NaN
InvalidOperationException  there are two infinite elements with different sign
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


D sumAbs(D)(const(D)[] x) if (isDecimal!D);
Sums absolute elements of x using a higher precision, rounding only once at the end.

Returns:
|x0| + |x1| + ... + |xn|

Throws:
InvalidOperationException  any x is signaling NaN
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


D sumSquare(D)(const(D)[] x) if (isDecimal!D);
Sums squares of elements of x using a higher precision, rounding only once at the end.

Returns:
x02 + x12 + ... + xn2

Throws:
InvalidOperationException  any x is signaling NaN
OverflowException  result is too big to be represented
UnderflowException  result is too small to be represented
InexactException  result is inexact


D tan(D)(auto ref const D x) if (isDecimal!D);
Returns tangent of x.

Throws:
InvalidOperationException  x is signaling NaN or ±∞
UnderflowException  result is too small to be represented
OverflowException  result is too big to be represented
InexactException  the result is inexact


Special values

x tan(x)
NaN NaN
±∞ NaN
-π/2 -∞
-π/3 -√3
-π/4 -1.0
-π/6 -1/√3
±0.0 +0.0
+π/6 +1/√3
+π/4 +1.0
+π/3 +√3
+π/2 +∞


D tanh(D)(auto ref const D x) if (isDecimal!D);
Returns tangent of x.

Throws:
InvalidOperationException  x is signaling NaN
UnderflowException  result is too small to be represented
OverflowException  result is too big to be represented
InexactException  the result is inexact


Special values

x tanh(x)
NaN NaN
±∞ ±1.0
±0.0 ±0.0


T to(T, D)(auto ref const D x, const RoundingMode mode) if (isIntegral!T && isDecimal!D);
Converts x to the specified integral type rounded if necessary by mode

Throws:
InvalidOperationException  if x is NaN, UnderflowException , OverflowException 

Special values

x to!T(x)
NaN 0
+∞ T.max
-∞ T.min
±0.0 0


F to(F, D)(auto ref const D x, const RoundingMode mode) if (isFloatingPoint!F && isDecimal!D);
Converts x to the specified binary floating point type rounded if necessary by mode

Throws:
UnderflowException , OverflowException 

pure nothrow @nogc @safe decimal32 toDPD(const decimal32 x);
pure nothrow @nogc @safe decimal64 toDPD(const decimal64 x);
pure nothrow @nogc @safe decimal128 toDPD(const decimal128 x);
pure nothrow @nogc @safe decimal32 fromDPD(const decimal32 x);
pure nothrow @nogc @safe decimal64 fromDPD(const decimal64 x);
pure nothrow @nogc @safe decimal128 fromDPD(const decimal128 x);
Converts the specified value from internal encoding from/to densely packed decimal encoding

Notes

Decimal values are represented internaly using binary integer decimal encoding, supported by Intel (BID). This function converts the specified value to/from densely packed decimal encoding, supported by IBM (DPD). Please note that a DPD encoded decimal cannot be passed to a function from this module, there is no way to determine if a decimal value is BID-encoded or DPD-encoded, all functions will assume a BID-encoding.

T toExact(T, D)(auto ref const D x, const RoundingMode mode) if (isIntegral!T && isDecimal!D);
Converts x to the specified integral type rounded if necessary by mode

Throws:
InvalidOperationException  if x is NaN, InexactException  UnderflowException , OverflowException 

Special values

x toExact!T(x)
NaN 0
+∞ T.max
-∞ T.min
±0.0 0


F toExact(F, D)(auto ref const D x, const RoundingMode mode) if (isFloatingPoint!F && isDecimal!D);
Converts x to the specified binary floating point type rounded if necessary by mode

Throws:
UnderflowException , OverflowException , InexactException 

long toMsCurrency(D)(auto ref const D x) if (isDecimal!D);
D fromMsCurrency(D)(const ulong x) if (isDecimal!D);
Converts the specified value to/from Microsoft currency data type;

Throws:
InvalidOperationException  x is NaN
OverflowException  x is infinite or outside the Currency limits
UnderflowException  x is too small to be represented as Currency
InexactException  x cannot be represented exactly


Notes

The Microsoft currency data type is stored as long always scaled by 10-4

DECIMAL toMsDecimal(D)(auto ref const D x);
D fromMsDecimal(D)(auto ref const DECIMAL x);
Converts the specified value to/from Microsoft decimal data type;

Throws:
InvalidOperationException  x is NaN
OverflowException  x is infinite or outside the DECIMAL limits
UnderflowException  x is too small to be represented as DECIMAL
InexactException  x cannot be represented exactly


Notes

The Microsoft decimal data type is stored as a 96 bit integral scaled by a variable exponent between 10-28 and 100.

bool totalOrder(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
bool totalOrderAbs(D1, D2)(auto ref const D1 x, auto ref const D2 y) if (isDecimal!(D1, D2));
Checks the order between two decimal values

Params:
D1 x a decimal value
D2 y a decimal value

Returns:
true if x precedes y, false otherwise

Notes

totalOrderAbs checks the order between |x| and |y|

See Also:
cmp 

Examples:
assert (totalOrder(decimal32.min_normal, decimal64.max));
assert (!totalOrder(decimal32.max, decimal128.min_normal));
assert (totalOrder(-decimal64(0), decimal64(0)));
assert (totalOrderAbs(decimal64(0), -decimal64(0)));


pure nothrow @nogc @safe D trunc(D)(auto ref const D x) if (isDecimal!D);
Returns the value of x rounded up or down, depending on sign (toward zero). This operation is silent, doesn't throw any exception.

Special values

x trunc(x)
NaN NaN
±0.0 ±0.0
±∞ ±∞


D truncPow10(D)(auto ref const D x) if (isDecimal!D);
Gives the previous power of 10 before x.

Throws:
InvalidOperationException , OverflowException , UnderflowException , InexactException 

Special values

x truncPow10(x)
NaN NaN
±∞ ±∞
±0.0 ±0.0