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

Feature | Implemented ------------ | ------------- 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 | ✓ Addition | ✓ 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 ms | 105 ms | 152 ms | 2,358 ms | 799 ms | 741 ms Subtraction (n = 5M) | 494 ms | 103 ms | 149 ms | 2,247 ms | 800 ms | 830 ms Multiplication (n = 5M) | 156 ms | 16 ms | 1,689 ms | 999 ms | 695 ms | 1541 ms Division (n = 1M) | 207 ms | 25 ms | 532 ms | 11,116 ms | 215 ms | 416 ms Sorting 1M Uniformly Random Numbers | 592 ms | 386 ms | 502 ms | 5,170 ms | 1,536 ms | 1,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)
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
Show all 6 versions
Download Stats:
  • 0 downloads today

  • 3 downloads this week

  • 33 downloads this month

  • 353 downloads total

Score:
1.4
Short URL:
stdxdecimal.dub.pm