Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Deep recursion on subroutine with bignum and Astro::Units

by Anonymous Monk
on Jan 21, 2023 at 02:54 UTC ( #11149736=perlquestion: print w/replies, xml ) Need Help??

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

I'm doing some stuff with the module Astro::Units whose pod suggests to explicitly use bignum. The program works fine until I try doing some simple arithmetic. I can't reproduce the error with an SSCE so here are the errors and the calculations causing them:
Deep recursion on subroutine "Math::BigFloat::bdiv" at /5.26.2/Math/BigInt.pm line 1867.
Deep recursion on subroutine "Math::BigInt::Calc::_lsft" at /5.26.2/Math/BigFloat.pm line 1948.
Deep recursion on anonymous subroutine at /5.26.2/Math/BigInt/Calc.pm line 1412.
Deep recursion on subroutine "Math::BigInt::bdiv" at /5.26.2/Math/BigInt.pm line 54.
Here's the math and the results when loading and not loading (but not directly using) both bignum and Astro::Units:
perl -le '$Da=406700; $Dp=356400; $Dm=361918; $pct = ($Da - $Dm) / ($D +a - $Dp); print $pct'
0.890298210735586
perl -Mbignum -le '$Da=406700; $Dp=356400; $Dm=361918; $pct = ($Da - $ +Dm) / ($Da - $Dp); print $pct'
0.8902982107355864811133200795228628230616
perl -Mbignum -MAstro::Units -le '$Da=406700; $Dp=356400; $Dm=361918; +$pct = ($Da - $Dm) / ($Da - $Dp); print $pct'
0.8902982
perl -MAstro::Units -le '$Da=406700; $Dp=356400; $Dm=361918; $pct = ($ +Da - $Dm) / ($Da - $Dp); print $pct'
0.890298210735586

Can anyone see what's going on here? That third result is puzzling. What could be causing such errors? Thanks!

Replies are listed 'Best First'.
Re: Deep recursion on subroutine with bignum and Astro::Units
by hv (Prior) on Jan 21, 2023 at 12:02 UTC

    On a general note, I consider it unwise of the module author to use bignum and doubly-unwise for them to recommend that everyone using their module should do so. Each of bignum, bigint, bigrat can be fine and useful for simple one-liners, but for anything more complex they are very likely to cause problems.

    For this specific issue, note that the current Math::BigInt maintainer has made wide-reaching changes multiple times in recent months in an attempt to make the interactions between the different classes behave more sanely in the context of use bignum; it is not clear to me whether it has now reached a stable point. So you may want to try both more recent and older versions of the distribution - check the changelog for references to "upgrading and downgrading".

    For getting the calculations you are using to work reliably, I suggest ditching bignum; instead instantiate your numbers as Math::BigFloat, and hope that the use of bignum inside Astro::Units doesn't result in unexpected conversions to Math::BigInt or Math::BigRat.

    Note that you should also be able to change the Math::BigFloat configuration after loading Astro::Units, if the latter's choices are not suitable for your needs.

      Thank you Monks! I'm still struggling to isolate the problem to an SSCE but did make an interesting discovery. Turns out the math is not causing the error. I though it was because commenting that line out eliminated it. Just before doing the math I use "int" on one of the numbers. If I don't use "int" the program works! It also works if "int" is used and "use bignum" is commented out AND this is done: Math::BigFloat->config({ div_scale => 1 }); (don't need to use Math::BigFloat, cause I guess Astro::Units already loaded it). I have no idea what is going on but will just not use "int". Hope this makes sense. Thanks again!
Re: Deep recursion on subroutine with bignum and Astro::Units
by soonix (Canon) on Jan 21, 2023 at 10:10 UTC
Re: Deep recursion on subroutine with bignum and Astro::Units
by karlgoethebier (Abbot) on Jan 21, 2023 at 13:52 UTC

    I always thought it was like converting miles to kilometers. You don't really need a Perl module for that. Does not make a big difference whether AU or nautical miles, light years or whatever - except the size of the numbers. But I am only a hobby astronomer and anyway very bad in math.

    «The Crux of the Biscuit is the Apostrophe»

      I always thought it was like converting miles to kilometers. You don't really need a Perl module for that.

      Thanks for the reality check. Converting AU to KM is easier than using the module. Multiply by 149597870.7 and done.

Re: Deep recursion on subroutine with bignum and Astro::Units
by Anonymous Monk on Jan 21, 2023 at 07:35 UTC

    See bug report, this Astro::Units won't even install with modern Math::BigInt. I don't know what's the purpose of line 12, and why setting div_scale to 0 with older Math::BigInt results in 7 digits you observe in your third case, but that's direct consequence.

    I'd suggest installing latest Math::BigInt, fixing line#12 (change value to 1? don't know) and see if deep recursion problem goes away.

Re: Deep recursion on subroutine with bignum and Astro::Units
by Anonymous Monk on Jan 21, 2023 at 03:36 UTC
    OP here. Forgot to mention after the errors Perl uses a lot of CPU for about 10 seconds and dies with: Segmentation fault: 11
Re: Deep recursion on subroutine with bignum and Astro::Units
by Anonymous Monk on Jan 21, 2023 at 05:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11149736]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-02-01 02:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?