in reply to Converting -4.84800000E+001 to -48.48 doesn't work... but 48.49 does

Computers can't be accurate with all fractions because Humans have too many fingers .

update

Now unfortunately the calculation path to the inner representation of 48.48 and 4.848e1 seem to produce different results.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

  • Comment on Re: Converting -4.84800000E+001 to -48.48 doesn't work... but 48.49 does

Replies are listed 'Best First'.
Re^2: Converting -4.84800000E+001 to -48.48 doesn't work... but 48.49 does
by syphilis (Archbishop) on Jan 05, 2017 at 23:54 UTC
    Computers can't be accurate with all fractions

    But this is no excuse for -4.848e1 and -48.48 being treated as different values.

    Cheers,
    Rob
      Yes, sure, this is not satisfactory, but the bottom line, in C, in Perl and in most other programming languages, is that you should not compare floats for equality (unless you really know very well what you're doing). To check is two such numbers shall be deemed equal, one should check that the absolute value of their difference is smaller than a certain very small value.

      Perl 6 has a rational type, in which numbers are stored internally as a numerator and a denominator, allowing for accurate comparison of decimal numbers.

      Yeah just updated it before I saw your reply.

      I think the way -48.48 is transformed into a float follows a different calculation path which rounds the last bit into another direction.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!