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

Bohuslav Svancara <>
Thu Nov 28 00:07:11 CET 2013


It must be some misunderstanding.

This is my result in Erlang:

28 * 85 * 312 * 5 / 0.85.
4.368e6

or

28 * 85 * 312 * 5 / (85 / 100).
4.368e6

or

28 * 85 * 312 * 5 / (85 * 0.01).
4.368e6

or using "decimal":

decimal:divide(
decimal:multiply(28,decimal:multiply(85,decimal:multiply(312,5))),
 decimal:divide(85,100) ).
{0,43680,2}

or

decimal:divide(
decimal:multiply(28,decimal:multiply(85,decimal:multiply(312,5))),
decimal:multiply(85,0.01) ).
{0,4368,3}

No problem anywhere. Correct?

Bob

2013/11/27 Alexander Alexeev <>

> 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 <>
> >
> > > This looks like a bug in decimal though, this time.
> > >
> > > --
> > > Anthony Ramine
> > >
> > > Le 27 nov. 2013 à 14:37, Alexander Alexeev <> a écrit :
> > >
> > > > Unfortunately, decimal does not solves all problems.
> > >
> > > _______________________________________________
> > > erlang-questions mailing list
> > > 
> > > http://erlang.org/mailman/listinfo/erlang-questions
> > >
>
>
> --
> Best regards,
> Alexander Alexeev
> http://eax.me/
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131128/8845f1bc/attachment.html>


More information about the erlang-questions mailing list