We don't bite newbies here... much PerlMonks

### Re: Comparing results of math operations

by syphilis (Chancellor)
 on Apr 16, 2014 at 12:43 UTC ( #1082497=note: print w/ replies, xml ) Need Help??

in reply to Comparing results of math operations

Hi,
The essential difference between the first assignments to \$x and \$y, and the assignments made later (in the "#now with simpler math" section) is that while 2, 1.25, 5, and 0.5 can *all* be represented exactly in base 2, the same cannot be said of 0.95, 806, 1.3 and 589.
Sure ... both 806 and 589 can be represented exactly in base 2, but 0.95 and 1.3 cannot.

Hence the first calculations do not yield "expected" values, whereas the second calculations *do* yield expected values.

Workarounds include doing base 10 calculations - eg by using Math::Decimal (pure perl) or Math::Decimal64 (XS access to the C compiler's _Decimal64 arithmetic).

Cheers,
Rob
Comment on Re: Comparing results of math operations
Replies are listed 'Best First'.
Re^2: Comparing results of math operations
by LanX (Chancellor) on Apr 16, 2014 at 13:42 UTC
I agree that the transformation from decimal to binary fractions is the biggest source of problems.

But does switching to decimal floats solve all problems?

1/3 can't be represented loss-free in neither decimal nor binary floats (3 is not a primefactor of 10 or 2)

So (1/3 * 3) can be different to 1.

Though it isn't ATM

```  DB<125> 1/3 *3 <=> 1
=> 0

I'm not sure if that's always the case for similar calculations.

I remember similar discussions talking about something like "symbolic calculations", where divisor and denominator are kept separate till the end.

Cheers Rolf

( addicted to the Perl Programming Language)

But does switching to decimal floats solve all problems?

No ... but then rational arithmetic (where you keep divisor and numerator separate) also fails to solve all problems, as it falls down when you start to deal with irrational numbers such as sqrt(2) or transcendentals such as pi.

I think what drew me to decimal arithmetic was that the values that appeared in the OP's post were all *exactly* representable in base 10, as were the results of the calculations he presented.
If there had been a "1 / 3" (or some other rational value that couldn't be exactly represented in base 10) in that post then I would more likely have been drawn to modules such as Math::BigRat or Math::GMPq.

Cheers,
Rob
discussions talking about something like "symbolic calculations", where divisor and denominator are kept separate till the end
Do you mean Math::Fraction?
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Nope, IIRC someone (Moritz?) mentioned Perl6 will/would try to do this automatically...

But didn't want to generate a new "sixuality" dispute ... ;-)

Cheers Rolf

( addicted to the Perl Programming Language)

Create A New User
Node Status?
node history
Node Type: note [id://1082497]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2016-05-25 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What font do you use for programming?

Results (330 votes). Check out past polls.