Skip Menu |
 

This queue is for tickets about the bignum CPAN distribution.

Report information
The Basics
Id: 104400
Status: open
Priority: 0/
Queue: bignum

People
Owner: Nobody in particular
Requestors: tails.saito [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: bignum produces a Math::BigInt with a wrong value
Date: Wed, 13 May 2015 17:20:07 +0900
To: bug-bignum [...] rt.cpan.org
From: Saito Takaaki <tails.saito [...] gmail.com>
Download (untitled) / with headers
text/plain 476b
Greetings, Let me report a strange behavior of Perl bignum which I think is a bug. I am using Perl v5.14.4 and bignum 0.27 which may be a bit old but is the latest in the Cygwin packages. Sorry if it is already fixed. The following code results in $y == 9 in Math::BigInt. Please note $x is a scalar that is printed as 10. I think $y should be 10 in BigInt, or a BigFloat with a value near 9.999. $x=10/77*77; use bignum; $y=$x+0; Thank you in advance. -- SAITO Takaaki
Download (untitled) / with headers
text/plain 1.2k
On Wed May 13 04:20:16 2015, tails.saito@gmail.com wrote: Show quoted text
> Greetings, > > Let me report a strange behavior of Perl bignum which I think is a bug. > I am using Perl v5.14.4 and bignum 0.27 which may be a bit old > but is the latest in the Cygwin packages. > Sorry if it is already fixed. > > The following code results in $y == 9 in Math::BigInt. > Please note $x is a scalar that is printed as 10. > I think $y should be 10 in BigInt, or a BigFloat with a value near 9.999. > > $x=10/77*77; > use bignum; > $y=$x+0;
This was an interesting case. The first line creates $x as a Perl scalar with a value of approximately 9.9999999999999982. The second line loads the bignum pragma, after which binary operators are overloaded and constants are converted into objects. The third line converts the Perl scalar 0 into a Math::BigInt object with the value 0. It is a Math::BigInt object, not a Math::BigFloat object, because 0 is an integer. Then the addition is performed, and it is done by Math::BigInt->badd(). Since that method can only handle integers, the value of $x is truncated to an integer, i.e., 9. Adding 9 to 0 gives 9. This case shows that perhaps all Perl scalar numerics should be converted into Math::BigFloats.
Subject: Re: [rt.cpan.org #104400] bignum produces a Math::BigInt with a wrong value
Date: Fri, 18 Sep 2015 19:07:21 +0900
To: bug-bignum [...] rt.cpan.org
From: Saito Takaaki <tails.saito [...] gmail.com>
Download (untitled) / with headers
text/plain 855b
Show quoted text
>> $x=10/77*77; >> use bignum; >> $y=$x+0;
> > This was an interesting case. The first line creates $x as a Perl scalar with a value of approximately 9.9999999999999982. The second line loads the bignum pragma, after which binary operators are overloaded and constants are converted into objects. The third line converts the Perl scalar 0 into a Math::BigInt object with the value 0. It is a Math::BigInt object, not a Math::BigFloat object, because 0 is an integer. Then the addition is performed, and it is done by Math::BigInt->badd(). Since that method can only handle integers, the value of $x is truncated to an integer, i.e., 9. Adding 9 to 0 gives 9.
Thank you for the explanation. However, please also note, $x=10/78*77; use bignum; $y=$x+0; (note "78" in the first line) would produce a Math::BigFloat object with a value of approx. 9.87179.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.