[erlang-questions] Maths Problem -> 2452.45*100. = 245244.99999999997

Alexander Alexeev mail@REDACTED
Wed Nov 27 19:02:13 CET 2013


Here is a real world example. We are calculating a commission:

28 x 85 x 312 x 5 / 0.85

In this case decimal gives you a rounding error +/- 0.01 USD. With
_any_ chosen precision. But if you are using something like Data.Ratio
you'll get:

28 x 85 x 312 x 5 / 0.85 =
28 x 85 x 312 x 5 x 85 / 100 =
exactly 4368000 USD

See the difference?

> >>Unfortunately, decimal does not solves all problems.
> 
> I do not want to be sarcastic, but it is really hard to divide 100
> dollars between three guys,
> give them all the same amount (100/3) and then say "Sorry, I need my
> money back".
> Do you think you will have 100 dollars then?
> 
> You need to decide what precision is right for you (specifically when
> you ared dividing), how to correctly round and so on. There is a whole
> mathematics about it.
> 
> You need to determine needed precision.
> Sometimes the value 3.14 is good, but sometimes the value
> 3,14159265358979323846 is not good enough...
> 
> Decimals do solve problems if you know needed precision.
> 
> In financials you need to calculate millions and billions with a
> precision to cents.
> 
> And - for example - for US Debt ( http://www.usdebtclock.org/ ) you
> need 16 digits (trillions) if you want to be accurate to cents right
> now. :-) Imagine to express US Debt in Japanese Yen :-)
> 
> Regards,
> Bob
> 
> 
> 
> 
> 2013/11/27 Anthony Ramine <n.oxyde@REDACTED>
> 
> > This looks like a bug in decimal though, this time.
> >
> > --
> > Anthony Ramine
> >
> > Le 27 nov. 2013 à 14:37, Alexander Alexeev <mail@REDACTED> a écrit :
> >
> > > Unfortunately, decimal does not solves all problems.
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >


-- 
Best regards,
Alexander Alexeev
http://eax.me/



More information about the erlang-questions mailing list