develooper Front page | perl.perl5.porters | Postings from April 2006

Re: [perl #38899] nondeterministic perl floating point math

Thread Previous
From:
Justin Palmer
Date:
April 12, 2006 07:21
Subject:
Re: [perl #38899] nondeterministic perl floating point math
Message ID:
443CFF69.2000204@mitre.org
Wow.  You're right.  Thanks, Paul.

For the record, and in case you wonder what compelled me to agree 
so quickly, see the following code:

int main () {
   float n = 70.7, d = 0.1;
   int j = (int) n / d;

   cout << "j = " << j << endl;

   return 0;
}

What does it print, you ask?

$ ./a.out
j = 699

Scary!

Paul Johnson via RT wrote:
> On Tue, Apr 11, 2006 at 11:42:22AM -0700, Justin Palmer wrote:
> 
>> I think this one is pretty self-explanatory.  I can't seem to 
>> generate another number that this sort of problem happens with, 
>> and I haven't looked harder to find out what other numbers this 
>> happens to.  Any idea what's going on?  Bad machine language-perl 
>> float interaction, I bet.
> 
>> $ perl -e 'print int(70.7/0.1)."\n";'
>> 707
>> $ perl -e 'print int(70.8/0.1)."\n";'
>> 707
> 
> Thanks for your report.  Please see:
> 
> perldoc -q 999
> perldoc perlnumber
> 
> I don't think this is a bug in perl.
> 


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About