in reply to Fun with floating points

I think this is not a problem with float point numbers, but with the comparison operator:
perl -le'$x=0.05;$y=sqrt($x+$x+$x);$g="$y";print "Is $y equal to $g?"; +print $y eq $g?"Yes":"No"' Is 0.387298334620742 equal to 0.387298334620742? Yes

Replies are listed 'Best First'.
Re^2: Fun with floating points
by kikuchiyo (Hermit) on Nov 06, 2009 at 15:04 UTC
    I had a reason to post it as I did.

    In the actual application where I had this issue, the workflow was something like this: data was read off from data files and stored in a Storable file. Later, these Storable files were retrieved, and the data in them was further processed. And finally I used a numerical comparison to get the result (is the number dug up from the Storable archives bigger than a pre-specified limit). However, Storable stores floating point numbers as text, at least it did in my application.

    So the real issue here is that it is awesomely convenient to have scalars-that-are-both-numbers-and-strings in Perl, but one has to be careful and know that it may lead to strange results sometimes.
      String/float conversion is not the only problem while dealing with floats. Consider following example:
      perl -e '$x=1/3; $x-=1/2; $x+=1/3; $x-=1/2; $x+=1/3; print $x,"\n"'
      $x should be equal to zero but it isn't.