# stdxdecimal 0.5.0

Proposal for a General Decimal Arithmetic type to Phobos

To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

## Decimal Type for Phobos

This code is a work in progress for a new type `Decimal` that follows the "General Decimal Arithmetic Specification" http://speleotrove.com/decimal/decarith.html

### Example

``````import stdxdecimal;

void main()
{
auto d1 = decimal("1.23");
auto d2 = decimal("1.23E-4");
auto d3 = decimal("1.23E4");

assert(d1.toString() == "1.23");
assert(d2.toString() == "0.000123");
assert(d3.toString() == "12300");

auto d4 = d1 + d2;
assert(d4.toString() == "1.230123");

auto d5 = d3 - d1;
assert(d5.toString() == "12298.87");

auto d6 = decimal!(HighPrecision)("1.000000000000000000000000000000000000000000000000001");
auto d7 = decimal!(HighPrecision)("1E-51");
auto d8 = d6 + d7;
assert(d8.toString() == "1.000000000000000000000000000000000000000000000000002");
}
``````

### Current Features

FeatureImplemented
Construction from strings/string-like ranges
Construction from number types
To decimal string
To scientific string
To engineering string
All rounding types
Overflow/Underflow Behavior
Subnormal Behavior
Clamping Behavior
Equals
toHash
Compare
Subtraction
Multiply
Divide
Divide-Integer
Modulo
DivMod
opOpAssign `+`,`-`,`*`,`/`,`^^`
Unary `+`,`-`,`++`,`--`
opCast `int`,`real`,`bool`
abs
ln
log10
exp
sqrt
power
reduce

### Current Performance

Run on a 15", 2015 Macbook Pro, (rounded to the nearest ms; test values purposely avoid rounding, which is the slowest part by far)

Test`BigInt``Decimal` (P = 9)`Decimal` (P = 19)`Decimal` (P = 64)Python `Decimal`Python `Decimal` (64 Digits)
Addition (n = 5M)594 ms105 ms152 ms2,358 ms799 ms741 ms
Subtraction (n = 5M)494 ms103 ms149 ms2,247 ms800 ms830 ms
Multiplication (n = 5M)156 ms16 ms1,689 ms999 ms695 ms1541 ms
Division (n = 1M)207 ms25 ms532 ms11,116 ms215 ms416 ms
Sorting 1M Uniformly Random Numbers592 ms386 ms502 ms5,170 ms1,536 ms1,228 ms

#### Run It Yourself

D: `ldc2 -O -release bench.d source/stdxdecimal/package.d && ./bench`

Python:

``````import timeit

timeit.timeit("c = a + b", setup="from decimal import Decimal; a = Decimal(10000.12); b = Decimal(5000000); c = None", number=5000000)
timeit.timeit("c = a + b", setup="from decimal import Decimal; a = Decimal(1000000000000000000000000000000000000000000000000000000000000000); b = Decimal(5000000000000000000000000000000000000000000000000000000000000000); c = None", number=5000000)
timeit.timeit("c = a - b", setup="from decimal import Decimal; a = Decimal(10000.12); b = Decimal(5000000); c = None", number=5000000)
timeit.timeit("c = a - b", setup="from decimal import Decimal; a = Decimal('10000000000000000000000000000000000000000000000000000000000000.12'); b = Decimal('5000000000000000000000000000000000000000000000000000000000000000'); c = None", number=5000000)
timeit.timeit("c = a * b", setup="from decimal import Decimal; a = Decimal(10000.12); b = Decimal(5000000); c = None", number=5000000)
timeit.timeit("c = a * b", setup="from decimal import Decimal; a = Decimal('10000000000000000000000000000000000000000000000000000000000000.12'); b = Decimal('5000000000000000000000000000000000000000000000000000000000000000'); c = None", number=5000000)
timeit.timeit("c = a / b", setup="from decimal import Decimal; a = Decimal(10000.12); b = Decimal(5000000); c = None", number=1000000)
timeit.timeit("c = a / b", setup="from decimal import Decimal; a = Decimal('10000000000000000000000000000000000000000000000000000000000000.12'); b = Decimal('5000000000000000000000000000000000000000000000000000000000000000'); c = None", number=1000000)
timeit.timeit("c = sorted(a)", setup="from decimal import Decimal;import random; a = [Decimal(random.randint(-10000000, 10000000) * random.random()) for x in range(1000000)]", number=1)
timeit.timeit("c = sorted(a)", setup="from decimal import Decimal;import random; a = [Decimal(random.randint(-100000000000000000000000000000000000000000000000000, 100000000000000000000000000000000000000000000000000) * random.random()) for x in range(1000000)]", number=1)
``````
• Registered by Jack Stouffer
• 0.5.0 released 6 years ago
• JackStouffer/stdxdecimal
• BSL 1.0
Authors:
• Jack Stouffer
Dependencies:
none
Versions:
 0.5.0 2018-Jan-05 0.4.0 2017-Dec-21 0.3.0 2017-Dec-08 0.2.0 2017-Dec-06 0.1.0 2017-Dec-02