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

Re: int(1.0) == 0?

Thread Previous | Thread Next
From:
Paul Schinder
Date:
April 1, 2007 05:01
Subject:
Re: int(1.0) == 0?
Message ID:
460F9E15.5060708@pobox.com
Jerry D. Hedden wrote:
> Tels wrote:
>> 	my $lnev = -7 / (10**17); my $ev=exp($lnev);
>> 	my $nv = 1.0;
>>
>> 	ok 1 - $ev == 1
>> 	ok 2 - ($ev) is approx. 1
>> 	not ok 3 - int($ev) == 1
>> 	#   Failed test 'int($ev) == 1'
>> 	#   in fail.pl at line 16.
>> 	#          got: '0'
>> 	#     expected: '1'
>> 	ok 4 - $nv == 1
>> 	ok 5 - ($nv) is approx. 1
>> 	ok 6 - int($nv) == 1
>> 	# Looks like you failed 1 test of 6.
> 
> This is a typical floating point equality issue.  $ev =
> e^(-7/10^17) is not exactly 1.  Using my calculatator I get:
> 
>     1 - e^(-7/10^17) = 1.11 * 10^-16.

Not a precise calculator at this level (of course, my HP 15C doesn't
have near enough precision to get anything different from zero).  e(-x)
= 1 - x for small x.  (The next term in the series is x^2/2).  So you
should get 7e-17.  The fact remains, int($ev) == 0, but I'm a little
surprised that IEEE doubles getting passed back into 64 bit doubles have
that much precision.  They certainly don't on my G5:

g5% perl -e '$x = -7e-18;$y=exp($x);$z=int($y);printf("%.20f",$y);print
" $z\n";'
1.00000000000000000000 1
g5% perl -e '$x = -7e-17;$y=exp($x);$z=int($y);printf("%.20f",$y);print
" $z\n";'
0.99999999999999988898 0
g5% perl -e '$x = -7e-16;$y=exp($x);$z=int($y);printf("%.20f",$y);print
" $z\n";'
0.99999999999999933387 0
g5% perl -e '$x = -7e-15;$y=exp($x);$z=int($y);printf("%.20f",$y);print
" $z\n";'
0.99999999999999300559 0

So -7e-16 is the first decade it gets even close to the right numerical
answer, and -7e-17 the first where it notices that the exponential is
different from 1.

> 
> So int($ev) == 0 is correct!
> 
> If you have precision requirements, then define and add
> it before taking int():
> 
>     my $precision = 10 ** -15;
>     my $result = int($ev + $precision);
> 
> 
> 
>  
> ____________________________________________________________________________________
> Be a PS3 game guru.
> Get your game face on with the latest PS3 news and previews at Yahoo! Games.
> http://videogames.yahoo.com/platform?platform=120121


-- 
Paul Schinder
schinder@pobox.com

Thread Previous | Thread Next


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