spiritway has asked for the wisdom of the Perl Monks concerning the following question:

Beloved Brethren and Sistren:

I am having a difficulty that is sorely trying me. I am trying to use the BigFloat module for various calculations. My code is thus:

#!/usr/bin/perl -w use warnings; use strict; use diagnostics; use Math::BigFloat; my $float=Math::BigFloat->new(2.0); $float->bsqrt(); print "\$float is a:\t" . ref($float) . "\n"; my $int = $float->as_int(); print "\$int is a:\t" .ref($int) . "\n"; print "\$float\t$float\n\$int:\t$int\n";

According to the documents, the as_int() method should return an BigInt value. However, as you can see from this output, it does not:

$float is a: Math::BigFloat $int is a: Math::BigFloat $float 1.41421356237309504880168872420969807857 $int: 1.41421356237309504880168872420969807857

The documentation for this module says:

$x->as_int(); # return $x as BigInt
What am I doing wrong? I assume I've made some assumption that isn't true, but I haven't been able to find anything in the documentation that explains this mysterious behavior. I cannot believe that this is a bug... any help would be appreciated. Thank you.

Update: Thanks, Zaxo. That worked beautifully...

Replies are listed 'Best First'.
Re: Unexpected behavior of a method in BigFloat
by Zaxo (Archbishop) on Nov 02, 2005 at 09:36 UTC

    Math::BigFloat doesn't appear to have an as_int() method, and it doesn't look like its AUTOLOAD() will provide a non-trivial one.

    There is an as_number() method which does what as_int() is documented to do. Changing the call to that in your code gives:

    $ perl as_int.pl $float is a: Math::BigFloat $int is a: Math::BigInt $float 1.41421356237309504880168872420969807857 $int: 1 $
    Documentation bug.

    After Compline,
    Zaxo

      Now you've got me curious... The POD for Math::BigFloat talks of this method. In fact, I even went to the author's Website for more information. He states that he added as_int() for v1.68 of this module.

      So my questions are, first, is this something that might be considered a "bug", since the documentation appears to conflict with the actual behavior of the method? IOW, should I perhaps report this to the author?

      Also important for my sanity, though, is how could I have avoided knocking my head against a wall? Such as, how did you know about as_number()? If I had known where to look, I could have saved myself hours of going crazy over this...

        Ah, that explains it, my version is 1.50.

        I found the problem in that version by running perldoc -m Math::BigFloat, giving the source listing. My pager is less so I'm able to search text by typing "/as_int". That found only the instance in pod. Finding where the as_* methods are by the same method, I noticed as_number and saw from its definition that it was what we wanted.

        Since it's been remedied in recent versions, I don't think it's worth bothering the author with it.

        After Compline,
        Zaxo