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

Alexander Alexeev <>
Wed Nov 27 14:37:46 CET 2013


Unfortunately, decimal does not solves all problems. For example:

http://pastexen.com/code.php?file=zt4viJXw1I.txt

I wonder, does Erlang has a Data.Ratio equivalent from Haskell?

> You might want to try:-
> 
> https://github.com/tim/erlang-decimal
> 
> Run
> 
> Iqbal-Bhatti:ebin afrobeard$ erl
> Erlang R13B03 (erts-5.7.4) [source] [smp:4:4] [rq:4] [async-threads:0]
> [kernel-poll:false]
> 
> Eshell V5.7.4  (abort with ^G)
> 1> decimal:multiply("2452.45", "100").
> {0,24524500,-2}
> 2>
> 
> Best Regards,
> 
> 
> On 27 November 2013 09:35, Michael Turner
> <>wrote:
> 
> > Gee, now might be a good time to bore everyone with a war story: a
> > startup I once worked for independently /pre/-invented the Pentium
> > Floating Point Bug, against my strenuous protests. How could this
> > happen, I wondered, as I was losing the battle for IEEE-standard
> > purity? Maybe, I thought, it's because we're a small company that
> > can't afford to hire experts to ratify my opinion?
> >
> > We were using Weitek FPU adder and multiplier chips in our hardware,
> > and I implemented floating point division the way they said to do
> > it - the right way. Tried to, that is. I got overruled.
> >
> > Not too many years later, for the Pentium design, Intel acquired
> > Weitek's IP. Then they introduced very similar mistakes in floating
> > point arithmetic, through no fault of Weitek. Ours, at least, could
> > have been patched by sending customers a new ROM to socket. Intel's
> > errors went onto a single chip.
> >
> > What I learned about IEEE floating point arithmetic is that if you
> > try to clean up floating point "errors" one way, you just squeeze
> > the errors out some place else.
> >
> > What I learned about people is that Size Doesn't Matter.
> >
> > The proximate provocation in both cases: somebody didn't like how
> > the number printed. I first saw "errors" like when I was in high
> > school, programming in Dartmouth BASIC. I was told, "floating point
> > is like that - if it weren't that, it would be something worse." So
> > I was prepared. But lots of people now graduate with C.S. degrees
> > who aren't prepared.
> >
> >
> > Regards,
> > Michael Turner
> > Executive Director
> > Project Persephone
> > K-1 bldg 3F
> > 7-2-6 Nishishinjuku
> > Shinjuku-ku Tokyo 160-0023
> > Tel: +81 (3) 6890-1140
> > Fax: +81 (3) 6890-1158
> > Mobile: +81 (90) 5203-8682
> > 
> > http://www.projectpersephone.org/
> >
> > "Love does not consist in gazing at each other, but in looking
> > outward together in the same direction." -- Antoine de Saint-Exupéry
> >
> >
> > On Wed, Nov 27, 2013 at 12:28 PM, Richard A. O'Keefe
> > <> wrote:
> > >
> > > On 27/11/2013, at 3:01 PM, Sanath Prasanna wrote:
> > >
> > >> I accepted your comment. But generally we expected full value. I
> > >> did
> > same in another scripting language like php. It is given correct
> > result.
> > >
> > > Wrong.  It *calculated* the same number as Erlang did,
> > > but then it *displayed* a different number, a rounded version.
> > >
> > > m% cat >foo.php
> > > <?php
> > > $a = 2452.45*100;
> > > echo $a; echo "\n";
> > > $a = $a - 245245.0;
> > > echo $a; echo "\n";
> > > exit;
> > > ?>
> > > <EOF>
> > > m% php foo.php
> > > 245245
> > > -2.9103830456734E-11
> > >
> > > We see from this that while the number *displays as*
> > > 245245, it is not *equal to* 245245.
> > >
> > > As I said, PHP computed *exactly* the same answer as Erlang.
> > > Who is this "we" who "expect full value"?
> > > This is floating-point arithmetic, not rational arithmetic.
> > >
> > > (Our 2nd year students are taught this stuff, but it doesn't
> > > really take.  In 3rd year they have an exercise they have to
> > > do where they start to really understand that floating point
> > > arithmetic is *not* arithmetic on the mathematical real
> > > numbers, but a different mathematical system which sort of
> > > approximates the algebra of the reals.  They learn that
> > > adding up an array of numbers from left to right generally
> > > gives you a different answer from adding up from right to
> > > left, for example.)
> > >
> > > Numbers in IEEE floating point arithmetic are precise,
> > > but are bounded in their precision, and arithmetic operations
> > > on them (including conversion between decimal and binary)
> > > have to round their answers to something that is representable.
> > > There's a rounding in the conversion of 2452.45 to binary,
> > > and then the multiplication incurs another rounding.
> > >
> > > _______________________________________________
> > > erlang-questions mailing list
> > > 
> > > http://erlang.org/mailman/listinfo/erlang-questions
> > _______________________________________________
> > erlang-questions mailing list
> > 
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
> 
> 
> 
> -- 
> Iqbal Talaat Bhatti
> 
> "If we did all the things we are capable of doing, we would literally
> astound ourselves." - Thomas Edison


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



More information about the erlang-questions mailing list