|Perl Monk, Perl Meditation|
Re: Comparing results of math operationsby syphilis (Chancellor)
|on Apr 16, 2014 at 12:43 UTC||Need Help??|
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).