in reply to Using big numbers correctly

Hello robert44444uk,

I do not have much knowledge on but did you read this page Chinese remainder theorem/Perl?

See sample bellow:

#!/usr/bin/env perl use strict; use warnings; use feature qw (say); use ntheory qw/chinese lcm/; say chinese( [2328,16256], [410,5418] ), " mod ", lcm(16256,5418); # 28450328 mod 44037504 use Math::ModInt qw(mod); use Math::ModInt::ChineseRemainder qw(cr_combine); say cr_combine(mod(2,3),mod(3,5),mod(2,7)); # mod(23, 105)

You forgot to add on your sample of code: use ntheory qw/chinese/;.

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Using big numbers correctly
by robert44444uk (Acolyte) on Oct 16, 2018 at 10:41 UTC

    Hi BR. Thanks for the reply. The used of chinese as an instruction works in my code, the result is a long number, 954846259588805228035541587771, much greater than 2^64. My question is how to ensure that the result is used properly throughout any programming thereafter, and how the result is shown using print or printf.

    using the instruction print $crt1 -> bdstr(); produces the same result as the say instruction, which makes me think that probably $crt1 is being used correctly, but how can that instruction be used with a more complex print or printf line, whose output reads "crt solution is (put correct long number here)". Below is proof that the use of bdstr works

    #!/usr/bin/env perl use warnings; use strict; use Math::BigFloat lib=>"GMP"; use Math::Prime::Util qw/:all/; use Math::BigInt qw/:constant/; use feature ':5.10'; my $crt1=chinese([1,2],[1,3],[1,5],[5,7],[4,11],[9,13],[0,17],[14,19], +[22,23],[28,29],[28,31],[27,37],[3,41],[15,43],[9,47],[51,53],[55,59] +,[35,61],[17,67],[35,71],[67,73],[28,79]); say $crt1; print $crt1 -> bdstr();

      Well I seem to have solved the print problem with print 'crt solution =', $crt1 -> bdstr(),"\n"; So I have solved my own problem I think. 2 hours of my time I'll never get back though!

        Well I seem to have solved the print problem with print 'crt solution =', $crt1 -> bdstr(),"\n";

        You can just as well use print $crt1, "\n"; or say $crt1;.
        But printf "%.0f\n", $crt1; will simply round the value 954846259588805228035541587771 to a double:
        use warnings; use strict; use Math::BigFloat; use feature ':5.10'; my $x = Math::BigFloat->new('954846259588805228035541587771'); print $x->bdstr(), "\n"; print $x, "\n"; say $x; printf "%.0f\n", $x; printf "%.29e\n", 954846259588805228035541587771; __END__ Outputs: 954846259588805228035541587771 954846259588805228035541587771 954846259588805228035541587771 954846259588805282215996948480 9.54846259588805282215996948480e+29
        You get the correct output using "print" and "say" owing to operator overloading, and because $crt1 is a Math::BigFloat object.
        Similarly, it doesn't matter whether you multiply/divide/add/subtract $crt1 with an integer, a perl floating point value, another Math::BigFloat object, or even a string.
        This, again, can be attributed to operator overloading:
        use warnings; use strict; use Math::BigFloat; use feature ':5.10'; my $x = Math::BigFloat->new('954846259588805228035541587771'); print $x * 2, "\n"; print $x * 2.5, "\n"; print $x * Math::BigFloat->new('2.5'), "\n"; print $x * "2.5"; __END__ Outputs: 1909692519177610456071083175542 2387115648972013070088853969427.5 2387115648972013070088853969427.5 2387115648972013070088853969427.5

        Cheers,
        Rob