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

int(1.0) == 0?

Thread Next
From:
Tels
Date:
March 30, 2007 10:55
Subject:
int(1.0) == 0?
Message ID:
200703301954.47279@bloodgate.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Moin,

I now tracked the problem for this bugreport 
http://rt.cpan.org/Ticket/Display.html?id=18025 (filed in bignum) down to 
some problem with int() and Look Ma!, I can replicate it without bignum at 
all:

	te@linux:~/perl/math/Math-BigInt-1.82> cat fail.pl
	#!perl
	use strict;
	use warnings;
	use Test::More tests => 6;

	my $lnev = -7 / (10**17); my $ev=exp($lnev);
	my $nv = 1.0;

	use Devel::Peek;
	print Dump($ev);
	print Dump($nv);
	is( $ev, '1', '$ev == 1');
	is( sprintf('%0.15f',$ev) , '1.000000000000000', '($ev) is approx. 1' );
	is( int($ev), '1', 'int($ev) == 1');

	is( $nv, '1', '$nv == 1');
	is( sprintf('%0.15f',$nv) , '1.000000000000000', '($nv) is approx. 1' );
	is( int($nv), '1', 'int($nv) == 1');

	exit;

This produces under Perl 5.8.8:

	te@linux:~/perl/math/Math-BigInt-1.82> perl fail.pl
	1..6
	SV = NV(0x682f70) at 0x76eaa0
	  REFCNT = 1
	  FLAGS = (PADBUSY,PADMY,NOK,pNOK)
	  NV = 1
	SV = NV(0x682f78) at 0x76ea90
	  REFCNT = 1
	  FLAGS = (PADBUSY,PADMY,NOK,pNOK)
	  NV = 1
	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.

Is this a known issue? As far as I can see, there should be no difference 
between $nv and $ev, but for some reason int($ev) == 0, which really 
confuses bignum - because deep down Calc::_new() does a int($_[1]) which 
produces 0 instead of 1, so basically Math::BigInt->new($ev) results in "0" 
instead of "1" and from there on everything goes wrong.

All the best,

Tels

- -- 
 Signed on Fri Mar 30 19:49:41 2007 with key 0x93B84C15.
 View my photo gallery: http://bloodgate.com/photos
 PGP key on http://bloodgate.com/tels.asc or per email.

 "When it's done in 2001."

  -- 2000 Christmas card about Duke Nukem Forever (http://tinyurl.com/6m8nh)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iQEVAwUBRg1rB3cLPEOTuEwVAQIddQf/VgdzL/JUuXuRtlC3QQrB+M8k27UFMCHr
FQgnnjgTm88oTtjc0ydC4Cer7m3k3qpFQ91WaoIjKn/8XKEJ6vrreyNbbKRKO5VZ
PRXobhYVXLsaZrR4Leixs/cJbuZlrDH9yf9Y2Mre9N7hbsAIOhXNGiijgjNO2o7m
+p7MqMjymakQxpUK9Jrsm5Pq6oMlecHG2ck/KLCxysV92It3m3E6fJI0nUtjgCiH
pjsYG/2oDhlk8IotfNpWnVZddDSVGsDMf/BEsCdy05+lSoyqNtTWKamDn767bQWs
wojEYvUkVnnMp2tJognt627cBzqXYJVHYjELjUZhE/sstJMnAtFRUQ==
=W8eE
-----END PGP SIGNATURE-----

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