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

Re: [ID 20000316.002] Math::BigInt and string ops

Thread Previous | Thread Next
From:
Tom Christiansen
Date:
March 16, 2000 10:19
Subject:
Re: [ID 20000316.002] Math::BigInt and string ops
Message ID:
6862.953230746@chthon
>>$ perl -e 'use Math::BigInt;
>>           $foo = new Math::BigInt 12345;
>>           $foo =~ s/^\+//;
>>           print "$foo\n";
>>           $foo = int($foo/10+0.5);
>>           print "$foo\n"; '
>>+12345
>>NaN
>>
>>*Sigh* Under 5.005, the above code was a legitimate method to convert
>>$foo to a normal string/number/whatever, thus the above code actually
>>printed
>>
>>12345
>>1235
>>
>>instead. Can we please reinstate the old behavior?

>Is it the s/^\+// that's broken?

No, it's that if you try to initialize a bigint to "0.5", you get
"NaN", as there's no integer that "0.5" represents.  When the
overloaded + sees that one of its object operands is a mundane, it
tries to enchant it, and gets one that stringifies and nummifies
to NaN.  And doing anything to NaN buys you a NaN.

    #!/usr/bin/perl -l
    use Math::BigInt;
    use overload;
    use attributes 'reftype';

    $foo = new Math::BigInt "0.5";
    print "\nbigint of 0.5 ==>";
    inspect($foo);

    $foo = new Math::BigInt 12345;

    print "\nint [12345] ==>";
    inspect(int($foo));

    print "\n[12345]/10 ==>";
    inspect($foo / 10);

    print "\n[12345] + 0.5 ==>";
    inspect($foo + 0.5);

    print "\n[12345] + 5 ==>";
    inspect($foo + 5);

    sub inspect {
	my $x = shift;
	print "Dump of $x";
	unless (ref $x) {
	    print "This is not an reference.\n";
	    return;
	}
	print "This is a reference to a ", ref($x);
	if (reftype($x) eq ref($x)) {
	    print "But it is not an object.\n";
	    return;
	} 
	print "It appears to be an object that's really a ", reftype($x);
	print "And it is", overload::Overloaded($x) 
	    ? " indeed" : "n't", " overloaded.";
	print "Its non-overloaded string value is ", overload::StrVal($x)
	    if overload::Overloaded($x);
    } 


Yields:

    bigint of 0.5 ==>
    Dump of NaN
    This is a reference to a Math::BigInt
    It appears to be an object that's really a SCALAR
    And it is indeed overloaded.
    Its non-overloaded string value is Math::BigInt=SCALAR(0x11b724)

    int [12345] ==>
    Dump of 12345
    This is not an reference.


    [12345]/10 ==>
    Dump of +1234
    This is a reference to a Math::BigInt
    It appears to be an object that's really a SCALAR
    And it is indeed overloaded.
    Its non-overloaded string value is Math::BigInt=SCALAR(0x1556ac)

    [12345] + 0.5 ==>
    Dump of NaN
    This is a reference to a Math::BigInt
    It appears to be an object that's really a SCALAR
    And it is indeed overloaded.
    Its non-overloaded string value is Math::BigInt=SCALAR(0x155914)

    [12345] + 5 ==>
    Dump of +12350
    This is a reference to a Math::BigInt
    It appears to be an object that's really a SCALAR
    And it is indeed overloaded.
    Its non-overloaded string value is Math::BigInt=SCALAR(0x1558fc)

I leave this matter in the hands of the numerological cabalists.

--tom

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