J
jl_post
Hi,
I've recently discovered that if I create a "bignum" object in Perl
and call the ->bdiv() method, sometimes the object will get modified
and sometimes it won't. Here's a sample program that illustrates what
I'm talking about:
#!/usr/bin/perl
use strict;
use warnings;
use bignum;
my $n1 = 6.6;
print "\$n1 before \$n1->bdiv(2): $n1\n"; # prints 6.6
$n1->bdiv(2);
print "\$n1 after \$n1->bdiv(2): $n1\n"; # prints 3.3
print "\n";
my $n2 = 6;
print "\$n2 before \$n2->bdiv(2): $n2\n"; # prints 6
$n2->bdiv(2);
print "\$n2 after \$n2->bdiv(2): $n2\n"; # prints 6 (error?)
__END__
I've tested this script on both Windows and Unix, and the output is
the same. I changed "use bignum" to "use bigint" and I saw the
expected 6, 3, and 6, 3 as output. (The floating-point numbers were
truncated, but that's no surprise since we're working with integers.)
I also changed "bdiv" to "bmul" (using the "bignum" module) and I
saw the expected 6.6, 13.2, and 6, 12 as output, so apparently
bignum::dmul() works consistently.
I've noticed that bignum::bdiv() seems to modify the calling object
if the value is a floating-point number (that is, cannot be
represented as an integer). But if the calling object is an integer
(even one with a decimal point like 6.0), then bignum::bdiv() does not
modify it.
I noticed that if I print out ref($n1) and ref($n2), I see that $n1
is a Math::BigFloat and that $n2 is a Math::BigInt. So I might think
that the problem lies in the Math::BigInt module. However, when I
change "use bignum" to "use bigint", they both become Math::BigInt
objects, but now the bdiv() method is modifying the calling object (as
I think it should).
So it doesn't look like the problem is with Math::BigInt, but
rather with the "bignum" module.
Is this incosistent behavior of the "bignum" module a bug, or am I
missing something? I haven't seen much code that uses the "bigint"
and "bignum" modules, so as far as I know they're deprecated, but I
haven't read anything to that effect.
-- Jean-Luc
I've recently discovered that if I create a "bignum" object in Perl
and call the ->bdiv() method, sometimes the object will get modified
and sometimes it won't. Here's a sample program that illustrates what
I'm talking about:
#!/usr/bin/perl
use strict;
use warnings;
use bignum;
my $n1 = 6.6;
print "\$n1 before \$n1->bdiv(2): $n1\n"; # prints 6.6
$n1->bdiv(2);
print "\$n1 after \$n1->bdiv(2): $n1\n"; # prints 3.3
print "\n";
my $n2 = 6;
print "\$n2 before \$n2->bdiv(2): $n2\n"; # prints 6
$n2->bdiv(2);
print "\$n2 after \$n2->bdiv(2): $n2\n"; # prints 6 (error?)
__END__
I've tested this script on both Windows and Unix, and the output is
the same. I changed "use bignum" to "use bigint" and I saw the
expected 6, 3, and 6, 3 as output. (The floating-point numbers were
truncated, but that's no surprise since we're working with integers.)
I also changed "bdiv" to "bmul" (using the "bignum" module) and I
saw the expected 6.6, 13.2, and 6, 12 as output, so apparently
bignum::dmul() works consistently.
I've noticed that bignum::bdiv() seems to modify the calling object
if the value is a floating-point number (that is, cannot be
represented as an integer). But if the calling object is an integer
(even one with a decimal point like 6.0), then bignum::bdiv() does not
modify it.
I noticed that if I print out ref($n1) and ref($n2), I see that $n1
is a Math::BigFloat and that $n2 is a Math::BigInt. So I might think
that the problem lies in the Math::BigInt module. However, when I
change "use bignum" to "use bigint", they both become Math::BigInt
objects, but now the bdiv() method is modifying the calling object (as
I think it should).
So it doesn't look like the problem is with Math::BigInt, but
rather with the "bignum" module.
Is this incosistent behavior of the "bignum" module a bug, or am I
missing something? I haven't seen much code that uses the "bigint"
and "bignum" modules, so as far as I know they're deprecated, but I
haven't read anything to that effect.
-- Jean-Luc