Another take on decimal data types

rumbu rumbu at rumbu.ro
Sat Jan 13 19:28:40 UTC 2018


On Saturday, 13 January 2018 at 18:37:10 UTC, kdevel wrote:

> I get large numerical dicrepancies and an exception:

That's because you are mixing floating point and decimal.

Just to take one example: double 1.1 cannot be represented 
exactly as floating point and it's in fact 
1.10000002384185791015625.

sin is calculated using a Taylor series: sin(x) = x - x^3/3! + 
x^5/5! - x^7/7! ... and so on. Raising to power all that junk 
after 1.1 will lead finally to error. If you really want to find 
out sin(1.1) using decimal, try sin(decimal128("1.1")) or 
sin(decimal128(1)/10);


For exact values like sin(1.0), I let you decide which one is 
more exact:

Wolfram Alpha: 0.8414709848078965066525023216302989
real:          0.8414709848078965066645910000000000
double:        0.8414709848078965048700000000000000
float:         0.8414709568023681640600000000000000
decimal128:    0.8414709848078965066329679978908351
decimal64:     0.8414709848078965000000000000000000
decimal32:     0.8414710000000000000000000000000000

Anyway, I wouldn't call a difference at the 18th digit a "large 
discrepancy" when we are talking about irrational numbers.

Regarding the exception, I cannot reproduce it, but I'll look 
into it.


Thank you for your exhaustive testing :)



More information about the Digitalmars-d-announce mailing list