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

Wise monks,

I'm running the same calculation with and without the bignum pragma. With the bignum pragma, I get an incorrect result. Could you explain me why?

$ perl -we 'print atan2(1, 0), $/, atan2(0, 1), $/;' 1.5707963267949 0 $ perl -we 'use bignum; print atan2(1, 0), $/, atan2(0, 1), $/;' 0 1.5707963267949
My perl is v5.8.5 built for i686-linux.

Update: I was fiddling with this snippet: Approximation with chain fractions when I found this.

Update: Tels has fixed this bug in Math::BigInt v1.75 (2005-03-20). Upgrade your Math::BigInt package if you are affected by this bug. Thanks.

Replies are listed 'Best First'.
[PATCH] Re: wrong results of atan2 with bignum
by Zaxo (Archbishop) on Jan 24, 2005 at 03:59 UTC

    Overloading atan2 occurs in Math::BigInt. It's obvious that bignum is getting the order of arguments wrong. That smells like an improper use of overload in the BigInt module.

    Going on that theory, lets try patching Math::BigInt to account for the third argument to an overloaded binary operator.

    Looking at Math-Bigint-1.74, the most recent, we see.

    use overload { # . . . other operators 'atan2' => sub { atan2($_[0]->numify(),$_[1]) }, # . . . the rest of them };
    Clearly, no attention is paid to the argument order. Here's a patch:
    --- BigInt.pm~ Sat Jan 1 10:57:35 2005 +++ BigInt.pm Sun Jan 23 22:04:36 2005 @@ -75,7 +75,9 @@ 'cos' => sub { cos($_[0]->numify()) }, 'sin' => sub { sin($_[0]->numify()) }, 'exp' => sub { exp($_[0]->numify()) }, -'atan2' => sub { atan2($_[0]->numify(),$_[1]) }, +'atan2' => sub { $_[2] ? + atan2($_[1],$_[0]->numify()) : + atan2($_[0]->numify(),$_[1]) }, # are not yet overloadable #'hex' => sub { print "hex"; $_[0]; },
    That is not applicable with patch as is, since I've converted tabs to spaces for web presentation. Use it as a guide to hand-edit the file.

    A quick grep for "atan2" in t/ reveals that the only tests of atan2 occur in fallback.t. The tests there are not too rigorous, only testing atan2 for equal values of the arguments, and testing twice for the same values. I'll patch that, too.

    --- fallback.t~ Thu Dec 11 14:17:58 2003 +++ fallback.t Sun Jan 23 22:36:17 2005 @@ -28,7 +28,7 @@ } print "# INC = @INC\n"; - plan tests => 8; + plan tests => 9; } # The tests below test that cos(BigInt) = cos(Scalar) which is DWIM, +but not @@ -46,10 +46,11 @@ ok (exp($bi), exp(1)); ok (atan2($bi,$bi), atan2(1,1)); -my $bf = Math::BigInt->new(1); +my $bf = Math::BigInt->new(0); -ok (cos($bf), cos(1)); -ok (sin($bf), sin(1)); -ok (exp($bf), exp(1)); -ok (atan2($bf,$bf), atan2(1,1)); +ok (cos($bf), cos(0)); +ok (sin($bf), sin(0)); +ok (exp($bf), exp(0)); +ok (atan2($bi,$bf), atan2(1,0)); +ok (atan2($bf,$bi), atan2(0,1));

    With these changes to lib/Math/BigInt.pm and t/fallback.t, Math::BigInt builds correctly and passes all tests.

    With that installed, now I get for your problem,

    $ perl -e'use bignum;print atan2(1,0), " ", atan2(0,1),$/' 1.5707963267949 0 $
    in complete agreement with reality.

    I'll review Math::BigInt for any other non-commutative binary opertors, and post the patches to rt.cpan.org.

    After Compline,
    Zaxo

Re: wrong results of atan2 with bignum
by ysth (Canon) on Jan 23, 2005 at 19:12 UTC
    If you haven't already, upgrade to the newest Math::BigInt and Math::BigFloat from CPAN and confirm that it still happens.

    BTW, I find it a lot easier to say:

    $ perl -wle'print for atan2(1,0), atan2(0,1)'
Re: wrong results of atan2 with bignum
by jbrugger (Parson) on Jan 23, 2005 at 19:04 UTC
    weird... it seems to give the opposite result to not using bignum... listening here as well.
    By the way, i was trying to use search.cpan.org to to search for the module, however i'm not getting to it for more than a week now.
    Anyone having problems as well using the search engine?
      i was trying to use search.cpan.org to to search for the module, however i'm not getting to it for more than a week now.Anyone having problems as well using the search engine?

      <hearsay>Someone said that it is "trouble with the network cards at FUNET</hearsay>


      I'm not really a human, but I play one on earth. flash japh
Never mind.
by gam3 (Curate) on Jan 24, 2005 at 16:23 UTC
    I got a little over exuberant and posted a reply before I read the other replies.

    Considered by davido: "delete: Empty node." Vote: (keep/edit/delete)=2/1/11.
    Unconsidered by davido: The keeps blocked reaping.