From 6b0f9b46aaa4198a1e8ed620a940f4f2bd304859 Mon Sep 17 00:00:00 2001 From: Ricardo Signes Date: Sat, 5 Sep 2015 10:20:42 -0400 Subject: move Math-Big* from ./dist to ./cpan --- MANIFEST | 166 +- META.json | 6 +- META.yml | 6 +- Porting/Maintainers.pl | 6 +- cpan/Math-BigInt-FastCalc/FastCalc.xs | 410 ++ .../lib/Math/BigInt/FastCalc.pm | 112 + cpan/Math-BigInt-FastCalc/t/bigintfc.t | 430 ++ cpan/Math-BigInt-FastCalc/t/bootstrap.t | 8 + cpan/Math-BigInt-FastCalc/t/leak.t | 79 + cpan/Math-BigInt-FastCalc/t/mbi_rand.t | 55 + cpan/Math-BigInt/lib/Math/BigFloat.pm | 4692 +++++++++++++++++ cpan/Math-BigInt/lib/Math/BigInt.pm | 5556 ++++++++++++++++++++ cpan/Math-BigInt/lib/Math/BigInt/Calc.pm | 3029 +++++++++++ cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm | 395 ++ cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm | 49 + cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm | 44 + cpan/Math-BigInt/t/Math/BigInt/Scalar.pm | 355 ++ cpan/Math-BigInt/t/Math/BigInt/Subclass.pm | 90 + cpan/Math-BigInt/t/_e_math.t | 85 + cpan/Math-BigInt/t/alias.inc | 12 + cpan/Math-BigInt/t/bare_mbf.t | 14 + cpan/Math-BigInt/t/bare_mbi.t | 18 + cpan/Math-BigInt/t/bare_mif.t | 24 + cpan/Math-BigInt/t/big_pi_e.t | 33 + cpan/Math-BigInt/t/bigfltpm.inc | 1836 +++++++ cpan/Math-BigInt/t/bigfltpm.t | 29 + cpan/Math-BigInt/t/bigintc.t | 454 ++ cpan/Math-BigInt/t/bigintpm.inc | 2707 ++++++++++ cpan/Math-BigInt/t/bigintpm.t | 36 + cpan/Math-BigInt/t/bigints.t | 99 + cpan/Math-BigInt/t/biglog.t | 187 + cpan/Math-BigInt/t/bigroot.t | 43 + cpan/Math-BigInt/t/calling.t | 149 + cpan/Math-BigInt/t/config.t | 128 + cpan/Math-BigInt/t/const_mbf.t | 14 + cpan/Math-BigInt/t/constant.t | 35 + cpan/Math-BigInt/t/downgrade.t | 45 + cpan/Math-BigInt/t/inf_nan.t | 404 ++ cpan/Math-BigInt/t/isa.t | 34 + cpan/Math-BigInt/t/lib_load.t | 29 + cpan/Math-BigInt/t/mbf_ali.t | 14 + cpan/Math-BigInt/t/mbi_ali.t | 14 + cpan/Math-BigInt/t/mbi_rand.t | 86 + cpan/Math-BigInt/t/mbimbf.inc | 951 ++++ cpan/Math-BigInt/t/mbimbf.t | 78 + cpan/Math-BigInt/t/nan_cmp.t | 36 + cpan/Math-BigInt/t/new_overloaded.t | 32 + cpan/Math-BigInt/t/req_mbf0.t | 13 + cpan/Math-BigInt/t/req_mbf1.t | 10 + cpan/Math-BigInt/t/req_mbfa.t | 10 + cpan/Math-BigInt/t/req_mbfi.t | 10 + cpan/Math-BigInt/t/req_mbfn.t | 10 + cpan/Math-BigInt/t/req_mbfw.t | 23 + cpan/Math-BigInt/t/require.t | 15 + cpan/Math-BigInt/t/round.t | 94 + cpan/Math-BigInt/t/rt-16221.t | 77 + cpan/Math-BigInt/t/sub_ali.t | 15 + cpan/Math-BigInt/t/sub_mbf.t | 33 + cpan/Math-BigInt/t/sub_mbi.t | 34 + cpan/Math-BigInt/t/sub_mif.t | 19 + cpan/Math-BigInt/t/trap.t | 84 + cpan/Math-BigInt/t/upgrade.inc | 1494 ++++++ cpan/Math-BigInt/t/upgrade.t | 19 + cpan/Math-BigInt/t/upgrade2.t | 14 + cpan/Math-BigInt/t/upgradef.t | 57 + cpan/Math-BigInt/t/use.t | 19 + cpan/Math-BigInt/t/use_lib1.t | 15 + cpan/Math-BigInt/t/use_lib2.t | 16 + cpan/Math-BigInt/t/use_lib3.t | 16 + cpan/Math-BigInt/t/use_lib4.t | 17 + cpan/Math-BigInt/t/use_mbfw.t | 26 + cpan/Math-BigInt/t/with_sub.t | 17 + cpan/Math-BigRat/lib/Math/BigRat.pm | 2202 ++++++++ cpan/Math-BigRat/t/Math/BigRat/Test.pm | 122 + cpan/Math-BigRat/t/big_ap.t | 94 + cpan/Math-BigRat/t/bigfltpm.inc | 1673 ++++++ cpan/Math-BigRat/t/bigfltrt.t | 19 + cpan/Math-BigRat/t/biglog.t | 72 + cpan/Math-BigRat/t/bigrat.t | 332 ++ cpan/Math-BigRat/t/bigratpm.inc | 916 ++++ cpan/Math-BigRat/t/bigratpm.t | 12 + cpan/Math-BigRat/t/bigratup.t | 31 + cpan/Math-BigRat/t/bigroot.t | 41 + cpan/Math-BigRat/t/bitwise.t | 15 + cpan/Math-BigRat/t/hang.t | 18 + cpan/Math-BigRat/t/requirer.t | 14 + cpan/Math-BigRat/t/trap.t | 74 + dist/Math-BigInt-FastCalc/FastCalc.xs | 410 -- .../lib/Math/BigInt/FastCalc.pm | 112 - dist/Math-BigInt-FastCalc/t/bigintfc.t | 430 -- dist/Math-BigInt-FastCalc/t/bootstrap.t | 8 - dist/Math-BigInt-FastCalc/t/leak.t | 79 - dist/Math-BigInt-FastCalc/t/mbi_rand.t | 55 - dist/Math-BigInt/lib/Math/BigFloat.pm | 4692 ----------------- dist/Math-BigInt/lib/Math/BigInt.pm | 5556 -------------------- dist/Math-BigInt/lib/Math/BigInt/Calc.pm | 3029 ----------- dist/Math-BigInt/lib/Math/BigInt/CalcEmu.pm | 395 -- dist/Math-BigInt/t/Math/BigFloat/Subclass.pm | 49 - dist/Math-BigInt/t/Math/BigInt/BareCalc.pm | 44 - dist/Math-BigInt/t/Math/BigInt/Scalar.pm | 355 -- dist/Math-BigInt/t/Math/BigInt/Subclass.pm | 90 - dist/Math-BigInt/t/_e_math.t | 85 - dist/Math-BigInt/t/alias.inc | 12 - dist/Math-BigInt/t/bare_mbf.t | 14 - dist/Math-BigInt/t/bare_mbi.t | 18 - dist/Math-BigInt/t/bare_mif.t | 24 - dist/Math-BigInt/t/big_pi_e.t | 33 - dist/Math-BigInt/t/bigfltpm.inc | 1836 ------- dist/Math-BigInt/t/bigfltpm.t | 29 - dist/Math-BigInt/t/bigintc.t | 454 -- dist/Math-BigInt/t/bigintpm.inc | 2707 ---------- dist/Math-BigInt/t/bigintpm.t | 36 - dist/Math-BigInt/t/bigints.t | 99 - dist/Math-BigInt/t/biglog.t | 187 - dist/Math-BigInt/t/bigroot.t | 43 - dist/Math-BigInt/t/calling.t | 149 - dist/Math-BigInt/t/config.t | 128 - dist/Math-BigInt/t/const_mbf.t | 14 - dist/Math-BigInt/t/constant.t | 35 - dist/Math-BigInt/t/downgrade.t | 45 - dist/Math-BigInt/t/inf_nan.t | 404 -- dist/Math-BigInt/t/isa.t | 34 - dist/Math-BigInt/t/lib_load.t | 29 - dist/Math-BigInt/t/mbf_ali.t | 14 - dist/Math-BigInt/t/mbi_ali.t | 14 - dist/Math-BigInt/t/mbi_rand.t | 86 - dist/Math-BigInt/t/mbimbf.inc | 951 ---- dist/Math-BigInt/t/mbimbf.t | 78 - dist/Math-BigInt/t/nan_cmp.t | 36 - dist/Math-BigInt/t/new_overloaded.t | 32 - dist/Math-BigInt/t/req_mbf0.t | 13 - dist/Math-BigInt/t/req_mbf1.t | 10 - dist/Math-BigInt/t/req_mbfa.t | 10 - dist/Math-BigInt/t/req_mbfi.t | 10 - dist/Math-BigInt/t/req_mbfn.t | 10 - dist/Math-BigInt/t/req_mbfw.t | 23 - dist/Math-BigInt/t/require.t | 15 - dist/Math-BigInt/t/round.t | 94 - dist/Math-BigInt/t/rt-16221.t | 77 - dist/Math-BigInt/t/sub_ali.t | 15 - dist/Math-BigInt/t/sub_mbf.t | 33 - dist/Math-BigInt/t/sub_mbi.t | 34 - dist/Math-BigInt/t/sub_mif.t | 19 - dist/Math-BigInt/t/trap.t | 84 - dist/Math-BigInt/t/upgrade.inc | 1494 ------ dist/Math-BigInt/t/upgrade.t | 19 - dist/Math-BigInt/t/upgrade2.t | 14 - dist/Math-BigInt/t/upgradef.t | 57 - dist/Math-BigInt/t/use.t | 19 - dist/Math-BigInt/t/use_lib1.t | 15 - dist/Math-BigInt/t/use_lib2.t | 16 - dist/Math-BigInt/t/use_lib3.t | 16 - dist/Math-BigInt/t/use_lib4.t | 17 - dist/Math-BigInt/t/use_mbfw.t | 26 - dist/Math-BigInt/t/with_sub.t | 17 - dist/Math-BigRat/lib/Math/BigRat.pm | 2202 -------- dist/Math-BigRat/t/Math/BigRat/Test.pm | 122 - dist/Math-BigRat/t/big_ap.t | 94 - dist/Math-BigRat/t/bigfltpm.inc | 1673 ------ dist/Math-BigRat/t/bigfltrt.t | 19 - dist/Math-BigRat/t/biglog.t | 72 - dist/Math-BigRat/t/bigrat.t | 332 -- dist/Math-BigRat/t/bigratpm.inc | 916 ---- dist/Math-BigRat/t/bigratpm.t | 12 - dist/Math-BigRat/t/bigratup.t | 31 - dist/Math-BigRat/t/bigroot.t | 41 - dist/Math-BigRat/t/bitwise.t | 15 - dist/Math-BigRat/t/hang.t | 18 - dist/Math-BigRat/t/requirer.t | 14 - dist/Math-BigRat/t/trap.t | 74 - t/porting/known_pod_issues.dat | 2 - 171 files changed, 30814 insertions(+), 30816 deletions(-) create mode 100644 cpan/Math-BigInt-FastCalc/FastCalc.xs create mode 100644 cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm create mode 100644 cpan/Math-BigInt-FastCalc/t/bigintfc.t create mode 100644 cpan/Math-BigInt-FastCalc/t/bootstrap.t create mode 100644 cpan/Math-BigInt-FastCalc/t/leak.t create mode 100644 cpan/Math-BigInt-FastCalc/t/mbi_rand.t create mode 100644 cpan/Math-BigInt/lib/Math/BigFloat.pm create mode 100644 cpan/Math-BigInt/lib/Math/BigInt.pm create mode 100644 cpan/Math-BigInt/lib/Math/BigInt/Calc.pm create mode 100644 cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm create mode 100644 cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm create mode 100644 cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm create mode 100644 cpan/Math-BigInt/t/Math/BigInt/Scalar.pm create mode 100644 cpan/Math-BigInt/t/Math/BigInt/Subclass.pm create mode 100644 cpan/Math-BigInt/t/_e_math.t create mode 100644 cpan/Math-BigInt/t/alias.inc create mode 100644 cpan/Math-BigInt/t/bare_mbf.t create mode 100644 cpan/Math-BigInt/t/bare_mbi.t create mode 100644 cpan/Math-BigInt/t/bare_mif.t create mode 100644 cpan/Math-BigInt/t/big_pi_e.t create mode 100644 cpan/Math-BigInt/t/bigfltpm.inc create mode 100644 cpan/Math-BigInt/t/bigfltpm.t create mode 100644 cpan/Math-BigInt/t/bigintc.t create mode 100644 cpan/Math-BigInt/t/bigintpm.inc create mode 100644 cpan/Math-BigInt/t/bigintpm.t create mode 100644 cpan/Math-BigInt/t/bigints.t create mode 100644 cpan/Math-BigInt/t/biglog.t create mode 100644 cpan/Math-BigInt/t/bigroot.t create mode 100644 cpan/Math-BigInt/t/calling.t create mode 100644 cpan/Math-BigInt/t/config.t create mode 100644 cpan/Math-BigInt/t/const_mbf.t create mode 100644 cpan/Math-BigInt/t/constant.t create mode 100644 cpan/Math-BigInt/t/downgrade.t create mode 100644 cpan/Math-BigInt/t/inf_nan.t create mode 100644 cpan/Math-BigInt/t/isa.t create mode 100644 cpan/Math-BigInt/t/lib_load.t create mode 100644 cpan/Math-BigInt/t/mbf_ali.t create mode 100644 cpan/Math-BigInt/t/mbi_ali.t create mode 100644 cpan/Math-BigInt/t/mbi_rand.t create mode 100644 cpan/Math-BigInt/t/mbimbf.inc create mode 100644 cpan/Math-BigInt/t/mbimbf.t create mode 100644 cpan/Math-BigInt/t/nan_cmp.t create mode 100644 cpan/Math-BigInt/t/new_overloaded.t create mode 100644 cpan/Math-BigInt/t/req_mbf0.t create mode 100644 cpan/Math-BigInt/t/req_mbf1.t create mode 100644 cpan/Math-BigInt/t/req_mbfa.t create mode 100644 cpan/Math-BigInt/t/req_mbfi.t create mode 100644 cpan/Math-BigInt/t/req_mbfn.t create mode 100644 cpan/Math-BigInt/t/req_mbfw.t create mode 100644 cpan/Math-BigInt/t/require.t create mode 100644 cpan/Math-BigInt/t/round.t create mode 100644 cpan/Math-BigInt/t/rt-16221.t create mode 100644 cpan/Math-BigInt/t/sub_ali.t create mode 100644 cpan/Math-BigInt/t/sub_mbf.t create mode 100644 cpan/Math-BigInt/t/sub_mbi.t create mode 100644 cpan/Math-BigInt/t/sub_mif.t create mode 100644 cpan/Math-BigInt/t/trap.t create mode 100644 cpan/Math-BigInt/t/upgrade.inc create mode 100644 cpan/Math-BigInt/t/upgrade.t create mode 100644 cpan/Math-BigInt/t/upgrade2.t create mode 100644 cpan/Math-BigInt/t/upgradef.t create mode 100644 cpan/Math-BigInt/t/use.t create mode 100644 cpan/Math-BigInt/t/use_lib1.t create mode 100644 cpan/Math-BigInt/t/use_lib2.t create mode 100644 cpan/Math-BigInt/t/use_lib3.t create mode 100644 cpan/Math-BigInt/t/use_lib4.t create mode 100644 cpan/Math-BigInt/t/use_mbfw.t create mode 100644 cpan/Math-BigInt/t/with_sub.t create mode 100644 cpan/Math-BigRat/lib/Math/BigRat.pm create mode 100644 cpan/Math-BigRat/t/Math/BigRat/Test.pm create mode 100644 cpan/Math-BigRat/t/big_ap.t create mode 100644 cpan/Math-BigRat/t/bigfltpm.inc create mode 100644 cpan/Math-BigRat/t/bigfltrt.t create mode 100644 cpan/Math-BigRat/t/biglog.t create mode 100644 cpan/Math-BigRat/t/bigrat.t create mode 100644 cpan/Math-BigRat/t/bigratpm.inc create mode 100644 cpan/Math-BigRat/t/bigratpm.t create mode 100644 cpan/Math-BigRat/t/bigratup.t create mode 100644 cpan/Math-BigRat/t/bigroot.t create mode 100644 cpan/Math-BigRat/t/bitwise.t create mode 100644 cpan/Math-BigRat/t/hang.t create mode 100644 cpan/Math-BigRat/t/requirer.t create mode 100644 cpan/Math-BigRat/t/trap.t delete mode 100644 dist/Math-BigInt-FastCalc/FastCalc.xs delete mode 100644 dist/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm delete mode 100644 dist/Math-BigInt-FastCalc/t/bigintfc.t delete mode 100644 dist/Math-BigInt-FastCalc/t/bootstrap.t delete mode 100644 dist/Math-BigInt-FastCalc/t/leak.t delete mode 100644 dist/Math-BigInt-FastCalc/t/mbi_rand.t delete mode 100644 dist/Math-BigInt/lib/Math/BigFloat.pm delete mode 100644 dist/Math-BigInt/lib/Math/BigInt.pm delete mode 100644 dist/Math-BigInt/lib/Math/BigInt/Calc.pm delete mode 100644 dist/Math-BigInt/lib/Math/BigInt/CalcEmu.pm delete mode 100644 dist/Math-BigInt/t/Math/BigFloat/Subclass.pm delete mode 100644 dist/Math-BigInt/t/Math/BigInt/BareCalc.pm delete mode 100644 dist/Math-BigInt/t/Math/BigInt/Scalar.pm delete mode 100644 dist/Math-BigInt/t/Math/BigInt/Subclass.pm delete mode 100644 dist/Math-BigInt/t/_e_math.t delete mode 100644 dist/Math-BigInt/t/alias.inc delete mode 100644 dist/Math-BigInt/t/bare_mbf.t delete mode 100644 dist/Math-BigInt/t/bare_mbi.t delete mode 100644 dist/Math-BigInt/t/bare_mif.t delete mode 100644 dist/Math-BigInt/t/big_pi_e.t delete mode 100644 dist/Math-BigInt/t/bigfltpm.inc delete mode 100644 dist/Math-BigInt/t/bigfltpm.t delete mode 100644 dist/Math-BigInt/t/bigintc.t delete mode 100644 dist/Math-BigInt/t/bigintpm.inc delete mode 100644 dist/Math-BigInt/t/bigintpm.t delete mode 100644 dist/Math-BigInt/t/bigints.t delete mode 100644 dist/Math-BigInt/t/biglog.t delete mode 100644 dist/Math-BigInt/t/bigroot.t delete mode 100644 dist/Math-BigInt/t/calling.t delete mode 100644 dist/Math-BigInt/t/config.t delete mode 100644 dist/Math-BigInt/t/const_mbf.t delete mode 100644 dist/Math-BigInt/t/constant.t delete mode 100644 dist/Math-BigInt/t/downgrade.t delete mode 100644 dist/Math-BigInt/t/inf_nan.t delete mode 100644 dist/Math-BigInt/t/isa.t delete mode 100644 dist/Math-BigInt/t/lib_load.t delete mode 100644 dist/Math-BigInt/t/mbf_ali.t delete mode 100644 dist/Math-BigInt/t/mbi_ali.t delete mode 100644 dist/Math-BigInt/t/mbi_rand.t delete mode 100644 dist/Math-BigInt/t/mbimbf.inc delete mode 100644 dist/Math-BigInt/t/mbimbf.t delete mode 100644 dist/Math-BigInt/t/nan_cmp.t delete mode 100644 dist/Math-BigInt/t/new_overloaded.t delete mode 100644 dist/Math-BigInt/t/req_mbf0.t delete mode 100644 dist/Math-BigInt/t/req_mbf1.t delete mode 100644 dist/Math-BigInt/t/req_mbfa.t delete mode 100644 dist/Math-BigInt/t/req_mbfi.t delete mode 100644 dist/Math-BigInt/t/req_mbfn.t delete mode 100644 dist/Math-BigInt/t/req_mbfw.t delete mode 100644 dist/Math-BigInt/t/require.t delete mode 100644 dist/Math-BigInt/t/round.t delete mode 100644 dist/Math-BigInt/t/rt-16221.t delete mode 100644 dist/Math-BigInt/t/sub_ali.t delete mode 100644 dist/Math-BigInt/t/sub_mbf.t delete mode 100644 dist/Math-BigInt/t/sub_mbi.t delete mode 100644 dist/Math-BigInt/t/sub_mif.t delete mode 100644 dist/Math-BigInt/t/trap.t delete mode 100644 dist/Math-BigInt/t/upgrade.inc delete mode 100644 dist/Math-BigInt/t/upgrade.t delete mode 100644 dist/Math-BigInt/t/upgrade2.t delete mode 100644 dist/Math-BigInt/t/upgradef.t delete mode 100644 dist/Math-BigInt/t/use.t delete mode 100644 dist/Math-BigInt/t/use_lib1.t delete mode 100644 dist/Math-BigInt/t/use_lib2.t delete mode 100644 dist/Math-BigInt/t/use_lib3.t delete mode 100644 dist/Math-BigInt/t/use_lib4.t delete mode 100644 dist/Math-BigInt/t/use_mbfw.t delete mode 100644 dist/Math-BigInt/t/with_sub.t delete mode 100644 dist/Math-BigRat/lib/Math/BigRat.pm delete mode 100644 dist/Math-BigRat/t/Math/BigRat/Test.pm delete mode 100644 dist/Math-BigRat/t/big_ap.t delete mode 100644 dist/Math-BigRat/t/bigfltpm.inc delete mode 100644 dist/Math-BigRat/t/bigfltrt.t delete mode 100644 dist/Math-BigRat/t/biglog.t delete mode 100644 dist/Math-BigRat/t/bigrat.t delete mode 100644 dist/Math-BigRat/t/bigratpm.inc delete mode 100644 dist/Math-BigRat/t/bigratpm.t delete mode 100644 dist/Math-BigRat/t/bigratup.t delete mode 100644 dist/Math-BigRat/t/bigroot.t delete mode 100644 dist/Math-BigRat/t/bitwise.t delete mode 100644 dist/Math-BigRat/t/hang.t delete mode 100644 dist/Math-BigRat/t/requirer.t delete mode 100644 dist/Math-BigRat/t/trap.t diff --git a/MANIFEST b/MANIFEST index 69fb34e273..e38d22851d 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1603,6 +1603,89 @@ cpan/Locale-Maketext-Simple/t/po_with_i_default/fr.po Locale::Simple tests cpan/Locale-Maketext-Simple/t/po_with_i_default/i_default.po Locale::Simple tests cpan/Locale-Maketext-Simple/t/po_without_i_default/en.po Locale::Simple tests cpan/Locale-Maketext-Simple/t/po_without_i_default/fr.po Locale::Simple tests +cpan/Math-BigInt-FastCalc/FastCalc.xs Math::BigInt::FastCalc extension +cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm Math::BigInt::FastCalc extension +cpan/Math-BigInt-FastCalc/t/bigintfc.t Math::BigInt::FastCalc extension +cpan/Math-BigInt-FastCalc/t/bootstrap.t Math::BigInt::FastCalc extension +cpan/Math-BigInt-FastCalc/t/leak.t test for memory leaks in Math::BigInt::FastCalc +cpan/Math-BigInt-FastCalc/t/mbi_rand.t Math::BigInt::FastCalc extension +cpan/Math-BigInt/lib/Math/BigFloat.pm An arbitrary precision floating-point arithmetic package +cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm Pure Perl module to support Math::BigInt +cpan/Math-BigInt/lib/Math/BigInt/Calc.pm Pure Perl module to support Math::BigInt +cpan/Math-BigInt/lib/Math/BigInt.pm An arbitrary precision integer arithmetic package +cpan/Math-BigInt/t/alias.inc Support for BigInt tests +cpan/Math-BigInt/t/bare_mbf.t Test MBF under Math::BigInt::BareCalc +cpan/Math-BigInt/t/bare_mbi.t Test MBI under Math::BigInt::BareCalc +cpan/Math-BigInt/t/bare_mif.t Rounding tests under BareCalc +cpan/Math-BigInt/t/bigfltpm.inc Shared tests for bigfltpm.t and sub_mbf.t +cpan/Math-BigInt/t/bigfltpm.t See if BigFloat.pm works +cpan/Math-BigInt/t/bigintc.t See if BigInt/Calc.pm works +cpan/Math-BigInt/t/bigintpm.inc Shared tests for bigintpm.t and sub_mbi.t +cpan/Math-BigInt/t/bigintpm.t See if BigInt.pm works +cpan/Math-BigInt/t/bigints.t See if BigInt.pm works +cpan/Math-BigInt/t/biglog.t Test the log function +cpan/Math-BigInt/t/big_pi_e.t test bpi() and bexp() +cpan/Math-BigInt/t/bigroot.t Test the broot function +cpan/Math-BigInt/t/calling.t Test calling conventions +cpan/Math-BigInt/t/config.t Test Math::BigInt->config() +cpan/Math-BigInt/t/constant.t Test Math::BigInt/BigFloat under :constant +cpan/Math-BigInt/t/const_mbf.t Test Math::BigInt +cpan/Math-BigInt/t/downgrade.t Test if use Math::BigInt(); under downgrade works +cpan/Math-BigInt/t/_e_math.t Helper routine in BigFloat for _e math +cpan/Math-BigInt/t/inf_nan.t Special tests for inf and *NaN* handling +cpan/Math-BigInt/t/isa.t Test for Math::BigInt inheritance +cpan/Math-BigInt/t/lib_load.t Test sane lib names +cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm Empty subclass of BigFloat for test +cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm Bigint's simulation of Calc +cpan/Math-BigInt/t/Math/BigInt/Scalar.pm Pure Perl module to support Math::BigInt +cpan/Math-BigInt/t/Math/BigInt/Subclass.pm Empty subclass of BigInt for test +cpan/Math-BigInt/t/mbf_ali.t Tests for BigFloat +cpan/Math-BigInt/t/mbi_ali.t Tests for BigInt +cpan/Math-BigInt/t/mbimbf.inc Actual BigInt/BigFloat accuracy, precision and fallback, round_mode tests +cpan/Math-BigInt/t/mbimbf.t BigInt/BigFloat accuracy, precision and fallback, round_mode +cpan/Math-BigInt/t/mbi_rand.t Test Math::BigInt randomly +cpan/Math-BigInt/t/nan_cmp.t overloaded comparison involving *NaN* +cpan/Math-BigInt/t/new_overloaded.t test overloaded numbers in BigFloat's new() +cpan/Math-BigInt/t/req_mbf0.t test: require Math::BigFloat; ->bzero(); +cpan/Math-BigInt/t/req_mbf1.t test: require Math::BigFloat; ->bone(); +cpan/Math-BigInt/t/req_mbfa.t test: require Math::BigFloat; ->bnan(); +cpan/Math-BigInt/t/req_mbfi.t test: require Math::BigFloat; ->binf(); +cpan/Math-BigInt/t/req_mbfn.t test: require Math::BigFloat; ->new(); +cpan/Math-BigInt/t/req_mbfw.t require Math::BigFloat; import ( with => ); +cpan/Math-BigInt/t/require.t Test if require Math::BigInt works +cpan/Math-BigInt/t/round.t Test rounding with non-integer A and P +cpan/Math-BigInt/t/rt-16221.t Tests for objectify() w/foreign objs +cpan/Math-BigInt/t/sub_ali.t Tests for aliases in BigInt subclasses +cpan/Math-BigInt/t/sub_mbf.t Empty subclass test of BigFloat +cpan/Math-BigInt/t/sub_mbi.t Empty subclass test of BigInt +cpan/Math-BigInt/t/sub_mif.t Test A & P with subclasses using mbimbf.inc +cpan/Math-BigInt/t/trap.t Test whether trap_nan and trap_inf work +cpan/Math-BigInt/t/upgrade2.t Test that two upgrade levels work +cpan/Math-BigInt/t/upgradef.t Test if use Math::BigFloat(); under upgrade works +cpan/Math-BigInt/t/upgrade.inc Actual tests for upgrade.t +cpan/Math-BigInt/t/upgrade.t Test if use Math::BigInt(); under upgrade works +cpan/Math-BigInt/t/use_lib1.t Test combinations of Math::BigInt and BigFloat +cpan/Math-BigInt/t/use_lib2.t Test combinations of Math::BigInt and BigFloat +cpan/Math-BigInt/t/use_lib3.t Test combinations of Math::BigInt and BigFloat +cpan/Math-BigInt/t/use_lib4.t Test combinations of Math::BigInt and BigFloat +cpan/Math-BigInt/t/use_mbfw.t use BigFloat w/ with and lib at the same time +cpan/Math-BigInt/t/use.t Test if use Math::BigInt(); works +cpan/Math-BigInt/t/with_sub.t Test use Math::BigFloat with => package +cpan/Math-BigRat/lib/Math/BigRat.pm Math::BigRat +cpan/Math-BigRat/t/big_ap.t Math::BigRat test +cpan/Math-BigRat/t/bigfltpm.inc Math::BigRat test +cpan/Math-BigRat/t/bigfltrt.t Math::BigRat test +cpan/Math-BigRat/t/biglog.t Math::BigRat test +cpan/Math-BigRat/t/bigratpm.inc Math::BigRat test +cpan/Math-BigRat/t/bigratpm.t Math::BigRat test +cpan/Math-BigRat/t/bigrat.t Math::BigRat test +cpan/Math-BigRat/t/bigratup.t test under $Math::BigInt::upgrade +cpan/Math-BigRat/t/bigroot.t Math::BigRat test +cpan/Math-BigRat/t/bitwise.t Math::BigRat test +cpan/Math-BigRat/t/hang.t Math::BigRat test for bug #34584 - hang in exp() +cpan/Math-BigRat/t/Math/BigRat/Test.pm Math::BigRat test helper +cpan/Math-BigRat/t/requirer.t see if require works properly +cpan/Math-BigRat/t/trap.t see if trap_nan and trap_inf work cpan/Math-Complex/lib/Math/Complex.pm A Complex package cpan/Math-Complex/lib/Math/Trig.pm A simple interface to complex trigonometry cpan/Math-Complex/t/Complex.t See if Math::Complex works @@ -3185,89 +3268,6 @@ dist/Locale-Maketext/t/60_super.t See if Locale::Maketext works dist/Locale-Maketext/t/70_fail_auto.t See if Locale::Maketext works dist/Locale-Maketext/t/90_utf8.t See if Locale::Maketext works dist/Locale-Maketext/t/91_backslash.t See if Locale::Maketext works -dist/Math-BigInt-FastCalc/FastCalc.xs Math::BigInt::FastCalc extension -dist/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm Math::BigInt::FastCalc extension -dist/Math-BigInt-FastCalc/t/bigintfc.t Math::BigInt::FastCalc extension -dist/Math-BigInt-FastCalc/t/bootstrap.t Math::BigInt::FastCalc extension -dist/Math-BigInt-FastCalc/t/leak.t test for memory leaks in Math::BigInt::FastCalc -dist/Math-BigInt-FastCalc/t/mbi_rand.t Math::BigInt::FastCalc extension -dist/Math-BigInt/lib/Math/BigFloat.pm An arbitrary precision floating-point arithmetic package -dist/Math-BigInt/lib/Math/BigInt/CalcEmu.pm Pure Perl module to support Math::BigInt -dist/Math-BigInt/lib/Math/BigInt/Calc.pm Pure Perl module to support Math::BigInt -dist/Math-BigInt/lib/Math/BigInt.pm An arbitrary precision integer arithmetic package -dist/Math-BigInt/t/alias.inc Support for BigInt tests -dist/Math-BigInt/t/bare_mbf.t Test MBF under Math::BigInt::BareCalc -dist/Math-BigInt/t/bare_mbi.t Test MBI under Math::BigInt::BareCalc -dist/Math-BigInt/t/bare_mif.t Rounding tests under BareCalc -dist/Math-BigInt/t/bigfltpm.inc Shared tests for bigfltpm.t and sub_mbf.t -dist/Math-BigInt/t/bigfltpm.t See if BigFloat.pm works -dist/Math-BigInt/t/bigintc.t See if BigInt/Calc.pm works -dist/Math-BigInt/t/bigintpm.inc Shared tests for bigintpm.t and sub_mbi.t -dist/Math-BigInt/t/bigintpm.t See if BigInt.pm works -dist/Math-BigInt/t/bigints.t See if BigInt.pm works -dist/Math-BigInt/t/biglog.t Test the log function -dist/Math-BigInt/t/big_pi_e.t test bpi() and bexp() -dist/Math-BigInt/t/bigroot.t Test the broot function -dist/Math-BigInt/t/calling.t Test calling conventions -dist/Math-BigInt/t/config.t Test Math::BigInt->config() -dist/Math-BigInt/t/constant.t Test Math::BigInt/BigFloat under :constant -dist/Math-BigInt/t/const_mbf.t Test Math::BigInt -dist/Math-BigInt/t/downgrade.t Test if use Math::BigInt(); under downgrade works -dist/Math-BigInt/t/_e_math.t Helper routine in BigFloat for _e math -dist/Math-BigInt/t/inf_nan.t Special tests for inf and *NaN* handling -dist/Math-BigInt/t/isa.t Test for Math::BigInt inheritance -dist/Math-BigInt/t/lib_load.t Test sane lib names -dist/Math-BigInt/t/Math/BigFloat/Subclass.pm Empty subclass of BigFloat for test -dist/Math-BigInt/t/Math/BigInt/BareCalc.pm Bigint's simulation of Calc -dist/Math-BigInt/t/Math/BigInt/Scalar.pm Pure Perl module to support Math::BigInt -dist/Math-BigInt/t/Math/BigInt/Subclass.pm Empty subclass of BigInt for test -dist/Math-BigInt/t/mbf_ali.t Tests for BigFloat -dist/Math-BigInt/t/mbi_ali.t Tests for BigInt -dist/Math-BigInt/t/mbimbf.inc Actual BigInt/BigFloat accuracy, precision and fallback, round_mode tests -dist/Math-BigInt/t/mbimbf.t BigInt/BigFloat accuracy, precision and fallback, round_mode -dist/Math-BigInt/t/mbi_rand.t Test Math::BigInt randomly -dist/Math-BigInt/t/nan_cmp.t overloaded comparison involving *NaN* -dist/Math-BigInt/t/new_overloaded.t test overloaded numbers in BigFloat's new() -dist/Math-BigInt/t/req_mbf0.t test: require Math::BigFloat; ->bzero(); -dist/Math-BigInt/t/req_mbf1.t test: require Math::BigFloat; ->bone(); -dist/Math-BigInt/t/req_mbfa.t test: require Math::BigFloat; ->bnan(); -dist/Math-BigInt/t/req_mbfi.t test: require Math::BigFloat; ->binf(); -dist/Math-BigInt/t/req_mbfn.t test: require Math::BigFloat; ->new(); -dist/Math-BigInt/t/req_mbfw.t require Math::BigFloat; import ( with => ); -dist/Math-BigInt/t/require.t Test if require Math::BigInt works -dist/Math-BigInt/t/round.t Test rounding with non-integer A and P -dist/Math-BigInt/t/rt-16221.t Tests for objectify() w/foreign objs -dist/Math-BigInt/t/sub_ali.t Tests for aliases in BigInt subclasses -dist/Math-BigInt/t/sub_mbf.t Empty subclass test of BigFloat -dist/Math-BigInt/t/sub_mbi.t Empty subclass test of BigInt -dist/Math-BigInt/t/sub_mif.t Test A & P with subclasses using mbimbf.inc -dist/Math-BigInt/t/trap.t Test whether trap_nan and trap_inf work -dist/Math-BigInt/t/upgrade2.t Test that two upgrade levels work -dist/Math-BigInt/t/upgradef.t Test if use Math::BigFloat(); under upgrade works -dist/Math-BigInt/t/upgrade.inc Actual tests for upgrade.t -dist/Math-BigInt/t/upgrade.t Test if use Math::BigInt(); under upgrade works -dist/Math-BigInt/t/use_lib1.t Test combinations of Math::BigInt and BigFloat -dist/Math-BigInt/t/use_lib2.t Test combinations of Math::BigInt and BigFloat -dist/Math-BigInt/t/use_lib3.t Test combinations of Math::BigInt and BigFloat -dist/Math-BigInt/t/use_lib4.t Test combinations of Math::BigInt and BigFloat -dist/Math-BigInt/t/use_mbfw.t use BigFloat w/ with and lib at the same time -dist/Math-BigInt/t/use.t Test if use Math::BigInt(); works -dist/Math-BigInt/t/with_sub.t Test use Math::BigFloat with => package -dist/Math-BigRat/lib/Math/BigRat.pm Math::BigRat -dist/Math-BigRat/t/big_ap.t Math::BigRat test -dist/Math-BigRat/t/bigfltpm.inc Math::BigRat test -dist/Math-BigRat/t/bigfltrt.t Math::BigRat test -dist/Math-BigRat/t/biglog.t Math::BigRat test -dist/Math-BigRat/t/bigratpm.inc Math::BigRat test -dist/Math-BigRat/t/bigratpm.t Math::BigRat test -dist/Math-BigRat/t/bigrat.t Math::BigRat test -dist/Math-BigRat/t/bigratup.t test under $Math::BigInt::upgrade -dist/Math-BigRat/t/bigroot.t Math::BigRat test -dist/Math-BigRat/t/bitwise.t Math::BigRat test -dist/Math-BigRat/t/hang.t Math::BigRat test for bug #34584 - hang in exp() -dist/Math-BigRat/t/Math/BigRat/Test.pm Math::BigRat test helper -dist/Math-BigRat/t/requirer.t see if require works properly -dist/Math-BigRat/t/trap.t see if trap_nan and trap_inf work dist/Module-CoreList/Changes Module::CoreList Changes dist/Module-CoreList/corelist The corelist command-line utility dist/Module-CoreList/identify-dependencies A usage example for Module::CoreList diff --git a/META.json b/META.json index 4697ad447a..a04e8d4661 100644 --- a/META.json +++ b/META.json @@ -34,9 +34,6 @@ "dist/IO/", "dist/lib/", "dist/Locale-Maketext", - "dist/Math-BigInt", - "dist/Math-BigInt-FastCalc", - "dist/Math-BigRat", "dist/Module-CoreList", "dist/Net-Ping", "dist/PathTools", @@ -101,6 +98,9 @@ "dist/lib/lib_pm.PL", "dist/lib/Makefile.PL", "dist/lib/t/01lib.t", + "dist/Math-BigInt", + "dist/Math-BigInt-FastCalc", + "dist/Math-BigRat", "lib/unicore/mktables", "pod/perlfilter.pod", "pod/perlpodstyle.pod", diff --git a/META.yml b/META.yml index 9330bba06b..3e7b204374 100644 --- a/META.yml +++ b/META.yml @@ -32,9 +32,6 @@ no_index: - dist/IO/ - dist/lib/ - dist/Locale-Maketext - - dist/Math-BigInt - - dist/Math-BigInt-FastCalc - - dist/Math-BigRat - dist/Module-CoreList - dist/Net-Ping - dist/PathTools @@ -98,6 +95,9 @@ no_index: - dist/lib/lib_pm.PL - dist/lib/Makefile.PL - dist/lib/t/01lib.t + - dist/Math-BigInt + - dist/Math-BigInt-FastCalc + - dist/Math-BigRat - lib/unicore/mktables - pod/perlfilter.pod - pod/perlpodstyle.pod diff --git a/Porting/Maintainers.pl b/Porting/Maintainers.pl index 0f8a50a391..da7157649c 100755 --- a/Porting/Maintainers.pl +++ b/Porting/Maintainers.pl @@ -752,7 +752,7 @@ use File::Glob qw(:case); 'Math::BigInt' => { 'DISTRIBUTION' => 'PJACKLAM/Math-BigInt-1.9997.tar.gz', - 'FILES' => q[dist/Math-BigInt], + 'FILES' => q[cpan/Math-BigInt], 'EXCLUDED' => [ qr{^inc/}, qr{^examples/}, @@ -766,7 +766,7 @@ use File::Glob qw(:case); 'Math::BigInt::FastCalc' => { 'DISTRIBUTION' => 'PJACKLAM/Math-BigInt-FastCalc-0.31.tar.gz', - 'FILES' => q[dist/Math-BigInt-FastCalc], + 'FILES' => q[cpan/Math-BigInt-FastCalc], 'EXCLUDED' => [ qr{^inc/}, qw( t/00sig.t @@ -789,7 +789,7 @@ use File::Glob qw(:case); 'Math::BigRat' => { 'DISTRIBUTION' => 'PJACKLAM/Math-BigRat-0.2606.tar.gz', - 'FILES' => q[dist/Math-BigRat], + 'FILES' => q[cpan/Math-BigRat], 'EXCLUDED' => [ qr{^inc/}, qw( t/00sig.t diff --git a/cpan/Math-BigInt-FastCalc/FastCalc.xs b/cpan/Math-BigInt-FastCalc/FastCalc.xs new file mode 100644 index 0000000000..a045c7172e --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/FastCalc.xs @@ -0,0 +1,410 @@ +#define PERL_NO_GET_CONTEXT + +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" + +/* for Perl prior to v5.7.1 */ +#ifndef SvUOK +# define SvUOK(sv) SvIOK_UV(sv) +#endif + +/* for Perl v5.6 (RT #63859) */ +#ifndef croak_xs_usage +# define croak_xs_usage croak +#endif + +double XS_BASE = 0; +double XS_BASE_LEN = 0; + +MODULE = Math::BigInt::FastCalc PACKAGE = Math::BigInt::FastCalc + +PROTOTYPES: DISABLE + + ############################################################################# + # 2002-08-12 0.03 Tels unreleased + # * is_zero/is_one/is_odd/is_even/len work now (pass v1.61 tests) + # 2002-08-13 0.04 Tels unreleased + # * returns no/yes for is_foo() methods to be faster + # 2002-08-18 0.06alpha + # * added _num(), _inc() and _dec() + # 2002-08-25 0.06 Tels + # * added __strip_zeros(), _copy() + # 2004-08-13 0.07 Tels + # * added _is_two(), _is_ten(), _ten() + # 2007-04-02 0.08 Tels + # * plug leaks by creating mortals + # 2007-05-27 0.09 Tels + # * add _new() + +#define RETURN_MORTAL_INT(value) \ + ST(0) = sv_2mortal(newSViv(value)); \ + XSRETURN(1); + +BOOT: +{ + if (items < 4) + croak("Usage: Math::BigInt::FastCalc::BOOT(package, version, base_len, base)"); + XS_BASE_LEN = SvIV(ST(2)); + XS_BASE = SvNV(ST(3)); +} + +############################################################################## +# _new + +SV * +_new(class, x) + SV* x + INIT: + STRLEN len; + char* cur; + STRLEN part_len; + AV *av = newAV(); + + CODE: + if (SvUOK(x) && SvUV(x) < XS_BASE) + { + /* shortcut for integer arguments */ + av_push (av, newSVuv( SvUV(x) )); + } + else + { + /* split the input (as string) into XS_BASE_LEN long parts */ + /* in perl: + [ reverse(unpack("a" . ($il % $BASE_LEN+1) + . ("a$BASE_LEN" x ($il / $BASE_LEN)), $_[1])) ]; + */ + cur = SvPV(x, len); /* convert to string & store length */ + cur += len; /* doing "cur = SvEND(x)" does not work! */ + # process the string from the back + while (len > 0) + { + /* use either BASE_LEN or the amount of remaining digits */ + part_len = (STRLEN) XS_BASE_LEN; + if (part_len > len) + { + part_len = len; + } + /* processed so many digits */ + cur -= part_len; + len -= part_len; + /* printf ("part '%s' (part_len: %i, len: %i, BASE_LEN: %i)\n", cur, part_len, len, XS_BASE_LEN); */ + if (part_len > 0) + { + av_push (av, newSVpvn(cur, part_len) ); + } + } + } + RETVAL = newRV_noinc((SV *)av); + OUTPUT: + RETVAL + +############################################################################## +# _copy + +void +_copy(class, x) + SV* x + INIT: + AV* a; + AV* a2; + SSize_t elems; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + a2 = (AV*)sv_2mortal((SV*)newAV()); + av_extend (a2, elems); /* pre-padd */ + while (elems >= 0) + { + /* av_store( a2, elems, newSVsv( (SV*)*av_fetch(a, elems, 0) ) ); */ + + /* looking and trying to preserve IV is actually slower when copying */ + /* temp = (SV*)*av_fetch(a, elems, 0); + if (SvIOK(temp)) + { + av_store( a2, elems, newSViv( SvIV( (SV*)*av_fetch(a, elems, 0) ))); + } + else + { + av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) ))); + } + */ + av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) ))); + elems--; + } + ST(0) = sv_2mortal( newRV_inc((SV*) a2) ); + +############################################################################## +# __strip_zeros (also check for empty arrays from div) + +void +__strip_zeros(x) + SV* x + INIT: + AV* a; + SV* temp; + SSize_t elems; + SSize_t index; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + ST(0) = x; /* we return x */ + if (elems == -1) + { + av_push (a, newSViv(0)); /* correct empty arrays */ + XSRETURN(1); + } + if (elems == 0) + { + XSRETURN(1); /* nothing to do since only one elem */ + } + index = elems; + while (index > 0) + { + temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ + if (SvNV(temp) != 0) + { + break; + } + index--; + } + if (index < elems) + { + index = elems - index; + while (index-- > 0) + { + av_pop (a); + } + } + XSRETURN(1); + +############################################################################## +# decrement (subtract one) + +void +_dec(class,x) + SV* x + INIT: + AV* a; + SV* temp; + SSize_t elems; + SSize_t index; + NV MAX; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + ST(0) = x; /* we return x */ + + MAX = XS_BASE - 1; + index = 0; + while (index <= elems) + { + temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ + sv_setnv (temp, SvNV(temp)-1); /* decrement */ + if (SvNV(temp) >= 0) + { + break; /* early out */ + } + sv_setnv (temp, MAX); /* overflow, so set this to $MAX */ + index++; + } + /* do have more than one element? */ + /* (more than one because [0] should be kept as single-element) */ + if (elems > 0) + { + temp = *av_fetch(a, elems, 0); /* fetch last element */ + if (SvIV(temp) == 0) /* did last elem overflow? */ + { + av_pop(a); /* yes, so shrink array */ + /* aka remove leading zeros */ + } + } + XSRETURN(1); /* return x */ + +############################################################################## +# increment (add one) + +void +_inc(class,x) + SV* x + INIT: + AV* a; + SV* temp; + SSize_t elems; + SSize_t index; + NV BASE; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + ST(0) = x; /* we return x */ + + BASE = XS_BASE; + index = 0; + while (index <= elems) + { + temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ + sv_setnv (temp, SvNV(temp)+1); + if (SvNV(temp) < BASE) + { + XSRETURN(1); /* return (early out) */ + } + sv_setiv (temp, 0); /* overflow, so set this elem to 0 */ + index++; + } + temp = *av_fetch(a, elems, 0); /* fetch last element */ + if (SvIV(temp) == 0) /* did last elem overflow? */ + { + av_push(a, newSViv(1)); /* yes, so extend array by 1 */ + } + XSRETURN(1); /* return x */ + +############################################################################## + +SV * +_zero(class) + ALIAS: + _one = 1 + _two = 2 + _ten = 10 + PREINIT: + AV *av = newAV(); + CODE: + av_push (av, newSViv( ix )); + RETVAL = newRV_noinc((SV *)av); + OUTPUT: + RETVAL + +############################################################################## + +void +_is_even(class, x) + SV* x + ALIAS: + _is_odd = 1 + INIT: + AV* a; + SV* temp; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + temp = *av_fetch(a, 0, 0); /* fetch first element */ + ST(0) = sv_2mortal(boolSV((SvIV(temp) & 1) == ix)); + +############################################################################## + +void +_is_zero(class, x) + SV* x + ALIAS: + _is_one = 1 + _is_two = 2 + _is_ten = 10 + INIT: + AV* a; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + if ( av_len(a) != 0) + { + ST(0) = &PL_sv_no; /* len != 1, can't be '0' */ + } + else + { + SV *const temp = *av_fetch(a, 0, 0); /* fetch first element */ + ST(0) = boolSV(SvIV(temp) == ix); + } + XSRETURN(1); + +############################################################################## + +void +_len(class,x) + SV* x + INIT: + AV* a; + SV* temp; + IV elems; + STRLEN len; + + CODE: + a = (AV*)SvRV(x); /* ref to aray, don't check ref */ + elems = av_len(a); /* number of elems in array */ + temp = *av_fetch(a, elems, 0); /* fetch last element */ + SvPV(temp, len); /* convert to string & store length */ + len += (IV) XS_BASE_LEN * elems; + ST(0) = sv_2mortal(newSViv(len)); + +############################################################################## + +void +_acmp(class, cx, cy); + SV* cx + SV* cy + INIT: + AV* array_x; + AV* array_y; + SSize_t elemsx, elemsy, diff; + SV* tempx; + SV* tempy; + STRLEN lenx; + STRLEN leny; + NV diff_nv; + SSize_t diff_str; + + CODE: + array_x = (AV*)SvRV(cx); /* ref to aray, don't check ref */ + array_y = (AV*)SvRV(cy); /* ref to aray, don't check ref */ + elemsx = av_len(array_x); + elemsy = av_len(array_y); + diff = elemsx - elemsy; /* difference */ + + if (diff > 0) + { + RETURN_MORTAL_INT(1); /* len differs: X > Y */ + } + else if (diff < 0) + { + RETURN_MORTAL_INT(-1); /* len differs: X < Y */ + } + /* both have same number of elements, so check length of last element + and see if it differs */ + tempx = *av_fetch(array_x, elemsx, 0); /* fetch last element */ + tempy = *av_fetch(array_y, elemsx, 0); /* fetch last element */ + SvPV(tempx, lenx); /* convert to string & store length */ + SvPV(tempy, leny); /* convert to string & store length */ + diff_str = (SSize_t)lenx - (SSize_t)leny; + if (diff_str > 0) + { + RETURN_MORTAL_INT(1); /* same len, but first elems differs in len */ + } + if (diff_str < 0) + { + RETURN_MORTAL_INT(-1); /* same len, but first elems differs in len */ + } + /* same number of digits, so need to make a full compare */ + diff_nv = 0; + while (elemsx >= 0) + { + tempx = *av_fetch(array_x, elemsx, 0); /* fetch curr x element */ + tempy = *av_fetch(array_y, elemsx, 0); /* fetch curr y element */ + diff_nv = SvNV(tempx) - SvNV(tempy); + if (diff_nv != 0) + { + break; + } + elemsx--; + } + if (diff_nv > 0) + { + RETURN_MORTAL_INT(1); + } + if (diff_nv < 0) + { + RETURN_MORTAL_INT(-1); + } + ST(0) = sv_2mortal(newSViv(0)); /* X and Y are equal */ + diff --git a/cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm b/cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm new file mode 100644 index 0000000000..9bf5a60839 --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm @@ -0,0 +1,112 @@ +package Math::BigInt::FastCalc; + +use 5.006; +use strict; +use warnings; + +use Math::BigInt::Calc 1.997; + +use vars '$VERSION'; + +$VERSION = '0.31'; + +############################################################################## +# global constants, flags and accessory + +# announce that we are compatible with MBI v1.83 and up +sub api_version () { 2; } + +# use Calc to override the methods that we do not provide in XS + +for my $method (qw/ + str num + add sub mul div + rsft lsft + mod modpow modinv + gcd + pow root sqrt log_int fac nok + digit check + from_hex from_bin from_oct as_hex as_bin as_oct + zeros base_len + xor or and + alen 1ex + /) + { + no strict 'refs'; + *{'Math::BigInt::FastCalc::_' . $method} = \&{'Math::BigInt::Calc::_' . $method}; + } + +require XSLoader; +XSLoader::load(__PACKAGE__, $VERSION, Math::BigInt::Calc::_base_len()); + +############################################################################## +############################################################################## + +1; +__END__ +=pod + +=head1 NAME + +Math::BigInt::FastCalc - Math::BigInt::Calc with some XS for more speed + +=head1 SYNOPSIS + +Provides support for big integer calculations. Not intended to be used by +other modules. Other modules which sport the same functions can also be used +to support Math::BigInt, like L or L. + +=head1 DESCRIPTION + +In order to allow for multiple big integer libraries, Math::BigInt was +rewritten to use library modules for core math routines. Any module which +follows the same API as this can be used instead by using the following: + + use Math::BigInt lib => 'libname'; + +'libname' is either the long name ('Math::BigInt::Pari'), or only the short +version like 'Pari'. To use this library: + + use Math::BigInt lib => 'FastCalc'; + +Note that from L v1.76 onwards, FastCalc will be loaded +automatically, if possible. + +=head1 STORAGE + +FastCalc works exactly like Calc, in stores the numbers in decimal form, +chopped into parts. + +=head1 METHODS + +The following functions are now implemented in FastCalc.xs: + + _is_odd _is_even _is_one _is_zero + _is_two _is_ten + _zero _one _two _ten + _acmp _len + _inc _dec + __strip_zeros _copy + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHORS + +Original math code by Mark Biggar, rewritten by Tels L +in late 2000. +Separated from BigInt and shaped API with the help of John Peacock. + +Fixed, sped-up and enhanced by Tels http://bloodgate.com 2001-2003. +Further streamlining (api_version 1 etc.) by Tels 2004-2007. + +Bug-fixing by Peter John Acklam Epjacklam@online.noE 2010-2011. + +=head1 SEE ALSO + +L, L, +L, L and L. + +=cut diff --git a/cpan/Math-BigInt-FastCalc/t/bigintfc.t b/cpan/Math-BigInt-FastCalc/t/bigintfc.t new file mode 100644 index 0000000000..c8751ad81b --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/t/bigintfc.t @@ -0,0 +1,430 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 359; + +use Math::BigInt::FastCalc; + +my ($BASE_LEN, undef, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL) = + Math::BigInt::FastCalc->_base_len(); + +print "# BASE_LEN = $BASE_LEN\n"; +print "# MAX_VAL = $MAX_VAL\n"; +print "# AND_BITS = $AND_BITS\n"; +print "# XOR_BITS = $XOR_BITS\n"; +print "# IOR_BITS = $OR_BITS\n"; + +# testing of Math::BigInt::FastCalc + +my $C = 'Math::BigInt::FastCalc'; # pass classname to sub's + +# _new and _str +my $x = $C->_new("123"); my $y = $C->_new("321"); +is (ref($x),'ARRAY'); is ($C->_str($x),123); is ($C->_str($y),321); + +############################################################################### +# _add, _sub, _mul, _div +is ($C->_str($C->_add($x,$y)),444); +is ($C->_str($C->_sub($x,$y)),123); +is ($C->_str($C->_mul($x,$y)),39483); +is ($C->_str($C->_div($x,$y)),123); + +############################################################################### +# check that mul/div doesn't change $y +# and returns the same reference, not something new +is ($C->_str($C->_mul($x,$y)),39483); +is ($C->_str($x),39483); is ($C->_str($y),321); + +is ($C->_str($C->_div($x,$y)),123); +is ($C->_str($x),123); is ($C->_str($y),321); + +$x = $C->_new("39483"); +my ($x1,$r1) = $C->_div($x,$y); +is ("$x1","$x"); +$C->_inc($x1); +is ("$x1","$x"); +is ($C->_str($r1),'0'); + +$x = $C->_new("39483"); # reset + +############################################################################### +my $z = $C->_new("2"); +is ($C->_str($C->_add($x,$z)),39485); +my ($re,$rr) = $C->_div($x,$y); + +is ($C->_str($re),123); is ($C->_str($rr),2); + +# is_zero, _is_one, _one, _zero +is ($C->_is_zero($x),''); +is ($C->_is_one($x),''); + +is ($C->_str($C->_zero()),"0"); +is ($C->_str($C->_one()),"1"); + +# _two() and _ten() +is ($C->_str($C->_two()),"2"); +is ($C->_str($C->_ten()),"10"); +is ($C->_is_ten($C->_two()),''); +is ($C->_is_two($C->_two()),1); +is ($C->_is_ten($C->_ten()),1); +is ($C->_is_two($C->_ten()),''); + +is ($C->_is_one($C->_one()),1); +is ($C->_is_one($C->_two()), ''); +is ($C->_is_one($C->_ten()), ''); + +is ($C->_is_one($C->_zero()), ''); + +is ($C->_is_zero($C->_zero()),1); + +is ($C->_is_zero($C->_one()), ''); + +# is_odd, is_even +is ($C->_is_odd($C->_one()),1); is ($C->_is_odd($C->_zero()),''); +is ($C->_is_even($C->_one()), ''); is ($C->_is_even($C->_zero()),1); + +# _len +for my $method (qw/_alen _len/) + { + $x = $C->_new("1"); is ($C->$method($x),1); + $x = $C->_new("12"); is ($C->$method($x),2); + $x = $C->_new("123"); is ($C->$method($x),3); + $x = $C->_new("1234"); is ($C->$method($x),4); + $x = $C->_new("12345"); is ($C->$method($x),5); + $x = $C->_new("123456"); is ($C->$method($x),6); + $x = $C->_new("1234567"); is ($C->$method($x),7); + $x = $C->_new("12345678"); is ($C->$method($x),8); + $x = $C->_new("123456789"); is ($C->$method($x),9); + + $x = $C->_new("8"); is ($C->$method($x),1); + $x = $C->_new("21"); is ($C->$method($x),2); + $x = $C->_new("321"); is ($C->$method($x),3); + $x = $C->_new("4321"); is ($C->$method($x),4); + $x = $C->_new("54321"); is ($C->$method($x),5); + $x = $C->_new("654321"); is ($C->$method($x),6); + $x = $C->_new("7654321"); is ($C->$method($x),7); + $x = $C->_new("87654321"); is ($C->$method($x),8); + $x = $C->_new("987654321"); is ($C->$method($x),9); + + $x = $C->_new("0"); is ($C->$method($x),1); + $x = $C->_new("20"); is ($C->$method($x),2); + $x = $C->_new("320"); is ($C->$method($x),3); + $x = $C->_new("4320"); is ($C->$method($x),4); + $x = $C->_new("54320"); is ($C->$method($x),5); + $x = $C->_new("654320"); is ($C->$method($x),6); + $x = $C->_new("7654320"); is ($C->$method($x),7); + $x = $C->_new("87654320"); is ($C->$method($x),8); + $x = $C->_new("987654320"); is ($C->$method($x),9); + + for (my $i = 1; $i < 9; $i++) + { + my $a = "$i" . '0' x ($i-1); + $x = $C->_new($a); + print "# Tried len '$a'\n" unless is ($C->_len($x),$i); + } + } + +# _digit +$x = $C->_new("123456789"); +is ($C->_digit($x,0),9); +is ($C->_digit($x,1),8); +is ($C->_digit($x,2),7); +is ($C->_digit($x,-1),1); +is ($C->_digit($x,-2),2); +is ($C->_digit($x,-3),3); + +# _copy +foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/) + { + $x = $C->_new("$_"); + is ($C->_str($C->_copy($x)),"$_"); + is ($C->_str($x),"$_"); # did _copy destroy original x? + } + +# _zeros +$x = $C->_new("1256000000"); is ($C->_zeros($x),6); +$x = $C->_new("152"); is ($C->_zeros($x),0); +$x = $C->_new("123000"); is ($C->_zeros($x),3); +$x = $C->_new("0"); is ($C->_zeros($x),0); + +# _lsft, _rsft +$x = $C->_new("10"); $y = $C->_new("3"); +is ($C->_str($C->_lsft($x,$y,10)),10000); +$x = $C->_new("20"); $y = $C->_new("3"); +is ($C->_str($C->_lsft($x,$y,10)),20000); + +$x = $C->_new("128"); $y = $C->_new("4"); +is ($C->_str($C->_lsft($x,$y,2)), 128 << 4); + +$x = $C->_new("1000"); $y = $C->_new("3"); +is ($C->_str($C->_rsft($x,$y,10)),1); +$x = $C->_new("20000"); $y = $C->_new("3"); +is ($C->_str($C->_rsft($x,$y,10)),20); +$x = $C->_new("256"); $y = $C->_new("4"); +is ($C->_str($C->_rsft($x,$y,2)),256 >> 4); + +$x = $C->_new("6411906467305339182857313397200584952398"); +$y = $C->_new("45"); +is ($C->_str($C->_rsft($x,$y,10)),0); + +# _acmp +$x = $C->_new("123456789"); +$y = $C->_new("987654321"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); +$x = $C->_new("12"); +$y = $C->_new("12"); +is ($C->_acmp($x,$y),0); +$x = $C->_new("21"); +is ($C->_acmp($x,$y),1); +is ($C->_acmp($y,$x),-1); +$x = $C->_new("123456789"); +$y = $C->_new("1987654321"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),+1); + +$x = $C->_new("1234567890123456789"); +$y = $C->_new("987654321012345678"); +is ($C->_acmp($x,$y),1); +is ($C->_acmp($y,$x),-1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); + +$x = $C->_new("1234"); +$y = $C->_new("987654321012345678"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); + +# _modinv +$x = $C->_new("8"); +$y = $C->_new("5033"); +my ($xmod,$sign) = $C->_modinv($x,$y); +is ($C->_str($xmod),'629'); # -629 % 5033 == 4404 +is ($sign, '-'); + +# _div +$x = $C->_new("3333"); $y = $C->_new("1111"); +is ($C->_str(scalar $C->_div($x,$y)),3); +$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); +is ($C->_str($x),30); is ($C->_str($y),3); +$x = $C->_new("123"); $y = $C->_new("1111"); +($x,$y) = $C->_div($x,$y); is ($C->_str($x),0); is ($C->_str($y),123); + +# _num +foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/) + { + $x = $C->_new("$_"); + is (ref($x),'ARRAY'); is ($C->_str($x),"$_"); + $x = $C->_num($x); is (ref($x),''); is ($x,$_); + } + +# _sqrt +$x = $C->_new("144"); is ($C->_str($C->_sqrt($x)),'12'); +$x = $C->_new("144000000000000"); is ($C->_str($C->_sqrt($x)),'12000000'); + +# _root +$x = $C->_new("81"); my $n = $C->_new("3"); # 4*4*4 = 64, 5*5*5 = 125 +is ($C->_str($C->_root($x,$n)),'4'); # 4.xx => 4.0 +$x = $C->_new("81"); $n = $C->_new("4"); # 3*3*3*3 == 81 +is ($C->_str($C->_root($x,$n)),'3'); + +# _pow (and _root) +$x = $C->_new("0"); $n = $C->_new("3"); # 0 ** y => 0 +is ($C->_str($C->_pow($x,$n)), 0); +$x = $C->_new("3"); $n = $C->_new("0"); # x ** 0 => 1 +is ($C->_str($C->_pow($x,$n)), 1); +$x = $C->_new("1"); $n = $C->_new("3"); # 1 ** y => 1 +is ($C->_str($C->_pow($x,$n)), 1); +$x = $C->_new("5"); $n = $C->_new("1"); # x ** 1 => x +is ($C->_str($C->_pow($x,$n)), 5); + +$x = $C->_new("81"); $n = $C->_new("3"); # 81 ** 3 == 531441 +is ($C->_str($C->_pow($x,$n)),81 ** 3); + +is ($C->_str($C->_root($x,$n)),81); + +$x = $C->_new("81"); +is ($C->_str($C->_pow($x,$n)),81 ** 3); +is ($C->_str($C->_pow($x,$n)),'150094635296999121'); # 531441 ** 3 == + +is ($C->_str($C->_root($x,$n)),'531441'); +is ($C->_str($C->_root($x,$n)),'81'); + +$x = $C->_new("81"); $n = $C->_new("14"); +is ($C->_str($C->_pow($x,$n)),'523347633027360537213511521'); +is ($C->_str($C->_root($x,$n)),'81'); + +$x = $C->_new("523347633027360537213511520"); +is ($C->_str($C->_root($x,$n)),'80'); + +$x = $C->_new("523347633027360537213511522"); +is ($C->_str($C->_root($x,$n)),'81'); + +my $res = [ qw/ 9 31 99 316 999 3162 9999/ ]; + +# 99 ** 2 = 9801, 999 ** 2 = 998001 etc +for my $i (2 .. 9) + { + $x = '9' x $i; $x = $C->_new($x); + $n = $C->_new("2"); + my $rc = '9' x ($i-1). '8' . '0' x ($i-1) . '1'; + print "# _pow( ", '9' x $i, ", 2) \n" unless + is ($C->_str($C->_pow($x,$n)),$rc); + + if ($i <= 7) + { + $x = '9' x $i; $x = $C->_new($x); + $n = '9' x $i; $n = $C->_new($n); + print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless + is ($C->_str($C->_root($x,$n)),'1'); + + $x = '9' x $i; $x = $C->_new($x); + $n = $C->_new("2"); + print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless + is ($C->_str($C->_root($x,$n)), $res->[$i-2]); + } + } + +############################################################################## +# _fac +$x = $C->_new("0"); is ($C->_str($C->_fac($x)),'1'); +$x = $C->_new("1"); is ($C->_str($C->_fac($x)),'1'); +$x = $C->_new("2"); is ($C->_str($C->_fac($x)),'2'); +$x = $C->_new("3"); is ($C->_str($C->_fac($x)),'6'); +$x = $C->_new("4"); is ($C->_str($C->_fac($x)),'24'); +$x = $C->_new("5"); is ($C->_str($C->_fac($x)),'120'); +$x = $C->_new("10"); is ($C->_str($C->_fac($x)),'3628800'); +$x = $C->_new("11"); is ($C->_str($C->_fac($x)),'39916800'); +$x = $C->_new("12"); is ($C->_str($C->_fac($x)),'479001600'); +$x = $C->_new("13"); is ($C->_str($C->_fac($x)),'6227020800'); + +# test that _fac modifies $x in place for small arguments +$x = $C->_new("3"); $C->_fac($x); is ($C->_str($x),'6'); +$x = $C->_new("13"); $C->_fac($x); is ($C->_str($x),'6227020800'); + +############################################################################## +# _inc and _dec +foreach (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/) + { + $x = $C->_new("$_"); $C->_inc($x); + print "# \$x = ",$C->_str($x),"\n" + unless is ($C->_str($x),substr($_,0,length($_)-1) . '2'); + $C->_dec($x); is ($C->_str($x),$_); + } +foreach (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/) + { + $x = $C->_new("$_"); $C->_inc($x); + print "# \$x = ",$C->_str($x),"\n" + unless is ($C->_str($x),substr($_,0,length($_)-2) . '20'); + $C->_dec($x); is ($C->_str($x),$_); + } +foreach (qw/999 9999 99999 9999999 99999999 999999999 9999999999 99999999999/) + { + $x = $C->_new("$_"); $C->_inc($x); + print "# \$x = ",$C->_str($x),"\n" + unless is ($C->_str($x), '1' . '0' x (length($_))); + $C->_dec($x); is ($C->_str($x),$_); + } + +$x = $C->_new("1000"); $C->_inc($x); is ($C->_str($x),'1001'); +$C->_dec($x); is ($C->_str($x),'1000'); + +my $BL; +{ + no strict 'refs'; + $BL = &{"$C"."::_base_len"}(); +} + +$x = '1' . '0' x $BL; +$z = '1' . '0' x ($BL-1); $z .= '1'; +$x = $C->_new($x); $C->_inc($x); is ($C->_str($x),$z); + +$x = '1' . '0' x $BL; $z = '9' x $BL; +$x = $C->_new($x); $C->_dec($x); is ($C->_str($x),$z); + +# should not happen: +# $x = $C->_new("-2"); $y = $C->_new("4"); is ($C->_acmp($x,$y),-1); + +############################################################################### +# _mod +$x = $C->_new("1000"); $y = $C->_new("3"); +is ($C->_str(scalar $C->_mod($x,$y)),1); +$x = $C->_new("1000"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_mod($x,$y)),0); + +# _and, _or, _xor +$x = $C->_new("5"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_xor($x,$y)),7); +$x = $C->_new("5"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_or($x,$y)),7); +$x = $C->_new("5"); $y = $C->_new("3"); +is ($C->_str(scalar $C->_and($x,$y)),1); + +# _from_hex, _from_bin, _from_oct +is ($C->_str( $C->_from_hex("0xFf")),255); +is ($C->_str( $C->_from_bin("0b10101011")),160+11); +is ($C->_str( $C->_from_oct("0100")), 8*8); +is ($C->_str( $C->_from_oct("01000")), 8*8*8); +is ($C->_str( $C->_from_oct("010001")), 8*8*8*8+1); +is ($C->_str( $C->_from_oct("010007")), 8*8*8*8+7); + +# _as_hex, _as_bin, as_oct +is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("128")))), 128); +is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("128")))), 128); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("128")))), 128); + +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456")))), 123456); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456789")))), "123456789"); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("1234567890123")))), "1234567890123"); + +# _1ex +is ($C->_str($C->_1ex(0)), "1"); +is ($C->_str($C->_1ex(1)), "10"); +is ($C->_str($C->_1ex(2)), "100"); +is ($C->_str($C->_1ex(12)), "1000000000000"); +is ($C->_str($C->_1ex(16)), "10000000000000000"); + +# _check +$x = $C->_new("123456789"); +is ($C->_check($x),0); +is ($C->_check(123),'123 is not a reference'); + +############################################################################### +# __strip_zeros + +{ + no strict 'refs'; + # correct empty arrays + $x = &{$C."::__strip_zeros"}([]); is (@$x,1); is ($x->[0],0); + # don't strip single elements + $x = &{$C."::__strip_zeros"}([0]); is (@$x,1); is ($x->[0],0); + $x = &{$C."::__strip_zeros"}([1]); is (@$x,1); is ($x->[0],1); + # don't strip non-zero elements + $x = &{$C."::__strip_zeros"}([0,1]); + is (@$x,2); is ($x->[0],0); is ($x->[1],1); + $x = &{$C."::__strip_zeros"}([0,1,2]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + # but strip leading zeros + $x = &{$C."::__strip_zeros"}([0,1,2,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + $x = &{$C."::__strip_zeros"}([0,1,2,0,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + # collapse multiple zeros + $x = &{$C."::__strip_zeros"}([0,0,0,0]); + is (@$x,1); is ($x->[0],0); +} + +# done + +1; + diff --git a/cpan/Math-BigInt-FastCalc/t/bootstrap.t b/cpan/Math-BigInt-FastCalc/t/bootstrap.t new file mode 100644 index 0000000000..d73afcb47c --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/t/bootstrap.t @@ -0,0 +1,8 @@ +#!/usr/bin/perl -w + +use Test::More tests => 1; + +BEGIN { + use_ok('Math::BigInt::FastCalc'); +} + diff --git a/cpan/Math-BigInt-FastCalc/t/leak.t b/cpan/Math-BigInt-FastCalc/t/leak.t new file mode 100644 index 0000000000..5db38e1dbd --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/t/leak.t @@ -0,0 +1,79 @@ +#!/usr/bin/perl -w + +# Test for memory leaks. + +# XXX TODO: This test file doesn't actually seem to work! If you remove +# the sv_2mortal() in the XS file, it still happily passes all tests... + +use strict; +use Test::More tests => 22; + +use Math::BigInt::FastCalc; + +############################################################################# +package Math::BigInt::FastCalc::LeakCheck; +use parent qw(Math::BigInt::FastCalc); + +my $destroyed = 0; +sub DESTROY { $destroyed++; } + +############################################################################# +package main; + +for my $method (qw(_zero _one _two _ten)) + { + $destroyed = 0; + { + my $num = Math::BigInt::FastCalc::LeakCheck->$method(); + bless $num, "Math::BigInt::FastCalc::LeakCheck"; + } + is ($destroyed, 1, "$method does not leak memory"); + } + +my $num = Math::BigInt::FastCalc->_zero(); +for my $method (qw(_is_zero _is_one _is_two _is_ten _num)) + { + $destroyed = 0; + { + my $rc = Math::BigInt::FastCalc->$method($num); + bless \$rc, "Math::BigInt::FastCalc::LeakCheck"; + } + is ($destroyed, 1, "$method does not leak memory"); + } + +my $num_10 = Math::BigInt::FastCalc->_ten(); +my $num_2 = Math::BigInt::FastCalc->_two(); + +my $num_long = Math::BigInt::FastCalc->_new("1234567890"); +my $num_long_2 = Math::BigInt::FastCalc->_new("12345678900987654321"); + +is (Math::BigInt::FastCalc->_str($num_long), "1234567890"); +is (Math::BigInt::FastCalc->_str($num_long_2), "12345678900987654321"); + +# to hit all possible code branches +_test_acmp($num, $num); +_test_acmp($num_10, $num_10); +_test_acmp($num, $num_10); +_test_acmp($num_10, $num); +_test_acmp($num, $num_2); +_test_acmp($num_2, $num); +_test_acmp($num_long, $num); +_test_acmp($num, $num_long); +_test_acmp($num_long, $num_long); +_test_acmp($num_long, $num_long_2); +_test_acmp($num_long_2, $num_long); + +sub _test_acmp + { + my ($n1,$n2) = @_; + + $destroyed = 0; + { + my $rc = Math::BigInt::FastCalc->_acmp($n1,$n2); + bless \$rc, "Math::BigInt::FastCalc::LeakCheck"; + } + my $n_1 = Math::BigInt::FastCalc->_str($n1); + my $n_2 = Math::BigInt::FastCalc->_str($n2); + is ($destroyed, 1, "_acmp($n_1,$n_2) does not leak memory"); + } + diff --git a/cpan/Math-BigInt-FastCalc/t/mbi_rand.t b/cpan/Math-BigInt-FastCalc/t/mbi_rand.t new file mode 100644 index 0000000000..4ad473d882 --- /dev/null +++ b/cpan/Math-BigInt-FastCalc/t/mbi_rand.t @@ -0,0 +1,55 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More; + +my $count = 128; + +plan(($^O eq 'os390') + ? (skip_all => 'takes too long on os390') : (tests => $count*2)); + +use Math::BigInt lib => 'FastCalc'; +my $c = 'Math::BigInt'; + +my $length = 128; + +# If you get a failure here, please re-run the test with the printed seed +# value as input: perl t/mbi_rand.t seed + +my $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(65537)); +print "# seed: $seed\n"; srand($seed); + +my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb); +my $two = Math::BigInt->new(2); +for (my $i = 0; $i < $count; $i++) + { + # length of A and B + $la = int(rand($length)+1); $lb = int(rand($length)+1); + $As = ''; $Bs = ''; + # we create the numbers from "patterns", e.g. get a random number and a + # random count and string them together. This means things like + # "100000999999999999911122222222" are much more likely. If we just strung + # together digits, we would end up with "1272398823211223" etc. + while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); } + while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); } + $As =~ s/^0+//; $Bs =~ s/^0+//; + $As = $As || '0'; $Bs = $Bs || '0'; + # print "# As $As\n# Bs $Bs\n"; + $A = $c->new($As); $B = $c->new($Bs); + # print "# A $A\n# B $B\n"; + if ($A->is_zero() || $B->is_zero()) + { + is (1,1); is (1,1); next; + } + # check that int(A/B)*B + A % B == A holds for all inputs + # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B); + ($ADB,$AMB) = $A->copy()->bdiv($B); + print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n" + unless is ($ADB*$B+$two*$AMB-$AMB,$As); + # swap 'em and try this, too + # $X = ($B/$A)*$A + $B % $A; + ($ADB,$AMB) = $B->copy()->bdiv($A); + print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n" + unless is ($ADB*$A+$two*$AMB-$AMB,$Bs); + } + diff --git a/cpan/Math-BigInt/lib/Math/BigFloat.pm b/cpan/Math-BigInt/lib/Math/BigFloat.pm new file mode 100644 index 0000000000..a423b35f02 --- /dev/null +++ b/cpan/Math-BigInt/lib/Math/BigFloat.pm @@ -0,0 +1,4692 @@ +package Math::BigFloat; + +# +# Mike grinned. 'Two down, infinity to go' - Mike Nostrus in 'Before and After' +# + +# The following hash values are internally used: +# _e : exponent (ref to $CALC object) +# _m : mantissa (ref to $CALC object) +# _es : sign of _e +# sign : +,-,+inf,-inf, or "NaN" if not a number +# _a : accuracy +# _p : precision + +$VERSION = '1.999701'; +require 5.006002; + +require Exporter; +@ISA = qw/Math::BigInt/; +@EXPORT_OK = qw/bpi/; + +use strict; +# $_trap_inf/$_trap_nan are internal and should never be accessed from outside +use vars qw/$AUTOLOAD $accuracy $precision $div_scale $round_mode $rnd_mode + $upgrade $downgrade $_trap_nan $_trap_inf/; +my $class = "Math::BigFloat"; + +use overload +'<=>' => sub { my $rc = $_[2] ? + ref($_[0])->bcmp($_[1],$_[0]) : + ref($_[0])->bcmp($_[0],$_[1]); + $rc = 1 unless defined $rc; + $rc <=> 0; + }, +# we need '>=' to get things like "1 >= NaN" right: +'>=' => sub { my $rc = $_[2] ? + ref($_[0])->bcmp($_[1],$_[0]) : + ref($_[0])->bcmp($_[0],$_[1]); + # if there was a NaN involved, return false + return '' unless defined $rc; + $rc >= 0; + }, +'int' => sub { $_[0]->as_number() }, # 'trunc' to bigint +; + +############################################################################## +# global constants, flags and assorted stuff + +# the following are public, but their usage is not recommended. Use the +# accessor methods instead. + +# class constants, use Class->constant_name() to access +# one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common' +$round_mode = 'even'; +$accuracy = undef; +$precision = undef; +$div_scale = 40; + +$upgrade = undef; +$downgrade = undef; +# the package we are using for our private parts, defaults to: +# Math::BigInt->config()->{lib} +my $MBI = 'Math::BigInt::Calc'; + +# are NaNs ok? (otherwise it dies when encountering an NaN) set w/ config() +$_trap_nan = 0; +# the same for infinity +$_trap_inf = 0; + +# constant for easier life +my $nan = 'NaN'; + +my $IMPORT = 0; # was import() called yet? used to make require work + +# some digits of accuracy for blog(undef,10); which we use in blog() for speed +my $LOG_10 = + '2.3025850929940456840179914546843642076011014886287729760333279009675726097'; +my $LOG_10_A = length($LOG_10)-1; +# ditto for log(2) +my $LOG_2 = + '0.6931471805599453094172321214581765680755001343602552541206800094933936220'; +my $LOG_2_A = length($LOG_2)-1; +my $HALF = '0.5'; # made into an object if nec. + +############################################################################## +# the old code had $rnd_mode, so we need to support it, too + +sub TIESCALAR { my ($class) = @_; bless \$round_mode, $class; } +sub FETCH { return $round_mode; } +sub STORE { $rnd_mode = $_[0]->round_mode($_[1]); } + +BEGIN + { + # when someone sets $rnd_mode, we catch this and check the value to see + # whether it is valid or not. + $rnd_mode = 'even'; tie $rnd_mode, 'Math::BigFloat'; + + # we need both of them in this package: + *as_int = \&as_number; + } + +############################################################################## + +{ + # valid method aliases for AUTOLOAD + my %methods = map { $_ => 1 } + qw / fadd fsub fmul fdiv fround ffround fsqrt fmod fstr fsstr fpow fnorm + fint facmp fcmp fzero fnan finf finc fdec ffac fneg + fceil ffloor frsft flsft fone flog froot fexp + /; + # valid methods that can be handed up (for AUTOLOAD) + my %hand_ups = map { $_ => 1 } + qw / is_nan is_inf is_negative is_positive is_pos is_neg + accuracy precision div_scale round_mode fabs fnot + objectify upgrade downgrade + bone binf bnan bzero + bsub + /; + + sub _method_alias { exists $methods{$_[0]||''}; } + sub _method_hand_up { exists $hand_ups{$_[0]||''}; } +} + +############################################################################## +# constructors + +sub new + { + # create a new BigFloat object from a string or another bigfloat object. + # _e: exponent + # _m: mantissa + # sign => sign (+/-), or "NaN" + + my ($class,$wanted,@r) = @_; + + # avoid numify-calls by not using || on $wanted! + return $class->bzero() if !defined $wanted; # default to 0 + return $wanted->copy() if UNIVERSAL::isa($wanted,'Math::BigFloat'); + + $class->import() if $IMPORT == 0; # make require work + + my $self = {}; bless $self, $class; + # shortcut for bigints and its subclasses + if ((ref($wanted)) && UNIVERSAL::can( $wanted, "as_number")) + { + $self->{_m} = $wanted->as_number()->{value}; # get us a bigint copy + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + $self->{sign} = $wanted->sign(); + return $self->bnorm(); + } + # else: got a string or something masquerading as number (with overload) + + # handle '+inf', '-inf' first + if ($wanted =~ /^[+-]?inf\z/) + { + return $downgrade->new($wanted) if $downgrade; + + $self->{sign} = $wanted; # set a default sign for bstr() + return $self->binf($wanted); + } + + # shortcut for simple forms like '12' that neither have trailing nor leading + # zeros + if ($wanted =~ /^([+-]?)([1-9][0-9]*[1-9])$/) + { + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + $self->{sign} = $1 || '+'; + $self->{_m} = $MBI->_new($2); + return $self->round(@r) if !$downgrade; + } + + my ($mis,$miv,$mfv,$es,$ev) = Math::BigInt::_split($wanted); + if (!ref $mis) + { + if ($_trap_nan) + { + require Carp; + Carp::croak ("$wanted is not a number initialized to $class"); + } + + return $downgrade->bnan() if $downgrade; + + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + $self->{_m} = $MBI->_zero(); + $self->{sign} = $nan; + } + else + { + # make integer from mantissa by adjusting exp, then convert to int + $self->{_e} = $MBI->_new($$ev); # exponent + $self->{_es} = $$es || '+'; + my $mantissa = "$$miv$$mfv"; # create mant. + $mantissa =~ s/^0+(\d)/$1/; # strip leading zeros + $self->{_m} = $MBI->_new($mantissa); # create mant. + + # 3.123E0 = 3123E-3, and 3.123E-2 => 3123E-5 + if (CORE::length($$mfv) != 0) + { + my $len = $MBI->_new( CORE::length($$mfv)); + ($self->{_e}, $self->{_es}) = + _e_sub ($self->{_e}, $len, $self->{_es}, '+'); + } + # we can only have trailing zeros on the mantissa if $$mfv eq '' + else + { + # Use a regexp to count the trailing zeros in $$miv instead of _zeros() + # because that is faster, especially when _m is not stored in base 10. + my $zeros = 0; $zeros = CORE::length($1) if $$miv =~ /[1-9](0*)$/; + if ($zeros != 0) + { + my $z = $MBI->_new($zeros); + # turn '120e2' into '12e3' + $MBI->_rsft ( $self->{_m}, $z, 10); + ($self->{_e}, $self->{_es}) = + _e_add ( $self->{_e}, $z, $self->{_es}, '+'); + } + } + $self->{sign} = $$mis; + + # for something like 0Ey, set y to 1, and -0 => +0 + # Check $$miv for being '0' and $$mfv eq '', because otherwise _m could not + # have become 0. That's faster than to call $MBI->_is_zero(). + $self->{sign} = '+', $self->{_e} = $MBI->_one() + if $$miv eq '0' and $$mfv eq ''; + + return $self->round(@r) if !$downgrade; + } + # if downgrade, inf, NaN or integers go down + + if ($downgrade && $self->{_es} eq '+') + { + if ($MBI->_is_zero( $self->{_e} )) + { + return $downgrade->new($$mis . $MBI->_str( $self->{_m} )); + } + return $downgrade->new($self->bsstr()); + } + $self->bnorm()->round(@r); # first normalize, then round + } + +sub copy + { + # if two arguments, the first one is the class to "swallow" subclasses + if (@_ > 1) + { + my $self = bless { + sign => $_[1]->{sign}, + _es => $_[1]->{_es}, + _m => $MBI->_copy($_[1]->{_m}), + _e => $MBI->_copy($_[1]->{_e}), + }, $_[0] if @_ > 1; + + $self->{_a} = $_[1]->{_a} if defined $_[1]->{_a}; + $self->{_p} = $_[1]->{_p} if defined $_[1]->{_p}; + return $self; + } + + my $self = bless { + sign => $_[0]->{sign}, + _es => $_[0]->{_es}, + _m => $MBI->_copy($_[0]->{_m}), + _e => $MBI->_copy($_[0]->{_e}), + }, ref($_[0]); + + $self->{_a} = $_[0]->{_a} if defined $_[0]->{_a}; + $self->{_p} = $_[0]->{_p} if defined $_[0]->{_p}; + $self; + } + +sub _bnan + { + # used by parent class bone() to initialize number to NaN + my $self = shift; + + if ($_trap_nan) + { + require Carp; + my $class = ref($self); + Carp::croak ("Tried to set $self to NaN in $class\::_bnan()"); + } + + $IMPORT=1; # call our import only once + $self->{_m} = $MBI->_zero(); + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + } + +sub _binf + { + # used by parent class bone() to initialize number to +-inf + my $self = shift; + + if ($_trap_inf) + { + require Carp; + my $class = ref($self); + Carp::croak ("Tried to set $self to +-inf in $class\::_binf()"); + } + + $IMPORT=1; # call our import only once + $self->{_m} = $MBI->_zero(); + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + } + +sub _bone + { + # used by parent class bone() to initialize number to 1 + my $self = shift; + $IMPORT=1; # call our import only once + $self->{_m} = $MBI->_one(); + $self->{_e} = $MBI->_zero(); + $self->{_es} = '+'; + } + +sub _bzero + { + # used by parent class bone() to initialize number to 0 + my $self = shift; + $IMPORT=1; # call our import only once + $self->{_m} = $MBI->_zero(); + $self->{_e} = $MBI->_one(); + $self->{_es} = '+'; + } + +sub isa + { + my ($self,$class) = @_; + return if $class =~ /^Math::BigInt/; # we aren't one of these + UNIVERSAL::isa($self,$class); + } + +sub config + { + # return (later set?) configuration data as hash ref + my $class = shift || 'Math::BigFloat'; + + if (@_ == 1 && ref($_[0]) ne 'HASH') + { + my $cfg = $class->SUPER::config(); + return $cfg->{$_[0]}; + } + + my $cfg = $class->SUPER::config(@_); + + # now we need only to override the ones that are different from our parent + $cfg->{class} = $class; + $cfg->{with} = $MBI; + $cfg; + } + +############################################################################## +# string conversion + +sub bstr + { + # (ref to BFLOAT or num_str ) return num_str + # Convert number from internal format to (non-scientific) string format. + # internal format is always normalized (no leading zeros, "-0" => "+0") + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN + return 'inf'; # +inf + } + + my $es = '0'; my $len = 1; my $cad = 0; my $dot = '.'; + + # $x is zero? + my $not_zero = !($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})); + if ($not_zero) + { + $es = $MBI->_str($x->{_m}); + $len = CORE::length($es); + my $e = $MBI->_num($x->{_e}); + $e = -$e if $x->{_es} eq '-'; + if ($e < 0) + { + $dot = ''; + # if _e is bigger than a scalar, the following will blow your memory + if ($e <= -$len) + { + my $r = abs($e) - $len; + $es = '0.'. ('0' x $r) . $es; $cad = -($len+$r); + } + else + { + substr($es,$e,0) = '.'; $cad = $MBI->_num($x->{_e}); + $cad = -$cad if $x->{_es} eq '-'; + } + } + elsif ($e > 0) + { + # expand with zeros + $es .= '0' x $e; $len += $e; $cad = 0; + } + } # if not zero + + $es = '-'.$es if $x->{sign} eq '-'; + # if set accuracy or precision, pad with zeros on the right side + if ((defined $x->{_a}) && ($not_zero)) + { + # 123400 => 6, 0.1234 => 4, 0.001234 => 4 + my $zeros = $x->{_a} - $cad; # cad == 0 => 12340 + $zeros = $x->{_a} - $len if $cad != $len; + $es .= $dot.'0' x $zeros if $zeros > 0; + } + elsif ((($x->{_p} || 0) < 0)) + { + # 123400 => 6, 0.1234 => 4, 0.001234 => 6 + my $zeros = -$x->{_p} + $cad; + $es .= $dot.'0' x $zeros if $zeros > 0; + } + $es; + } + +sub bsstr + { + # (ref to BFLOAT or num_str ) return num_str + # Convert number from internal format to scientific string format. + # internal format is always normalized (no leading zeros, "-0E0" => "+0E0") + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN + return 'inf'; # +inf + } + my $sep = 'e'.$x->{_es}; + my $sign = $x->{sign}; $sign = '' if $sign eq '+'; + $sign . $MBI->_str($x->{_m}) . $sep . $MBI->_str($x->{_e}); + } + +sub numify + { + # Convert a Perl scalar number from a BigFloat object. + # Create a string and let Perl's atoi()/atof() handle the rest. + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + return 0 + $x->bsstr(); + } + +############################################################################## +# public stuff (usually prefixed with "b") + +sub bneg + { + # (BINT or num_str) return BINT + # negate number or make a negated number from string + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->modify('bneg'); + + # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN' + $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})); + $x; + } + +# tels 2001-08-04 +# XXX TODO this must be overwritten and return NaN for non-integer values +# band(), bior(), bxor(), too +#sub bnot +# { +# $class->SUPER::bnot($class,@_); +# } + +sub bcmp + { + # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort) + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + return $upgrade->bcmp($x,$y) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + # Handle all 'nan' cases. + + return undef if ($x->{sign} eq $nan) || ($y->{sign} eq $nan); + + # Handle all '+inf' and '-inf' cases. + + return 0 if ($x->{sign} eq '+inf' && $y->{sign} eq '+inf' || + $x->{sign} eq '-inf' && $y->{sign} eq '-inf'); + return +1 if $x->{sign} eq '+inf'; # x = +inf and y < +inf + return -1 if $x->{sign} eq '-inf'; # x = -inf and y > -inf + return -1 if $y->{sign} eq '+inf'; # x < +inf and y = +inf + return +1 if $y->{sign} eq '-inf'; # x > -inf and y = -inf + + # Handle all cases with opposite signs. + + return +1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # also does 0 <=> -y + return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # also does -x <=> 0 + + # Handle all remaining zero cases. + + my $xz = $x->is_zero(); + my $yz = $y->is_zero(); + return 0 if $xz && $yz; # 0 <=> 0 + return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y + return +1 if $yz && $x->{sign} eq '+'; # +x <=> 0 + + # Both arguments are now finite, non-zero numbers with the same sign. + + my $cmp; + + # The next step is to compare the exponents, but since each mantissa is an + # integer of arbitrary value, the exponents must be normalized by the length + # of the mantissas before we can compare them. + + my $mxl = $MBI->_len($x->{_m}); + my $myl = $MBI->_len($y->{_m}); + + # If the mantissas have the same length, there is no point in normalizing the + # exponents by the length of the mantissas, so treat that as a special case. + + if ($mxl == $myl) { + + # First handle the two cases where the exponents have different signs. + + if ($x->{_es} eq '+' && $y->{_es} eq '-') { + $cmp = +1; + } + + elsif ($x->{_es} eq '-' && $y->{_es} eq '+') { + $cmp = -1; + } + + # Then handle the case where the exponents have the same sign. + + else { + $cmp = $MBI->_acmp($x->{_e}, $y->{_e}); + $cmp = -$cmp if $x->{_es} eq '-'; + } + + # Adjust for the sign, which is the same for x and y, and bail out if + # we're done. + + $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123 + return $cmp if $cmp; + + } + + # We must normalize each exponent by the length of the corresponding + # mantissa. Life is a lot easier if we first make both exponents + # non-negative. We do this by adding the same positive value to both + # exponent. This is safe, because when comparing the exponents, only the + # relative difference is important. + + my $ex; + my $ey; + + if ($x->{_es} eq '+') { + + # If the exponent of x is >= 0 and the exponent of y is >= 0, there is no + # need to do anything special. + + if ($y->{_es} eq '+') { + $ex = $MBI->_copy($x->{_e}); + $ey = $MBI->_copy($y->{_e}); + } + + # If the exponent of x is >= 0 and the exponent of y is < 0, add the + # absolute value of the exponent of y to both. + + else { + $ex = $MBI->_copy($x->{_e}); + $ex = $MBI->_add($ex, $y->{_e}); # ex + |ey| + $ey = $MBI->_zero(); # -ex + |ey| = 0 + } + + } else { + + # If the exponent of x is < 0 and the exponent of y is >= 0, add the + # absolute value of the exponent of x to both. + + if ($y->{_es} eq '+') { + $ex = $MBI->_zero(); # -ex + |ex| = 0 + $ey = $MBI->_copy($y->{_e}); + $ey = $MBI->_add($ey, $x->{_e}); # ey + |ex| + } + + # If the exponent of x is < 0 and the exponent of y is < 0, add the + # absolute values of both exponents to both exponents. + + else { + $ex = $MBI->_copy($y->{_e}); # -ex + |ey| + |ex| = |ey| + $ey = $MBI->_copy($x->{_e}); # -ey + |ex| + |ey| = |ex| + } + + } + + # Now we can normalize the exponents by adding lengths of the mantissas. + + $MBI->_add($ex, $MBI->_new($mxl)); + $MBI->_add($ey, $MBI->_new($myl)); + + # We're done if the exponents are different. + + $cmp = $MBI->_acmp($ex, $ey); + $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123 + return $cmp if $cmp; + + # Compare the mantissas, but first normalize them by padding the shorter + # mantissa with zeros (shift left) until it has the same length as the longer + # mantissa. + + my $mx = $x->{_m}; + my $my = $y->{_m}; + + if ($mxl > $myl) { + $my = $MBI->_lsft($MBI->_copy($my), $MBI->_new($mxl - $myl), 10); + } elsif ($mxl < $myl) { + $mx = $MBI->_lsft($MBI->_copy($mx), $MBI->_new($myl - $mxl), 10); + } + + $cmp = $MBI->_acmp($mx, $my); + $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123 + return $cmp; + + } + +sub bacmp + { + # Compares 2 values, ignoring their signs. + # Returns one of undef, <0, =0, >0. (suitable for sort) + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + return $upgrade->bacmp($x,$y) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + # handle +-inf and NaN's + if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) + { + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if ($x->is_inf() && $y->is_inf()); + return 1 if ($x->is_inf() && !$y->is_inf()); + return -1; + } + + # shortcut + my $xz = $x->is_zero(); + my $yz = $y->is_zero(); + return 0 if $xz && $yz; # 0 <=> 0 + return -1 if $xz && !$yz; # 0 <=> +y + return 1 if $yz && !$xz; # +x <=> 0 + + # adjust so that exponents are equal + my $lxm = $MBI->_len($x->{_m}); + my $lym = $MBI->_len($y->{_m}); + my ($xes,$yes) = (1,1); + $xes = -1 if $x->{_es} ne '+'; + $yes = -1 if $y->{_es} ne '+'; + # the numify somewhat limits our length, but makes it much faster + my $lx = $lxm + $xes * $MBI->_num($x->{_e}); + my $ly = $lym + $yes * $MBI->_num($y->{_e}); + my $l = $lx - $ly; + return $l <=> 0 if $l != 0; + + # lengths (corrected by exponent) are equal + # so make mantissa equal-length by padding with zero (shift left) + my $diff = $lxm - $lym; + my $xm = $x->{_m}; # not yet copy it + my $ym = $y->{_m}; + if ($diff > 0) + { + $ym = $MBI->_copy($y->{_m}); + $ym = $MBI->_lsft($ym, $MBI->_new($diff), 10); + } + elsif ($diff < 0) + { + $xm = $MBI->_copy($x->{_m}); + $xm = $MBI->_lsft($xm, $MBI->_new(-$diff), 10); + } + $MBI->_acmp($xm,$ym); + } + +sub badd + { + # add second arg (BFLOAT or string) to first (BFLOAT) (modifies first) + # return result as BFLOAT + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('badd'); + + # inf and NaN handling + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # NaN first + return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) + { + # +inf++inf or -inf+-inf => same, rest is NaN + return $x if $x->{sign} eq $y->{sign}; + return $x->bnan(); + } + # +-inf + something => +inf; something +-inf => +-inf + $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/; + return $x; + } + + return $upgrade->badd($x,$y,@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + $r[3] = $y; # no push! + + # speed: no add for 0+y or x+0 + return $x->bround(@r) if $y->is_zero(); # x+0 + if ($x->is_zero()) # 0+y + { + # make copy, clobbering up x (modify in place!) + $x->{_e} = $MBI->_copy($y->{_e}); + $x->{_es} = $y->{_es}; + $x->{_m} = $MBI->_copy($y->{_m}); + $x->{sign} = $y->{sign} || $nan; + return $x->round(@r); + } + + # take lower of the two e's and adapt m1 to it to match m2 + my $e = $y->{_e}; + $e = $MBI->_zero() if !defined $e; # if no BFLOAT? + $e = $MBI->_copy($e); # make copy (didn't do it yet) + + my $es; + + ($e,$es) = _e_sub($e, $x->{_e}, $y->{_es} || '+', $x->{_es}); + + my $add = $MBI->_copy($y->{_m}); + + if ($es eq '-') # < 0 + { + $MBI->_lsft( $x->{_m}, $e, 10); + ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es); + } + elsif (!$MBI->_is_zero($e)) # > 0 + { + $MBI->_lsft($add, $e, 10); + } + # else: both e are the same, so just leave them + + if ($x->{sign} eq $y->{sign}) + { + # add + $x->{_m} = $MBI->_add($x->{_m}, $add); + } + else + { + ($x->{_m}, $x->{sign}) = + _e_add($x->{_m}, $add, $x->{sign}, $y->{sign}); + } + + # delete trailing zeros, then round + $x->bnorm()->round(@r); + } + +# sub bsub is inherited from Math::BigInt! + +sub binc + { + # increment arg by one + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('binc'); + + if ($x->{_es} eq '-') + { + return $x->badd($self->bone(),@r); # digits after dot + } + + if (!$MBI->_is_zero($x->{_e})) # _e == 0 for NaN, inf, -inf + { + # 1e2 => 100, so after the shift below _m has a '0' as last digit + $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100 + $x->{_e} = $MBI->_zero(); # normalize + $x->{_es} = '+'; + # we know that the last digit of $x will be '1' or '9', depending on the + # sign + } + # now $x->{_e} == 0 + if ($x->{sign} eq '+') + { + $MBI->_inc($x->{_m}); + return $x->bnorm()->bround(@r); + } + elsif ($x->{sign} eq '-') + { + $MBI->_dec($x->{_m}); + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0 + return $x->bnorm()->bround(@r); + } + # inf, nan handling etc + $x->badd($self->bone(),@r); # badd() does round + } + +sub bdec + { + # decrement arg by one + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bdec'); + + if ($x->{_es} eq '-') + { + return $x->badd($self->bone('-'),@r); # digits after dot + } + + if (!$MBI->_is_zero($x->{_e})) + { + $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100 + $x->{_e} = $MBI->_zero(); # normalize + $x->{_es} = '+'; + } + # now $x->{_e} == 0 + my $zero = $x->is_zero(); + # <= 0 + if (($x->{sign} eq '-') || $zero) + { + $MBI->_inc($x->{_m}); + $x->{sign} = '-' if $zero; # 0 => 1 => -1 + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0 + return $x->bnorm()->round(@r); + } + # > 0 + elsif ($x->{sign} eq '+') + { + $MBI->_dec($x->{_m}); + return $x->bnorm()->round(@r); + } + # inf, nan handling etc + $x->badd($self->bone('-'),@r); # does round + } + +sub DEBUG () { 0; } + +sub blog + { + my ($self,$x,$base,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('blog'); + + # $base > 0, $base != 1; if $base == undef default to $base == e + # $x >= 0 + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters($a,$p,$r); + + # also takes care of the "error in _find_round_parameters?" case + return $x->bnan() if $x->{sign} ne '+' || $x->is_zero(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # P = undef + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + return $x->bzero(@params) if $x->is_one(); + # base not defined => base == Euler's number e + if (defined $base) + { + # make object, since we don't feed it through objectify() to still get the + # case of $base == undef + $base = $self->new($base) unless ref($base); + # $base > 0; $base != 1 + return $x->bnan() if $base->is_zero() || $base->is_one() || + $base->{sign} ne '+'; + # if $x == $base, we know the result must be 1.0 + if ($x->bcmp($base) == 0) + { + $x->bone('+',@params); + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + return $x; + } + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + local $Math::BigFloat::downgrade = undef; + + # upgrade $x if $x is not a BigFloat (handle BigInt input) + # XXX TODO: rebless! + if (!$x->isa('Math::BigFloat')) + { + $x = Math::BigFloat->new($x); + $self = ref($x); + } + + my $done = 0; + + # If the base is defined and an integer, try to calculate integer result + # first. This is very fast, and in case the real result was found, we can + # stop right here. + if (defined $base && $base->is_int() && $x->is_int()) + { + my $i = $MBI->_copy( $x->{_m} ); + $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); + my $int = Math::BigInt->bzero(); + $int->{value} = $i; + $int->blog($base->as_number()); + # if ($exact) + if ($base->as_number()->bpow($int) == $x) + { + # found result, return it + $x->{_m} = $int->{value}; + $x->{_e} = $MBI->_zero(); + $x->{_es} = '+'; + $x->bnorm(); + $done = 1; + } + } + + if ($done == 0) + { + # base is undef, so base should be e (Euler's number), so first calculate the + # log to base e (using reduction by 10 (and probably 2)): + $self->_log_10($x,$scale); + + # and if a different base was requested, convert it + if (defined $base) + { + $base = Math::BigFloat->new($base) unless $base->isa('Math::BigFloat'); + # not ln, but some other base (don't modify $base) + $x->bdiv( $base->copy()->blog(undef,$scale), $scale ); + } + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + + $x; + } + +sub _len_to_steps + { + # Given D (digits in decimal), compute N so that N! (N factorial) is + # at least D digits long. D should be at least 50. + my $d = shift; + + # two constants for the Ramanujan estimate of ln(N!) + my $lg2 = log(2 * 3.14159265) / 2; + my $lg10 = log(10); + + # D = 50 => N => 42, so L = 40 and R = 50 + my $l = 40; my $r = $d; + + # Otherwise this does not work under -Mbignum and we do not yet have "no bignum;" :( + $l = $l->numify if ref($l); + $r = $r->numify if ref($r); + $lg2 = $lg2->numify if ref($lg2); + $lg10 = $lg10->numify if ref($lg10); + + # binary search for the right value (could this be written as the reverse of lg(n!)?) + while ($r - $l > 1) + { + my $n = int(($r - $l) / 2) + $l; + my $ramanujan = + int(($n * log($n) - $n + log( $n * (1 + 4*$n*(1+2*$n)) ) / 6 + $lg2) / $lg10); + $ramanujan > $d ? $r = $n : $l = $n; + } + $l; + } + +sub bnok + { + # Calculate n over k (binomial coefficient or "choose" function) as integer. + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bnok'); + + return $x->bnan() if $x->is_nan() || $y->is_nan(); + return $x->binf() if $x->is_inf(); + + my $u = $x->as_int(); + $u->bnok($y->as_int()); + + $x->{_m} = $u->{value}; + $x->{_e} = $MBI->_zero(); + $x->{_es} = '+'; + $x->{sign} = '+'; + $x->bnorm(@r); + } + +sub bexp + { + # Calculate e ** X (Euler's number to the power of X) + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bexp'); + + return $x->binf() if $x->{sign} eq '+inf'; + return $x->bzero() if $x->{sign} eq '-inf'; + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters($a,$p,$r); + + # also takes care of the "error in _find_round_parameters?" case + return $x if $x->{sign} eq 'NaN'; + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # P = undef + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it's not enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + return $x->bone(@params) if $x->is_zero(); + + if (!$x->isa('Math::BigFloat')) + { + $x = Math::BigFloat->new($x); + $self = ref($x); + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + local $Math::BigFloat::downgrade = undef; + + my $x_org = $x->copy(); + + # We use the following Taylor series: + + # x x^2 x^3 x^4 + # e = 1 + --- + --- + --- + --- ... + # 1! 2! 3! 4! + + # The difference for each term is X and N, which would result in: + # 2 copy, 2 mul, 2 add, 1 inc, 1 div operations per term + + # But it is faster to compute exp(1) and then raising it to the + # given power, esp. if $x is really big and an integer because: + + # * The numerator is always 1, making the computation faster + # * the series converges faster in the case of x == 1 + # * We can also easily check when we have reached our limit: when the + # term to be added is smaller than "1E$scale", we can stop - f.i. + # scale == 5, and we have 1/40320, then we stop since 1/40320 < 1E-5. + # * we can compute the *exact* result by simulating bigrat math: + + # 1 1 gcd(3,4) = 1 1*24 + 1*6 5 + # - + - = ---------- = -- + # 6 24 6*24 24 + + # We do not compute the gcd() here, but simple do: + # 1 1 1*24 + 1*6 30 + # - + - = --------- = -- + # 6 24 6*24 144 + + # In general: + # a c a*d + c*b and note that c is always 1 and d = (b*f) + # - + - = --------- + # b d b*d + + # This leads to: which can be reduced by b to: + # a 1 a*b*f + b a*f + 1 + # - + - = --------- = ------- + # b b*f b*b*f b*f + + # The first terms in the series are: + + # 1 1 1 1 1 1 1 1 13700 + # -- + -- + -- + -- + -- + --- + --- + ---- = ----- + # 1 1 2 6 24 120 720 5040 5040 + + # Note that we cannot simple reduce 13700/5040 to 685/252, but must keep A and B! + + if ($scale <= 75) + { + # set $x directly from a cached string form + $x->{_m} = $MBI->_new( + "27182818284590452353602874713526624977572470936999595749669676277240766303535476"); + $x->{sign} = '+'; + $x->{_es} = '-'; + $x->{_e} = $MBI->_new(79); + } + else + { + # compute A and B so that e = A / B. + + # After some terms we end up with this, so we use it as a starting point: + my $A = $MBI->_new("90933395208605785401971970164779391644753259799242"); + my $F = $MBI->_new(42); my $step = 42; + + # Compute how many steps we need to take to get $A and $B sufficiently big + my $steps = _len_to_steps($scale - 4); +# print STDERR "# Doing $steps steps for ", $scale-4, " digits\n"; + while ($step++ <= $steps) + { + # calculate $a * $f + 1 + $A = $MBI->_mul($A, $F); + $A = $MBI->_inc($A); + # increment f + $F = $MBI->_inc($F); + } + # compute $B as factorial of $steps (this is faster than doing it manually) + my $B = $MBI->_fac($MBI->_new($steps)); + +# print "A ", $MBI->_str($A), "\nB ", $MBI->_str($B), "\n"; + + # compute A/B with $scale digits in the result (truncate, not round) + $A = $MBI->_lsft( $A, $MBI->_new($scale), 10); + $A = $MBI->_div( $A, $B ); + + $x->{_m} = $A; + $x->{sign} = '+'; + $x->{_es} = '-'; + $x->{_e} = $MBI->_new($scale); + } + + # $x contains now an estimate of e, with some surplus digits, so we can round + if (!$x_org->is_one()) + { + # raise $x to the wanted power and round it in one step: + $x->bpow($x_org, @params); + } + else + { + # else just round the already computed result + delete $x->{_a}; delete $x->{_p}; + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + + $x; # return modified $x + } + +sub _log + { + # internal log function to calculate ln() based on Taylor series. + # Modifies $x in place. + my ($self,$x,$scale) = @_; + + # in case of $x == 1, result is 0 + return $x->bzero() if $x->is_one(); + + # XXX TODO: rewrite this in a similar manner to bexp() + + # http://www.efunda.com/math/taylor_series/logarithmic.cfm?search_string=log + + # u = x-1, v = x+1 + # _ _ + # Taylor: | u 1 u^3 1 u^5 | + # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 0 + # |_ v 3 v^3 5 v^5 _| + + # This takes much more steps to calculate the result and is thus not used + # u = x-1 + # _ _ + # Taylor: | u 1 u^2 1 u^3 | + # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 1/2 + # |_ x 2 x^2 3 x^3 _| + + my ($limit,$v,$u,$below,$factor,$two,$next,$over,$f); + + $v = $x->copy(); $v->binc(); # v = x+1 + $x->bdec(); $u = $x->copy(); # u = x-1; x = x-1 + $x->bdiv($v,$scale); # first term: u/v + $below = $v->copy(); + $over = $u->copy(); + $u *= $u; $v *= $v; # u^2, v^2 + $below->bmul($v); # u^3, v^3 + $over->bmul($u); + $factor = $self->new(3); $f = $self->new(2); + + my $steps = 0 if DEBUG; + $limit = $self->new("1E-". ($scale-1)); + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop + + # calculating the next term simple from over/below will result in quite + # a time hog if the input has many digits, since over and below will + # accumulate more and more digits, and the result will also have many + # digits, but in the end it is rounded to $scale digits anyway. So if we + # round $over and $below first, we save a lot of time for the division + # (not with log(1.2345), but try log (123**123) to see what I mean. This + # can introduce a rounding error if the division result would be f.i. + # 0.1234500000001 and we round it to 5 digits it would become 0.12346, but + # if we truncated $over and $below we might get 0.12345. Does this matter + # for the end result? So we give $over and $below 4 more digits to be + # on the safe side (unscientific error handling as usual... :+D + + $next = $over->copy->bround($scale+4)->bdiv( + $below->copy->bmul($factor)->bround($scale+4), + $scale); + +## old version: +## $next = $over->copy()->bdiv($below->copy()->bmul($factor),$scale); + + last if $next->bacmp($limit) <= 0; + + delete $next->{_a}; delete $next->{_p}; + $x->badd($next); + # calculate things for the next term + $over *= $u; $below *= $v; $factor->badd($f); + if (DEBUG) + { + $steps++; print "step $steps = $x\n" if $steps % 10 == 0; + } + } + print "took $steps steps\n" if DEBUG; + $x->bmul($f); # $x *= 2 + } + +sub _log_10 + { + # Internal log function based on reducing input to the range of 0.1 .. 9.99 + # and then "correcting" the result to the proper one. Modifies $x in place. + my ($self,$x,$scale) = @_; + + # Taking blog() from numbers greater than 10 takes a *very long* time, so we + # break the computation down into parts based on the observation that: + # blog(X*Y) = blog(X) + blog(Y) + # We set Y here to multiples of 10 so that $x becomes below 1 - the smaller + # $x is the faster it gets. Since 2*$x takes about 10 times as + # long, we make it faster by about a factor of 100 by dividing $x by 10. + + # The same observation is valid for numbers smaller than 0.1, e.g. computing + # log(1) is fastest, and the further away we get from 1, the longer it takes. + # So we also 'break' this down by multiplying $x with 10 and subtract the + # log(10) afterwards to get the correct result. + + # To get $x even closer to 1, we also divide by 2 and then use log(2) to + # correct for this. For instance if $x is 2.4, we use the formula: + # blog(2.4 * 2) == blog (1.2) + blog(2) + # and thus calculate only blog(1.2) and blog(2), which is faster in total + # than calculating blog(2.4). + + # In addition, the values for blog(2) and blog(10) are cached. + + # Calculate nr of digits before dot: + my $dbd = $MBI->_num($x->{_e}); + $dbd = -$dbd if $x->{_es} eq '-'; + $dbd += $MBI->_len($x->{_m}); + + # more than one digit (e.g. at least 10), but *not* exactly 10 to avoid + # infinite recursion + + my $calc = 1; # do some calculation? + + # disable the shortcut for 10, since we need log(10) and this would recurse + # infinitely deep + if ($x->{_es} eq '+' && $MBI->_is_one($x->{_e}) && $MBI->_is_one($x->{_m})) + { + $dbd = 0; # disable shortcut + # we can use the cached value in these cases + if ($scale <= $LOG_10_A) + { + $x->bzero(); $x->badd($LOG_10); # modify $x in place + $calc = 0; # no need to calc, but round + } + # if we can't use the shortcut, we continue normally + } + else + { + # disable the shortcut for 2, since we maybe have it cached + if (($MBI->_is_zero($x->{_e}) && $MBI->_is_two($x->{_m}))) + { + $dbd = 0; # disable shortcut + # we can use the cached value in these cases + if ($scale <= $LOG_2_A) + { + $x->bzero(); $x->badd($LOG_2); # modify $x in place + $calc = 0; # no need to calc, but round + } + # if we can't use the shortcut, we continue normally + } + } + + # if $x = 0.1, we know the result must be 0-log(10) + if ($calc != 0 && $x->{_es} eq '-' && $MBI->_is_one($x->{_e}) && + $MBI->_is_one($x->{_m})) + { + $dbd = 0; # disable shortcut + # we can use the cached value in these cases + if ($scale <= $LOG_10_A) + { + $x->bzero(); $x->bsub($LOG_10); + $calc = 0; # no need to calc, but round + } + } + + return if $calc == 0; # already have the result + + # default: these correction factors are undef and thus not used + my $l_10; # value of ln(10) to A of $scale + my $l_2; # value of ln(2) to A of $scale + + my $two = $self->new(2); + + # $x == 2 => 1, $x == 13 => 2, $x == 0.1 => 0, $x == 0.01 => -1 + # so don't do this shortcut for 1 or 0 + if (($dbd > 1) || ($dbd < 0)) + { + # convert our cached value to an object if not already (avoid doing this + # at import() time, since not everybody needs this) + $LOG_10 = $self->new($LOG_10,undef,undef) unless ref $LOG_10; + + #print "x = $x, dbd = $dbd, calc = $calc\n"; + # got more than one digit before the dot, or more than one zero after the + # dot, so do: + # log(123) == log(1.23) + log(10) * 2 + # log(0.0123) == log(1.23) - log(10) * 2 + + if ($scale <= $LOG_10_A) + { + # use cached value + $l_10 = $LOG_10->copy(); # copy for mul + } + else + { + # else: slower, compute and cache result + # also disable downgrade for this code path + local $Math::BigFloat::downgrade = undef; + + # shorten the time to calculate log(10) based on the following: + # log(1.25 * 8) = log(1.25) + log(8) + # = log(1.25) + log(2) + log(2) + log(2) + + # first get $l_2 (and possible compute and cache log(2)) + $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2; + if ($scale <= $LOG_2_A) + { + # use cached value + $l_2 = $LOG_2->copy(); # copy() for the mul below + } + else + { + # else: slower, compute and cache result + $l_2 = $two->copy(); $self->_log($l_2, $scale); # scale+4, actually + $LOG_2 = $l_2->copy(); # cache the result for later + # the copy() is for mul below + $LOG_2_A = $scale; + } + + # now calculate log(1.25): + $l_10 = $self->new('1.25'); $self->_log($l_10, $scale); # scale+4, actually + + # log(1.25) + log(2) + log(2) + log(2): + $l_10->badd($l_2); + $l_10->badd($l_2); + $l_10->badd($l_2); + $LOG_10 = $l_10->copy(); # cache the result for later + # the copy() is for mul below + $LOG_10_A = $scale; + } + $dbd-- if ($dbd > 1); # 20 => dbd=2, so make it dbd=1 + $l_10->bmul( $self->new($dbd)); # log(10) * (digits_before_dot-1) + my $dbd_sign = '+'; + if ($dbd < 0) + { + $dbd = -$dbd; + $dbd_sign = '-'; + } + ($x->{_e}, $x->{_es}) = + _e_sub( $x->{_e}, $MBI->_new($dbd), $x->{_es}, $dbd_sign); # 123 => 1.23 + + } + + # Now: 0.1 <= $x < 10 (and possible correction in l_10) + + ### Since $x in the range 0.5 .. 1.5 is MUCH faster, we do a repeated div + ### or mul by 2 (maximum times 3, since x < 10 and x > 0.1) + + $HALF = $self->new($HALF) unless ref($HALF); + + my $twos = 0; # default: none (0 times) + while ($x->bacmp($HALF) <= 0) # X <= 0.5 + { + $twos--; $x->bmul($two); + } + while ($x->bacmp($two) >= 0) # X >= 2 + { + $twos++; $x->bdiv($two,$scale+4); # keep all digits + } + $x->bround($scale+4); + # $twos > 0 => did mul 2, < 0 => did div 2 (but we never did both) + # So calculate correction factor based on ln(2): + if ($twos != 0) + { + $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2; + if ($scale <= $LOG_2_A) + { + # use cached value + $l_2 = $LOG_2->copy(); # copy() for the mul below + } + else + { + # else: slower, compute and cache result + # also disable downgrade for this code path + local $Math::BigFloat::downgrade = undef; + $l_2 = $two->copy(); $self->_log($l_2, $scale); # scale+4, actually + $LOG_2 = $l_2->copy(); # cache the result for later + # the copy() is for mul below + $LOG_2_A = $scale; + } + $l_2->bmul($twos); # * -2 => subtract, * 2 => add + } + else + { + undef $l_2; + } + + $self->_log($x,$scale); # need to do the "normal" way + $x->badd($l_10) if defined $l_10; # correct it by ln(10) + $x->badd($l_2) if defined $l_2; # and maybe by ln(2) + + # all done, $x contains now the result + $x; + } + +sub blcm + { + # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT + # does not modify arguments, but returns new object + # Lowest Common Multiplicator + + my ($self,@arg) = objectify(0,@_); + my $x = $self->new(shift @arg); + while (@arg) { $x = Math::BigInt::__lcm($x,shift @arg); } + $x; + } + +sub bgcd + { + # (BINT or num_str, BINT or num_str) return BINT + # does not modify arguments, but returns new object + + my $y = shift; + $y = __PACKAGE__->new($y) if !ref($y); + my $self = ref($y); + my $x = $y->copy()->babs(); # keep arguments + + return $x->bnan() if $x->{sign} !~ /^[+-]$/ # x NaN? + || !$x->is_int(); # only for integers now + + while (@_) + { + my $t = shift; $t = $self->new($t) if !ref($t); + $y = $t->copy()->babs(); + + return $x->bnan() if $y->{sign} !~ /^[+-]$/ # y NaN? + || !$y->is_int(); # only for integers now + + # greatest common divisor + while (! $y->is_zero()) + { + ($x,$y) = ($y->copy(), $x->copy()->bmod($y)); + } + + last if $x->is_one(); + } + $x; + } + +############################################################################## + +sub _e_add + { + # Internal helper sub to take two positive integers and their signs and + # then add them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')), + # output ($CALC,('+'|'-')) + my ($x,$y,$xs,$ys) = @_; + + # if the signs are equal we can add them (-5 + -3 => -(5 + 3) => -8) + if ($xs eq $ys) + { + $x = $MBI->_add ($x, $y ); # a+b + # the sign follows $xs + return ($x, $xs); + } + + my $a = $MBI->_acmp($x,$y); + if ($a > 0) + { + $x = $MBI->_sub ($x , $y); # abs sub + } + elsif ($a == 0) + { + $x = $MBI->_zero(); # result is 0 + $xs = '+'; + } + else # a < 0 + { + $x = $MBI->_sub ( $y, $x, 1 ); # abs sub + $xs = $ys; + } + ($x,$xs); + } + +sub _e_sub + { + # Internal helper sub to take two positive integers and their signs and + # then subtract them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')), + # output ($CALC,('+'|'-')) + my ($x,$y,$xs,$ys) = @_; + + # flip sign + $ys =~ tr/+-/-+/; + _e_add($x,$y,$xs,$ys); # call add (does subtract now) + } + +############################################################################### +# is_foo methods (is_negative, is_positive are inherited from BigInt) + +sub is_int + { + # return true if arg (BFLOAT or num_str) is an integer + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + (($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't + ($x->{_es} eq '+')) ? 1 : 0; # 1e-1 => no integer + } + +sub is_zero + { + # return true if arg (BFLOAT or num_str) is zero + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})) ? 1 : 0; + } + +sub is_one + { + # return true if arg (BFLOAT or num_str) is +1 or -1 if signis given + my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $sign = '+' if !defined $sign || $sign ne '-'; + + ($x->{sign} eq $sign && + $MBI->_is_zero($x->{_e}) && + $MBI->_is_one($x->{_m}) ) ? 1 : 0; + } + +sub is_odd + { + # return true if arg (BFLOAT or num_str) is odd or false if even + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't + ($MBI->_is_zero($x->{_e})) && + ($MBI->_is_odd($x->{_m}))) ? 1 : 0; + } + +sub is_even + { + # return true if arg (BINT or num_str) is even or false if odd + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't + ($x->{_es} eq '+') && # 123.45 isn't + ($MBI->_is_even($x->{_m}))) ? 1 : 0; # but 1200 is + } + +sub bmul + { + # multiply two numbers + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmul'); + + return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + + return $upgrade->bmul($x,$y,@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + # aEb * cEd = (a*c)E(b+d) + $MBI->_mul($x->{_m},$y->{_m}); + ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); + + $r[3] = $y; # no push! + + # adjust sign: + $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+'; + $x->bnorm->round(@r); + } + +sub bmuladd + { + # multiply two numbers and add the third to the result + + # set up parameters + my ($self,$x,$y,$z,@r) = objectify(3,@_); + + return $x if $x->modify('bmuladd'); + + return $x->bnan() if (($x->{sign} eq $nan) || + ($y->{sign} eq $nan) || + ($z->{sign} eq $nan)); + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + + return $upgrade->bmul($x,$y,@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + # aEb * cEd = (a*c)E(b+d) + $MBI->_mul($x->{_m},$y->{_m}); + ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); + + $r[3] = $y; # no push! + + # adjust sign: + $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+'; + + # z=inf handling (z=NaN handled above) + $x->{sign} = $z->{sign}, return $x if $z->{sign} =~ /^[+-]inf$/; + + # take lower of the two e's and adapt m1 to it to match m2 + my $e = $z->{_e}; + $e = $MBI->_zero() if !defined $e; # if no BFLOAT? + $e = $MBI->_copy($e); # make copy (didn't do it yet) + + my $es; + + ($e,$es) = _e_sub($e, $x->{_e}, $z->{_es} || '+', $x->{_es}); + + my $add = $MBI->_copy($z->{_m}); + + if ($es eq '-') # < 0 + { + $MBI->_lsft( $x->{_m}, $e, 10); + ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es); + } + elsif (!$MBI->_is_zero($e)) # > 0 + { + $MBI->_lsft($add, $e, 10); + } + # else: both e are the same, so just leave them + + if ($x->{sign} eq $z->{sign}) + { + # add + $x->{_m} = $MBI->_add($x->{_m}, $add); + } + else + { + ($x->{_m}, $x->{sign}) = + _e_add($x->{_m}, $add, $x->{sign}, $z->{sign}); + } + + # delete trailing zeros, then round + $x->bnorm()->round(@r); + } + +sub bdiv + { + # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return + # (BFLOAT, BFLOAT) (quo, rem) or BFLOAT (only quo) + + # set up parameters + my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('bdiv'); + + my $wantarray = wantarray; # call only once + + # At least one argument is NaN. This is handled the same way as in + # Math::BigInt -> bdiv(). + + if ($x -> is_nan() || $y -> is_nan()) { + return $wantarray ? ($x -> bnan(), $self -> bnan()) : $x -> bnan(); + } + + # Divide by zero and modulo zero. This is handled the same way as in + # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt -> + # bdiv() for further details. + + if ($y -> is_zero()) { + my ($quo, $rem); + if ($wantarray) { + $rem = $x -> copy(); + } + if ($x -> is_zero()) { + $quo = $x -> bnan(); + } else { + $quo = $x -> binf($x -> {sign}); + } + return $wantarray ? ($quo, $rem) : $quo; + } + + # Numerator (dividend) is +/-inf. This is handled the same way as in + # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt -> + # bdiv() for further details. + + if ($x -> is_inf()) { + my ($quo, $rem); + $rem = $self -> bnan() if $wantarray; + if ($y -> is_inf()) { + $quo = $x -> bnan(); + } else { + my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-'; + $quo = $x -> binf($sign); + } + return $wantarray ? ($quo, $rem) : $quo; + } + + # Denominator (divisor) is +/-inf. This is handled the same way as in + # Math::BigInt -> bdiv(), with one exception: In scalar context, + # Math::BigFloat does true division (although rounded), not floored division + # (F-division), so a finite number divided by +/-inf is always zero. See the + # comment in the code for Math::BigInt -> bdiv() for further details. + + if ($y -> is_inf()) { + my ($quo, $rem); + if ($wantarray) { + if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { + $rem = $x -> copy(); + $quo = $x -> bzero(); + } else { + $rem = $self -> binf($y -> {sign}); + $quo = $x -> bone('-'); + } + return ($quo, $rem); + } else { + if ($y -> is_inf()) { + if ($x -> is_nan() || $x -> is_inf()) { + return $x -> bnan(); + } else { + return $x -> bzero(); + } + } + } + } + + # At this point, both the numerator and denominator are finite numbers, and + # the denominator (divisor) is non-zero. + + # x == 0? + return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); + + # upgrade ? + return $upgrade->bdiv($upgrade->new($x),$y,$a,$p,$r) if defined $upgrade; + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my (@params,$scale); + ($x,@params) = $x->_find_round_parameters($a,$p,$r,$y); + + return $x if $x->is_nan(); # error in _find_round_parameters? + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } else { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + my $rem; + $rem = $self -> bzero() if wantarray; + + $y = $self->new($y) unless $y->isa('Math::BigFloat'); + + my $lx = $MBI -> _len($x->{_m}); my $ly = $MBI -> _len($y->{_m}); + $scale = $lx if $lx > $scale; + $scale = $ly if $ly > $scale; + my $diff = $ly - $lx; + $scale += $diff if $diff > 0; # if lx << ly, but not if ly << lx! + + # check that $y is not 1 nor -1 and cache the result: + my $y_not_one = !($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m})); + + # flipping the sign of $y will also flip the sign of $x for the special + # case of $x->bsub($x); so we can catch it below: + my $xsign = $x->{sign}; + $y->{sign} =~ tr/+-/-+/; + + if ($xsign ne $x->{sign}) + { + # special case of $x /= $x results in 1 + $x->bone(); # "fixes" also sign of $y, since $x is $y + } + else + { + # correct $y's sign again + $y->{sign} =~ tr/+-/-+/; + # continue with normal div code: + + # make copy of $x in case of list context for later remainder calculation + if (wantarray && $y_not_one) + { + $rem = $x->copy(); + } + + $x->{sign} = $x->{sign} ne $y->sign() ? '-' : '+'; + + # check for / +-1 ( +/- 1E0) + if ($y_not_one) + { + # promote BigInts and it's subclasses (except when already a BigFloat) + $y = $self->new($y) unless $y->isa('Math::BigFloat'); + + # calculate the result to $scale digits and then round it + # a * 10 ** b / c * 10 ** d => a/c * 10 ** (b-d) + $MBI->_lsft($x->{_m},$MBI->_new($scale),10); + $MBI->_div ($x->{_m},$y->{_m}); # a/c + + # correct exponent of $x + ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); + # correct for 10**scale + ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $MBI->_new($scale), $x->{_es}, '+'); + $x->bnorm(); # remove trailing 0's + } + } # end else $x != $y + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + delete $x->{_a}; # clear before round + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + delete $x->{_p}; # clear before round + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + + if (wantarray) + { + if ($y_not_one) + { + $x -> bfloor(); + $rem->bmod($y,@params); # copy already done + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $rem->{_a}; delete $rem->{_p}; + } + return ($x,$rem); + } + $x; + } + +sub bmod + { + # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return remainder + + # set up parameters + my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('bmod'); + + # At least one argument is NaN. This is handled the same way as in + # Math::BigInt -> bmod(). + + if ($x -> is_nan() || $y -> is_nan()) { + return $x -> bnan(); + } + + # Modulo zero. This is handled the same way as in Math::BigInt -> bmod(). + + if ($y -> is_zero()) { + return $x; + } + + # Numerator (dividend) is +/-inf. This is handled the same way as in + # Math::BigInt -> bmod(). + + if ($x -> is_inf()) { + return $x -> bnan(); + } + + # Denominator (divisor) is +/-inf. This is handled the same way as in + # Math::BigInt -> bmod(). + + if ($y -> is_inf()) { + if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { + return $x; + } else { + return $x -> binf($y -> sign()); + } + } + + return $x->bzero() if $x->is_zero() + || ($x->is_int() && + # check that $y == +1 or $y == -1: + ($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m}))); + + my $cmp = $x->bacmp($y); # equal or $x < $y? + if ($cmp == 0) { # $x == $y => result 0 + return $x -> bzero($a, $p); + } + + # only $y of the operands negative? + my $neg = $x->{sign} ne $y->{sign} ? 1 : 0; + + $x->{sign} = $y->{sign}; # calc sign first + if ($cmp < 0 && $neg == 0) { # $x < $y => result $x + return $x -> round($a, $p, $r); + } + + my $ym = $MBI->_copy($y->{_m}); + + # 2e1 => 20 + $MBI->_lsft( $ym, $y->{_e}, 10) + if $y->{_es} eq '+' && !$MBI->_is_zero($y->{_e}); + + # if $y has digits after dot + my $shifty = 0; # correct _e of $x by this + if ($y->{_es} eq '-') # has digits after dot + { + # 123 % 2.5 => 1230 % 25 => 5 => 0.5 + $shifty = $MBI->_num($y->{_e}); # no more digits after dot + $MBI->_lsft($x->{_m}, $y->{_e}, 10);# 123 => 1230, $y->{_m} is already 25 + } + # $ym is now mantissa of $y based on exponent 0 + + my $shiftx = 0; # correct _e of $x by this + if ($x->{_es} eq '-') # has digits after dot + { + # 123.4 % 20 => 1234 % 200 + $shiftx = $MBI->_num($x->{_e}); # no more digits after dot + $MBI->_lsft($ym, $x->{_e}, 10); # 123 => 1230 + } + # 123e1 % 20 => 1230 % 20 + if ($x->{_es} eq '+' && !$MBI->_is_zero($x->{_e})) + { + $MBI->_lsft( $x->{_m}, $x->{_e},10); # es => '+' here + } + + $x->{_e} = $MBI->_new($shiftx); + $x->{_es} = '+'; + $x->{_es} = '-' if $shiftx != 0 || $shifty != 0; + $MBI->_add( $x->{_e}, $MBI->_new($shifty)) if $shifty != 0; + + # now mantissas are equalized, exponent of $x is adjusted, so calc result + + $x->{_m} = $MBI->_mod( $x->{_m}, $ym); + + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0 + $x->bnorm(); + + if ($neg != 0 && ! $x -> is_zero()) # one of them negative => correct in place + { + my $r = $y - $x; + $x->{_m} = $r->{_m}; + $x->{_e} = $r->{_e}; + $x->{_es} = $r->{_es}; + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0 + $x->bnorm(); + } + + $x->round($a,$p,$r,$y); # round and return + } + +sub broot + { + # calculate $y'th root of $x + + # set up parameters + my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('broot'); + + # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0 + return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() || + $y->{sign} !~ /^\+$/; + + return $x if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one(); + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my (@params,$scale); + ($x,@params) = $x->_find_round_parameters($a,$p,$r); + + return $x if $x->is_nan(); # error in _find_round_parameters? + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI + + # remember sign and make $x positive, since -4 ** (1/2) => -2 + my $sign = 0; $sign = 1 if $x->{sign} eq '-'; $x->{sign} = '+'; + + my $is_two = 0; + if ($y->isa('Math::BigFloat')) + { + $is_two = ($y->{sign} eq '+' && $MBI->_is_two($y->{_m}) && $MBI->_is_zero($y->{_e})); + } + else + { + $is_two = ($y == 2); + } + + # normal square root if $y == 2: + if ($is_two) + { + $x->bsqrt($scale+4); + } + elsif ($y->is_one('-')) + { + # $x ** -1 => 1/$x + my $u = $self->bone()->bdiv($x,$scale); + # copy private parts over + $x->{_m} = $u->{_m}; + $x->{_e} = $u->{_e}; + $x->{_es} = $u->{_es}; + } + else + { + # calculate the broot() as integer result first, and if it fits, return + # it rightaway (but only if $x and $y are integer): + + my $done = 0; # not yet + if ($y->is_int() && $x->is_int()) + { + my $i = $MBI->_copy( $x->{_m} ); + $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); + my $int = Math::BigInt->bzero(); + $int->{value} = $i; + $int->broot($y->as_number()); + # if ($exact) + if ($int->copy()->bpow($y) == $x) + { + # found result, return it + $x->{_m} = $int->{value}; + $x->{_e} = $MBI->_zero(); + $x->{_es} = '+'; + $x->bnorm(); + $done = 1; + } + } + if ($done == 0) + { + my $u = $self->bone()->bdiv($y,$scale+4); + delete $u->{_a}; delete $u->{_p}; # otherwise it conflicts + $x->bpow($u,$scale+4); # el cheapo + } + } + $x->bneg() if $sign == 1; + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub bsqrt + { + # calculate square root + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bsqrt'); + + return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0 + return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf + return $x->round($a,$p,$r) if $x->is_zero() || $x->is_one(); + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my (@params,$scale); + ($x,@params) = $x->_find_round_parameters($a,$p,$r); + + return $x if $x->is_nan(); # error in _find_round_parameters? + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI + + my $i = $MBI->_copy( $x->{_m} ); + $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); + my $xas = Math::BigInt->bzero(); + $xas->{value} = $i; + + my $gs = $xas->copy()->bsqrt(); # some guess + + if (($x->{_es} ne '-') # guess can't be accurate if there are + # digits after the dot + && ($xas->bacmp($gs * $gs) == 0)) # guess hit the nail on the head? + { + # exact result, copy result over to keep $x + $x->{_m} = $gs->{value}; $x->{_e} = $MBI->_zero(); $x->{_es} = '+'; + $x->bnorm(); + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # re-enable A and P, upgrade is taken care of by "local" + ${"$self\::accuracy"} = $ab; ${"$self\::precision"} = $pb; + return $x; + } + + # sqrt(2) = 1.4 because sqrt(2*100) = 1.4*10; so we can increase the accuracy + # of the result by multiplying the input by 100 and then divide the integer + # result of sqrt(input) by 10. Rounding afterwards returns the real result. + + # The following steps will transform 123.456 (in $x) into 123456 (in $y1) + my $y1 = $MBI->_copy($x->{_m}); + + my $length = $MBI->_len($y1); + + # Now calculate how many digits the result of sqrt(y1) would have + my $digits = int($length / 2); + + # But we need at least $scale digits, so calculate how many are missing + my $shift = $scale - $digits; + + # This happens if the input had enough digits + # (we take care of integer guesses above) + $shift = 0 if $shift < 0; + + # Multiply in steps of 100, by shifting left two times the "missing" digits + my $s2 = $shift * 2; + + # We now make sure that $y1 has the same odd or even number of digits than + # $x had. So when _e of $x is odd, we must shift $y1 by one digit left, + # because we always must multiply by steps of 100 (sqrt(100) is 10) and not + # steps of 10. The length of $x does not count, since an even or odd number + # of digits before the dot is not changed by adding an even number of digits + # after the dot (the result is still odd or even digits long). + $s2++ if $MBI->_is_odd($x->{_e}); + + $MBI->_lsft( $y1, $MBI->_new($s2), 10); + + # now take the square root and truncate to integer + $y1 = $MBI->_sqrt($y1); + + # By "shifting" $y1 right (by creating a negative _e) we calculate the final + # result, which is than later rounded to the desired scale. + + # calculate how many zeros $x had after the '.' (or before it, depending + # on sign of $dat, the result should have half as many: + my $dat = $MBI->_num($x->{_e}); + $dat = -$dat if $x->{_es} eq '-'; + $dat += $length; + + if ($dat > 0) + { + # no zeros after the dot (e.g. 1.23, 0.49 etc) + # preserve half as many digits before the dot than the input had + # (but round this "up") + $dat = int(($dat+1)/2); + } + else + { + $dat = int(($dat)/2); + } + $dat -= $MBI->_len($y1); + if ($dat < 0) + { + $dat = abs($dat); + $x->{_e} = $MBI->_new( $dat ); + $x->{_es} = '-'; + } + else + { + $x->{_e} = $MBI->_new( $dat ); + $x->{_es} = '+'; + } + $x->{_m} = $y1; + $x->bnorm(); + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub bfac + { + # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT + # compute factorial number, modifies first argument + + # set up parameters + my ($self,$x,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + ($self,$x,@r) = objectify(1,@_) if !ref($x); + + # inf => inf + return $x if $x->modify('bfac') || $x->{sign} eq '+inf'; + + return $x->bnan() + if (($x->{sign} ne '+') || # inf, NaN, <0 etc => NaN + ($x->{_es} ne '+')); # digits after dot? + + # use BigInt's bfac() for faster calc + if (! $MBI->_is_zero($x->{_e})) + { + $MBI->_lsft($x->{_m}, $x->{_e},10); # change 12e1 to 120e0 + $x->{_e} = $MBI->_zero(); # normalize + $x->{_es} = '+'; + } + $MBI->_fac($x->{_m}); # calculate factorial + $x->bnorm()->round(@r); # norm again and round result + } + +sub _pow + { + # Calculate a power where $y is a non-integer, like 2 ** 0.3 + my ($x,$y,@r) = @_; + my $self = ref($x); + + # if $y == 0.5, it is sqrt($x) + $HALF = $self->new($HALF) unless ref($HALF); + return $x->bsqrt(@r,$y) if $y->bcmp($HALF) == 0; + + # Using: + # a ** x == e ** (x * ln a) + + # u = y * ln x + # _ _ + # Taylor: | u u^2 u^3 | + # x ** y = 1 + | --- + --- + ----- + ... | + # |_ 1 1*2 1*2*3 _| + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + return $x if $x->is_nan(); # error in _find_round_parameters? + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + + my ($limit,$v,$u,$below,$factor,$next,$over); + + $u = $x->copy()->blog(undef,$scale)->bmul($y); + $v = $self->bone(); # 1 + $factor = $self->new(2); # 2 + $x->bone(); # first term: 1 + + $below = $v->copy(); + $over = $u->copy(); + + $limit = $self->new("1E-". ($scale-1)); + #my $steps = 0; + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop: + $next = $over->copy()->bdiv($below,$scale); + last if $next->bacmp($limit) <= 0; + $x->badd($next); + # calculate things for the next term + $over *= $u; $below *= $factor; $factor->binc(); + + last if $x->{sign} !~ /^[-+]$/; + + #$steps++; + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub bpow + { + # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT + # compute power of two numbers, second arg is used as integer + # modifies first argument + + # set up parameters + my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('bpow'); + + return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; + return $x if $x->{sign} =~ /^[+-]inf$/; + + # cache the result of is_zero + my $y_is_zero = $y->is_zero(); + return $x->bone() if $y_is_zero; + return $x if $x->is_one() || $y->is_one(); + + my $x_is_zero = $x->is_zero(); + return $x->_pow($y,$a,$p,$r) if !$x_is_zero && !$y->is_int(); # non-integer power + + my $y1 = $y->as_number()->{value}; # make MBI part + + # if ($x == -1) + if ($x->{sign} eq '-' && $MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) + { + # if $x == -1 and odd/even y => +1/-1 because +-1 ^ (+-1) => +-1 + return $MBI->_is_odd($y1) ? $x : $x->babs(1); + } + if ($x_is_zero) + { + return $x if $y->{sign} eq '+'; # 0**y => 0 (if not y <= 0) + # 0 ** -y => 1 / (0 ** y) => 1 / 0! (1 / 0 => +inf) + return $x->binf(); + } + + my $new_sign = '+'; + $new_sign = $MBI->_is_odd($y1) ? '-' : '+' if $x->{sign} ne '+'; + + # calculate $x->{_m} ** $y and $x->{_e} * $y separately (faster) + $x->{_m} = $MBI->_pow( $x->{_m}, $y1); + $x->{_e} = $MBI->_mul ($x->{_e}, $y1); + + $x->{sign} = $new_sign; + $x->bnorm(); + if ($y->{sign} eq '-') + { + # modify $x in place! + my $z = $x->copy(); $x->bone(); + return scalar $x->bdiv($z,$a,$p,$r); # round in one go (might ignore y's A!) + } + $x->round($a,$p,$r,$y); + } + +sub bmodpow + { + # takes a very large number to a very large exponent in a given very + # large modulus, quickly, thanks to binary exponentiation. Supports + # negative exponents. + my ($self,$num,$exp,$mod,@r) = objectify(3,@_); + + return $num if $num->modify('bmodpow'); + + # check modulus for valid values + return $num->bnan() if ($mod->{sign} ne '+' # NaN, - , -inf, +inf + || $mod->is_zero()); + + # check exponent for valid values + if ($exp->{sign} =~ /\w/) + { + # i.e., if it's NaN, +inf, or -inf... + return $num->bnan(); + } + + $num->bmodinv ($mod) if ($exp->{sign} eq '-'); + + # check num for valid values (also NaN if there was no inverse but $exp < 0) + return $num->bnan() if $num->{sign} !~ /^[+-]$/; + + # $mod is positive, sign on $exp is ignored, result also positive + + # XXX TODO: speed it up when all three numbers are integers + $num->bpow($exp)->bmod($mod); + } + +############################################################################### +# trigonometric functions + +# helper function for bpi() and batan2(), calculates arcus tanges (1/x) + +sub _atan_inv + { + # return a/b so that a/b approximates atan(1/x) to at least limit digits + my ($self, $x, $limit) = @_; + + # Taylor: x^3 x^5 x^7 x^9 + # atan = x - --- + --- - --- + --- - ... + # 3 5 7 9 + + # 1 1 1 1 + # atan 1/x = - - ------- + ------- - ------- + ... + # x x^3 * 3 x^5 * 5 x^7 * 7 + + # 1 1 1 1 + # atan 1/x = - - --------- + ---------- - ----------- + ... + # 5 3 * 125 5 * 3125 7 * 78125 + + # Subtraction/addition of a rational: + + # 5 7 5*3 +- 7*4 + # - +- - = ---------- + # 4 3 4*3 + + # Term: N N+1 + # + # a 1 a * d * c +- b + # ----- +- ------------------ = ---------------- + # b d * c b * d * c + + # since b1 = b0 * (d-2) * c + + # a 1 a * d +- b / c + # ----- +- ------------------ = ---------------- + # b d * c b * d + + # and d = d + 2 + # and c = c * x * x + + # u = d * c + # stop if length($u) > limit + # a = a * u +- b + # b = b * u + # d = d + 2 + # c = c * x * x + # sign = 1 - sign + + my $a = $MBI->_one(); + my $b = $MBI->_copy($x); + + my $x2 = $MBI->_mul( $MBI->_copy($x), $b); # x2 = x * x + my $d = $MBI->_new( 3 ); # d = 3 + my $c = $MBI->_mul( $MBI->_copy($x), $x2); # c = x ^ 3 + my $two = $MBI->_new( 2 ); + + # run the first step unconditionally + my $u = $MBI->_mul( $MBI->_copy($d), $c); + $a = $MBI->_mul($a, $u); + $a = $MBI->_sub($a, $b); + $b = $MBI->_mul($b, $u); + $d = $MBI->_add($d, $two); + $c = $MBI->_mul($c, $x2); + + # a is now a * (d-3) * c + # b is now b * (d-2) * c + + # run the second step unconditionally + $u = $MBI->_mul( $MBI->_copy($d), $c); + $a = $MBI->_mul($a, $u); + $a = $MBI->_add($a, $b); + $b = $MBI->_mul($b, $u); + $d = $MBI->_add($d, $two); + $c = $MBI->_mul($c, $x2); + + # a is now a * (d-3) * (d-5) * c * c + # b is now b * (d-2) * (d-4) * c * c + + # so we can remove c * c from both a and b to shorten the numbers involved: + $a = $MBI->_div($a, $x2); + $b = $MBI->_div($b, $x2); + $a = $MBI->_div($a, $x2); + $b = $MBI->_div($b, $x2); + +# my $step = 0; + my $sign = 0; # 0 => -, 1 => + + while (3 < 5) + { +# $step++; +# if (($i++ % 100) == 0) +# { +# print "a=",$MBI->_str($a),"\n"; +# print "b=",$MBI->_str($b),"\n"; +# } +# print "d=",$MBI->_str($d),"\n"; +# print "x2=",$MBI->_str($x2),"\n"; +# print "c=",$MBI->_str($c),"\n"; + + my $u = $MBI->_mul( $MBI->_copy($d), $c); + # use _alen() for libs like GMP where _len() would be O(N^2) + last if $MBI->_alen($u) > $limit; + my ($bc,$r) = $MBI->_div( $MBI->_copy($b), $c); + if ($MBI->_is_zero($r)) + { + # b / c is an integer, so we can remove c from all terms + # this happens almost every time: + $a = $MBI->_mul($a, $d); + $a = $MBI->_sub($a, $bc) if $sign == 0; + $a = $MBI->_add($a, $bc) if $sign == 1; + $b = $MBI->_mul($b, $d); + } + else + { + # b / c is not an integer, so we keep c in the terms + # this happens very rarely, for instance for x = 5, this happens only + # at the following steps: + # 1, 5, 14, 32, 72, 157, 340, ... + $a = $MBI->_mul($a, $u); + $a = $MBI->_sub($a, $b) if $sign == 0; + $a = $MBI->_add($a, $b) if $sign == 1; + $b = $MBI->_mul($b, $u); + } + $d = $MBI->_add($d, $two); + $c = $MBI->_mul($c, $x2); + $sign = 1 - $sign; + + } + +# print "Took $step steps for ", $MBI->_str($x),"\n"; +# print "a=",$MBI->_str($a),"\n"; print "b=",$MBI->_str($b),"\n"; + # return a/b so that a/b approximates atan(1/x) + ($a,$b); + } + +sub bpi + { + my ($self,$n) = @_; + if (@_ == 0) + { + $self = $class; + } + if (@_ == 1) + { + # called like Math::BigFloat::bpi(10); + $n = $self; $self = $class; + # called like Math::BigFloat->bpi(); + $n = undef if $n eq 'Math::BigFloat'; + } + $self = ref($self) if ref($self); + my $fallback = defined $n ? 0 : 1; + $n = 40 if !defined $n || $n < 1; + + # after 黃見利 (Hwang Chien-Lih) (1997) + # pi/4 = 183 * atan(1/239) + 32 * atan(1/1023) – 68 * atan(1/5832) + # + 12 * atan(1/110443) - 12 * atan(1/4841182) - 100 * atan(1/6826318) + + # a few more to prevent rounding errors + $n += 4; + + my ($a,$b) = $self->_atan_inv( $MBI->_new(239),$n); + my ($c,$d) = $self->_atan_inv( $MBI->_new(1023),$n); + my ($e,$f) = $self->_atan_inv( $MBI->_new(5832),$n); + my ($g,$h) = $self->_atan_inv( $MBI->_new(110443),$n); + my ($i,$j) = $self->_atan_inv( $MBI->_new(4841182),$n); + my ($k,$l) = $self->_atan_inv( $MBI->_new(6826318),$n); + + $MBI->_mul($a, $MBI->_new(732)); + $MBI->_mul($c, $MBI->_new(128)); + $MBI->_mul($e, $MBI->_new(272)); + $MBI->_mul($g, $MBI->_new(48)); + $MBI->_mul($i, $MBI->_new(48)); + $MBI->_mul($k, $MBI->_new(400)); + + my $x = $self->bone(); $x->{_m} = $a; my $x_d = $self->bone(); $x_d->{_m} = $b; + my $y = $self->bone(); $y->{_m} = $c; my $y_d = $self->bone(); $y_d->{_m} = $d; + my $z = $self->bone(); $z->{_m} = $e; my $z_d = $self->bone(); $z_d->{_m} = $f; + my $u = $self->bone(); $u->{_m} = $g; my $u_d = $self->bone(); $u_d->{_m} = $h; + my $v = $self->bone(); $v->{_m} = $i; my $v_d = $self->bone(); $v_d->{_m} = $j; + my $w = $self->bone(); $w->{_m} = $k; my $w_d = $self->bone(); $w_d->{_m} = $l; + $x->bdiv($x_d, $n); + $y->bdiv($y_d, $n); + $z->bdiv($z_d, $n); + $u->bdiv($u_d, $n); + $v->bdiv($v_d, $n); + $w->bdiv($w_d, $n); + + delete $x->{_a}; delete $y->{_a}; delete $z->{_a}; + delete $u->{_a}; delete $v->{_a}; delete $w->{_a}; + $x->badd($y)->bsub($z)->badd($u)->bsub($v)->bsub($w); + + $x->bround($n-4); + delete $x->{_a} if $fallback == 1; + $x; + } + +sub bcos + { + # Calculate a cosinus of x. + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + # Taylor: x^2 x^4 x^6 x^8 + # cos = 1 - --- + --- - --- + --- ... + # 2! 4! 6! 8! + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + # constant object or error in _find_round_parameters? + return $x if $x->modify('bcos') || $x->is_nan(); + + return $x->bone(@r) if $x->is_zero(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + + my $last = 0; + my $over = $x * $x; # X ^ 2 + my $x2 = $over->copy(); # X ^ 2; difference between terms + my $sign = 1; # start with -= + my $below = $self->new(2); my $factorial = $self->new(3); + $x->bone(); delete $x->{_a}; delete $x->{_p}; + + my $limit = $self->new("1E-". ($scale-1)); + #my $steps = 0; + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop: + my $next = $over->copy()->bdiv($below,$scale); + last if $next->bacmp($limit) <= 0; + + if ($sign == 0) + { + $x->badd($next); + } + else + { + $x->bsub($next); + } + $sign = 1-$sign; # alternate + # calculate things for the next term + $over->bmul($x2); # $x*$x + $below->bmul($factorial); $factorial->binc(); # n*(n+1) + $below->bmul($factorial); $factorial->binc(); # n*(n+1) + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub bsin + { + # Calculate a sinus of x. + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + # taylor: x^3 x^5 x^7 x^9 + # sin = x - --- + --- - --- + --- ... + # 3! 5! 7! 9! + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + # constant object or error in _find_round_parameters? + return $x if $x->modify('bsin') || $x->is_nan(); + + return $x->bzero(@r) if $x->is_zero(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + + my $last = 0; + my $over = $x * $x; # X ^ 2 + my $x2 = $over->copy(); # X ^ 2; difference between terms + $over->bmul($x); # X ^ 3 as starting value + my $sign = 1; # start with -= + my $below = $self->new(6); my $factorial = $self->new(4); + delete $x->{_a}; delete $x->{_p}; + + my $limit = $self->new("1E-". ($scale-1)); + #my $steps = 0; + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop: + my $next = $over->copy()->bdiv($below,$scale); + last if $next->bacmp($limit) <= 0; + + if ($sign == 0) + { + $x->badd($next); + } + else + { + $x->bsub($next); + } + $sign = 1-$sign; # alternate + # calculate things for the next term + $over->bmul($x2); # $x*$x + $below->bmul($factorial); $factorial->binc(); # n*(n+1) + $below->bmul($factorial); $factorial->binc(); # n*(n+1) + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +sub batan2 + { + # calculate arcus tangens of ($y/$x) + + # set up parameters + my ($self,$y,$x,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$y,$x,@r) = objectify(2,@_); + } + + return $y if $y->modify('batan2'); + + return $y->bnan() if ($y->{sign} eq $nan) || ($x->{sign} eq $nan); + + # Y X + # 0 0 result is 0 + # 0 +x result is 0 + # ? inf result is 0 + return $y->bzero(@r) if ($x->is_inf('+') && !$y->is_inf()) || ($y->is_zero() && $x->{sign} eq '+'); + + # Y X + # != 0 -inf result is +- pi + if ($x->is_inf() || $y->is_inf()) + { + # calculate PI + my $pi = $self->bpi(@r); + if ($y->is_inf()) + { + # upgrade to BigRat etc. + return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; + if ($x->{sign} eq '-inf') + { + # calculate 3 pi/4 + $MBI->_mul($pi->{_m}, $MBI->_new(3)); + $MBI->_div($pi->{_m}, $MBI->_new(4)); + } + elsif ($x->{sign} eq '+inf') + { + # calculate pi/4 + $MBI->_div($pi->{_m}, $MBI->_new(4)); + } + else + { + # calculate pi/2 + $MBI->_div($pi->{_m}, $MBI->_new(2)); + } + $y->{sign} = substr($y->{sign},0,1); # keep +/- + } + # modify $y in place + $y->{_m} = $pi->{_m}; + $y->{_e} = $pi->{_e}; + $y->{_es} = $pi->{_es}; + # keep the sign of $y + return $y; + } + + return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; + + # Y X + # 0 -x result is PI + if ($y->is_zero()) + { + # calculate PI + my $pi = $self->bpi(@r); + # modify $y in place + $y->{_m} = $pi->{_m}; + $y->{_e} = $pi->{_e}; + $y->{_es} = $pi->{_es}; + $y->{sign} = '+'; + return $y; + } + + # Y X + # +y 0 result is PI/2 + # -y 0 result is -PI/2 + if ($x->is_zero()) + { + # calculate PI/2 + my $pi = $self->bpi(@r); + # modify $y in place + $y->{_m} = $pi->{_m}; + $y->{_e} = $pi->{_e}; + $y->{_es} = $pi->{_es}; + # -y => -PI/2, +y => PI/2 + $MBI->_div($y->{_m}, $MBI->_new(2)); + return $y; + } + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($y,@params) = $y->_find_round_parameters(@r); + + # error in _find_round_parameters? + return $y if $y->is_nan(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # inlined is_one() && is_one('-') + if ($MBI->_is_one($y->{_m}) && $MBI->_is_zero($y->{_e})) + { + # shortcut: 1 1 result is PI/4 + # inlined is_one() && is_one('-') + if ($MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) + { + # 1,1 => PI/4 + my $pi_4 = $self->bpi( $scale - 3); + # modify $y in place + $y->{_m} = $pi_4->{_m}; + $y->{_e} = $pi_4->{_e}; + $y->{_es} = $pi_4->{_es}; + # 1 1 => + + # -1 1 => - + # 1 -1 => - + # -1 -1 => + + $y->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; + $MBI->_div($y->{_m}, $MBI->_new(4)); + return $y; + } + # shortcut: 1 int(X) result is _atan_inv(X) + + # is integer + if ($x->{_es} eq '+') + { + my $x1 = $MBI->_copy($x->{_m}); + $MBI->_lsft($x1, $x->{_e},10) unless $MBI->_is_zero($x->{_e}); + + my ($a,$b) = $self->_atan_inv($x1, $scale); + my $y_sign = $y->{sign}; + # calculate A/B + $y->bone(); $y->{_m} = $a; my $y_d = $self->bone(); $y_d->{_m} = $b; + $y->bdiv($y_d, @r); + $y->{sign} = $y_sign; + return $y; + } + } + + # handle all other cases + # X Y + # +x +y 0 to PI/2 + # -x +y PI/2 to PI + # +x -y 0 to -PI/2 + # -x -y -PI/2 to -PI + + my $y_sign = $y->{sign}; + + # divide $x by $y + $y->bdiv($x, $scale) unless $x->is_one(); + $y->batan(@r); + + # restore sign + $y->{sign} = $y_sign; + + $y; + } + +sub batan + { + # Calculate a arcus tangens of x. + my ($x,@r) = @_; + my $self = ref($x); + + # taylor: x^3 x^5 x^7 x^9 + # atan = x - --- + --- - --- + --- ... + # 3 5 7 9 + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + # constant object or error in _find_round_parameters? + return $x if $x->modify('batan') || $x->is_nan(); + + if ($x->{sign} =~ /^[+-]inf\z/) + { + # +inf result is PI/2 + # -inf result is -PI/2 + # calculate PI/2 + my $pi = $self->bpi(@r); + # modify $x in place + $x->{_m} = $pi->{_m}; + $x->{_e} = $pi->{_e}; + $x->{_es} = $pi->{_es}; + # -y => -PI/2, +y => PI/2 + $x->{sign} = substr($x->{sign},0,1); # +inf => + + $MBI->_div($x->{_m}, $MBI->_new(2)); + return $x; + } + + return $x->bzero(@r) if $x->is_zero(); + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # disable P + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it is not + # enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + # 1 or -1 => PI/4 + # inlined is_one() && is_one('-') + if ($MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) + { + my $pi = $self->bpi($scale - 3); + # modify $x in place + $x->{_m} = $pi->{_m}; + $x->{_e} = $pi->{_e}; + $x->{_es} = $pi->{_es}; + # leave the sign of $x alone (+1 => +PI/4, -1 => -PI/4) + $MBI->_div($x->{_m}, $MBI->_new(4)); + return $x; + } + + # This series is only valid if -1 < x < 1, so for other x we need to + # to calculate PI/2 - atan(1/x): + my $one = $MBI->_new(1); + my $pi = undef; + if ($x->{_es} eq '+' && ($MBI->_acmp($x->{_m},$one) >= 0)) + { + # calculate PI/2 + $pi = $self->bpi($scale - 3); + $MBI->_div($pi->{_m}, $MBI->_new(2)); + # calculate 1/$x: + my $x_copy = $x->copy(); + # modify $x in place + $x->bone(); $x->bdiv($x_copy,$scale); + } + + # when user set globals, they would interfere with our calculation, so + # disable them and later re-enable them + no strict 'refs'; + my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; + my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; + # we also need to disable any set A or P on $x (_find_round_parameters took + # them already into account), since these would interfere, too + delete $x->{_a}; delete $x->{_p}; + # need to disable $upgrade in BigInt, to avoid deep recursion + local $Math::BigInt::upgrade = undef; + + my $last = 0; + my $over = $x * $x; # X ^ 2 + my $x2 = $over->copy(); # X ^ 2; difference between terms + $over->bmul($x); # X ^ 3 as starting value + my $sign = 1; # start with -= + my $below = $self->new(3); + my $two = $self->new(2); + delete $x->{_a}; delete $x->{_p}; + + my $limit = $self->new("1E-". ($scale-1)); + #my $steps = 0; + while (3 < 5) + { + # we calculate the next term, and add it to the last + # when the next term is below our limit, it won't affect the outcome + # anymore, so we stop: + my $next = $over->copy()->bdiv($below,$scale); + last if $next->bacmp($limit) <= 0; + + if ($sign == 0) + { + $x->badd($next); + } + else + { + $x->bsub($next); + } + $sign = 1-$sign; # alternate + # calculate things for the next term + $over->bmul($x2); # $x*$x + $below->badd($two); # n += 2 + } + + if (defined $pi) + { + my $x_copy = $x->copy(); + # modify $x in place + $x->{_m} = $pi->{_m}; + $x->{_e} = $pi->{_e}; + $x->{_es} = $pi->{_es}; + # PI/2 - $x + $x->bsub($x_copy); + } + + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + # restore globals + $$abr = $ab; $$pbr = $pb; + $x; + } + +############################################################################### +# rounding functions + +sub bfround + { + # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.' + # $n == 0 means round to integer + # expects and returns normalized numbers! + my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x); + + my ($scale,$mode) = $x->_scale_p(@_); + return $x if !defined $scale || $x->modify('bfround'); # no-op + + # never round a 0, +-inf, NaN + if ($x->is_zero()) + { + $x->{_p} = $scale if !defined $x->{_p} || $x->{_p} < $scale; # -3 < -2 + return $x; + } + return $x if $x->{sign} !~ /^[+-]$/; + + # don't round if x already has lower precision + return $x if (defined $x->{_p} && $x->{_p} < 0 && $scale < $x->{_p}); + + $x->{_p} = $scale; # remember round in any case + delete $x->{_a}; # and clear A + if ($scale < 0) + { + # round right from the '.' + + return $x if $x->{_es} eq '+'; # e >= 0 => nothing to round + + $scale = -$scale; # positive for simplicity + my $len = $MBI->_len($x->{_m}); # length of mantissa + + # the following poses a restriction on _e, but if _e is bigger than a + # scalar, you got other problems (memory etc) anyway + my $dad = -(0+ ($x->{_es}.$MBI->_num($x->{_e}))); # digits after dot + my $zad = 0; # zeros after dot + $zad = $dad - $len if (-$dad < -$len); # for 0.00..00xxx style + + # print "scale $scale dad $dad zad $zad len $len\n"; + # number bsstr len zad dad + # 0.123 123e-3 3 0 3 + # 0.0123 123e-4 3 1 4 + # 0.001 1e-3 1 2 3 + # 1.23 123e-2 3 0 2 + # 1.2345 12345e-4 5 0 4 + + # do not round after/right of the $dad + return $x if $scale > $dad; # 0.123, scale >= 3 => exit + + # round to zero if rounding inside the $zad, but not for last zero like: + # 0.0065, scale -2, round last '0' with following '65' (scale == zad case) + return $x->bzero() if $scale < $zad; + if ($scale == $zad) # for 0.006, scale -3 and trunc + { + $scale = -$len; + } + else + { + # adjust round-point to be inside mantissa + if ($zad != 0) + { + $scale = $scale-$zad; + } + else + { + my $dbd = $len - $dad; $dbd = 0 if $dbd < 0; # digits before dot + $scale = $dbd+$scale; + } + } + } + else + { + # round left from the '.' + + # 123 => 100 means length(123) = 3 - $scale (2) => 1 + + my $dbt = $MBI->_len($x->{_m}); + # digits before dot + my $dbd = $dbt + ($x->{_es} . $MBI->_num($x->{_e})); + # should be the same, so treat it as this + $scale = 1 if $scale == 0; + # shortcut if already integer + return $x if $scale == 1 && $dbt <= $dbd; + # maximum digits before dot + ++$dbd; + + if ($scale > $dbd) + { + # not enough digits before dot, so round to zero + return $x->bzero; + } + elsif ( $scale == $dbd ) + { + # maximum + $scale = -$dbt; + } + else + { + $scale = $dbd - $scale; + } + } + # pass sign to bround for rounding modes '+inf' and '-inf' + my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt'; + $m->bround($scale,$mode); + $x->{_m} = $m->{value}; # get our mantissa back + $x->bnorm(); + } + +sub bround + { + # accuracy: preserve $N digits, and overwrite the rest with 0's + my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x); + + if (($_[0] || 0) < 0) + { + require Carp; Carp::croak ('bround() needs positive accuracy'); + } + + my ($scale,$mode) = $x->_scale_a(@_); + return $x if !defined $scale || $x->modify('bround'); # no-op + + # scale is now either $x->{_a}, $accuracy, or the user parameter + # test whether $x already has lower accuracy, do nothing in this case + # but do round if the accuracy is the same, since a math operation might + # want to round a number with A=5 to 5 digits afterwards again + return $x if defined $x->{_a} && $x->{_a} < $scale; + + # scale < 0 makes no sense + # scale == 0 => keep all digits + # never round a +-inf, NaN + return $x if ($scale <= 0) || $x->{sign} !~ /^[+-]$/; + + # 1: never round a 0 + # 2: if we should keep more digits than the mantissa has, do nothing + if ($x->is_zero() || $MBI->_len($x->{_m}) <= $scale) + { + $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; + return $x; + } + + # pass sign to bround for '+inf' and '-inf' rounding modes + my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt'; + + $m->bround($scale,$mode); # round mantissa + $x->{_m} = $m->{value}; # get our mantissa back + $x->{_a} = $scale; # remember rounding + delete $x->{_p}; # and clear P + $x->bnorm(); # del trailing zeros gen. by bround() + } + +sub bfloor + { + # round towards minus infinity + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bfloor'); + + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + # if $x has digits after dot + if ($x->{_es} eq '-') + { + $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot + $x->{_e} = $MBI->_zero(); # trunc/norm + $x->{_es} = '+'; # abs e + $MBI->_inc($x->{_m}) if $x->{sign} eq '-'; # increment if negative + } + $x->round($a,$p,$r); + } + +sub bceil + { + # round towards plus infinity + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bceil'); + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + # if $x has digits after dot + if ($x->{_es} eq '-') + { + $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot + $x->{_e} = $MBI->_zero(); # trunc/norm + $x->{_es} = '+'; # abs e + if ($x->{sign} eq '+') { + $MBI->_inc($x->{_m}); # increment if positive + } else { + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # avoid -0 + } + } + $x->round($a,$p,$r); + } + +sub bint + { + # round towards zero + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->modify('bint'); + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + # if $x has digits after the decimal point + if ($x->{_es} eq '-') + { + $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot + $x->{_e} = $MBI->_zero(); # truncate/normalize + $x->{_es} = '+'; # abs e + $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # avoid -0 + } + $x->round($a,$p,$r); + } + +sub brsft + { + # shift right by $y (divide by power of $n) + + # set up parameters + my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('brsft'); + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + $n = 2 if !defined $n; $n = $self->new($n); + + # negative amount? + return $x->blsft($y->copy()->babs(),$n) if $y->{sign} =~ /^-/; + + # the following call to bdiv() will return either quo or (quo,remainder): + $x->bdiv($n->bpow($y),$a,$p,$r,$y); + } + +sub blsft + { + # shift left by $y (multiply by power of $n) + + # set up parameters + my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_); + } + + return $x if $x->modify('blsft'); + return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf + + $n = 2 if !defined $n; $n = $self->new($n); + + # negative amount? + return $x->brsft($y->copy()->babs(),$n) if $y->{sign} =~ /^-/; + + $x->bmul($n->bpow($y),$a,$p,$r,$y); + } + +############################################################################### + +sub DESTROY + { + # going through AUTOLOAD for every DESTROY is costly, avoid it by empty sub + } + +sub AUTOLOAD + { + # make fxxx and bxxx both work by selectively mapping fxxx() to MBF::bxxx() + # or falling back to MBI::bxxx() + my $name = $AUTOLOAD; + + $name =~ s/(.*):://; # split package + my $c = $1 || $class; + no strict 'refs'; + $c->import() if $IMPORT == 0; + if (!_method_alias($name)) + { + if (!defined $name) + { + # delayed load of Carp and avoid recursion + require Carp; + Carp::croak ("$c: Can't call a method without name"); + } + if (!_method_hand_up($name)) + { + # delayed load of Carp and avoid recursion + require Carp; + Carp::croak ("Can't call $c\-\>$name, not a valid method"); + } + # try one level up, but subst. bxxx() for fxxx() since MBI only got bxxx() + $name =~ s/^f/b/; + return &{"Math::BigInt"."::$name"}(@_); + } + my $bname = $name; $bname =~ s/^f/b/; + $c .= "::$name"; + *{$c} = \&{$bname}; + &{$c}; # uses @_ + } + +sub exponent + { + # return a copy of the exponent + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + my $s = $x->{sign}; $s =~ s/^[+-]//; + return Math::BigInt->new($s); # -inf, +inf => +inf + } + Math::BigInt->new( $x->{_es} . $MBI->_str($x->{_e})); + } + +sub mantissa + { + # return a copy of the mantissa + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + my $s = $x->{sign}; $s =~ s/^[+]//; + return Math::BigInt->new($s); # -inf, +inf => +inf + } + my $m = Math::BigInt->new( $MBI->_str($x->{_m})); + $m->bneg() if $x->{sign} eq '-'; + + $m; + } + +sub parts + { + # return a copy of both the exponent and the mantissa + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + my $s = $x->{sign}; $s =~ s/^[+]//; my $se = $s; $se =~ s/^[-]//; + return ($self->new($s),$self->new($se)); # +inf => inf and -inf,+inf => inf + } + my $m = Math::BigInt->bzero(); + $m->{value} = $MBI->_copy($x->{_m}); + $m->bneg() if $x->{sign} eq '-'; + ($m, Math::BigInt->new( $x->{_es} . $MBI->_num($x->{_e}) )); + } + +############################################################################## +# private stuff (internal use only) + +sub import + { + my $self = shift; + my $l = scalar @_; + my $lib = ''; my @a; + my $lib_kind = 'try'; + $IMPORT=1; + for ( my $i = 0; $i < $l ; $i++) + { + if ( $_[$i] eq ':constant' ) + { + # This causes overlord er load to step in. 'binary' and 'integer' + # are handled by BigInt. + overload::constant float => sub { $self->new(shift); }; + } + elsif ($_[$i] eq 'upgrade') + { + # this causes upgrading + $upgrade = $_[$i+1]; # or undef to disable + $i++; + } + elsif ($_[$i] eq 'downgrade') + { + # this causes downgrading + $downgrade = $_[$i+1]; # or undef to disable + $i++; + } + elsif ($_[$i] =~ /^(lib|try|only)\z/) + { + # alternative library + $lib = $_[$i+1] || ''; # default Calc + $lib_kind = $1; # lib, try or only + $i++; + } + elsif ($_[$i] eq 'with') + { + # alternative class for our private parts() + # XXX: no longer supported + # $MBI = $_[$i+1] || 'Math::BigInt'; + $i++; + } + else + { + push @a, $_[$i]; + } + } + + $lib =~ tr/a-zA-Z0-9,://cd; # restrict to sane characters + # let use Math::BigInt lib => 'GMP'; use Math::BigFloat; still work + my $mbilib = eval { Math::BigInt->config()->{lib} }; + if ((defined $mbilib) && ($MBI eq 'Math::BigInt::Calc')) + { + # MBI already loaded + Math::BigInt->import( $lib_kind, "$lib,$mbilib", 'objectify'); + } + else + { + # MBI not loaded, or with ne "Math::BigInt::Calc" + $lib .= ",$mbilib" if defined $mbilib; + $lib =~ s/^,//; # don't leave empty + + # replacement library can handle lib statement, but also could ignore it + + # Perl < 5.6.0 dies with "out of memory!" when eval() and ':constant' is + # used in the same script, or eval inside import(). So we require MBI: + require Math::BigInt; + Math::BigInt->import( $lib_kind => $lib, 'objectify' ); + } + if ($@) + { + require Carp; Carp::croak ("Couldn't load $lib: $! $@"); + } + # find out which one was actually loaded + $MBI = Math::BigInt->config()->{lib}; + + # register us with MBI to get notified of future lib changes + Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } ); + + $self->export_to_level(1,$self,@a); # export wanted functions + } + +sub bnorm + { + # adjust m and e so that m is smallest possible + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/; # inf, nan etc + + my $zeros = $MBI->_zeros($x->{_m}); # correct for trailing zeros + if ($zeros != 0) + { + my $z = $MBI->_new($zeros); + $x->{_m} = $MBI->_rsft ($x->{_m}, $z, 10); + if ($x->{_es} eq '-') + { + if ($MBI->_acmp($x->{_e},$z) >= 0) + { + $x->{_e} = $MBI->_sub ($x->{_e}, $z); + $x->{_es} = '+' if $MBI->_is_zero($x->{_e}); + } + else + { + $x->{_e} = $MBI->_sub ( $MBI->_copy($z), $x->{_e}); + $x->{_es} = '+'; + } + } + else + { + $x->{_e} = $MBI->_add ($x->{_e}, $z); + } + } + else + { + # $x can only be 0Ey if there are no trailing zeros ('0' has 0 trailing + # zeros). So, for something like 0Ey, set y to 1, and -0 => +0 + $x->{sign} = '+', $x->{_es} = '+', $x->{_e} = $MBI->_one() + if $MBI->_is_zero($x->{_m}); + } + + $x; # MBI bnorm is no-op, so do not call it + } + +############################################################################## + +sub as_hex + { + # return number as hexadecimal string (only for integers defined) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + return '0x0' if $x->is_zero(); + + return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? + + my $z = $MBI->_copy($x->{_m}); + if (! $MBI->_is_zero($x->{_e})) # > 0 + { + $MBI->_lsft( $z, $x->{_e},10); + } + $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); + $z->as_hex(); + } + +sub as_bin + { + # return number as binary digit string (only for integers defined) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + return '0b0' if $x->is_zero(); + + return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? + + my $z = $MBI->_copy($x->{_m}); + if (! $MBI->_is_zero($x->{_e})) # > 0 + { + $MBI->_lsft( $z, $x->{_e},10); + } + $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); + $z->as_bin(); + } + +sub as_oct + { + # return number as octal digit string (only for integers defined) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + return '0' if $x->is_zero(); + + return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? + + my $z = $MBI->_copy($x->{_m}); + if (! $MBI->_is_zero($x->{_e})) # > 0 + { + $MBI->_lsft( $z, $x->{_e},10); + } + $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); + $z->as_oct(); + } + +sub as_number + { + # return copy as a bigint representation of this BigFloat number + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x if $x->modify('as_number'); + + if (!$x->isa('Math::BigFloat')) + { + # if the object can as_number(), use it + return $x->as_number() if $x->can('as_number'); + # otherwise, get us a float and then a number + $x = $x->can('as_float') ? $x->as_float() : $self->new(0+"$x"); + } + + return Math::BigInt->binf($x->sign()) if $x->is_inf(); + return Math::BigInt->bnan() if $x->is_nan(); + + my $z = $MBI->_copy($x->{_m}); + if ($x->{_es} eq '-') # < 0 + { + $MBI->_rsft( $z, $x->{_e},10); + } + elsif (! $MBI->_is_zero($x->{_e})) # > 0 + { + $MBI->_lsft( $z, $x->{_e},10); + } + $z = Math::BigInt->new( $x->{sign} . $MBI->_str($z)); + $z; + } + +sub length + { + my $x = shift; + my $class = ref($x) || $x; + $x = $class->new(shift) unless ref($x); + + return 1 if $MBI->_is_zero($x->{_m}); + + my $len = $MBI->_len($x->{_m}); + $len += $MBI->_num($x->{_e}) if $x->{_es} eq '+'; + if (wantarray()) + { + my $t = 0; + $t = $MBI->_num($x->{_e}) if $x->{_es} eq '-'; + return ($len, $t); + } + $len; + } + +1; + +__END__ + +=pod + +=head1 NAME + +Math::BigFloat - Arbitrary size floating point math package + +=head1 SYNOPSIS + + use Math::BigFloat; + + # Number creation + my $x = Math::BigFloat->new($str); # defaults to 0 + my $y = $x->copy(); # make a true copy + my $nan = Math::BigFloat->bnan(); # create a NotANumber + my $zero = Math::BigFloat->bzero(); # create a +0 + my $inf = Math::BigFloat->binf(); # create a +inf + my $inf = Math::BigFloat->binf('-'); # create a -inf + my $one = Math::BigFloat->bone(); # create a +1 + my $mone = Math::BigFloat->bone('-'); # create a -1 + + my $pi = Math::BigFloat->bpi(100); # PI to 100 digits + + # the following examples compute their result to 100 digits accuracy: + my $cos = Math::BigFloat->new(1)->bcos(100); # cosinus(1) + my $sin = Math::BigFloat->new(1)->bsin(100); # sinus(1) + my $atan = Math::BigFloat->new(1)->batan(100); # arcus tangens(1) + + my $atan2 = Math::BigFloat->new( 1 )->batan2( 1 ,100); # batan(1) + my $atan2 = Math::BigFloat->new( 1 )->batan2( 8 ,100); # batan(1/8) + my $atan2 = Math::BigFloat->new( -2 )->batan2( 1 ,100); # batan(-2) + + # Testing + $x->is_zero(); # true if arg is +0 + $x->is_nan(); # true if arg is NaN + $x->is_one(); # true if arg is +1 + $x->is_one('-'); # true if arg is -1 + $x->is_odd(); # true if odd, false for even + $x->is_even(); # true if even, false for odd + $x->is_pos(); # true if >= 0 + $x->is_neg(); # true if < 0 + $x->is_inf(sign); # true if +inf, or -inf (default is '+') + + $x->bcmp($y); # compare numbers (undef,<0,=0,>0) + $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) + $x->sign(); # return the sign, either +,- or NaN + $x->digit($n); # return the nth digit, counting from right + $x->digit(-$n); # return the nth digit, counting from left + + # The following all modify their first argument. If you want to pre- + # serve $x, use $z = $x->copy()->bXXX($y); See under L for + # necessary when mixing $a = $b assignments with non-overloaded math. + + # set + $x->bzero(); # set $i to 0 + $x->bnan(); # set $i to NaN + $x->bone(); # set $x to +1 + $x->bone('-'); # set $x to -1 + $x->binf(); # set $x to inf + $x->binf('-'); # set $x to -inf + + $x->bneg(); # negation + $x->babs(); # absolute value + $x->bnorm(); # normalize (no-op) + $x->bnot(); # two's complement (bit wise not) + $x->binc(); # increment x by 1 + $x->bdec(); # decrement x by 1 + + $x->badd($y); # addition (add $y to $x) + $x->bsub($y); # subtraction (subtract $y from $x) + $x->bmul($y); # multiplication (multiply $x by $y) + $x->bdiv($y); # divide, set $x to quotient + # return (quo,rem) or quo if scalar + + $x->bmod($y); # modulus ($x % $y) + $x->bpow($y); # power of arguments ($x ** $y) + $x->bmodpow($exp,$mod); # modular exponentiation (($num**$exp) % $mod)) + $x->blsft($y, $n); # left shift by $y places in base $n + $x->brsft($y, $n); # right shift by $y places in base $n + # returns (quo,rem) or quo if in scalar context + + $x->blog(); # logarithm of $x to base e (Euler's number) + $x->blog($base); # logarithm of $x to base $base (f.i. 2) + $x->bexp(); # calculate e ** $x where e is Euler's number + + $x->band($y); # bit-wise and + $x->bior($y); # bit-wise inclusive or + $x->bxor($y); # bit-wise exclusive or + $x->bnot(); # bit-wise not (two's complement) + + $x->bsqrt(); # calculate square-root + $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root) + $x->bfac(); # factorial of $x (1*2*3*4*..$x) + + $x->bround($N); # accuracy: preserve $N digits + $x->bfround($N); # precision: round to the $Nth digit + + $x->bfloor(); # return integer less or equal than $x + $x->bceil(); # return integer greater or equal than $x + $x->bint(); # round towards zero + + # The following do not modify their arguments: + + bgcd(@values); # greatest common divisor + blcm(@values); # lowest common multiplicator + + $x->bstr(); # return string + $x->bsstr(); # return string in scientific notation + + $x->as_int(); # return $x as BigInt + $x->exponent(); # return exponent as BigInt + $x->mantissa(); # return mantissa as BigInt + $x->parts(); # return (mantissa,exponent) as BigInt + + $x->length(); # number of digits (w/o sign and '.') + ($l,$f) = $x->length(); # number of digits, and length of fraction + + $x->precision(); # return P of $x (or global, if P of $x undef) + $x->precision($n); # set P of $x to $n + $x->accuracy(); # return A of $x (or global, if A of $x undef) + $x->accuracy($n); # set A $x to $n + + # these get/set the appropriate global value for all BigFloat objects + Math::BigFloat->precision(); # Precision + Math::BigFloat->accuracy(); # Accuracy + Math::BigFloat->round_mode(); # rounding mode + +=head1 DESCRIPTION + +All operators (including basic math operations) are overloaded if you +declare your big floating point numbers as + + $i = new Math::BigFloat '12_3.456_789_123_456_789E-2'; + +Operations with overloaded operators preserve the arguments, which is +exactly what you expect. + +=head2 Input + +Input to these routines are either BigFloat objects, or strings of the +following four forms: + +=over + +=item * + +C + +=item * + +C + +=item * + +C + +=item * + +C + +=back + +all with optional leading and trailing zeros and/or spaces. Additionally, +numbers are allowed to have an underscore between any two digits. + +Empty strings as well as other illegal numbers results in 'NaN'. + +bnorm() on a BigFloat object is now effectively a no-op, since the numbers +are always stored in normalized form. On a string, it creates a BigFloat +object. + +=head2 Output + +Output values are BigFloat objects (normalized), except for bstr() and bsstr(). + +The string output will always have leading and trailing zeros stripped and drop +a plus sign. C will give you always the form with a decimal point, +while C (s for scientific) gives you the scientific notation. + + Input bstr() bsstr() + '-0' '0' '0E1' + ' -123 123 123' '-123123123' '-123123123E0' + '00.0123' '0.0123' '123E-4' + '123.45E-2' '1.2345' '12345E-4' + '10E+3' '10000' '1E4' + +Some routines (C, C, C, C, +C) return true or false, while others (C, C) +return either undef, <0, 0 or >0 and are suited for sort. + +Actual math is done by using the class defined with C<< with => Class; >> +(which defaults to BigInts) to represent the mantissa and exponent. + +The sign C is stored separately. The string 'NaN' is used to +represent the result when input arguments are not numbers, and 'inf' and +'-inf' are used to represent positive and negative infinity, respectively. + +=head2 mantissa(), exponent() and parts() + +mantissa() and exponent() return the said parts of the BigFloat +as BigInts such that: + + $m = $x->mantissa(); + $e = $x->exponent(); + $y = $m * ( 10 ** $e ); + print "ok\n" if $x == $y; + +C<< ($m,$e) = $x->parts(); >> is just a shortcut giving you both of them. + +A zero is represented and returned as C<0E1>, B C<0E0> (after Knuth). + +Currently the mantissa is reduced as much as possible, favouring higher +exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0). +This might change in the future, so do not depend on it. + +=head2 Accuracy vs. Precision + +See also: L. + +Math::BigFloat supports both precision (rounding to a certain place before or +after the dot) and accuracy (rounding to a certain number of digits). For a +full documentation, examples and tips on these topics please see the large +section about rounding in L. + +Since things like C or C<1 / 3> must presented with a limited +accuracy lest a operation consumes all resources, each operation produces +no more than the requested number of digits. + +If there is no global precision or accuracy set, B the operation in +question was not called with a requested precision or accuracy, B the +input $x has no accuracy or precision set, then a fallback parameter will +be used. For historical reasons, it is called C and can be accessed +via: + + $d = Math::BigFloat->div_scale(); # query + Math::BigFloat->div_scale($n); # set to $n digits + +The default value for C is 40. + +In case the result of one operation has more digits than specified, +it is rounded. The rounding mode taken is either the default mode, or the one +supplied to the operation after the I: + + $x = Math::BigFloat->new(2); + Math::BigFloat->accuracy(5); # 5 digits max + $y = $x->copy()->bdiv(3); # will give 0.66667 + $y = $x->copy()->bdiv(3,6); # will give 0.666667 + $y = $x->copy()->bdiv(3,6,undef,'odd'); # will give 0.666667 + Math::BigFloat->round_mode('zero'); + $y = $x->copy()->bdiv(3,6); # will also give 0.666667 + +Note that C<< Math::BigFloat->accuracy() >> and C<< Math::BigFloat->precision() >> +set the global variables, and thus B newly created number will be subject +to the global rounding B. This means that in the examples above, the +C<3> as argument to C will also get an accuracy of B<5>. + +It is less confusing to either calculate the result fully, and afterwards +round it explicitly, or use the additional parameters to the math +functions like so: + + use Math::BigFloat; + $x = Math::BigFloat->new(2); + $y = $x->copy()->bdiv(3); + print $y->bround(5),"\n"; # will give 0.66667 + + or + + use Math::BigFloat; + $x = Math::BigFloat->new(2); + $y = $x->copy()->bdiv(3,5); # will give 0.66667 + print "$y\n"; + +=head2 Rounding + +=over + +=item ffround ( +$scale ) + +Rounds to the $scale'th place left from the '.', counting from the dot. +The first digit is numbered 1. + +=item ffround ( -$scale ) + +Rounds to the $scale'th place right from the '.', counting from the dot. + +=item ffround ( 0 ) + +Rounds to an integer. + +=item fround ( +$scale ) + +Preserves accuracy to $scale digits from the left (aka significant digits) +and pads the rest with zeros. If the number is between 1 and -1, the +significant digits count from the first non-zero after the '.' + +=item fround ( -$scale ) and fround ( 0 ) + +These are effectively no-ops. + +=back + +All rounding functions take as a second parameter a rounding mode from one of +the following: 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'. + +The default rounding mode is 'even'. By using +C<< Math::BigFloat->round_mode($round_mode); >> you can get and set the default +mode for subsequent rounding. The usage of C<$Math::BigFloat::$round_mode> is +no longer supported. +The second parameter to the round functions then overrides the default +temporarily. + +The C function returns a BigInt from a Math::BigFloat. It uses +'trunc' as rounding mode to make it equivalent to: + + $x = 2.5; + $y = int($x) + 2; + +You can override this by passing the desired rounding mode as parameter to +C: + + $x = Math::BigFloat->new(2.5); + $y = $x->as_number('odd'); # $y = 3 + +=head1 METHODS + +Math::BigFloat supports all methods that Math::BigInt supports, except it +calculates non-integer results when possible. Please see L +for a full description of each method. Below are just the most important +differences: + +=over + +=item accuracy() + + $x->accuracy(5); # local for $x + CLASS->accuracy(5); # global for all members of CLASS + # Note: This also applies to new()! + + $A = $x->accuracy(); # read out accuracy that affects $x + $A = CLASS->accuracy(); # read out global accuracy + +Set or get the global or local accuracy, aka how many significant digits the +results have. If you set a global accuracy, then this also applies to new()! + +Warning! The accuracy I, e.g. once you created a number under the +influence of C<< CLASS->accuracy($A) >>, all results from math operations with +that number will also be rounded. + +In most cases, you should probably round the results explicitly using one of +L, L or L or by passing the desired accuracy +to the math operation as additional parameter: + + my $x = Math::BigInt->new(30000); + my $y = Math::BigInt->new(7); + print scalar $x->copy()->bdiv($y, 2); # print 4300 + print scalar $x->copy()->bdiv($y)->bround(2); # print 4300 + +=item precision() + + $x->precision(-2); # local for $x, round at the second + # digit right of the dot + $x->precision(2); # ditto, round at the second digit + # left of the dot + + CLASS->precision(5); # Global for all members of CLASS + # This also applies to new()! + CLASS->precision(-5); # ditto + + $P = CLASS->precision(); # read out global precision + $P = $x->precision(); # read out precision that affects $x + +Note: You probably want to use L instead. With L you +set the number of digits each result should have, with L you +set the place where to round! + +=item bdiv() + + $q = $x->bdiv($y); + ($q, $r) = $x->bdiv($y); + +In scalar context, divides $x by $y and returns the result to the given or +default accuracy/precision. In list context, does floored division +(F-division), returning an integer $q and a remainder $r so that $x = $q * $y + +$r. The remainer (modulo) is equal to what is returned by C<$x->bmod($y)>. + +=item bmod() + + $x->bmod($y); + +Returns $x modulo $y. When $x is finite, and $y is finite and non-zero, the +result is identical to the remainder after floored division (F-division). If, +in addition, both $x and $y are integers, the result is identical to the result +from Perl's % operator. + +=item bexp() + + $x->bexp($accuracy); # calculate e ** X + +Calculates the expression C where C is Euler's number. + +This method was added in v1.82 of Math::BigInt (April 2007). + +=item bnok() + + $x->bnok($y); # x over y (binomial coefficient n over k) + +Calculates the binomial coefficient n over k, also called the "choose" +function. The result is equivalent to: + + ( n ) n! + | - | = ------- + ( k ) k!(n-k)! + +This method was added in v1.84 of Math::BigInt (April 2007). + +=item bpi() + + print Math::BigFloat->bpi(100), "\n"; + +Calculate PI to N digits (including the 3 before the dot). The result is +rounded according to the current rounding mode, which defaults to "even". + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item bcos() + + my $x = Math::BigFloat->new(1); + print $x->bcos(100), "\n"; + +Calculate the cosinus of $x, modifying $x in place. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item bsin() + + my $x = Math::BigFloat->new(1); + print $x->bsin(100), "\n"; + +Calculate the sinus of $x, modifying $x in place. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item batan2() + + my $y = Math::BigFloat->new(2); + my $x = Math::BigFloat->new(3); + print $y->batan2($x), "\n"; + +Calculate the arcus tanges of C<$y> divided by C<$x>, modifying $y in place. +See also L. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item batan() + + my $x = Math::BigFloat->new(1); + print $x->batan(100), "\n"; + +Calculate the arcus tanges of $x, modifying $x in place. See also L. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item bmuladd() + + $x->bmuladd($y,$z); + +Multiply $x by $y, and then add $z to the result. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=back + +=head1 Autocreating constants + +After C all the floating point constants +in the given scope are converted to C. This conversion +happens at compile time. + +In particular + + perl -MMath::BigFloat=:constant -e 'print 2E-100,"\n"' + +prints the value of C<2E-100>. Note that without conversion of +constants the expression 2E-100 will be calculated as normal floating point +number. + +Please note that ':constant' does not affect integer constants, nor binary +nor hexadecimal constants. Use L or L to get this to +work. + +=head2 Math library + +Math with the numbers is done (by default) by a module called +Math::BigInt::Calc. This is equivalent to saying: + + use Math::BigFloat lib => 'Calc'; + +You can change this by using: + + use Math::BigFloat lib => 'GMP'; + +B: General purpose packages should not be explicit about the library +to use; let the script author decide which is best. + +Note: The keyword 'lib' will warn when the requested library could not be +loaded. To suppress the warning use 'try' instead: + + use Math::BigFloat try => 'GMP'; + +If your script works with huge numbers and Calc is too slow for them, +you can also for the loading of one of these libraries and if none +of them can be used, the code will die: + + use Math::BigFloat only => 'GMP,Pari'; + +The following would first try to find Math::BigInt::Foo, then +Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: + + use Math::BigFloat lib => 'Foo,Math::BigInt::Bar'; + +See the respective low-level library documentation for further details. + +Please note that Math::BigFloat does B use the denoted library itself, +but it merely passes the lib argument to Math::BigInt. So, instead of the need +to do: + + use Math::BigInt lib => 'GMP'; + use Math::BigFloat; + +you can roll it all into one line: + + use Math::BigFloat lib => 'GMP'; + +It is also possible to just require Math::BigFloat: + + require Math::BigFloat; + +This will load the necessary things (like BigInt) when they are needed, and +automatically. + +See L for more details than you ever wanted to know about using +a different low-level library. + +=head2 Using Math::BigInt::Lite + +For backwards compatibility reasons it is still possible to +request a different storage class for use with Math::BigFloat: + + use Math::BigFloat with => 'Math::BigInt::Lite'; + +However, this request is ignored, as the current code now uses the low-level +math library for directly storing the number parts. + +=head1 EXPORTS + +C exports nothing by default, but can export the C method: + + use Math::BigFloat qw/bpi/; + + print bpi(10), "\n"; + +=head1 CAVEATS + +Do not try to be clever to insert some operations in between switching +libraries: + + require Math::BigFloat; + my $matter = Math::BigFloat->bone() + 4; # load BigInt and Calc + Math::BigFloat->import( lib => 'Pari' ); # load Pari, too + my $anti_matter = Math::BigFloat->bone()+4; # now use Pari + +This will create objects with numbers stored in two different backend libraries, +and B will happen when you use these together: + + my $flash_and_bang = $matter + $anti_matter; # Don't do this! + +=over + +=item stringify, bstr() + +Both stringify and bstr() now drop the leading '+'. The old code would return +'+1.23', the new returns '1.23'. See the documentation in L for +reasoning and details. + +=item bdiv() + +The following will probably not print what you expect: + + print $c->bdiv(123.456),"\n"; + +It prints both quotient and remainder since print works in list context. Also, +bdiv() will modify $c, so be careful. You probably want to use + + print $c / 123.456,"\n"; + # or if you want to modify $c: + print scalar $c->bdiv(123.456),"\n"; + +instead. + +=item brsft() + +The following will probably not print what you expect: + + my $c = Math::BigFloat->new('3.14159'); + print $c->brsft(3,10),"\n"; # prints 0.00314153.1415 + +It prints both quotient and remainder, since print calls C in list +context. Also, C<< $c->brsft() >> will modify $c, so be careful. +You probably want to use + + print scalar $c->copy()->brsft(3,10),"\n"; + # or if you really want to modify $c + print scalar $c->brsft(3,10),"\n"; + +instead. + +=item Modifying and = + +Beware of: + + $x = Math::BigFloat->new(5); + $y = $x; + +It will not do what you think, e.g. making a copy of $x. Instead it just makes +a second reference to the B object and stores it in $y. Thus anything +that modifies $x will modify $y (except overloaded math operators), and vice +versa. See L for details and how to avoid that. + +=item bpow() + +C now modifies the first argument, unlike the old code which left +it alone and only returned the result. This is to be consistent with +C etc. The first will modify $x, the second one won't: + + print bpow($x,$i),"\n"; # modify $x + print $x->bpow($i),"\n"; # ditto + print $x ** $i,"\n"; # leave $x alone + +=item precision() vs. accuracy() + +A common pitfall is to use L when you want to round a result to +a certain number of digits: + + use Math::BigFloat; + + Math::BigFloat->precision(4); # does not do what you + # think it does + my $x = Math::BigFloat->new(12345); # rounds $x to "12000"! + print "$x\n"; # print "12000" + my $y = Math::BigFloat->new(3); # rounds $y to "0"! + print "$y\n"; # print "0" + $z = $x / $y; # 12000 / 0 => NaN! + print "$z\n"; + print $z->precision(),"\n"; # 4 + +Replacing L with L is probably not what you want, either: + + use Math::BigFloat; + + Math::BigFloat->accuracy(4); # enables global rounding: + my $x = Math::BigFloat->new(123456); # rounded immediately + # to "12350" + print "$x\n"; # print "123500" + my $y = Math::BigFloat->new(3); # rounded to "3 + print "$y\n"; # print "3" + print $z = $x->copy()->bdiv($y),"\n"; # 41170 + print $z->accuracy(),"\n"; # 4 + +What you want to use instead is: + + use Math::BigFloat; + + my $x = Math::BigFloat->new(123456); # no rounding + print "$x\n"; # print "123456" + my $y = Math::BigFloat->new(3); # no rounding + print "$y\n"; # print "3" + print $z = $x->copy()->bdiv($y,4),"\n"; # 41150 + print $z->accuracy(),"\n"; # undef + +In addition to computing what you expected, the last example also does B +"taint" the result with an accuracy or precision setting, which would +influence any further operation. + +=back + +=head1 BUGS + +Please report any bugs or feature requests to +C, or through the web interface at +L +(requires login). +We will be notified, and then you'll automatically be notified of progress on +your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc Math::BigFloat + +You can also look for information at: + +=over 4 + +=item * RT: CPAN's request tracker + +L + +=item * AnnoCPAN: Annotated CPAN documentation + +L + +=item * CPAN Ratings + +L + +=item * Search CPAN + +L + +=item * CPAN Testers Matrix + +L + +=item * The Bignum mailing list + +=over 4 + +=item * Post to mailing list + +C + +=item * View mailing list + +L + +=item * Subscribe/Unsubscribe + +L + +=back + +=back + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 SEE ALSO + +L and L as well as the backends +L, L, and L. + +The pragmas L, L and L also might be of interest +because they solve the autoupgrading/downgrading issue, at least partly. + +=head1 AUTHORS + +Mark Biggar, overloaded interface by Ilya Zakharevich. +Completely rewritten by Tels L in 2001 - 2006, and still +at it in 2007. + +=cut diff --git a/cpan/Math-BigInt/lib/Math/BigInt.pm b/cpan/Math-BigInt/lib/Math/BigInt.pm new file mode 100644 index 0000000000..d990272d39 --- /dev/null +++ b/cpan/Math-BigInt/lib/Math/BigInt.pm @@ -0,0 +1,5556 @@ +package Math::BigInt; + +# +# "Mike had an infinite amount to do and a negative amount of time in which +# to do it." - Before and After +# + +# The following hash values are used: +# value: unsigned int with actual value (as a Math::BigInt::Calc or similar) +# sign : +,-,NaN,+inf,-inf +# _a : accuracy +# _p : precision +# _f : flags, used by MBF to flag parts of a float as untouchable + +# Remember not to take shortcuts ala $xs = $x->{value}; $CALC->foo($xs); since +# underlying lib might change the reference! + +my $class = "Math::BigInt"; +use 5.006002; + +$VERSION = '1.999701'; + +@ISA = qw(Exporter); +@EXPORT_OK = qw(objectify bgcd blcm); + +# _trap_inf and _trap_nan are internal and should never be accessed from the +# outside +use vars qw/$round_mode $accuracy $precision $div_scale $rnd_mode + $upgrade $downgrade $_trap_nan $_trap_inf/; +use strict; + +# Inside overload, the first arg is always an object. If the original code had +# it reversed (like $x = 2 * $y), then the third parameter is true. +# In some cases (like add, $x = $x + 2 is the same as $x = 2 + $x) this makes +# no difference, but in some cases it does. + +# For overloaded ops with only one argument we simple use $_[0]->copy() to +# preserve the argument. + +# Thus inheritance of overload operators becomes possible and transparent for +# our subclasses without the need to repeat the entire overload section there. + +# We register ops that are not registerable yet, so suppress warnings +{ no warnings; +use overload +'=' => sub { $_[0]->copy(); }, + +# some shortcuts for speed (assumes that reversed order of arguments is routed +# to normal '+' and we thus can always modify first arg. If this is changed, +# this breaks and must be adjusted.) +'+=' => sub { $_[0]->badd($_[1]); }, +'-=' => sub { $_[0]->bsub($_[1]); }, +'*=' => sub { $_[0]->bmul($_[1]); }, +'/=' => sub { scalar $_[0]->bdiv($_[1]); }, +'%=' => sub { $_[0]->bmod($_[1]); }, +'^=' => sub { $_[0]->bxor($_[1]); }, +'&=' => sub { $_[0]->band($_[1]); }, +'|=' => sub { $_[0]->bior($_[1]); }, + +'**=' => sub { $_[0]->bpow($_[1]); }, +'<<=' => sub { $_[0]->blsft($_[1]); }, +'>>=' => sub { $_[0]->brsft($_[1]); }, + +# not supported by Perl yet +'..' => \&_pointpoint, + +'<=>' => sub { my $rc = $_[2] ? + ref($_[0])->bcmp($_[1],$_[0]) : + $_[0]->bcmp($_[1]); + $rc = 1 unless defined $rc; + $rc <=> 0; + }, +# we need '>=' to get things like "1 >= NaN" right: +'>=' => sub { my $rc = $_[2] ? + ref($_[0])->bcmp($_[1],$_[0]) : + $_[0]->bcmp($_[1]); + # if there was a NaN involved, return false + return '' unless defined $rc; + $rc >= 0; + }, +'cmp' => sub { + $_[2] ? + "$_[1]" cmp $_[0]->bstr() : + $_[0]->bstr() cmp "$_[1]" }, + +'cos' => sub { $_[0]->copy->bcos(); }, +'sin' => sub { $_[0]->copy->bsin(); }, +'atan2' => sub { $_[2] ? + ref($_[0])->new($_[1])->batan2($_[0]) : + $_[0]->copy()->batan2($_[1]) }, + +# are not yet overloadable +#'hex' => sub { print "hex"; $_[0]; }, +#'oct' => sub { print "oct"; $_[0]; }, + +# log(N) is log(N, e), where e is Euler's number +'log' => sub { $_[0]->copy()->blog($_[1], undef); }, +'exp' => sub { $_[0]->copy()->bexp($_[1]); }, +'int' => sub { $_[0]->copy(); }, +'neg' => sub { $_[0]->copy()->bneg(); }, +'abs' => sub { $_[0]->copy()->babs(); }, +'sqrt' => sub { $_[0]->copy()->bsqrt(); }, +'~' => sub { $_[0]->copy()->bnot(); }, + +# for subtract it's a bit tricky to not modify b: b-a => -a+b +'-' => sub { my $c = $_[0]->copy; $_[2] ? + $c->bneg()->badd( $_[1]) : + $c->bsub( $_[1]) }, +'+' => sub { $_[0]->copy()->badd($_[1]); }, +'*' => sub { $_[0]->copy()->bmul($_[1]); }, + +'/' => sub { + $_[2] ? ref($_[0])->new($_[1])->bdiv($_[0]) : $_[0]->copy->bdiv($_[1]); + }, +'%' => sub { + $_[2] ? ref($_[0])->new($_[1])->bmod($_[0]) : $_[0]->copy->bmod($_[1]); + }, +'**' => sub { + $_[2] ? ref($_[0])->new($_[1])->bpow($_[0]) : $_[0]->copy->bpow($_[1]); + }, +'<<' => sub { + $_[2] ? ref($_[0])->new($_[1])->blsft($_[0]) : $_[0]->copy->blsft($_[1]); + }, +'>>' => sub { + $_[2] ? ref($_[0])->new($_[1])->brsft($_[0]) : $_[0]->copy->brsft($_[1]); + }, +'&' => sub { + $_[2] ? ref($_[0])->new($_[1])->band($_[0]) : $_[0]->copy->band($_[1]); + }, +'|' => sub { + $_[2] ? ref($_[0])->new($_[1])->bior($_[0]) : $_[0]->copy->bior($_[1]); + }, +'^' => sub { + $_[2] ? ref($_[0])->new($_[1])->bxor($_[0]) : $_[0]->copy->bxor($_[1]); + }, + +# can modify arg of ++ and --, so avoid a copy() for speed, but don't +# use $_[0]->bone(), it would modify $_[0] to be 1! +'++' => sub { $_[0]->binc() }, +'--' => sub { $_[0]->bdec() }, + +# if overloaded, O(1) instead of O(N) and twice as fast for small numbers +'bool' => sub { + # this kludge is needed for perl prior 5.6.0 since returning 0 here fails :-/ + # v5.6.1 dumps on this: return !$_[0]->is_zero() || undef; :-( + my $t = undef; + $t = 1 if !$_[0]->is_zero(); + $t; + }, + +# the original qw() does not work with the TIESCALAR below, why? +# Order of arguments insignificant +'""' => sub { $_[0]->bstr(); }, +'0+' => sub { $_[0]->numify(); } +; +} # no warnings scope + +############################################################################## +# global constants, flags and accessory + +# These vars are public, but their direct usage is not recommended, use the +# accessor methods instead + +$round_mode = 'even'; # one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common' +$accuracy = undef; +$precision = undef; +$div_scale = 40; + +$upgrade = undef; # default is no upgrade +$downgrade = undef; # default is no downgrade + +# These are internally, and not to be used from the outside at all + +$_trap_nan = 0; # are NaNs ok? set w/ config() +$_trap_inf = 0; # are infs ok? set w/ config() +my $nan = 'NaN'; # constants for easier life + +my $CALC = 'Math::BigInt::Calc'; # module to do the low level math + # default is Calc.pm +my $IMPORT = 0; # was import() called yet? + # used to make require work +my %WARN; # warn only once for low-level libs +my %CAN; # cache for $CALC->can(...) +my %CALLBACKS; # callbacks to notify on lib loads +my $EMU_LIB = 'Math/BigInt/CalcEmu.pm'; # emulate low-level math + +############################################################################## +# the old code had $rnd_mode, so we need to support it, too + +$rnd_mode = 'even'; +sub TIESCALAR { my ($class) = @_; bless \$round_mode, $class; } +sub FETCH { return $round_mode; } +sub STORE { $rnd_mode = $_[0]->round_mode($_[1]); } + +BEGIN + { + # tie to enable $rnd_mode to work transparently + tie $rnd_mode, 'Math::BigInt'; + + # set up some handy alias names + *as_int = \&as_number; + *is_pos = \&is_positive; + *is_neg = \&is_negative; + } + +############################################################################## + +sub round_mode + { + no strict 'refs'; + # make Class->round_mode() work + my $self = shift; + my $class = ref($self) || $self || __PACKAGE__; + if (defined $_[0]) + { + my $m = shift; + if ($m !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) + { + require Carp; Carp::croak ("Unknown round mode '$m'"); + } + return ${"${class}::round_mode"} = $m; + } + ${"${class}::round_mode"}; + } + +sub upgrade + { + no strict 'refs'; + # make Class->upgrade() work + my $self = shift; + my $class = ref($self) || $self || __PACKAGE__; + # need to set new value? + if (@_ > 0) + { + return ${"${class}::upgrade"} = $_[0]; + } + ${"${class}::upgrade"}; + } + +sub downgrade + { + no strict 'refs'; + # make Class->downgrade() work + my $self = shift; + my $class = ref($self) || $self || __PACKAGE__; + # need to set new value? + if (@_ > 0) + { + return ${"${class}::downgrade"} = $_[0]; + } + ${"${class}::downgrade"}; + } + +sub div_scale + { + no strict 'refs'; + # make Class->div_scale() work + my $self = shift; + my $class = ref($self) || $self || __PACKAGE__; + if (defined $_[0]) + { + if ($_[0] < 0) + { + require Carp; Carp::croak ('div_scale must be greater than zero'); + } + ${"${class}::div_scale"} = $_[0]; + } + ${"${class}::div_scale"}; + } + +sub accuracy + { + # $x->accuracy($a); ref($x) $a + # $x->accuracy(); ref($x) + # Class->accuracy(); class + # Class->accuracy($a); class $a + + my $x = shift; + my $class = ref($x) || $x || __PACKAGE__; + + no strict 'refs'; + # need to set new value? + if (@_ > 0) + { + my $a = shift; + # convert objects to scalars to avoid deep recursion. If object doesn't + # have numify(), then hopefully it will have overloading for int() and + # boolean test without wandering into a deep recursion path... + $a = $a->numify() if ref($a) && $a->can('numify'); + + if (defined $a) + { + # also croak on non-numerical + if (!$a || $a <= 0) + { + require Carp; + Carp::croak ('Argument to accuracy must be greater than zero'); + } + if (int($a) != $a) + { + require Carp; + Carp::croak ('Argument to accuracy must be an integer'); + } + } + if (ref($x)) + { + # $object->accuracy() or fallback to global + $x->bround($a) if $a; # not for undef, 0 + $x->{_a} = $a; # set/overwrite, even if not rounded + delete $x->{_p}; # clear P + $a = ${"${class}::accuracy"} unless defined $a; # proper return value + } + else + { + ${"${class}::accuracy"} = $a; # set global A + ${"${class}::precision"} = undef; # clear global P + } + return $a; # shortcut + } + + my $a; + # $object->accuracy() or fallback to global + $a = $x->{_a} if ref($x); + # but don't return global undef, when $x's accuracy is 0! + $a = ${"${class}::accuracy"} if !defined $a; + $a; + } + +sub precision + { + # $x->precision($p); ref($x) $p + # $x->precision(); ref($x) + # Class->precision(); class + # Class->precision($p); class $p + + my $x = shift; + my $class = ref($x) || $x || __PACKAGE__; + + no strict 'refs'; + if (@_ > 0) + { + my $p = shift; + # convert objects to scalars to avoid deep recursion. If object doesn't + # have numify(), then hopefully it will have overloading for int() and + # boolean test without wandering into a deep recursion path... + $p = $p->numify() if ref($p) && $p->can('numify'); + if ((defined $p) && (int($p) != $p)) + { + require Carp; Carp::croak ('Argument to precision must be an integer'); + } + if (ref($x)) + { + # $object->precision() or fallback to global + $x->bfround($p) if $p; # not for undef, 0 + $x->{_p} = $p; # set/overwrite, even if not rounded + delete $x->{_a}; # clear A + $p = ${"${class}::precision"} unless defined $p; # proper return value + } + else + { + ${"${class}::precision"} = $p; # set global P + ${"${class}::accuracy"} = undef; # clear global A + } + return $p; # shortcut + } + + my $p; + # $object->precision() or fallback to global + $p = $x->{_p} if ref($x); + # but don't return global undef, when $x's precision is 0! + $p = ${"${class}::precision"} if !defined $p; + $p; + } + +sub config + { + # return (or set) configuration data as hash ref + my $class = shift || 'Math::BigInt'; + + no strict 'refs'; + if (@_ > 1 || (@_ == 1 && (ref($_[0]) eq 'HASH'))) + { + # try to set given options as arguments from hash + + my $args = $_[0]; + if (ref($args) ne 'HASH') + { + $args = { @_ }; + } + # these values can be "set" + my $set_args = {}; + foreach my $key ( + qw/trap_inf trap_nan + upgrade downgrade precision accuracy round_mode div_scale/ + ) + { + $set_args->{$key} = $args->{$key} if exists $args->{$key}; + delete $args->{$key}; + } + if (keys %$args > 0) + { + require Carp; + Carp::croak ("Illegal key(s) '", + join("','",keys %$args),"' passed to $class\->config()"); + } + foreach my $key (keys %$set_args) + { + if ($key =~ /^trap_(inf|nan)\z/) + { + ${"${class}::_trap_$1"} = ($set_args->{"trap_$1"} ? 1 : 0); + next; + } + # use a call instead of just setting the $variable to check argument + $class->$key($set_args->{$key}); + } + } + + # now return actual configuration + + my $cfg = { + lib => $CALC, + lib_version => ${"${CALC}::VERSION"}, + class => $class, + trap_nan => ${"${class}::_trap_nan"}, + trap_inf => ${"${class}::_trap_inf"}, + version => ${"${class}::VERSION"}, + }; + foreach my $key (qw/ + upgrade downgrade precision accuracy round_mode div_scale + /) + { + $cfg->{$key} = ${"${class}::$key"}; + }; + if (@_ == 1 && (ref($_[0]) ne 'HASH')) + { + # calls of the style config('lib') return just this value + return $cfg->{$_[0]}; + } + $cfg; + } + +sub _scale_a + { + # select accuracy parameter based on precedence, + # used by bround() and bfround(), may return undef for scale (means no op) + my ($x,$scale,$mode) = @_; + + $scale = $x->{_a} unless defined $scale; + + no strict 'refs'; + my $class = ref($x); + + $scale = ${ $class . '::accuracy' } unless defined $scale; + $mode = ${ $class . '::round_mode' } unless defined $mode; + + if (defined $scale) + { + $scale = $scale->can('numify') ? $scale->numify() + : "$scale" if ref($scale); + $scale = int($scale); + } + + ($scale,$mode); + } + +sub _scale_p + { + # select precision parameter based on precedence, + # used by bround() and bfround(), may return undef for scale (means no op) + my ($x,$scale,$mode) = @_; + + $scale = $x->{_p} unless defined $scale; + + no strict 'refs'; + my $class = ref($x); + + $scale = ${ $class . '::precision' } unless defined $scale; + $mode = ${ $class . '::round_mode' } unless defined $mode; + + if (defined $scale) + { + $scale = $scale->can('numify') ? $scale->numify() + : "$scale" if ref($scale); + $scale = int($scale); + } + + ($scale,$mode); + } + +############################################################################## +# constructors + +sub copy + { + # if two arguments, the first one is the class to "swallow" subclasses + if (@_ > 1) + { + my $self = bless { + sign => $_[1]->{sign}, + value => $CALC->_copy($_[1]->{value}), + }, $_[0] if @_ > 1; + + $self->{_a} = $_[1]->{_a} if defined $_[1]->{_a}; + $self->{_p} = $_[1]->{_p} if defined $_[1]->{_p}; + return $self; + } + + my $self = bless { + sign => $_[0]->{sign}, + value => $CALC->_copy($_[0]->{value}), + }, ref($_[0]); + + $self->{_a} = $_[0]->{_a} if defined $_[0]->{_a}; + $self->{_p} = $_[0]->{_p} if defined $_[0]->{_p}; + $self; + } + +sub new + { + # create a new BigInt object from a string or another BigInt object. + # see hash keys documented at top + + # the argument could be an object, so avoid ||, && etc on it, this would + # cause costly overloaded code to be called. The only allowed ops are + # ref() and defined. + + my ($class,$wanted,$a,$p,$r) = @_; + + # avoid numify-calls by not using || on $wanted! + return $class->bzero($a,$p) if !defined $wanted; # default to 0 + return $class->copy($wanted,$a,$p,$r) + if ref($wanted) && $wanted->isa($class); # MBI or subclass + + $class->import() if $IMPORT == 0; # make require work + + my $self = bless {}, $class; + + # shortcut for "normal" numbers + if ((!ref $wanted) && ($wanted =~ /^([+-]?)[1-9][0-9]*\z/)) + { + $self->{sign} = $1 || '+'; + + if ($wanted =~ /^[+-]/) + { + # remove sign without touching wanted to make it work with constants + my $t = $wanted; $t =~ s/^[+-]//; + $self->{value} = $CALC->_new($t); + } + else + { + $self->{value} = $CALC->_new($wanted); + } + no strict 'refs'; + if ( (defined $a) || (defined $p) + || (defined ${"${class}::precision"}) + || (defined ${"${class}::accuracy"}) + ) + { + $self->round($a,$p,$r) unless (@_ == 4 && !defined $a && !defined $p); + } + return $self; + } + + # handle '+inf', '-inf' first + if ($wanted =~ /^[+-]?inf\z/) + { + $self->{sign} = $wanted; # set a default sign for bstr() + return $self->binf($wanted); + } + # split str in m mantissa, e exponent, i integer, f fraction, v value, s sign + my ($mis,$miv,$mfv,$es,$ev) = _split($wanted); + if (!ref $mis) + { + if ($_trap_nan) + { + require Carp; Carp::croak("$wanted is not a number in $class"); + } + $self->{value} = $CALC->_zero(); + $self->{sign} = $nan; + return $self; + } + if (!ref $miv) + { + # _from_hex or _from_bin + $self->{value} = $mis->{value}; + $self->{sign} = $mis->{sign}; + return $self; # throw away $mis + } + # make integer from mantissa by adjusting exp, then convert to bigint + $self->{sign} = $$mis; # store sign + $self->{value} = $CALC->_zero(); # for all the NaN cases + my $e = int("$$es$$ev"); # exponent (avoid recursion) + if ($e > 0) + { + my $diff = $e - CORE::length($$mfv); + if ($diff < 0) # Not integer + { + if ($_trap_nan) + { + require Carp; Carp::croak("$wanted not an integer in $class"); + } + #print "NOI 1\n"; + return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; + $self->{sign} = $nan; + } + else # diff >= 0 + { + # adjust fraction and add it to value + #print "diff > 0 $$miv\n"; + $$miv = $$miv . ($$mfv . '0' x $diff); + } + } + else + { + if ($$mfv ne '') # e <= 0 + { + # fraction and negative/zero E => NOI + if ($_trap_nan) + { + require Carp; Carp::croak("$wanted not an integer in $class"); + } + #print "NOI 2 \$\$mfv '$$mfv'\n"; + return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; + $self->{sign} = $nan; + } + elsif ($e < 0) + { + # xE-y, and empty mfv + # Split the mantissa at the decimal point. E.g., if + # $$miv = 12345 and $e = -2, then $frac = 45 and $$miv = 123. + + my $frac = substr($$miv, $e); # $frac is fraction part + substr($$miv, $e) = ""; # $$miv is now integer part + + if ($frac =~ /[^0]/) + { + if ($_trap_nan) + { + require Carp; Carp::croak("$wanted not an integer in $class"); + } + #print "NOI 3\n"; + return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; + $self->{sign} = $nan; + } + } + } + unless ($self->{sign} eq $nan) { + $self->{sign} = '+' if $$miv eq '0'; # normalize -0 => +0 + $self->{value} = $CALC->_new($$miv) if $self->{sign} =~ /^[+-]$/; + } + # if any of the globals is set, use them to round and store them inside $self + # do not round for new($x,undef,undef) since that is used by MBF to signal + # no rounding + $self->round($a,$p,$r) unless @_ == 4 && !defined $a && !defined $p; + $self; + } + +sub bnan + { + # create a bigint 'NaN', if given a BigInt, set it to 'NaN' + my $self = shift; + $self = $class if !defined $self; + if (!ref($self)) + { + my $c = $self; $self = {}; bless $self, $c; + } + no strict 'refs'; + if (${"${class}::_trap_nan"}) + { + require Carp; + Carp::croak ("Tried to set $self to NaN in $class\::bnan()"); + } + $self->import() if $IMPORT == 0; # make require work + return if $self->modify('bnan'); + if ($self->can('_bnan')) + { + # use subclass to initialize + $self->_bnan(); + } + else + { + # otherwise do our own thing + $self->{value} = $CALC->_zero(); + } + $self->{sign} = $nan; + delete $self->{_a}; delete $self->{_p}; # rounding NaN is silly + $self; + } + +sub binf + { + # create a bigint '+-inf', if given a BigInt, set it to '+-inf' + # the sign is either '+', or if given, used from there + my $self = shift; + my $sign = shift; $sign = '+' if !defined $sign || $sign !~ /^-(inf)?$/; + $self = $class if !defined $self; + if (!ref($self)) + { + my $c = $self; $self = {}; bless $self, $c; + } + no strict 'refs'; + if (${"${class}::_trap_inf"}) + { + require Carp; + Carp::croak ("Tried to set $self to +-inf in $class\::binf()"); + } + $self->import() if $IMPORT == 0; # make require work + return if $self->modify('binf'); + if ($self->can('_binf')) + { + # use subclass to initialize + $self->_binf(); + } + else + { + # otherwise do our own thing + $self->{value} = $CALC->_zero(); + } + $sign = $sign . 'inf' if $sign !~ /inf$/; # - => -inf + $self->{sign} = $sign; + ($self->{_a},$self->{_p}) = @_; # take over requested rounding + $self; + } + +sub bzero + { + # create a bigint '+0', if given a BigInt, set it to 0 + my $self = shift; + $self = __PACKAGE__ if !defined $self; + + if (!ref($self)) + { + my $c = $self; $self = {}; bless $self, $c; + } + $self->import() if $IMPORT == 0; # make require work + return if $self->modify('bzero'); + + if ($self->can('_bzero')) + { + # use subclass to initialize + $self->_bzero(); + } + else + { + # otherwise do our own thing + $self->{value} = $CALC->_zero(); + } + $self->{sign} = '+'; + if (@_ > 0) + { + if (@_ > 3) + { + # call like: $x->bzero($a,$p,$r,$y); + ($self,$self->{_a},$self->{_p}) = $self->_find_round_parameters(@_); + } + else + { + $self->{_a} = $_[0] + if ( (!defined $self->{_a}) || (defined $_[0] && $_[0] > $self->{_a})); + $self->{_p} = $_[1] + if ( (!defined $self->{_p}) || (defined $_[1] && $_[1] > $self->{_p})); + } + } + $self; + } + +sub bone + { + # create a bigint '+1' (or -1 if given sign '-'), + # if given a BigInt, set it to +1 or -1, respectively + my $self = shift; + my $sign = shift; $sign = '+' if !defined $sign || $sign ne '-'; + $self = $class if !defined $self; + + if (!ref($self)) + { + my $c = $self; $self = {}; bless $self, $c; + } + $self->import() if $IMPORT == 0; # make require work + return if $self->modify('bone'); + + if ($self->can('_bone')) + { + # use subclass to initialize + $self->_bone(); + } + else + { + # otherwise do our own thing + $self->{value} = $CALC->_one(); + } + $self->{sign} = $sign; + if (@_ > 0) + { + if (@_ > 3) + { + # call like: $x->bone($sign,$a,$p,$r,$y); + ($self,$self->{_a},$self->{_p}) = $self->_find_round_parameters(@_); + } + else + { + # call like: $x->bone($sign,$a,$p,$r); + $self->{_a} = $_[0] + if ( (!defined $self->{_a}) || (defined $_[0] && $_[0] > $self->{_a})); + $self->{_p} = $_[1] + if ( (!defined $self->{_p}) || (defined $_[1] && $_[1] > $self->{_p})); + } + } + $self; + } + +############################################################################## +# string conversion + +sub bsstr + { + # (ref to BFLOAT or num_str ) return num_str + # Convert number from internal format to scientific string format. + # internal format is always normalized (no leading zeros, "-0E0" => "+0E0") + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN + return 'inf'; # +inf + } + my ($m,$e) = $x->parts(); + #$m->bstr() . 'e+' . $e->bstr(); # e can only be positive in BigInt + # 'e+' because E can only be positive in BigInt + $m->bstr() . 'e+' . $CALC->_str($e->{value}); + } + +sub bstr + { + # make a string from bigint object + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN + return 'inf'; # +inf + } + my $es = ''; $es = $x->{sign} if $x->{sign} eq '-'; + $es.$CALC->_str($x->{value}); + } + +sub numify + { + # Make a "normal" scalar from a BigInt object + my $x = shift; $x = $class->new($x) unless ref $x; + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; + my $num = $CALC->_num($x->{value}); + return -$num if $x->{sign} eq '-'; + $num; + } + +############################################################################## +# public stuff (usually prefixed with "b") + +sub sign + { + # return the sign of the number: +/-/-inf/+inf/NaN + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + $x->{sign}; + } + +sub _find_round_parameters + { + # After any operation or when calling round(), the result is rounded by + # regarding the A & P from arguments, local parameters, or globals. + + # !!!!!!! If you change this, remember to change round(), too! !!!!!!!!!! + + # This procedure finds the round parameters, but it is for speed reasons + # duplicated in round. Otherwise, it is tested by the testsuite and used + # by fdiv(). + + # returns ($self) or ($self,$a,$p,$r) - sets $self to NaN of both A and P + # were requested/defined (locally or globally or both) + + my ($self,$a,$p,$r,@args) = @_; + # $a accuracy, if given by caller + # $p precision, if given by caller + # $r round_mode, if given by caller + # @args all 'other' arguments (0 for unary, 1 for binary ops) + + my $c = ref($self); # find out class of argument(s) + no strict 'refs'; + + # convert to normal scalar for speed and correctness in inner parts + $a = $a->can('numify') ? $a->numify() : "$a" if defined $a && ref($a); + $p = $p->can('numify') ? $p->numify() : "$p" if defined $p && ref($p); + + # now pick $a or $p, but only if we have got "arguments" + if (!defined $a) + { + foreach ($self,@args) + { + # take the defined one, or if both defined, the one that is smaller + $a = $_->{_a} if (defined $_->{_a}) && (!defined $a || $_->{_a} < $a); + } + } + if (!defined $p) + { + # even if $a is defined, take $p, to signal error for both defined + foreach ($self,@args) + { + # take the defined one, or if both defined, the one that is bigger + # -2 > -3, and 3 > 2 + $p = $_->{_p} if (defined $_->{_p}) && (!defined $p || $_->{_p} > $p); + } + } + # if still none defined, use globals (#2) + $a = ${"$c\::accuracy"} unless defined $a; + $p = ${"$c\::precision"} unless defined $p; + + # A == 0 is useless, so undef it to signal no rounding + $a = undef if defined $a && $a == 0; + + # no rounding today? + return ($self) unless defined $a || defined $p; # early out + + # set A and set P is an fatal error + return ($self->bnan()) if defined $a && defined $p; # error + + $r = ${"$c\::round_mode"} unless defined $r; + if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) + { + require Carp; Carp::croak ("Unknown round mode '$r'"); + } + + $a = int($a) if defined $a; + $p = int($p) if defined $p; + + ($self,$a,$p,$r); + } + +sub round + { + # Round $self according to given parameters, or given second argument's + # parameters or global defaults + + # for speed reasons, _find_round_parameters is embedded here: + + my ($self,$a,$p,$r,@args) = @_; + # $a accuracy, if given by caller + # $p precision, if given by caller + # $r round_mode, if given by caller + # @args all 'other' arguments (0 for unary, 1 for binary ops) + + my $c = ref($self); # find out class of argument(s) + no strict 'refs'; + + # now pick $a or $p, but only if we have got "arguments" + if (!defined $a) + { + foreach ($self,@args) + { + # take the defined one, or if both defined, the one that is smaller + $a = $_->{_a} if (defined $_->{_a}) && (!defined $a || $_->{_a} < $a); + } + } + if (!defined $p) + { + # even if $a is defined, take $p, to signal error for both defined + foreach ($self,@args) + { + # take the defined one, or if both defined, the one that is bigger + # -2 > -3, and 3 > 2 + $p = $_->{_p} if (defined $_->{_p}) && (!defined $p || $_->{_p} > $p); + } + } + # if still none defined, use globals (#2) + $a = ${"$c\::accuracy"} unless defined $a; + $p = ${"$c\::precision"} unless defined $p; + + # A == 0 is useless, so undef it to signal no rounding + $a = undef if defined $a && $a == 0; + + # no rounding today? + return $self unless defined $a || defined $p; # early out + + # set A and set P is an fatal error + return $self->bnan() if defined $a && defined $p; + + $r = ${"$c\::round_mode"} unless defined $r; + if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) + { + require Carp; Carp::croak ("Unknown round mode '$r'"); + } + + # now round, by calling either fround or ffround: + if (defined $a) + { + $self->bround(int($a),$r) if !defined $self->{_a} || $self->{_a} >= $a; + } + else # both can't be undefined due to early out + { + $self->bfround(int($p),$r) if !defined $self->{_p} || $self->{_p} <= $p; + } + # bround() or bfround() already called bnorm() if nec. + $self; + } + +sub bnorm + { + # (numstr or BINT) return BINT + # Normalize number -- no-op here + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + $x; + } + +sub babs + { + # (BINT or num_str) return BINT + # make number absolute, or return absolute BINT from string + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->modify('babs'); + # post-normalized abs for internal use (does nothing for NaN) + $x->{sign} =~ s/^-/+/; + $x; + } + +sub bsgn { + # Signum function. + + my $self = shift; + + return $self if $self->modify('bsgn'); + + return $self -> bone("+") if $self -> is_pos(); + return $self -> bone("-") if $self -> is_neg(); + return $self; # zero or NaN +} + +sub bneg + { + # (BINT or num_str) return BINT + # negate number or make a negated number from string + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->modify('bneg'); + + # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN' + $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $CALC->_is_zero($x->{value})); + $x; + } + +sub bcmp + { + # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort) + # (BINT or num_str, BINT or num_str) return cond_code + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + return $upgrade->bcmp($x,$y) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; + return +1 if $x->{sign} eq '+inf'; + return -1 if $x->{sign} eq '-inf'; + return -1 if $y->{sign} eq '+inf'; + return +1; + } + # check sign for speed first + return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y + return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0 + + # have same sign, so compare absolute values. Don't make tests for zero + # here because it's actually slower than testing in Calc (especially w/ Pari + # et al) + + # post-normalized compare for internal use (honors signs) + if ($x->{sign} eq '+') + { + # $x and $y both > 0 + return $CALC->_acmp($x->{value},$y->{value}); + } + + # $x && $y both < 0 + $CALC->_acmp($y->{value},$x->{value}); # swapped acmp (lib returns 0,1,-1) + } + +sub bacmp + { + # Compares 2 values, ignoring their signs. + # Returns one of undef, <0, =0, >0. (suitable for sort) + # (BINT, BINT) return cond_code + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + return $upgrade->bacmp($x,$y) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/; + return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/; + return -1; + } + $CALC->_acmp($x->{value},$y->{value}); # lib does only 0,1,-1 + } + +sub badd + { + # add second arg (BINT or string) to first (BINT) (modifies first) + # return result as BINT + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('badd'); + return $upgrade->badd($upgrade->new($x),$upgrade->new($y),@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + $r[3] = $y; # no push! + # inf and NaN handling + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # NaN first + return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) + { + # +inf++inf or -inf+-inf => same, rest is NaN + return $x if $x->{sign} eq $y->{sign}; + return $x->bnan(); + } + # +-inf + something => +inf + # something +-inf => +-inf + $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/; + return $x; + } + + my ($sx, $sy) = ( $x->{sign}, $y->{sign} ); # get signs + + if ($sx eq $sy) + { + $x->{value} = $CALC->_add($x->{value},$y->{value}); # same sign, abs add + } + else + { + my $a = $CALC->_acmp ($y->{value},$x->{value}); # absolute compare + if ($a > 0) + { + $x->{value} = $CALC->_sub($y->{value},$x->{value},1); # abs sub w/ swap + $x->{sign} = $sy; + } + elsif ($a == 0) + { + # speedup, if equal, set result to 0 + $x->{value} = $CALC->_zero(); + $x->{sign} = '+'; + } + else # a < 0 + { + $x->{value} = $CALC->_sub($x->{value}, $y->{value}); # abs sub + } + } + $x->round(@r); + } + +sub bsub + { + # (BINT or num_str, BINT or num_str) return BINT + # subtract second arg from first, modify first + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bsub'); + + return $upgrade->new($x)->bsub($upgrade->new($y),@r) if defined $upgrade && + ((!$x->isa($self)) || (!$y->isa($self))); + + return $x->round(@r) if $y->is_zero(); + + # To correctly handle the lone special case $x->bsub($x), we note the sign + # of $x, then flip the sign from $y, and if the sign of $x did change, too, + # then we caught the special case: + my $xsign = $x->{sign}; + $y->{sign} =~ tr/+\-/-+/; # does nothing for NaN + if ($xsign ne $x->{sign}) + { + # special case of $x->bsub($x) results in 0 + return $x->bzero(@r) if $xsign =~ /^[+-]$/; + return $x->bnan(); # NaN, -inf, +inf + } + $x->badd($y,@r); # badd does not leave internal zeros + $y->{sign} =~ tr/+\-/-+/; # refix $y (does nothing for NaN) + $x; # already rounded by badd() or no round nec. + } + +sub binc + { + # increment arg by one + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + return $x if $x->modify('binc'); + + if ($x->{sign} eq '+') + { + $x->{value} = $CALC->_inc($x->{value}); + return $x->round($a,$p,$r); + } + elsif ($x->{sign} eq '-') + { + $x->{value} = $CALC->_dec($x->{value}); + $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # -1 +1 => -0 => +0 + return $x->round($a,$p,$r); + } + # inf, nan handling etc + $x->badd($self->bone(),$a,$p,$r); # badd does round + } + +sub bdec + { + # decrement arg by one + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + return $x if $x->modify('bdec'); + + if ($x->{sign} eq '-') + { + # x already < 0 + $x->{value} = $CALC->_inc($x->{value}); + } + else + { + return $x->badd($self->bone('-'),@r) + unless $x->{sign} eq '+'; # inf or NaN + # >= 0 + if ($CALC->_is_zero($x->{value})) + { + # == 0 + $x->{value} = $CALC->_one(); $x->{sign} = '-'; # 0 => -1 + } + else + { + # > 0 + $x->{value} = $CALC->_dec($x->{value}); + } + } + $x->round(@r); + } + +sub blog + { + # calculate $x = $a ** $base + $b and return $a (e.g. the log() to base + # $base of $x) + + # set up parameters + my ($self,$x,$base,@r) = (undef,@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$base,@r) = objectify(2,@_); + } + + return $x if $x->modify('blog'); + + $base = $self->new($base) if defined $base && !ref $base; + + # inf, -inf, NaN, <0 => NaN + return $x->bnan() + if $x->{sign} ne '+' || (defined $base && $base->{sign} ne '+'); + + return $upgrade->blog($upgrade->new($x),$base,@r) if + defined $upgrade; + + # fix for bug #24969: + # the default base is e (Euler's number) which is not an integer + if (!defined $base) + { + require Math::BigFloat; + my $u = Math::BigFloat->blog(Math::BigFloat->new($x))->as_int(); + # modify $x in place + $x->{value} = $u->{value}; + $x->{sign} = $u->{sign}; + return $x; + } + + my ($rc,$exact) = $CALC->_log_int($x->{value},$base->{value}); + return $x->bnan() unless defined $rc; # not possible to take log? + $x->{value} = $rc; + $x->round(@r); + } + +sub bnok + { + # Calculate n over k (binomial coefficient or "choose" function) as integer. + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bnok'); + return $x->bnan() if $x->{sign} eq 'NaN' || $y->{sign} eq 'NaN'; + return $x->binf() if $x->{sign} eq '+inf'; + + # k > n or k < 0 => 0 + my $cmp = $x->bacmp($y); + return $x->bzero() if $cmp < 0 || $y->{sign} =~ /^-/; + # k == n => 1 + return $x->bone(@r) if $cmp == 0; + + if ($CALC->can('_nok')) + { + $x->{value} = $CALC->_nok($x->{value},$y->{value}); + } + else + { + # ( 7 ) 7! 1*2*3*4 * 5*6*7 5 * 6 * 7 6 7 + # ( - ) = --------- = --------------- = --------- = 5 * - * - + # ( 3 ) (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3 2 3 + + if (!$y->is_zero()) + { + my $z = $x - $y; + $z->binc(); + my $r = $z->copy(); $z->binc(); + my $d = $self->new(2); + while ($z->bacmp($x) <= 0) # f <= x ? + { + $r->bmul($z); $r->bdiv($d); + $z->binc(); $d->binc(); + } + $x->{value} = $r->{value}; $x->{sign} = '+'; + } + else { $x->bone(); } + } + $x->round(@r); + } + +sub bexp + { + # Calculate e ** $x (Euler's number to the power of X), truncated to + # an integer value. + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + return $x if $x->modify('bexp'); + + # inf, -inf, NaN, <0 => NaN + return $x->bnan() if $x->{sign} eq 'NaN'; + return $x->bone() if $x->is_zero(); + return $x if $x->{sign} eq '+inf'; + return $x->bzero() if $x->{sign} eq '-inf'; + + my $u; + { + # run through Math::BigFloat unless told otherwise + require Math::BigFloat unless defined $upgrade; + local $upgrade = 'Math::BigFloat' unless defined $upgrade; + # calculate result, truncate it to integer + $u = $upgrade->bexp($upgrade->new($x),@r); + } + + if (!defined $upgrade) + { + $u = $u->as_int(); + # modify $x in place + $x->{value} = $u->{value}; + $x->round(@r); + } + else { $x = $u; } + } + +sub blcm + { + # (BINT or num_str, BINT or num_str) return BINT + # does not modify arguments, but returns new object + # Lowest Common Multiple + + my $y = shift; my ($x); + if (ref($y)) + { + $x = $y->copy(); + } + else + { + $x = $class->new($y); + } + my $self = ref($x); + while (@_) + { + my $y = shift; $y = $self->new($y) if !ref ($y); + $x = __lcm($x,$y); + } + $x; + } + +sub bgcd + { + # (BINT or num_str, BINT or num_str) return BINT + # does not modify arguments, but returns new object + # GCD -- Euclid's algorithm, variant C (Knuth Vol 3, pg 341 ff) + + my $y = shift; + $y = $class->new($y) if !ref($y); + my $self = ref($y); + my $x = $y->copy()->babs(); # keep arguments + return $x->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN? + + while (@_) + { + $y = shift; $y = $self->new($y) if !ref($y); + return $x->bnan() if $y->{sign} !~ /^[+-]$/; # y NaN? + $x->{value} = $CALC->_gcd($x->{value},$y->{value}); + last if $CALC->_is_one($x->{value}); + } + $x; + } + +sub bnot + { + # (num_str or BINT) return BINT + # represent ~x as twos-complement number + # we don't need $self, so undef instead of ref($_[0]) make it slightly faster + my ($self,$x,$a,$p,$r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bnot'); + $x->binc()->bneg(); # binc already does round + } + +############################################################################## +# is_foo test routines +# we don't need $self, so undef instead of ref($_[0]) make it slightly faster + +sub is_zero + { + # return true if arg (BINT or num_str) is zero (array '+', '0') + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 0 if $x->{sign} !~ /^\+$/; # -, NaN & +-inf aren't + $CALC->_is_zero($x->{value}); + } + +sub is_nan + { + # return true if arg (BINT or num_str) is NaN + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + $x->{sign} eq $nan ? 1 : 0; + } + +sub is_inf + { + # return true if arg (BINT or num_str) is +-inf + my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + if (defined $sign) + { + $sign = '[+-]inf' if $sign eq ''; # +- doesn't matter, only that's inf + $sign = "[$1]inf" if $sign =~ /^([+-])(inf)?$/; # extract '+' or '-' + return $x->{sign} =~ /^$sign$/ ? 1 : 0; + } + $x->{sign} =~ /^[+-]inf$/ ? 1 : 0; # only +-inf is infinity + } + +sub is_one + { + # return true if arg (BINT or num_str) is +1, or -1 if sign is given + my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $sign = '+' if !defined $sign || $sign ne '-'; + + return 0 if $x->{sign} ne $sign; # -1 != +1, NaN, +-inf aren't either + $CALC->_is_one($x->{value}); + } + +sub is_odd + { + # return true when arg (BINT or num_str) is odd, false for even + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't + $CALC->_is_odd($x->{value}); + } + +sub is_even + { + # return true when arg (BINT or num_str) is even, false for odd + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't + $CALC->_is_even($x->{value}); + } + +sub is_positive + { + # return true when arg (BINT or num_str) is positive (> 0) + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 1 if $x->{sign} eq '+inf'; # +inf is positive + + # 0+ is neither positive nor negative + ($x->{sign} eq '+' && !$x->is_zero()) ? 1 : 0; + } + +sub is_negative + { + # return true when arg (BINT or num_str) is negative (< 0) + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + $x->{sign} =~ /^-/ ? 1 : 0; # -inf is negative, but NaN is not + } + +sub is_int + { + # return true when arg (BINT or num_str) is an integer + # always true for BigInt, but different for BigFloats + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + $x->{sign} =~ /^[+-]$/ ? 1 : 0; # inf/-inf/NaN aren't + } + +############################################################################### + +sub bmul + { + # multiply the first number by the second number + # (BINT or num_str, BINT or num_str) return BINT + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmul'); + + return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + + return $upgrade->bmul($x,$upgrade->new($y),@r) + if defined $upgrade && !$y->isa($self); + + $r[3] = $y; # no push here + + $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => + + + $x->{value} = $CALC->_mul($x->{value},$y->{value}); # do actual math + $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # no -0 + + $x->round(@r); + } + +sub bmuladd + { + # multiply two numbers and then add the third to the result + # (BINT or num_str, BINT or num_str, BINT or num_str) return BINT + + # set up parameters + my ($self,$x,$y,$z,@r) = objectify(3,@_); + + return $x if $x->modify('bmuladd'); + + return $x->bnan() if ($x->{sign} eq $nan) || + ($y->{sign} eq $nan) || + ($z->{sign} eq $nan); + + # inf handling of x and y + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + # inf handling x*y and z + if (($z->{sign} =~ /^[+-]inf$/)) + { + # something +-inf => +-inf + $x->{sign} = $z->{sign}, return $x if $z->{sign} =~ /^[+-]inf$/; + } + + return $upgrade->bmuladd($x,$upgrade->new($y),$upgrade->new($z),@r) + if defined $upgrade && (!$y->isa($self) || !$z->isa($self) || !$x->isa($self)); + + # TODO: what if $y and $z have A or P set? + $r[3] = $z; # no push here + + $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => + + + $x->{value} = $CALC->_mul($x->{value},$y->{value}); # do actual math + $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # no -0 + + my ($sx, $sz) = ( $x->{sign}, $z->{sign} ); # get signs + + if ($sx eq $sz) + { + $x->{value} = $CALC->_add($x->{value},$z->{value}); # same sign, abs add + } + else + { + my $a = $CALC->_acmp ($z->{value},$x->{value}); # absolute compare + if ($a > 0) + { + $x->{value} = $CALC->_sub($z->{value},$x->{value},1); # abs sub w/ swap + $x->{sign} = $sz; + } + elsif ($a == 0) + { + # speedup, if equal, set result to 0 + $x->{value} = $CALC->_zero(); + $x->{sign} = '+'; + } + else # a < 0 + { + $x->{value} = $CALC->_sub($x->{value}, $z->{value}); # abs sub + } + } + $x->round(@r); + } + +sub bdiv + { + + # This does floored division, where the quotient is floored toward negative + # infinity and the remainder has the same sign as the divisor. + + # Set up parameters. + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify() is costly, so avoid it if we can. + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bdiv'); + + my $wantarray = wantarray; # call only once + + # At least one argument is NaN. Return NaN for both quotient and the + # modulo/remainder. + + if ($x -> is_nan() || $y -> is_nan()) { + return $wantarray ? ($x -> bnan(), $self -> bnan()) : $x -> bnan(); + } + + # Divide by zero and modulo zero. + # + # Division: Use the common convention that x / 0 is inf with the same sign + # as x, except when x = 0, where we return NaN. This is also what earlier + # versions did. + # + # Modulo: In modular arithmetic, the congruence relation z = x (mod y) + # means that there is some integer k such that z - x = k y. If y = 0, we + # get z - x = 0 or z = x. This is also what earlier versions did, except + # that 0 % 0 returned NaN. + # + # inf / 0 = inf inf % 0 = inf + # 5 / 0 = inf 5 % 0 = 5 + # 0 / 0 = NaN 0 % 0 = 0 (before: NaN) + # -5 / 0 = -inf -5 % 0 = -5 + # -inf / 0 = -inf -inf % 0 = -inf + + if ($y -> is_zero()) { + my ($quo, $rem); + if ($wantarray) { + $rem = $x -> copy(); + } + if ($x -> is_zero()) { + $quo = $x -> bnan(); + } else { + $quo = $x -> binf($x -> {sign}); + } + return $wantarray ? ($quo, $rem) : $quo; + } + + # Numerator (dividend) is +/-inf, and denominator is finite and non-zero. + # The divide by zero cases are covered above. In all of the cases listed + # below we return the same as core Perl. + # + # inf / -inf = NaN inf % -inf = NaN + # inf / -5 = -inf inf % -5 = NaN (before: 0) + # inf / 5 = inf inf % 5 = NaN (before: 0) + # inf / inf = NaN inf % inf = NaN + # + # -inf / -inf = NaN -inf % -inf = NaN + # -inf / -5 = inf -inf % -5 = NaN (before: 0) + # -inf / 5 = -inf -inf % 5 = NaN (before: 0) + # -inf / inf = NaN -inf % inf = NaN + + if ($x -> is_inf()) { + my ($quo, $rem); + $rem = $self -> bnan() if $wantarray; + if ($y -> is_inf()) { + $quo = $x -> bnan(); + } else { + my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-'; + $quo = $x -> binf($sign); + } + return $wantarray ? ($quo, $rem) : $quo; + } + + # Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf + # are covered above. In the modulo cases (in the right column) we return + # the same as core Perl, which does floored division, so for consistency we + # also do floored division in the division cases (in the left column). + # + # -5 / inf = -1 (before: 0) -5 % inf = inf (before: -5) + # 0 / inf = 0 0 % inf = 0 + # 5 / inf = 0 5 % inf = 5 + # + # -5 / -inf = 0 -5 % -inf = -5 + # 0 / -inf = 0 0 % -inf = 0 + # 5 / -inf = -1 (before: 0) 5 % -inf = -inf (before: 5) + + if ($y -> is_inf()) { + my ($quo, $rem); + if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { + $rem = $x -> copy() if $wantarray; + $quo = $x -> bzero(); + } else { + $rem = $self -> binf($y -> {sign}) if $wantarray; + $quo = $x -> bone('-'); + } + return $wantarray ? ($quo, $rem) : $quo; + } + + # At this point, both the numerator and denominator are finite numbers, and + # the denominator (divisor) is non-zero. + + return $upgrade->bdiv($upgrade->new($x),$upgrade->new($y),@r) + if defined $upgrade; + + $r[3] = $y; # no push! + + # Inialize remainder. + + my $rem = $self->bzero(); + + # Are both operands the same object, i.e., like $x -> bdiv($x)? + # If so, flipping the sign of $y also flips the sign of $x. + + my $xsign = $x->{sign}; + my $ysign = $y->{sign}; + + $y->{sign} =~ tr/+-/-+/; # Flip the sign of $y, and see ... + my $same = $xsign ne $x->{sign}; # ... if that changed the sign of $x. + $y->{sign} = $ysign; # Re-insert the original sign. + + if ($same) { + $x -> bone(); + } else { + ($x->{value},$rem->{value}) = $CALC->_div($x->{value},$y->{value}); + + if ($CALC -> _is_zero($rem->{value})) { + if ($xsign eq $ysign || $CALC -> _is_zero($x->{value})) { + $x->{sign} = '+'; + } else { + $x->{sign} = '-'; + } + } else { + if ($xsign eq $ysign) { + $x->{sign} = '+'; + } else { + if ($xsign eq '+') { + $x -> badd(1); + } else { + $x -> bsub(1); + } + $x->{sign} = '-'; + } + } + } + + $x->round(@r); + + if ($wantarray) { + unless ($CALC -> _is_zero($rem->{value})) { + if ($xsign ne $ysign) { + $rem = $y -> copy() -> babs() -> bsub($rem); + } + $rem->{sign} = $ysign; + } + $rem->{_a} = $x->{_a}; + $rem->{_p} = $x->{_p}; + $rem->round(@r); + return ($x,$rem); + } + + return $x; + } + +############################################################################### +# modulus functions + +sub bmod + { + + # This is the remainder after floored division, where the quotient is + # floored toward negative infinity and the remainder has the same sign as + # the divisor. + + # Set up parameters. + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmod'); + $r[3] = $y; # no push! + + # At least one argument is NaN. + + if ($x -> is_nan() || $y -> is_nan()) { + return $x -> bnan(); + } + + # Modulo zero. See documentation for bdiv(). + + if ($y -> is_zero()) { + return $x; + } + + # Numerator (dividend) is +/-inf. + + if ($x -> is_inf()) { + return $x -> bnan(); + } + + # Denominator (divisor) is +/-inf. + + if ($y -> is_inf()) { + if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { + return $x; + } else { + return $x -> binf($y -> sign()); + } + } + + # Calc new sign and in case $y == +/- 1, return $x. + + $x->{value} = $CALC->_mod($x->{value},$y->{value}); + if ($CALC -> _is_zero($x->{value})) + { + $x->{sign} = '+'; # do not leave -0 + } + else + { + $x->{value} = $CALC->_sub($y->{value},$x->{value},1) # $y-$x + if ($x->{sign} ne $y->{sign}); + $x->{sign} = $y->{sign}; + } + + $x->round(@r); + } + +sub bmodinv + { + # Return modular multiplicative inverse: + # + # z is the modular inverse of x (mod y) if and only if + # + # x*z ≡ 1 (mod y) + # + # If the modulus y is larger than one, x and z are relative primes (i.e., + # their greatest common divisor is one). + # + # If no modular multiplicative inverse exists, NaN is returned. + + # set up parameters + my ($self,$x,$y,@r) = (undef,@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmodinv'); + + # Return NaN if one or both arguments is +inf, -inf, or nan. + + return $x->bnan() if ($y->{sign} !~ /^[+-]$/ || + $x->{sign} !~ /^[+-]$/); + + # Return NaN if $y is zero; 1 % 0 makes no sense. + + return $x->bnan() if $y->is_zero(); + + # Return 0 in the trivial case. $x % 1 or $x % -1 is zero for all finite + # integers $x. + + return $x->bzero() if ($y->is_one() || + $y->is_one('-')); + + # Return NaN if $x = 0, or $x modulo $y is zero. The only valid case when + # $x = 0 is when $y = 1 or $y = -1, but that was covered above. + # + # Note that computing $x modulo $y here affects the value we'll feed to + # $CALC->_modinv() below when $x and $y have opposite signs. E.g., if $x = + # 5 and $y = 7, those two values are fed to _modinv(), but if $x = -5 and + # $y = 7, the values fed to _modinv() are $x = 2 (= -5 % 7) and $y = 7. + # The value if $x is affected only when $x and $y have opposite signs. + + $x->bmod($y); + return $x->bnan() if $x->is_zero(); + + # Compute the modular multiplicative inverse of the absolute values. We'll + # correct for the signs of $x and $y later. Return NaN if no GCD is found. + + ($x->{value}, $x->{sign}) = $CALC->_modinv($x->{value}, $y->{value}); + return $x->bnan() if !defined $x->{value}; + + # Library inconsistency workaround: _modinv() in Math::BigInt::GMP versions + # <= 1.32 return undef rather than a "+" for the sign. + + $x->{sign} = '+' unless defined $x->{sign}; + + # When one or both arguments are negative, we have the following + # relations. If x and y are positive: + # + # modinv(-x, -y) = -modinv(x, y) + # modinv(-x, y) = y - modinv(x, y) = -modinv(x, y) (mod y) + # modinv( x, -y) = modinv(x, y) - y = modinv(x, y) (mod -y) + + # We must swap the sign of the result if the original $x is negative. + # However, we must compensate for ignoring the signs when computing the + # inverse modulo. The net effect is that we must swap the sign of the + # result if $y is negative. + + $x -> bneg() if $y->{sign} eq '-'; + + # Compute $x modulo $y again after correcting the sign. + + $x -> bmod($y) if $x->{sign} ne $y->{sign}; + + return $x; + } + +sub bmodpow + { + # Modular exponentiation. Raises a very large number to a very large exponent + # in a given very large modulus quickly, thanks to binary exponentiation. + # Supports negative exponents. + my ($self,$num,$exp,$mod,@r) = objectify(3,@_); + + return $num if $num->modify('bmodpow'); + + # When the exponent 'e' is negative, use the following relation, which is + # based on finding the multiplicative inverse 'd' of 'b' modulo 'm': + # + # b^(-e) (mod m) = d^e (mod m) where b*d = 1 (mod m) + + $num->bmodinv($mod) if ($exp->{sign} eq '-'); + + # Check for valid input. All operands must be finite, and the modulus must be + # non-zero. + + return $num->bnan() if ($num->{sign} =~ /NaN|inf/ || # NaN, -inf, +inf + $exp->{sign} =~ /NaN|inf/ || # NaN, -inf, +inf + $mod->{sign} =~ /NaN|inf/); # NaN, -inf, +inf + + # Modulo zero. See documentation for Math::BigInt's bmod() method. + + if ($mod -> is_zero()) { + if ($num -> is_zero()) { + return $self -> bnan(); + } else { + return $num -> copy(); + } + } + + # Compute 'a (mod m)', ignoring the signs on 'a' and 'm'. If the resulting + # value is zero, the output is also zero, regardless of the signs on 'a' and + # 'm'. + + my $value = $CALC->_modpow($num->{value}, $exp->{value}, $mod->{value}); + my $sign = '+'; + + # If the resulting value is non-zero, we have four special cases, depending + # on the signs on 'a' and 'm'. + + unless ($CALC->_is_zero($value)) { + + # There is a negative sign on 'a' (= $num**$exp) only if the number we + # are exponentiating ($num) is negative and the exponent ($exp) is odd. + + if ($num->{sign} eq '-' && $exp->is_odd()) { + + # When both the number 'a' and the modulus 'm' have a negative sign, + # use this relation: + # + # -a (mod -m) = -(a (mod m)) + + if ($mod->{sign} eq '-') { + $sign = '-'; + } + + # When only the number 'a' has a negative sign, use this relation: + # + # -a (mod m) = m - (a (mod m)) + + else { + # Use copy of $mod since _sub() modifies the first argument. + my $mod = $CALC->_copy($mod->{value}); + $value = $CALC->_sub($mod, $value); + $sign = '+'; + } + + } else { + + # When only the modulus 'm' has a negative sign, use this relation: + # + # a (mod -m) = (a (mod m)) - m + # = -(m - (a (mod m))) + + if ($mod->{sign} eq '-') { + # Use copy of $mod since _sub() modifies the first argument. + my $mod = $CALC->_copy($mod->{value}); + $value = $CALC->_sub($mod, $value); + $sign = '-'; + } + + # When neither the number 'a' nor the modulus 'm' have a negative + # sign, directly return the already computed value. + # + # (a (mod m)) + + } + + } + + $num->{value} = $value; + $num->{sign} = $sign; + + return $num; + } + +############################################################################### + +sub bfac + { + # (BINT or num_str, BINT or num_str) return BINT + # compute factorial number from $x, modify $x in place + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bfac') || $x->{sign} eq '+inf'; # inf => inf + return $x->bnan() if $x->{sign} ne '+'; # NaN, <0 etc => NaN + + $x->{value} = $CALC->_fac($x->{value}); + $x->round(@r); + } + +sub bpow + { + # (BINT or num_str, BINT or num_str) return BINT + # compute power of two numbers -- stolen from Knuth Vol 2 pg 233 + # modifies first argument + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bpow'); + + return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) + { + # +-inf ** +-inf + return $x->bnan(); + } + # +-inf ** Y + if ($x->{sign} =~ /^[+-]inf/) + { + # +inf ** 0 => NaN + return $x->bnan() if $y->is_zero(); + # -inf ** -1 => 1/inf => 0 + return $x->bzero() if $y->is_one('-') && $x->is_negative(); + + # +inf ** Y => inf + return $x if $x->{sign} eq '+inf'; + + # -inf ** Y => -inf if Y is odd + return $x if $y->is_odd(); + return $x->babs(); + } + # X ** +-inf + + # 1 ** +inf => 1 + return $x if $x->is_one(); + + # 0 ** inf => 0 + return $x if $x->is_zero() && $y->{sign} =~ /^[+]/; + + # 0 ** -inf => inf + return $x->binf() if $x->is_zero(); + + # -1 ** -inf => NaN + return $x->bnan() if $x->is_one('-') && $y->{sign} =~ /^[-]/; + + # -X ** -inf => 0 + return $x->bzero() if $x->{sign} eq '-' && $y->{sign} =~ /^[-]/; + + # -1 ** inf => NaN + return $x->bnan() if $x->{sign} eq '-'; + + # X ** inf => inf + return $x->binf() if $y->{sign} =~ /^[+]/; + # X ** -inf => 0 + return $x->bzero(); + } + + return $upgrade->bpow($upgrade->new($x),$y,@r) + if defined $upgrade && (!$y->isa($self) || $y->{sign} eq '-'); + + $r[3] = $y; # no push! + + # cases 0 ** Y, X ** 0, X ** 1, 1 ** Y are handled by Calc or Emu + + my $new_sign = '+'; + $new_sign = $y->is_odd() ? '-' : '+' if ($x->{sign} ne '+'); + + # 0 ** -7 => ( 1 / (0 ** 7)) => 1 / 0 => +inf + return $x->binf() + if $y->{sign} eq '-' && $x->{sign} eq '+' && $CALC->_is_zero($x->{value}); + # 1 ** -y => 1 / (1 ** |y|) + # so do test for negative $y after above's clause + return $x->bnan() if $y->{sign} eq '-' && !$CALC->_is_one($x->{value}); + + $x->{value} = $CALC->_pow($x->{value},$y->{value}); + $x->{sign} = $new_sign; + $x->{sign} = '+' if $CALC->_is_zero($y->{value}); + $x->round(@r); + } + +sub blsft + { + # (BINT or num_str, BINT or num_str) return BINT + # compute x << y, base n, y >= 0 + + # set up parameters + my ($self,$x,$y,$n,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$n,@r) = objectify(2,@_); + } + + return $x if $x->modify('blsft'); + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + return $x->round(@r) if $y->is_zero(); + + $n = 2 if !defined $n; return $x->bnan() if $n <= 0 || $y->{sign} eq '-'; + + $x->{value} = $CALC->_lsft($x->{value},$y->{value},$n); + $x->round(@r); + } + +sub brsft + { + # (BINT or num_str, BINT or num_str) return BINT + # compute x >> y, base n, y >= 0 + + # set up parameters + my ($self,$x,$y,$n,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$n,@r) = objectify(2,@_); + } + + return $x if $x->modify('brsft'); + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + return $x->round(@r) if $y->is_zero(); + return $x->bzero(@r) if $x->is_zero(); # 0 => 0 + + $n = 2 if !defined $n; return $x->bnan() if $n <= 0 || $y->{sign} eq '-'; + + # this only works for negative numbers when shifting in base 2 + if (($x->{sign} eq '-') && ($n == 2)) + { + return $x->round(@r) if $x->is_one('-'); # -1 => -1 + if (!$y->is_one()) + { + # although this is O(N*N) in calc (as_bin!) it is O(N) in Pari et al + # but perhaps there is a better emulation for two's complement shift... + # if $y != 1, we must simulate it by doing: + # convert to bin, flip all bits, shift, and be done + $x->binc(); # -3 => -2 + my $bin = $x->as_bin(); + $bin =~ s/^-0b//; # strip '-0b' prefix + $bin =~ tr/10/01/; # flip bits + # now shift + if ($y >= CORE::length($bin)) + { + $bin = '0'; # shifting to far right creates -1 + # 0, because later increment makes + # that 1, attached '-' makes it '-1' + # because -1 >> x == -1 ! + } + else + { + $bin =~ s/.{$y}$//; # cut off at the right side + $bin = '1' . $bin; # extend left side by one dummy '1' + $bin =~ tr/10/01/; # flip bits back + } + my $res = $self->new('0b'.$bin); # add prefix and convert back + $res->binc(); # remember to increment + $x->{value} = $res->{value}; # take over value + return $x->round(@r); # we are done now, magic, isn't? + } + # x < 0, n == 2, y == 1 + $x->bdec(); # n == 2, but $y == 1: this fixes it + } + + $x->{value} = $CALC->_rsft($x->{value},$y->{value},$n); + $x->round(@r); + } + +sub band + { + #(BINT or num_str, BINT or num_str) return BINT + # compute x & y + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('band'); + + $r[3] = $y; # no push! + + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + + my $sx = $x->{sign} eq '+' ? 1 : -1; + my $sy = $y->{sign} eq '+' ? 1 : -1; + + if ($sx == 1 && $sy == 1) + { + $x->{value} = $CALC->_and($x->{value},$y->{value}); + return $x->round(@r); + } + + if ($CAN{signed_and}) + { + $x->{value} = $CALC->_signed_and($x->{value},$y->{value},$sx,$sy); + return $x->round(@r); + } + + require $EMU_LIB; + __emu_band($self,$x,$y,$sx,$sy,@r); + } + +sub bior + { + #(BINT or num_str, BINT or num_str) return BINT + # compute x | y + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bior'); + $r[3] = $y; # no push! + + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + + my $sx = $x->{sign} eq '+' ? 1 : -1; + my $sy = $y->{sign} eq '+' ? 1 : -1; + + # the sign of X follows the sign of X, e.g. sign of Y irrelevant for bior() + + # don't use lib for negative values + if ($sx == 1 && $sy == 1) + { + $x->{value} = $CALC->_or($x->{value},$y->{value}); + return $x->round(@r); + } + + # if lib can do negative values, let it handle this + if ($CAN{signed_or}) + { + $x->{value} = $CALC->_signed_or($x->{value},$y->{value},$sx,$sy); + return $x->round(@r); + } + + require $EMU_LIB; + __emu_bior($self,$x,$y,$sx,$sy,@r); + } + +sub bxor + { + #(BINT or num_str, BINT or num_str) return BINT + # compute x ^ y + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bxor'); + $r[3] = $y; # no push! + + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + + my $sx = $x->{sign} eq '+' ? 1 : -1; + my $sy = $y->{sign} eq '+' ? 1 : -1; + + # don't use lib for negative values + if ($sx == 1 && $sy == 1) + { + $x->{value} = $CALC->_xor($x->{value},$y->{value}); + return $x->round(@r); + } + + # if lib can do negative values, let it handle this + if ($CAN{signed_xor}) + { + $x->{value} = $CALC->_signed_xor($x->{value},$y->{value},$sx,$sy); + return $x->round(@r); + } + + require $EMU_LIB; + __emu_bxor($self,$x,$y,$sx,$sy,@r); + } + +sub length + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + my $e = $CALC->_len($x->{value}); + wantarray ? ($e,0) : $e; + } + +sub digit + { + # return the nth decimal digit, negative values count backward, 0 is right + my ($self,$x,$n) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $n = $n->numify() if ref($n); + $CALC->_digit($x->{value},$n||0); + } + +sub _trailing_zeros + { + # return the amount of trailing zeros in $x (as scalar) + my $x = shift; + $x = $class->new($x) unless ref $x; + + return 0 if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf etc + + $CALC->_zeros($x->{value}); # must handle odd values, 0 etc + } + +sub bsqrt + { + # calculate square root of $x + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bsqrt'); + + return $x->bnan() if $x->{sign} !~ /^\+/; # -x or -inf or NaN => NaN + return $x if $x->{sign} eq '+inf'; # sqrt(+inf) == inf + + return $upgrade->bsqrt($x,@r) if defined $upgrade; + + $x->{value} = $CALC->_sqrt($x->{value}); + $x->round(@r); + } + +sub broot + { + # calculate $y'th root of $x + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + $y = $self->new(2) unless defined $y; + + # objectify is costly, so avoid it + if ((!ref($x)) || (ref($x) ne ref($y))) + { + ($self,$x,$y,@r) = objectify(2,$self || $class,@_); + } + + return $x if $x->modify('broot'); + + # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0 + return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() || + $y->{sign} !~ /^\+$/; + + return $x->round(@r) + if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one(); + + return $upgrade->new($x)->broot($upgrade->new($y),@r) if defined $upgrade; + + $x->{value} = $CALC->_root($x->{value},$y->{value}); + $x->round(@r); + } + +sub exponent + { + # return a copy of the exponent (here always 0, NaN or 1 for $m == 0) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + my $s = $x->{sign}; $s =~ s/^[+-]//; # NaN, -inf,+inf => NaN or inf + return $self->new($s); + } + return $self->bone() if $x->is_zero(); + + # 12300 => 2 trailing zeros => exponent is 2 + $self->new( $CALC->_zeros($x->{value}) ); + } + +sub mantissa + { + # return the mantissa (compatible to Math::BigFloat, e.g. reduced) + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) + { + # for NaN, +inf, -inf: keep the sign + return $self->new($x->{sign}); + } + my $m = $x->copy(); delete $m->{_p}; delete $m->{_a}; + + # that's a bit inefficient: + my $zeros = $CALC->_zeros($m->{value}); + $m->brsft($zeros,10) if $zeros != 0; + $m; + } + +sub parts + { + # return a copy of both the exponent and the mantissa + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + ($x->mantissa(),$x->exponent()); + } + +############################################################################## +# rounding functions + +sub bfround + { + # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.' + # $n == 0 || $n == 1 => round to integer + my $x = shift; my $self = ref($x) || $x; $x = $self->new($x) unless ref $x; + + my ($scale,$mode) = $x->_scale_p(@_); + + return $x if !defined $scale || $x->modify('bfround'); # no-op + + # no-op for BigInts if $n <= 0 + $x->bround( $x->length()-$scale, $mode) if $scale > 0; + + delete $x->{_a}; # delete to save memory + $x->{_p} = $scale; # store new _p + $x; + } + +sub _scan_for_nonzero + { + # internal, used by bround() to scan for non-zeros after a '5' + my ($x,$pad,$xs,$len) = @_; + + return 0 if $len == 1; # "5" is trailed by invisible zeros + my $follow = $pad - 1; + return 0 if $follow > $len || $follow < 1; + + # use the string form to check whether only '0's follow or not + substr ($xs,-$follow) =~ /[^0]/ ? 1 : 0; + } + +sub fround + { + # Exists to make life easier for switch between MBF and MBI (should we + # autoload fxxx() like MBF does for bxxx()?) + my $x = shift; $x = $class->new($x) unless ref $x; + $x->bround(@_); + } + +sub bround + { + # accuracy: +$n preserve $n digits from left, + # -$n preserve $n digits from right (f.i. for 0.1234 style in MBF) + # no-op for $n == 0 + # and overwrite the rest with 0's, return normalized number + # do not return $x->bnorm(), but $x + + my $x = shift; $x = $class->new($x) unless ref $x; + my ($scale,$mode) = $x->_scale_a(@_); + return $x if !defined $scale || $x->modify('bround'); # no-op + + if ($x->is_zero() || $scale == 0) + { + $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; # 3 > 2 + return $x; + } + return $x if $x->{sign} !~ /^[+-]$/; # inf, NaN + + # we have fewer digits than we want to scale to + my $len = $x->length(); + # convert $scale to a scalar in case it is an object (put's a limit on the + # number length, but this would already limited by memory constraints), makes + # it faster + $scale = $scale->numify() if ref ($scale); + + # scale < 0, but > -len (not >=!) + if (($scale < 0 && $scale < -$len-1) || ($scale >= $len)) + { + $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; # 3 > 2 + return $x; + } + + # count of 0's to pad, from left (+) or right (-): 9 - +6 => 3, or |-6| => 6 + my ($pad,$digit_round,$digit_after); + $pad = $len - $scale; + $pad = abs($scale-1) if $scale < 0; + + # do not use digit(), it is very costly for binary => decimal + # getting the entire string is also costly, but we need to do it only once + my $xs = $CALC->_str($x->{value}); + my $pl = -$pad-1; + + # pad: 123: 0 => -1, at 1 => -2, at 2 => -3, at 3 => -4 + # pad+1: 123: 0 => 0, at 1 => -1, at 2 => -2, at 3 => -3 + $digit_round = '0'; $digit_round = substr($xs,$pl,1) if $pad <= $len; + $pl++; $pl ++ if $pad >= $len; + $digit_after = '0'; $digit_after = substr($xs,$pl,1) if $pad > 0; + + # in case of 01234 we round down, for 6789 up, and only in case 5 we look + # closer at the remaining digits of the original $x, remember decision + my $round_up = 1; # default round up + $round_up -- if + ($mode eq 'trunc') || # trunc by round down + ($digit_after =~ /[01234]/) || # round down anyway, + # 6789 => round up + ($digit_after eq '5') && # not 5000...0000 + ($x->_scan_for_nonzero($pad,$xs,$len) == 0) && + ( + ($mode eq 'even') && ($digit_round =~ /[24680]/) || + ($mode eq 'odd') && ($digit_round =~ /[13579]/) || + ($mode eq '+inf') && ($x->{sign} eq '-') || + ($mode eq '-inf') && ($x->{sign} eq '+') || + ($mode eq 'zero') # round down if zero, sign adjusted below + ); + my $put_back = 0; # not yet modified + + if (($pad > 0) && ($pad <= $len)) + { + substr($xs,-$pad,$pad) = '0' x $pad; # replace with '00...' + $put_back = 1; # need to put back + } + elsif ($pad > $len) + { + $x->bzero(); # round to '0' + } + + if ($round_up) # what gave test above? + { + $put_back = 1; # need to put back + $pad = $len, $xs = '0' x $pad if $scale < 0; # tlr: whack 0.51=>1.0 + + # we modify directly the string variant instead of creating a number and + # adding it, since that is faster (we already have the string) + my $c = 0; $pad ++; # for $pad == $len case + while ($pad <= $len) + { + $c = substr($xs,-$pad,1) + 1; $c = '0' if $c eq '10'; + substr($xs,-$pad,1) = $c; $pad++; + last if $c != 0; # no overflow => early out + } + $xs = '1'.$xs if $c == 0; + + } + $x->{value} = $CALC->_new($xs) if $put_back == 1; # put back, if needed + + $x->{_a} = $scale if $scale >= 0; + if ($scale < 0) + { + $x->{_a} = $len+$scale; + $x->{_a} = 0 if $scale < -$len; + } + $x; + } + +sub bfloor + { + # round towards minus infinity; no-op since it's already integer + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $x->round(@r); + } + +sub bceil + { + # round towards plus infinity; no-op since it's already int + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $x->round(@r); + } + +sub bint { + # round towards zero; no-op since it's already integer + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + $x->round(@r); +} + +sub as_number + { + # An object might be asked to return itself as bigint on certain overloaded + # operations. This does exactly this, so that sub classes can simple inherit + # it or override with their own integer conversion routine. + $_[0]->copy(); + } + +sub as_hex + { + # return as hex string, with prefixed 0x + my $x = shift; $x = $class->new($x) if !ref($x); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + + my $s = ''; + $s = $x->{sign} if $x->{sign} eq '-'; + $s . $CALC->_as_hex($x->{value}); + } + +sub as_bin + { + # return as binary string, with prefixed 0b + my $x = shift; $x = $class->new($x) if !ref($x); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + + my $s = ''; $s = $x->{sign} if $x->{sign} eq '-'; + return $s . $CALC->_as_bin($x->{value}); + } + +sub as_oct + { + # return as octal string, with prefixed 0 + my $x = shift; $x = $class->new($x) if !ref($x); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc + + my $s = ''; $s = $x->{sign} if $x->{sign} eq '-'; + return $s . $CALC->_as_oct($x->{value}); + } + +############################################################################## +# private stuff (internal use only) + +sub objectify { + # Convert strings and "foreign objects" to the objects we want. + + # The first argument, $count, is the number of following arguments that + # objectify() looks at and converts to objects. The first is a classname. + # If the given count is 0, all arguments will be used. + + # After the count is read, objectify obtains the name of the class to which + # the following arguments are converted. If the second argument is a + # reference, use the reference type as the class name. Otherwise, if it is + # a string that looks like a class name, use that. Otherwise, use $class. + + # Caller: Gives us: + # + # $x->badd(1); => ref x, scalar y + # Class->badd(1,2); => classname x (scalar), scalar x, scalar y + # Class->badd(Class->(1),2); => classname x (scalar), ref x, scalar y + # Math::BigInt::badd(1,2); => scalar x, scalar y + + # A shortcut for the common case $x->unary_op(): + + return (ref($_[1]), $_[1]) if (@_ == 2) && ($_[0]||0 == 1) && ref($_[1]); + + # Check the context. + + unless (wantarray) { + require Carp; + Carp::croak ("${class}::objectify() needs list context"); + } + + # Get the number of arguments to objectify. + + my $count = shift; + $count ||= @_; + + # Initialize the output array. + + my @a = @_; + + # If the first argument is a reference, use that reference type as our + # class name. Otherwise, if the first argument looks like a class name, + # then use that as our class name. Otherwise, use the default class name. + + { + if (ref($a[0])) { # reference? + unshift @a, ref($a[0]); + last; + } + if ($a[0] =~ /^[A-Z].*::/) { # string with class name? + last; + } + unshift @a, $class; # default class name + } + + no strict 'refs'; + + # What we upgrade to, if anything. + + my $up = ${"$a[0]::upgrade"}; + + # Disable downgrading, because Math::BigFloat -> foo('1.0','2.0') needs + # floats. + + my $down; + if (defined ${"$a[0]::downgrade"}) { + $down = ${"$a[0]::downgrade"}; + ${"$a[0]::downgrade"} = undef; + } + + for my $i (1 .. $count) { + my $ref = ref $a[$i]; + + # Perl scalars are fed to the appropriate constructor. + + unless ($ref) { + $a[$i] = $a[0] -> new($a[$i]); + next; + } + + # If it is an object of the right class, all is fine. + + if ($ref -> isa($a[0])) { + next; + } + + # Upgrading is OK, so skip further tests if the argument is upgraded. + + if (defined $up && $ref -> isa($up)) { + next; + } + + # If we want a Math::BigInt, see if the object can become one. + # Support the old misnomer as_number(). + + if ($a[0] eq 'Math::BigInt') { + if ($a[$i] -> can('as_int')) { + $a[$i] = $a[$i] -> as_int(); + next; + } + if ($a[$i] -> can('as_number')) { + $a[$i] = $a[$i] -> as_number(); + next; + } + } + + # If we want a Math::BigFloat, see if the object can become one. + + if ($a[0] eq 'Math::BigFloat') { + if ($a[$i] -> can('as_float')) { + $a[$i] = $a[$i] -> as_float(); + next; + } + } + + # Last resort. + + $a[$i] = $a[0] -> new($a[$i]); + } + + # Reset the downgrading. + + ${"$a[0]::downgrade"} = $down; + + return @a; +} + +sub _register_callback + { + my ($class,$callback) = @_; + + if (ref($callback) ne 'CODE') + { + require Carp; + Carp::croak ("$callback is not a coderef"); + } + $CALLBACKS{$class} = $callback; + } + +sub import + { + my $self = shift; + + $IMPORT++; # remember we did import() + my @a; my $l = scalar @_; + my $warn_or_die = 0; # 0 - no warn, 1 - warn, 2 - die + for ( my $i = 0; $i < $l ; $i++ ) + { + if ($_[$i] eq ':constant') + { + # this causes overlord er load to step in + overload::constant + integer => sub { $self->new(shift) }, + binary => sub { $self->new(shift) }; + } + elsif ($_[$i] eq 'upgrade') + { + # this causes upgrading + $upgrade = $_[$i+1]; # or undef to disable + $i++; + } + elsif ($_[$i] =~ /^(lib|try|only)\z/) + { + # this causes a different low lib to take care... + $CALC = $_[$i+1] || ''; + # lib => 1 (warn on fallback), try => 0 (no warn), only => 2 (die on fallback) + $warn_or_die = 1 if $_[$i] eq 'lib'; + $warn_or_die = 2 if $_[$i] eq 'only'; + $i++; + } + else + { + push @a, $_[$i]; + } + } + # any non :constant stuff is handled by our parent, Exporter + if (@a > 0) + { + require Exporter; + + $self->SUPER::import(@a); # need it for subclasses + $self->export_to_level(1,$self,@a); # need it for MBF + } + + # try to load core math lib + my @c = split /\s*,\s*/,$CALC; + foreach (@c) + { + $_ =~ tr/a-zA-Z0-9://cd; # limit to sane characters + } + push @c, \'Calc' # if all fail, try these + if $warn_or_die < 2; # but not for "only" + $CALC = ''; # signal error + foreach my $l (@c) + { + # fallback libraries are "marked" as \'string', extract string if nec. + my $lib = $l; $lib = $$l if ref($l); + + next if ($lib || '') eq ''; + $lib = 'Math::BigInt::'.$lib if $lib !~ /^Math::BigInt/i; + $lib =~ s/\.pm$//; + if ($] < 5.006) + { + # Perl < 5.6.0 dies with "out of memory!" when eval("") and ':constant' is + # used in the same script, or eval("") inside import(). + my @parts = split /::/, $lib; # Math::BigInt => Math BigInt + my $file = pop @parts; $file .= '.pm'; # BigInt => BigInt.pm + require File::Spec; + $file = File::Spec->catfile (@parts, $file); + eval { require "$file"; $lib->import( @c ); } + } + else + { + eval "use $lib qw/@c/;"; + } + if ($@ eq '') + { + my $ok = 1; + # loaded it ok, see if the api_version() is high enough + if ($lib->can('api_version') && $lib->api_version() >= 1.0) + { + $ok = 0; + # api_version matches, check if it really provides anything we need + for my $method (qw/ + one two ten + str num + add mul div sub dec inc + acmp len digit is_one is_zero is_even is_odd + is_two is_ten + zeros new copy check + from_hex from_oct from_bin as_hex as_bin as_oct + rsft lsft xor and or + mod sqrt root fac pow modinv modpow log_int gcd + /) + { + if (!$lib->can("_$method")) + { + if (($WARN{$lib}||0) < 2) + { + require Carp; + Carp::carp ("$lib is missing method '_$method'"); + $WARN{$lib} = 1; # still warn about the lib + } + $ok++; last; + } + } + } + if ($ok == 0) + { + $CALC = $lib; + if ($warn_or_die > 0 && ref($l)) + { + require Carp; + my $msg = + "Math::BigInt: couldn't load specified math lib(s), fallback to $lib"; + Carp::carp ($msg) if $warn_or_die == 1; + Carp::croak ($msg) if $warn_or_die == 2; + } + last; # found a usable one, break + } + else + { + if (($WARN{$lib}||0) < 2) + { + my $ver = eval "\$$lib\::VERSION" || 'unknown'; + require Carp; + Carp::carp ("Cannot load outdated $lib v$ver, please upgrade"); + $WARN{$lib} = 2; # never warn again + } + } + } + } + if ($CALC eq '') + { + require Carp; + if ($warn_or_die == 2) + { + Carp::croak( + "Couldn't load specified math lib(s) and fallback disallowed"); + } + else + { + Carp::croak( + "Couldn't load any math lib(s), not even fallback to Calc.pm"); + } + } + + # notify callbacks + foreach my $class (keys %CALLBACKS) + { + &{$CALLBACKS{$class}}($CALC); + } + + # Fill $CAN with the results of $CALC->can(...) for emulating lower math lib + # functions + + %CAN = (); + for my $method (qw/ signed_and signed_or signed_xor /) + { + $CAN{$method} = $CALC->can("_$method") ? 1 : 0; + } + + # import done + } + +sub from_hex { + # Create a bigint from a hexadecimal string. + + my ($self, $str) = @_; + + if ($str =~ s/ + ^ + ( [+-]? ) + (0?x)? + ( + [0-9a-fA-F]* + ( _ [0-9a-fA-F]+ )* + ) + $ + //x) + { + # Get a "clean" version of the string, i.e., non-emtpy and with no + # underscores or invalid characters. + + my $sign = $1; + my $chrs = $3; + $chrs =~ tr/_//d; + $chrs = '0' unless CORE::length $chrs; + + # Initialize output. + + my $x = Math::BigInt->bzero(); + + # The library method requires a prefix. + + $x->{value} = $CALC->_from_hex('0x' . $chrs); + + # Place the sign. + + if ($sign eq '-' && ! $CALC->_is_zero($x->{value})) { + $x->{sign} = '-'; + } + + return $x; + } + + # CORE::hex() parses as much as it can, and ignores any trailing garbage. + # For backwards compatibility, we return NaN. + + return $self->bnan(); +} + +sub from_oct { + # Create a bigint from an octal string. + + my ($self, $str) = @_; + + if ($str =~ s/ + ^ + ( [+-]? ) + ( + [0-7]* + ( _ [0-7]+ )* + ) + $ + //x) + { + # Get a "clean" version of the string, i.e., non-emtpy and with no + # underscores or invalid characters. + + my $sign = $1; + my $chrs = $2; + $chrs =~ tr/_//d; + $chrs = '0' unless CORE::length $chrs; + + # Initialize output. + + my $x = Math::BigInt->bzero(); + + # The library method requires a prefix. + + $x->{value} = $CALC->_from_oct('0' . $chrs); + + # Place the sign. + + if ($sign eq '-' && ! $CALC->_is_zero($x->{value})) { + $x->{sign} = '-'; + } + + return $x; + } + + # CORE::oct() parses as much as it can, and ignores any trailing garbage. + # For backwards compatibility, we return NaN. + + return $self->bnan(); +} + +sub from_bin { + # Create a bigint from a binary string. + + my ($self, $str) = @_; + + if ($str =~ s/ + ^ + ( [+-]? ) + (0?b)? + ( + [01]* + ( _ [01]+ )* + ) + $ + //x) + { + # Get a "clean" version of the string, i.e., non-emtpy and with no + # underscores or invalid characters. + + my $sign = $1; + my $chrs = $3; + $chrs =~ tr/_//d; + $chrs = '0' unless CORE::length $chrs; + + # Initialize output. + + my $x = Math::BigInt->bzero(); + + # The library method requires a prefix. + + $x->{value} = $CALC->_from_bin('0b' . $chrs); + + # Place the sign. + + if ($sign eq '-' && ! $CALC->_is_zero($x->{value})) { + $x->{sign} = '-'; + } + + return $x; + } + + # For consistency with from_hex() and from_oct(), we return NaN when the + # input is invalid. + + return $self->bnan(); +} + +sub _split + { + # input: num_str; output: undef for invalid or + # (\$mantissa_sign,\$mantissa_value,\$mantissa_fraction, + # \$exp_sign,\$exp_value) + # Internal, take apart a string and return the pieces. + # Strip leading/trailing whitespace, leading zeros, underscore and reject + # invalid input. + my $x = shift; + + # strip white space at front, also extraneous leading zeros + $x =~ s/^\s*([-]?)0*([0-9])/$1$2/g; # will not strip ' .2' + $x =~ s/^\s+//; # but this will + $x =~ s/\s+$//g; # strip white space at end + + # shortcut, if nothing to split, return early + if ($x =~ /^[+-]?[0-9]+\z/) + { + $x =~ s/^([+-])0*([0-9])/$2/; my $sign = $1 || '+'; + return (\$sign, \$x, \'', \'', \0); + } + + # invalid starting char? + return if $x !~ /^[+-]?(\.?[0-9]|0b[0-1]|0x[0-9a-fA-F])/; + + return Math::BigInt->from_hex($x) if $x =~ /^[+-]?0x/; # hex string + return Math::BigInt->from_bin($x) if $x =~ /^[+-]?0b/; # binary string + + # strip underscores between digits + $x =~ s/([0-9])_([0-9])/$1$2/g; + $x =~ s/([0-9])_([0-9])/$1$2/g; # do twice for 1_2_3 + + # some possible inputs: + # 2.1234 # 0.12 # 1 # 1E1 # 2.134E1 # 434E-10 # 1.02009E-2 + # .2 # 1_2_3.4_5_6 # 1.4E1_2_3 # 1e3 # +.2 # 0e999 + + my ($m,$e,$last) = split /[Ee]/,$x; + return if defined $last; # last defined => 1e2E3 or others + $e = '0' if !defined $e || $e eq ""; + + # sign,value for exponent,mantint,mantfrac + my ($es,$ev,$mis,$miv,$mfv); + # valid exponent? + if ($e =~ /^([+-]?)0*([0-9]+)$/) # strip leading zeros + { + $es = $1; $ev = $2; + # valid mantissa? + return if $m eq '.' || $m eq ''; + my ($mi,$mf,$lastf) = split /\./,$m; + return if defined $lastf; # lastf defined => 1.2.3 or others + $mi = '0' if !defined $mi; + $mi .= '0' if $mi =~ /^[\-\+]?$/; + $mf = '0' if !defined $mf || $mf eq ''; + if ($mi =~ /^([+-]?)0*([0-9]+)$/) # strip leading zeros + { + $mis = $1||'+'; $miv = $2; + return unless ($mf =~ /^([0-9]*?)0*$/); # strip trailing zeros + $mfv = $1; + # handle the 0e999 case here + $ev = 0 if $miv eq '0' && $mfv eq ''; + return (\$mis,\$miv,\$mfv,\$es,\$ev); + } + } + return; # NaN, not a number + } + +############################################################################## +# internal calculation routines (others are in Math::BigInt::Calc etc) + +sub __lcm + { + # (BINT or num_str, BINT or num_str) return BINT + # does modify first argument + # LCM + + my ($x,$ty) = @_; + return $x->bnan() if ($x->{sign} eq $nan) || ($ty->{sign} eq $nan); + my $method = ref($x) . '::bgcd'; + no strict 'refs'; + $x * $ty / &$method($x,$ty); + } + +############################################################################### +# trigonometric functions + +sub bpi + { + # Calculate PI to N digits. Unless upgrading is in effect, returns the + # result truncated to an integer, that is, always returns '3'. + my ($self,$n) = @_; + if (@_ == 1) + { + # called like Math::BigInt::bpi(10); + $n = $self; $self = $class; + } + $self = ref($self) if ref($self); + + return $upgrade->new($n) if defined $upgrade; + + # hard-wired to "3" + $self->new(3); + } + +sub bcos + { + # Calculate cosinus(x) to N digits. Unless upgrading is in effect, returns the + # result truncated to an integer. + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bcos'); + + return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN + + return $upgrade->new($x)->bcos(@r) if defined $upgrade; + + require Math::BigFloat; + # calculate the result and truncate it to integer + my $t = Math::BigFloat->new($x)->bcos(@r)->as_int(); + + $x->bone() if $t->is_one(); + $x->bzero() if $t->is_zero(); + $x->round(@r); + } + +sub bsin + { + # Calculate sinus(x) to N digits. Unless upgrading is in effect, returns the + # result truncated to an integer. + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('bsin'); + + return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN + + return $upgrade->new($x)->bsin(@r) if defined $upgrade; + + require Math::BigFloat; + # calculate the result and truncate it to integer + my $t = Math::BigFloat->new($x)->bsin(@r)->as_int(); + + $x->bone() if $t->is_one(); + $x->bzero() if $t->is_zero(); + $x->round(@r); + } + +sub batan2 + { + # calculate arcus tangens of ($y/$x) + + # set up parameters + my ($self,$y,$x,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$y,$x,@r) = objectify(2,@_); + } + + return $y if $y->modify('batan2'); + + return $y->bnan() if ($y->{sign} eq $nan) || ($x->{sign} eq $nan); + + # Y X + # != 0 -inf result is +- pi + if ($x->is_inf() || $y->is_inf()) + { + # upgrade to BigFloat etc. + return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; + if ($y->is_inf()) + { + if ($x->{sign} eq '-inf') + { + # calculate 3 pi/4 => 2.3.. => 2 + $y->bone( substr($y->{sign},0,1) ); + $y->bmul($self->new(2)); + } + elsif ($x->{sign} eq '+inf') + { + # calculate pi/4 => 0.7 => 0 + $y->bzero(); + } + else + { + # calculate pi/2 => 1.5 => 1 + $y->bone( substr($y->{sign},0,1) ); + } + } + else + { + if ($x->{sign} eq '+inf') + { + # calculate pi/4 => 0.7 => 0 + $y->bzero(); + } + else + { + # PI => 3.1415.. => 3 + $y->bone( substr($y->{sign},0,1) ); + $y->bmul($self->new(3)); + } + } + return $y; + } + + return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; + + require Math::BigFloat; + my $r = Math::BigFloat->new($y) + ->batan2(Math::BigFloat->new($x),@r) + ->as_int(); + + $x->{value} = $r->{value}; + $x->{sign} = $r->{sign}; + + $x; + } + +sub batan + { + # Calculate arcus tangens of x to N digits. Unless upgrading is in effect, returns the + # result truncated to an integer. + my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); + + return $x if $x->modify('batan'); + + return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN + + return $upgrade->new($x)->batan(@r) if defined $upgrade; + + # calculate the result and truncate it to integer + my $t = Math::BigFloat->new($x)->batan(@r); + + $x->{value} = $CALC->_new( $x->as_int()->bstr() ); + $x->round(@r); + } + +############################################################################### +# this method returns 0 if the object can be modified, or 1 if not. +# We use a fast constant sub() here, to avoid costly calls. Subclasses +# may override it with special code (f.i. Math::BigInt::Constant does so) + +sub modify () { 0; } + +1; +__END__ + +=pod + +=head1 NAME + +Math::BigInt - Arbitrary size integer/float math package + +=head1 SYNOPSIS + + use Math::BigInt; + + # or make it faster with huge numbers: install (optional) + # Math::BigInt::GMP and always use (it will fall back to + # pure Perl if the GMP library is not installed): + # (See also the L section!) + + # will warn if Math::BigInt::GMP cannot be found + use Math::BigInt lib => 'GMP'; + + # to suppress the warning use this: + # use Math::BigInt try => 'GMP'; + + # dies if GMP cannot be loaded: + # use Math::BigInt only => 'GMP'; + + my $str = '1234567890'; + my @values = (64,74,18); + my $n = 1; my $sign = '-'; + + # Number creation + my $x = Math::BigInt->new($str); # defaults to 0 + my $y = $x->copy(); # make a true copy + my $nan = Math::BigInt->bnan(); # create a NotANumber + my $zero = Math::BigInt->bzero(); # create a +0 + my $inf = Math::BigInt->binf(); # create a +inf + my $inf = Math::BigInt->binf('-'); # create a -inf + my $one = Math::BigInt->bone(); # create a +1 + my $mone = Math::BigInt->bone('-'); # create a -1 + + my $pi = Math::BigInt->bpi(); # returns '3' + # see Math::BigFloat::bpi() + + $h = Math::BigInt->new('0x123'); # from hexadecimal + $b = Math::BigInt->new('0b101'); # from binary + $o = Math::BigInt->from_oct('0101'); # from octal + + # Testing (don't modify their arguments) + # (return true if the condition is met, otherwise false) + + $x->is_zero(); # if $x is +0 + $x->is_nan(); # if $x is NaN + $x->is_one(); # if $x is +1 + $x->is_one('-'); # if $x is -1 + $x->is_odd(); # if $x is odd + $x->is_even(); # if $x is even + $x->is_pos(); # if $x > 0 + $x->is_neg(); # if $x < 0 + $x->is_inf($sign); # if $x is +inf, or -inf (sign is default '+') + $x->is_int(); # if $x is an integer (not a float) + + # comparing and digit/sign extraction + $x->bcmp($y); # compare numbers (undef,<0,=0,>0) + $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) + $x->sign(); # return the sign, either +,- or NaN + $x->digit($n); # return the nth digit, counting from right + $x->digit(-$n); # return the nth digit, counting from left + + # The following all modify their first argument. If you want to pre- + # serve $x, use $z = $x->copy()->bXXX($y); See under L for + # why this is necessary when mixing $a = $b assignments with non-over- + # loaded math. + + $x->bzero(); # set $x to 0 + $x->bnan(); # set $x to NaN + $x->bone(); # set $x to +1 + $x->bone('-'); # set $x to -1 + $x->binf(); # set $x to inf + $x->binf('-'); # set $x to -inf + + $x->bneg(); # negation + $x->babs(); # absolute value + $x->bsgn(); # sign function (-1, 0, 1, or NaN) + $x->bnorm(); # normalize (no-op in BigInt) + $x->bnot(); # two's complement (bit wise not) + $x->binc(); # increment $x by 1 + $x->bdec(); # decrement $x by 1 + + $x->badd($y); # addition (add $y to $x) + $x->bsub($y); # subtraction (subtract $y from $x) + $x->bmul($y); # multiplication (multiply $x by $y) + $x->bdiv($y); # divide, set $x to quotient + # return (quo,rem) or quo if scalar + + $x->bmuladd($y,$z); # $x = $x * $y + $z + + $x->bmod($y); # modulus (x % y) + $x->bmodpow($y,$mod); # modular exponentiation (($x ** $y) % $mod) + $x->bmodinv($mod); # modular multiplicative inverse + $x->bpow($y); # power of arguments (x ** y) + $x->blsft($y); # left shift in base 2 + $x->brsft($y); # right shift in base 2 + # returns (quo,rem) or quo if in sca- + # lar context + $x->blsft($y,$n); # left shift by $y places in base $n + $x->brsft($y,$n); # right shift by $y places in base $n + # returns (quo,rem) or quo if in sca- + # lar context + + $x->band($y); # bitwise and + $x->bior($y); # bitwise inclusive or + $x->bxor($y); # bitwise exclusive or + $x->bnot(); # bitwise not (two's complement) + + $x->bsqrt(); # calculate square-root + $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root) + $x->bfac(); # factorial of $x (1*2*3*4*..$x) + + $x->bnok($y); # x over y (binomial coefficient n over k) + + $x->blog(); # logarithm of $x to base e (Euler's number) + $x->blog($base); # logarithm of $x to base $base (f.i. 2) + $x->bexp(); # calculate e ** $x where e is Euler's number + + $x->round($A,$P,$mode); # round to accuracy or precision using + # mode $mode + $x->bround($n); # accuracy: preserve $n digits + $x->bfround($n); # $n > 0: round $nth digits, + # $n < 0: round to the $nth digit after the + # dot, no-op for BigInts + + # The following do not modify their arguments in BigInt (are no-ops), + # but do so in BigFloat: + + $x->bfloor(); # round towards minus infinity + $x->bceil(); # round towards plus infinity + $x->bint(); # round towards zero + + # The following do not modify their arguments: + + # greatest common divisor (no OO style) + my $gcd = Math::BigInt::bgcd(@values); + # lowest common multiple (no OO style) + my $lcm = Math::BigInt::blcm(@values); + + $x->length(); # return number of digits in number + ($xl,$f) = $x->length(); # length of number and length of fraction + # part, latter is always 0 digits long + # for BigInts + + $x->exponent(); # return exponent as BigInt + $x->mantissa(); # return (signed) mantissa as BigInt + $x->parts(); # return (mantissa,exponent) as BigInt + $x->copy(); # make a true copy of $x (unlike $y = $x;) + $x->as_int(); # return as BigInt (in BigInt: same as copy()) + $x->numify(); # return as scalar (might overflow!) + + # conversion to string (do not modify their argument) + $x->bstr(); # normalized string (e.g. '3') + $x->bsstr(); # norm. string in scientific notation (e.g. '3E0') + $x->as_hex(); # as signed hexadecimal string with prefixed 0x + $x->as_bin(); # as signed binary string with prefixed 0b + $x->as_oct(); # as signed octal string with prefixed 0 + + + # precision and accuracy (see section about rounding for more) + $x->precision(); # return P of $x (or global, if P of $x undef) + $x->precision($n); # set P of $x to $n + $x->accuracy(); # return A of $x (or global, if A of $x undef) + $x->accuracy($n); # set A $x to $n + + # Global methods + Math::BigInt->precision(); # get/set global P for all BigInt objects + Math::BigInt->accuracy(); # get/set global A for all BigInt objects + Math::BigInt->round_mode(); # get/set global round mode, one of + # 'even', 'odd', '+inf', '-inf', 'zero', + # 'trunc' or 'common' + Math::BigInt->config(); # return hash containing configuration + +=head1 DESCRIPTION + +All operators (including basic math operations) are overloaded if you +declare your big integers as + + $i = new Math::BigInt '123_456_789_123_456_789'; + +Operations with overloaded operators preserve the arguments which is +exactly what you expect. + +=head2 Input + +Input values to these routines may be any string, that looks like a number +and results in an integer, including hexadecimal and binary numbers. + +Scalars holding numbers may also be passed, but note that non-integer numbers +may already have lost precision due to the conversion to float. Quote +your input if you want BigInt to see all the digits: + + $x = Math::BigInt->new(12345678890123456789); # bad + $x = Math::BigInt->new('12345678901234567890'); # good + +You can include one underscore between any two digits. + +This means integer values like 1.01E2 or even 1000E-2 are also accepted. +Non-integer values result in NaN. + +Hexadecimal (prefixed with "0x") and binary numbers (prefixed with "0b") +are accepted, too. Please note that octal numbers are not recognized +by new(), so the following will print "123": + + perl -MMath::BigInt -le 'print Math::BigInt->new("0123")' + +To convert an octal number, use from_oct(); + + perl -MMath::BigInt -le 'print Math::BigInt->from_oct("0123")' + +Currently, Math::BigInt::new() defaults to 0, while Math::BigInt::new('') +results in 'NaN'. This might change in the future, so use always the following +explicit forms to get a zero or NaN: + + $zero = Math::BigInt->bzero(); + $nan = Math::BigInt->bnan(); + +C on a BigInt object is now effectively a no-op, since the numbers +are always stored in normalized form. If passed a string, creates a BigInt +object from the input. + +=head2 Output + +Output values are BigInt objects (normalized), except for the methods which +return a string (see L). + +Some routines (C, C, C, C, +C, etc.) return true or false, while others (C, C) +return either undef (if NaN is involved), <0, 0 or >0 and are suited for sort. + +=head1 METHODS + +Each of the methods below (except config(), accuracy() and precision()) +accepts three additional parameters. These arguments C<$A>, C<$P> and C<$R> +are C, C and C. Please see the section about +L for more information. + +=over + +=item config() + + use Data::Dumper; + + print Dumper ( Math::BigInt->config() ); + print Math::BigInt->config()->{lib},"\n"; + +Returns a hash containing the configuration, e.g. the version number, lib +loaded etc. The following hash keys are currently filled in with the +appropriate information. + + key Description + Example + ============================================================ + lib Name of the low-level math library + Math::BigInt::Calc + lib_version Version of low-level math library (see 'lib') + 0.30 + class The class name of config() you just called + Math::BigInt + upgrade To which class math operations might be + upgraded Math::BigFloat + downgrade To which class math operations might be + downgraded undef + precision Global precision + undef + accuracy Global accuracy + undef + round_mode Global round mode + even + version version number of the class you used + 1.61 + div_scale Fallback accuracy for div + 40 + trap_nan If true, traps creation of NaN via croak() + 1 + trap_inf If true, traps creation of +inf/-inf via croak() + 1 + +The following values can be set by passing C a reference to a hash: + + trap_inf trap_nan + upgrade downgrade precision accuracy round_mode div_scale + +Example: + + $new_cfg = Math::BigInt->config( + { trap_inf => 1, precision => 5 } + ); + +=item accuracy() + + $x->accuracy(5); # local for $x + CLASS->accuracy(5); # global for all members of CLASS + # Note: This also applies to new()! + + $A = $x->accuracy(); # read out accuracy that affects $x + $A = CLASS->accuracy(); # read out global accuracy + +Set or get the global or local accuracy, aka how many significant digits the +results have. If you set a global accuracy, then this also applies to new()! + +Warning! The accuracy I, e.g. once you created a number under the +influence of C<< CLASS->accuracy($A) >>, all results from math operations with +that number will also be rounded. + +In most cases, you should probably round the results explicitly using one of +L, L or L or by passing the desired accuracy +to the math operation as additional parameter: + + my $x = Math::BigInt->new(30000); + my $y = Math::BigInt->new(7); + print scalar $x->copy()->bdiv($y, 2); # print 4300 + print scalar $x->copy()->bdiv($y)->bround(2); # print 4300 + +Please see the section about L for further details. + +Value must be greater than zero. Pass an undef value to disable it: + + $x->accuracy(undef); + Math::BigInt->accuracy(undef); + +Returns the current accuracy. For C<< $x->accuracy() >> it will return either +the local accuracy, or if not defined, the global. This means the return value +represents the accuracy that will be in effect for $x: + + $y = Math::BigInt->new(1234567); # unrounded + print Math::BigInt->accuracy(4),"\n"; # set 4, print 4 + $x = Math::BigInt->new(123456); # $x will be automatic- + # ally rounded! + print "$x $y\n"; # '123500 1234567' + print $x->accuracy(),"\n"; # will be 4 + print $y->accuracy(),"\n"; # also 4, since + # global is 4 + print Math::BigInt->accuracy(5),"\n"; # set to 5, print 5 + print $x->accuracy(),"\n"; # still 4 + print $y->accuracy(),"\n"; # 5, since global is 5 + +Note: Works also for subclasses like Math::BigFloat. Each class has it's own +globals separated from Math::BigInt, but it is possible to subclass +Math::BigInt and make the globals of the subclass aliases to the ones from +Math::BigInt. + +=item precision() + + $x->precision(-2); # local for $x, round at the second + # digit right of the dot + $x->precision(2); # ditto, round at the second digit + # left of the dot + + CLASS->precision(5); # Global for all members of CLASS + # This also applies to new()! + CLASS->precision(-5); # ditto + + $P = CLASS->precision(); # read out global precision + $P = $x->precision(); # read out precision that affects $x + +Note: You probably want to use L instead. With L you +set the number of digits each result should have, with L you +set the place where to round! + +C sets or gets the global or local precision, aka at which digit +before or after the dot to round all results. A set global precision also +applies to all newly created numbers! + +In Math::BigInt, passing a negative number precision has no effect since no +numbers have digits after the dot. In L, it will round all +results to P digits after the dot. + +Please see the section about L for further details. + +Pass an undef value to disable it: + + $x->precision(undef); + Math::BigInt->precision(undef); + +Returns the current precision. For C<< $x->precision() >> it will return either +the local precision of $x, or if not defined, the global. This means the return +value represents the prevision that will be in effect for $x: + + $y = Math::BigInt->new(1234567); # unrounded + print Math::BigInt->precision(4),"\n"; # set 4, print 4 + $x = Math::BigInt->new(123456); # will be automatically rounded + print $x; # print "120000"! + +Note: Works also for subclasses like L. Each class has its +own globals separated from Math::BigInt, but it is possible to subclass +Math::BigInt and make the globals of the subclass aliases to the ones from +Math::BigInt. + +=item brsft() + + $x->brsft($y,$n); + +Shifts $x right by $y in base $n. Default is base 2, used are usually 10 and +2, but others work, too. + +Right shifting usually amounts to dividing $x by $n ** $y and truncating the +result: + + + $x = Math::BigInt->new(10); + $x->brsft(1); # same as $x >> 1: 5 + $x = Math::BigInt->new(1234); + $x->brsft(2,10); # result 12 + +There is one exception, and that is base 2 with negative $x: + + + $x = Math::BigInt->new(-5); + print $x->brsft(1); + +This will print -3, not -2 (as it would if you divide -5 by 2 and truncate the +result). + +=item new() + + $x = Math::BigInt->new($str,$A,$P,$R); + +Creates a new BigInt object from a scalar or another BigInt object. The +input is accepted as decimal, hex (with leading '0x') or binary (with leading +'0b'). + +See L for more info on accepted input formats. + +=item from_oct() + + $x = Math::BigInt->from_oct("0775"); # input is octal + +Interpret the input as an octal string and return the corresponding value. A +"0" (zero) prefix is optional. A single underscore character may be placed +right after the prefix, if present, or between any two digits. If the input is +invalid, a NaN is returned. + +=item from_hex() + + $x = Math::BigInt->from_hex("0xcafe"); # input is hexadecimal + +Interpret input as a hexadecimal string. A "0x" or "x" prefix is optional. A +single underscore character may be placed right after the prefix, if present, +or between any two digits. If the input is invalid, a NaN is returned. + +=item from_bin() + + $x = Math::BigInt->from_bin("0b10011"); # input is binary + +Interpret the input as a binary string. A "0b" or "b" prefix is optional. A +single underscore character may be placed right after the prefix, if present, +or between any two digits. If the input is invalid, a NaN is returned. + +=item bnan() + + $x = Math::BigInt->bnan(); + +Creates a new BigInt object representing NaN (Not A Number). +If used on an object, it will set it to NaN: + + $x->bnan(); + +=item bzero() + + $x = Math::BigInt->bzero(); + +Creates a new BigInt object representing zero. +If used on an object, it will set it to zero: + + $x->bzero(); + +=item binf() + + $x = Math::BigInt->binf($sign); + +Creates a new BigInt object representing infinity. The optional argument is +either '-' or '+', indicating whether you want infinity or minus infinity. +If used on an object, it will set it to infinity: + + $x->binf(); + $x->binf('-'); + +=item bone() + + $x = Math::BigInt->binf($sign); + +Creates a new BigInt object representing one. The optional argument is +either '-' or '+', indicating whether you want one or minus one. +If used on an object, it will set it to one: + + $x->bone(); # +1 + $x->bone('-'); # -1 + +=item is_one()/is_zero()/is_nan()/is_inf() + + $x->is_zero(); # true if arg is +0 + $x->is_nan(); # true if arg is NaN + $x->is_one(); # true if arg is +1 + $x->is_one('-'); # true if arg is -1 + $x->is_inf(); # true if +inf + $x->is_inf('-'); # true if -inf (sign is default '+') + +These methods all test the BigInt for being one specific value and return +true or false depending on the input. These are faster than doing something +like: + + if ($x == 0) + +=item is_pos()/is_neg()/is_positive()/is_negative() + + $x->is_pos(); # true if > 0 + $x->is_neg(); # true if < 0 + +The methods return true if the argument is positive or negative, respectively. +C is neither positive nor negative, while C<+inf> counts as positive, and +C<-inf> is negative. A C is neither positive nor negative. + +These methods are only testing the sign, and not the value. + +C and C are aliases to C and +C, respectively. C and C were +introduced in v1.36, while C and C were only introduced +in v1.68. + +=item is_odd()/is_even()/is_int() + + $x->is_odd(); # true if odd, false for even + $x->is_even(); # true if even, false for odd + $x->is_int(); # true if $x is an integer + +The return true when the argument satisfies the condition. C, C<+inf>, +C<-inf> are not integers and are neither odd nor even. + +In BigInt, all numbers except C, C<+inf> and C<-inf> are integers. + +=item bcmp() + + $x->bcmp($y); + +Compares $x with $y and takes the sign into account. +Returns -1, 0, 1 or undef. + +=item bacmp() + + $x->bacmp($y); + +Compares $x with $y while ignoring their sign. Returns -1, 0, 1 or undef. + +=item sign() + + $x->sign(); + +Return the sign, of $x, meaning either C<+>, C<->, C<-inf>, C<+inf> or NaN. + +If you want $x to have a certain sign, use one of the following methods: + + $x->babs(); # '+' + $x->babs()->bneg(); # '-' + $x->bnan(); # 'NaN' + $x->binf(); # '+inf' + $x->binf('-'); # '-inf' + +=item digit() + + $x->digit($n); # return the nth digit, counting from right + +If C<$n> is negative, returns the digit counting from left. + +=item bneg() + + $x->bneg(); + +Negate the number, e.g. change the sign between '+' and '-', or between '+inf' +and '-inf', respectively. Does nothing for NaN or zero. + +=item babs() + + $x->babs(); + +Set the number to its absolute value, e.g. change the sign from '-' to '+' +and from '-inf' to '+inf', respectively. Does nothing for NaN or positive +numbers. + +=item bsgn() + + $x->bsgn(); + +Signum function. Set the number to -1, 0, or 1, depending on whether the +number is negative, zero, or positive, respectively. Does not modify NaNs. + +=item bnorm() + + $x->bnorm(); # normalize (no-op) + +=item bnot() + + $x->bnot(); + +Two's complement (bitwise not). This is equivalent to + + $x->binc()->bneg(); + +but faster. + +=item binc() + + $x->binc(); # increment x by 1 + +=item bdec() + + $x->bdec(); # decrement x by 1 + +=item badd() + + $x->badd($y); # addition (add $y to $x) + +=item bsub() + + $x->bsub($y); # subtraction (subtract $y from $x) + +=item bmul() + + $x->bmul($y); # multiplication (multiply $x by $y) + +=item bmuladd() + + $x->bmuladd($y,$z); + +Multiply $x by $y, and then add $z to the result, + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item bdiv() + + $x->bdiv($y); # divide, set $x to quotient + +Returns $x divided by $y. In list context, does floored division (F-division), +where the quotient is the greatest integer less than or equal to the quotient +of the two operands. Consequently, the remainder is either zero or has the same +sign as the second operand. In scalar context, only the quotient is returned. + +=item bmod() + + $x->bmod($y); # modulus (x % y) + +Returns $x modulo $y. When $x is finite, and $y is finite and non-zero, the +result is identical to the remainder after floored division (F-division), i.e., +identical to the result from Perl's % operator. + +=item bmodinv() + + $x->bmodinv($mod); # modular multiplicative inverse + +Returns the multiplicative inverse of C<$x> modulo C<$mod>. If + + $y = $x -> copy() -> bmodinv($mod) + +then C<$y> is the number closest to zero, and with the same sign as C<$mod>, +satisfying + + ($x * $y) % $mod = 1 % $mod + +If C<$x> and C<$y> are non-zero, they must be relative primes, i.e., +C. 'C' is returned when no modular multiplicative +inverse exists. + +=item bmodpow() + + $num->bmodpow($exp,$mod); # modular exponentiation + # ($num**$exp % $mod) + +Returns the value of C<$num> taken to the power C<$exp> in the modulus +C<$mod> using binary exponentiation. C is far superior to +writing + + $num ** $exp % $mod + +because it is much faster - it reduces internal variables into +the modulus whenever possible, so it operates on smaller numbers. + +C also supports negative exponents. + + bmodpow($num, -1, $mod) + +is exactly equivalent to + + bmodinv($num, $mod) + +=item bpow() + + $x->bpow($y); # power of arguments (x ** y) + +=item blog() + + $x->blog($base, $accuracy); # logarithm of x to the base $base + +If C<$base> is not defined, Euler's number (e) is used: + + print $x->blog(undef, 100); # log(x) to 100 digits + +=item bexp() + + $x->bexp($accuracy); # calculate e ** X + +Calculates the expression C where C is Euler's number. + +This method was added in v1.82 of Math::BigInt (April 2007). + +See also L. + +=item bnok() + + $x->bnok($y); # x over y (binomial coefficient n over k) + +Calculates the binomial coefficient n over k, also called the "choose" +function. The result is equivalent to: + + ( n ) n! + | - | = ------- + ( k ) k!(n-k)! + +This method was added in v1.84 of Math::BigInt (April 2007). + +=item bpi() + + print Math::BigInt->bpi(100), "\n"; # 3 + +Returns PI truncated to an integer, with the argument being ignored. This means +under BigInt this always returns C<3>. + +If upgrading is in effect, returns PI, rounded to N digits with the +current rounding mode: + + use Math::BigFloat; + use Math::BigInt upgrade => Math::BigFloat; + print Math::BigInt->bpi(3), "\n"; # 3.14 + print Math::BigInt->bpi(100), "\n"; # 3.1415.... + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item bcos() + + my $x = Math::BigInt->new(1); + print $x->bcos(100), "\n"; + +Calculate the cosinus of $x, modifying $x in place. + +In BigInt, unless upgrading is in effect, the result is truncated to an +integer. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item bsin() + + my $x = Math::BigInt->new(1); + print $x->bsin(100), "\n"; + +Calculate the sinus of $x, modifying $x in place. + +In BigInt, unless upgrading is in effect, the result is truncated to an +integer. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item batan2() + + my $x = Math::BigInt->new(1); + my $y = Math::BigInt->new(1); + print $y->batan2($x), "\n"; + +Calculate the arcus tangens of C<$y> divided by C<$x>, modifying $y in place. + +In BigInt, unless upgrading is in effect, the result is truncated to an +integer. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item batan() + + my $x = Math::BigFloat->new(0.5); + print $x->batan(100), "\n"; + +Calculate the arcus tangens of $x, modifying $x in place. + +In BigInt, unless upgrading is in effect, the result is truncated to an +integer. + +This method was added in v1.87 of Math::BigInt (June 2007). + +=item blsft() + + $x->blsft($y); # left shift in base 2 + $x->blsft($y,$n); # left shift, in base $n (like 10) + +=item brsft() + + $x->brsft($y); # right shift in base 2 + $x->brsft($y,$n); # right shift, in base $n (like 10) + +=item band() + + $x->band($y); # bitwise and + +=item bior() + + $x->bior($y); # bitwise inclusive or + +=item bxor() + + $x->bxor($y); # bitwise exclusive or + +=item bnot() + + $x->bnot(); # bitwise not (two's complement) + +=item bsqrt() + + $x->bsqrt(); # calculate square-root + +=item broot() + + $x->broot($N); + +Calculates the N'th root of C<$x>. + +=item bfac() + + $x->bfac(); # factorial of $x (1*2*3*4*..$x) + +=item round() + + $x->round($A,$P,$round_mode); + +Round $x to accuracy C<$A> or precision C<$P> using the round mode +C<$round_mode>. + +=item bround() + + $x->bround($N); # accuracy: preserve $N digits + +=item bfround() + + $x->bfround($N); + +If N is > 0, rounds to the Nth digit from the left. If N < 0, rounds to +the Nth digit after the dot. Since BigInts are integers, the case N < 0 +is a no-op for them. + +Examples: + + Input N Result + =================================================== + 123456.123456 3 123500 + 123456.123456 2 123450 + 123456.123456 -2 123456.12 + 123456.123456 -3 123456.123 + +=item bfloor() + + $x->bfloor(); + +Round $x towards minus infinity (i.e., set $x to the largest integer less than +or equal to $x). This is a no-op in BigInt, but changes $x in BigFloat, if $x +is not an integer. + +=item bceil() + + $x->bceil(); + +Round $x towards plus infinity (i.e., set $x to the smallest integer greater +than or equal to $x). This is a no-op in BigInt, but changes $x in BigFloat, if +$x is not an integer. + +=item bint() + + $x->bint(); + +Round $x towards zero. This is a no-op in BigInt, but changes $x in BigFloat, +if $x is not an integer. + +=item bgcd() + + bgcd(@values); # greatest common divisor (no OO style) + +=item blcm() + + blcm(@values); # lowest common multiple (no OO style) + +=item length() + + $x->length(); + ($xl,$fl) = $x->length(); + +Returns the number of digits in the decimal representation of the number. +In list context, returns the length of the integer and fraction part. For +BigInt's, the length of the fraction part will always be 0. + +=item exponent() + + $x->exponent(); + +Return the exponent of $x as BigInt. + +=item mantissa() + + $x->mantissa(); + +Return the signed mantissa of $x as BigInt. + +=item parts() + + $x->parts(); # return (mantissa,exponent) as BigInt + +=item copy() + + $x->copy(); # make a true copy of $x (unlike $y = $x;) + +=item as_int()/as_number() + + $x->as_int(); + +Returns $x as a BigInt (truncated towards zero). In BigInt this is the same as +C. + +C is an alias to this method. C was introduced in +v1.22, while C was only introduced in v1.68. + +=item bstr() + + $x->bstr(); + +Returns a normalized string representation of C<$x>. + +=item bsstr() + + $x->bsstr(); # normalized string in scientific notation + +=item as_hex() + + $x->as_hex(); # as signed hexadecimal string with prefixed 0x + +=item as_bin() + + $x->as_bin(); # as signed binary string with prefixed 0b + +=item as_oct() + + $x->as_oct(); # as signed octal string with prefixed 0 + +=item numify() + + print $x->numify(); + +This returns a normal Perl scalar from $x. It is used automatically +whenever a scalar is needed, for instance in array index operations. + +This loses precision, to avoid this use L instead. + +=item modify() + + $x->modify('bpowd'); + +This method returns 0 if the object can be modified with the given +operation, or 1 if not. + +This is used for instance by L. + +=item upgrade()/downgrade() + +Set/get the class for downgrade/upgrade operations. Thuis is used +for instance by L. The defaults are '', thus the following +operation will create a BigInt, not a BigFloat: + + my $i = Math::BigInt->new(123); + my $f = Math::BigFloat->new('123.1'); + + print $i + $f,"\n"; # print 246 + +=item div_scale() + +Set/get the number of digits for the default precision in divide +operations. + +=item round_mode() + +Set/get the current round mode. + +=back + +=head1 ACCURACY and PRECISION + +Since version v1.33, Math::BigInt and Math::BigFloat have full support for +accuracy and precision based rounding, both automatically after every +operation, as well as manually. + +This section describes the accuracy/precision handling in Math::Big* as it +used to be and as it is now, complete with an explanation of all terms and +abbreviations. + +Not yet implemented things (but with correct description) are marked with '!', +things that need to be answered are marked with '?'. + +In the next paragraph follows a short description of terms used here (because +these may differ from terms used by others people or documentation). + +During the rest of this document, the shortcuts A (for accuracy), P (for +precision), F (fallback) and R (rounding mode) will be used. + +=head2 Precision P + +A fixed number of digits before (positive) or after (negative) +the decimal point. For example, 123.45 has a precision of -2. 0 means an +integer like 123 (or 120). A precision of 2 means two digits to the left +of the decimal point are zero, so 123 with P = 1 becomes 120. Note that +numbers with zeros before the decimal point may have different precisions, +because 1200 can have p = 0, 1 or 2 (depending on what the initial value +was). It could also have p < 0, when the digits after the decimal point +are zero. + +The string output (of floating point numbers) will be padded with zeros: + + Initial value P A Result String + ------------------------------------------------------------ + 1234.01 -3 1000 1000 + 1234 -2 1200 1200 + 1234.5 -1 1230 1230 + 1234.001 1 1234 1234.0 + 1234.01 0 1234 1234 + 1234.01 2 1234.01 1234.01 + 1234.01 5 1234.01 1234.01000 + +For BigInts, no padding occurs. + +=head2 Accuracy A + +Number of significant digits. Leading zeros are not counted. A +number may have an accuracy greater than the non-zero digits +when there are zeros in it or trailing zeros. For example, 123.456 has +A of 6, 10203 has 5, 123.0506 has 7, 123.450000 has 8 and 0.000123 has 3. + +The string output (of floating point numbers) will be padded with zeros: + + Initial value P A Result String + ------------------------------------------------------------ + 1234.01 3 1230 1230 + 1234.01 6 1234.01 1234.01 + 1234.1 8 1234.1 1234.1000 + +For BigInts, no padding occurs. + +=head2 Fallback F + +When both A and P are undefined, this is used as a fallback accuracy when +dividing numbers. + +=head2 Rounding mode R + +When rounding a number, different 'styles' or 'kinds' +of rounding are possible. (Note that random rounding, as in +Math::Round, is not implemented.) + +=over + +=item 'trunc' + +truncation invariably removes all digits following the +rounding place, replacing them with zeros. Thus, 987.65 rounded +to tens (P=1) becomes 980, and rounded to the fourth sigdig +becomes 987.6 (A=4). 123.456 rounded to the second place after the +decimal point (P=-2) becomes 123.46. + +All other implemented styles of rounding attempt to round to the +"nearest digit." If the digit D immediately to the right of the +rounding place (skipping the decimal point) is greater than 5, the +number is incremented at the rounding place (possibly causing a +cascade of incrementation): e.g. when rounding to units, 0.9 rounds +to 1, and -19.9 rounds to -20. If D < 5, the number is similarly +truncated at the rounding place: e.g. when rounding to units, 0.4 +rounds to 0, and -19.4 rounds to -19. + +However the results of other styles of rounding differ if the +digit immediately to the right of the rounding place (skipping the +decimal point) is 5 and if there are no digits, or no digits other +than 0, after that 5. In such cases: + +=item 'even' + +rounds the digit at the rounding place to 0, 2, 4, 6, or 8 +if it is not already. E.g., when rounding to the first sigdig, 0.45 +becomes 0.4, -0.55 becomes -0.6, but 0.4501 becomes 0.5. + +=item 'odd' + +rounds the digit at the rounding place to 1, 3, 5, 7, or 9 if +it is not already. E.g., when rounding to the first sigdig, 0.45 +becomes 0.5, -0.55 becomes -0.5, but 0.5501 becomes 0.6. + +=item '+inf' + +round to plus infinity, i.e. always round up. E.g., when +rounding to the first sigdig, 0.45 becomes 0.5, -0.55 becomes -0.5, +and 0.4501 also becomes 0.5. + +=item '-inf' + +round to minus infinity, i.e. always round down. E.g., when +rounding to the first sigdig, 0.45 becomes 0.4, -0.55 becomes -0.6, +but 0.4501 becomes 0.5. + +=item 'zero' + +round to zero, i.e. positive numbers down, negative ones up. +E.g., when rounding to the first sigdig, 0.45 becomes 0.4, -0.55 +becomes -0.5, but 0.4501 becomes 0.5. + +=item 'common' + +round up if the digit immediately to the right of the rounding place +is 5 or greater, otherwise round down. E.g., 0.15 becomes 0.2 and +0.149 becomes 0.1. + +=back + +The handling of A & P in MBI/MBF (the old core code shipped with Perl +versions <= 5.7.2) is like this: + +=over + +=item Precision + + * ffround($p) is able to round to $p number of digits after the decimal + point + * otherwise P is unused + +=item Accuracy (significant digits) + + * fround($a) rounds to $a significant digits + * only fdiv() and fsqrt() take A as (optional) parameter + + other operations simply create the same number (fneg etc), or + more (fmul) of digits + + rounding/truncating is only done when explicitly calling one + of fround or ffround, and never for BigInt (not implemented) + * fsqrt() simply hands its accuracy argument over to fdiv. + * the documentation and the comment in the code indicate two + different ways on how fdiv() determines the maximum number + of digits it should calculate, and the actual code does yet + another thing + POD: + max($Math::BigFloat::div_scale,length(dividend)+length(divisor)) + Comment: + result has at most max(scale, length(dividend), length(divisor)) digits + Actual code: + scale = max(scale, length(dividend)-1,length(divisor)-1); + scale += length(divisor) - length(dividend); + So for lx = 3, ly = 9, scale = 10, scale will actually be 16 (10 + So for lx = 3, ly = 9, scale = 10, scale will actually be 16 + (10+9-3). Actually, the 'difference' added to the scale is cal- + culated from the number of "significant digits" in dividend and + divisor, which is derived by looking at the length of the man- + tissa. Which is wrong, since it includes the + sign (oops) and + actually gets 2 for '+100' and 4 for '+101'. Oops again. Thus + 124/3 with div_scale=1 will get you '41.3' based on the strange + assumption that 124 has 3 significant digits, while 120/7 will + get you '17', not '17.1' since 120 is thought to have 2 signif- + icant digits. The rounding after the division then uses the + remainder and $y to determine whether it must round up or down. + ? I have no idea which is the right way. That's why I used a slightly more + ? simple scheme and tweaked the few failing testcases to match it. + +=back + +This is how it works now: + +=over + +=item Setting/Accessing + + * You can set the A global via Math::BigInt->accuracy() or + Math::BigFloat->accuracy() or whatever class you are using. + * You can also set P globally by using Math::SomeClass->precision() + likewise. + * Globals are classwide, and not inherited by subclasses. + * to undefine A, use Math::SomeCLass->accuracy(undef); + * to undefine P, use Math::SomeClass->precision(undef); + * Setting Math::SomeClass->accuracy() clears automatically + Math::SomeClass->precision(), and vice versa. + * To be valid, A must be > 0, P can have any value. + * If P is negative, this means round to the P'th place to the right of the + decimal point; positive values mean to the left of the decimal point. + P of 0 means round to integer. + * to find out the current global A, use Math::SomeClass->accuracy() + * to find out the current global P, use Math::SomeClass->precision() + * use $x->accuracy() respective $x->precision() for the local + setting of $x. + * Please note that $x->accuracy() respective $x->precision() + return eventually defined global A or P, when $x's A or P is not + set. + +=item Creating numbers + + * When you create a number, you can give the desired A or P via: + $x = Math::BigInt->new($number,$A,$P); + * Only one of A or P can be defined, otherwise the result is NaN + * If no A or P is give ($x = Math::BigInt->new($number) form), then the + globals (if set) will be used. Thus changing the global defaults later on + will not change the A or P of previously created numbers (i.e., A and P of + $x will be what was in effect when $x was created) + * If given undef for A and P, NO rounding will occur, and the globals will + NOT be used. This is used by subclasses to create numbers without + suffering rounding in the parent. Thus a subclass is able to have its own + globals enforced upon creation of a number by using + $x = Math::BigInt->new($number,undef,undef): + + use Math::BigInt::SomeSubclass; + use Math::BigInt; + + Math::BigInt->accuracy(2); + Math::BigInt::SomeSubClass->accuracy(3); + $x = Math::BigInt::SomeSubClass->new(1234); + + $x is now 1230, and not 1200. A subclass might choose to implement + this otherwise, e.g. falling back to the parent's A and P. + +=item Usage + + * If A or P are enabled/defined, they are used to round the result of each + operation according to the rules below + * Negative P is ignored in Math::BigInt, since BigInts never have digits + after the decimal point + * Math::BigFloat uses Math::BigInt internally, but setting A or P inside + Math::BigInt as globals does not tamper with the parts of a BigFloat. + A flag is used to mark all Math::BigFloat numbers as 'never round'. + +=item Precedence + + * It only makes sense that a number has only one of A or P at a time. + If you set either A or P on one object, or globally, the other one will + be automatically cleared. + * If two objects are involved in an operation, and one of them has A in + effect, and the other P, this results in an error (NaN). + * A takes precedence over P (Hint: A comes before P). + If neither of them is defined, nothing is used, i.e. the result will have + as many digits as it can (with an exception for fdiv/fsqrt) and will not + be rounded. + * There is another setting for fdiv() (and thus for fsqrt()). If neither of + A or P is defined, fdiv() will use a fallback (F) of $div_scale digits. + If either the dividend's or the divisor's mantissa has more digits than + the value of F, the higher value will be used instead of F. + This is to limit the digits (A) of the result (just consider what would + happen with unlimited A and P in the case of 1/3 :-) + * fdiv will calculate (at least) 4 more digits than required (determined by + A, P or F), and, if F is not used, round the result + (this will still fail in the case of a result like 0.12345000000001 with A + or P of 5, but this can not be helped - or can it?) + * Thus you can have the math done by on Math::Big* class in two modi: + + never round (this is the default): + This is done by setting A and P to undef. No math operation + will round the result, with fdiv() and fsqrt() as exceptions to guard + against overflows. You must explicitly call bround(), bfround() or + round() (the latter with parameters). + Note: Once you have rounded a number, the settings will 'stick' on it + and 'infect' all other numbers engaged in math operations with it, since + local settings have the highest precedence. So, to get SaferRound[tm], + use a copy() before rounding like this: + + $x = Math::BigFloat->new(12.34); + $y = Math::BigFloat->new(98.76); + $z = $x * $y; # 1218.6984 + print $x->copy()->fround(3); # 12.3 (but A is now 3!) + $z = $x * $y; # still 1218.6984, without + # copy would have been 1210! + + + round after each op: + After each single operation (except for testing like is_zero()), the + method round() is called and the result is rounded appropriately. By + setting proper values for A and P, you can have all-the-same-A or + all-the-same-P modes. For example, Math::Currency might set A to undef, + and P to -2, globally. + + ?Maybe an extra option that forbids local A & P settings would be in order, + ?so that intermediate rounding does not 'poison' further math? + +=item Overriding globals + + * you will be able to give A, P and R as an argument to all the calculation + routines; the second parameter is A, the third one is P, and the fourth is + R (shift right by one for binary operations like badd). P is used only if + the first parameter (A) is undefined. These three parameters override the + globals in the order detailed as follows, i.e. the first defined value + wins: + (local: per object, global: global default, parameter: argument to sub) + + parameter A + + parameter P + + local A (if defined on both of the operands: smaller one is taken) + + local P (if defined on both of the operands: bigger one is taken) + + global A + + global P + + global F + * fsqrt() will hand its arguments to fdiv(), as it used to, only now for two + arguments (A and P) instead of one + +=item Local settings + + * You can set A or P locally by using $x->accuracy() or + $x->precision() + and thus force different A and P for different objects/numbers. + * Setting A or P this way immediately rounds $x to the new value. + * $x->accuracy() clears $x->precision(), and vice versa. + +=item Rounding + + * the rounding routines will use the respective global or local settings. + fround()/bround() is for accuracy rounding, while ffround()/bfround() + is for precision + * the two rounding functions take as the second parameter one of the + following rounding modes (R): + 'even', 'odd', '+inf', '-inf', 'zero', 'trunc', 'common' + * you can set/get the global R by using Math::SomeClass->round_mode() + or by setting $Math::SomeClass::round_mode + * after each operation, $result->round() is called, and the result may + eventually be rounded (that is, if A or P were set either locally, + globally or as parameter to the operation) + * to manually round a number, call $x->round($A,$P,$round_mode); + this will round the number by using the appropriate rounding function + and then normalize it. + * rounding modifies the local settings of the number: + + $x = Math::BigFloat->new(123.456); + $x->accuracy(5); + $x->bround(4); + + Here 4 takes precedence over 5, so 123.5 is the result and $x->accuracy() + will be 4 from now on. + +=item Default values + + * R: 'even' + * F: 40 + * A: undef + * P: undef + +=item Remarks + + * The defaults are set up so that the new code gives the same results as + the old code (except in a few cases on fdiv): + + Both A and P are undefined and thus will not be used for rounding + after each operation. + + round() is thus a no-op, unless given extra parameters A and P + +=back + +=head1 Infinity and Not a Number + +While BigInt has extensive handling of inf and NaN, certain quirks remain. + +=over + +=item oct()/hex() + +These perl routines currently (as of Perl v.5.8.6) cannot handle passed +inf. + + te@linux:~> perl -wle 'print 2 ** 3333' + inf + te@linux:~> perl -wle 'print 2 ** 3333 == 2 ** 3333' + 1 + te@linux:~> perl -wle 'print oct(2 ** 3333)' + 0 + te@linux:~> perl -wle 'print hex(2 ** 3333)' + Illegal hexadecimal digit 'i' ignored at -e line 1. + 0 + +The same problems occur if you pass them Math::BigInt->binf() objects. Since +overloading these routines is not possible, this cannot be fixed from BigInt. + +=item ==, !=, <, >, <=, >= with NaNs + +BigInt's bcmp() routine currently returns undef to signal that a NaN was +involved in a comparison. However, the overload code turns that into +either 1 or '' and thus operations like C<< NaN != NaN >> might return +wrong values. + +=item log(-inf) + +C<< log(-inf) >> is highly weird. Since log(-x)=pi*i+log(x), then +log(-inf)=pi*i+inf. However, since the imaginary part is finite, the real +infinity "overshadows" it, so the number might as well just be infinity. +However, the result is a complex number, and since BigInt/BigFloat can only +have real numbers as results, the result is NaN. + +=item exp(), cos(), sin(), atan2() + +These all might have problems handling infinity right. + +=back + +=head1 INTERNALS + +The actual numbers are stored as unsigned big integers (with separate sign). + +You should neither care about nor depend on the internal representation; it +might change without notice. Use B method calls like C<< $x->sign(); >> +instead relying on the internal representation. + +=head2 MATH LIBRARY + +Math with the numbers is done (by default) by a module called +C. This is equivalent to saying: + + use Math::BigInt try => 'Calc'; + +You can change this backend library by using: + + use Math::BigInt try => 'GMP'; + +B: General purpose packages should not be explicit about the library +to use; let the script author decide which is best. + +If your script works with huge numbers and Calc is too slow for them, +you can also for the loading of one of these libraries and if none +of them can be used, the code will die: + + use Math::BigInt only => 'GMP,Pari'; + +The following would first try to find Math::BigInt::Foo, then +Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: + + use Math::BigInt try => 'Foo,Math::BigInt::Bar'; + +The library that is loaded last will be used. Note that this can be +overwritten at any time by loading a different library, and numbers +constructed with different libraries cannot be used in math operations +together. + +=head3 What library to use? + +B: General purpose packages should not be explicit about the library +to use; let the script author decide which is best. + +L and L are in cases involving big +numbers much faster than Calc, however it is slower when dealing with very +small numbers (less than about 20 digits) and when converting very large +numbers to decimal (for instance for printing, rounding, calculating their +length in decimal etc). + +So please select carefully what library you want to use. + +Different low-level libraries use different formats to store the numbers. +However, you should B depend on the number having a specific format +internally. + +See the respective math library module documentation for further details. + +=head2 SIGN + +The sign is either '+', '-', 'NaN', '+inf' or '-inf'. + +A sign of 'NaN' is used to represent the result when input arguments are not +numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively +minus infinity. You will get '+inf' when dividing a positive number by 0, and +'-inf' when dividing any negative number by 0. + +=head2 mantissa(), exponent() and parts() + +C and C return the said parts of the BigInt such +that: + + $m = $x->mantissa(); + $e = $x->exponent(); + $y = $m * ( 10 ** $e ); + print "ok\n" if $x == $y; + +C<< ($m,$e) = $x->parts() >> is just a shortcut that gives you both of them +in one go. Both the returned mantissa and exponent have a sign. + +Currently, for BigInts C<$e> is always 0, except +inf and -inf, where it is +C<+inf>; and for NaN, where it is C; and for C<$x == 0>, where it is C<1> +(to be compatible with Math::BigFloat's internal representation of a zero as +C<0E1>). + +C<$m> is currently just a copy of the original number. The relation between +C<$e> and C<$m> will stay always the same, though their real values might +change. + +=head1 EXAMPLES + + use Math::BigInt; + + sub bigint { Math::BigInt->new(shift); } + + $x = Math::BigInt->bstr("1234") # string "1234" + $x = "$x"; # same as bstr() + $x = Math::BigInt->bneg("1234"); # BigInt "-1234" + $x = Math::BigInt->babs("-12345"); # BigInt "12345" + $x = Math::BigInt->bnorm("-0.00"); # BigInt "0" + $x = bigint(1) + bigint(2); # BigInt "3" + $x = bigint(1) + "2"; # ditto (auto-BigIntify of "2") + $x = bigint(1); # BigInt "1" + $x = $x + 5 / 2; # BigInt "3" + $x = $x ** 3; # BigInt "27" + $x *= 2; # BigInt "54" + $x = Math::BigInt->new(0); # BigInt "0" + $x--; # BigInt "-1" + $x = Math::BigInt->badd(4,5) # BigInt "9" + print $x->bsstr(); # 9e+0 + +Examples for rounding: + + use Math::BigFloat; + use Test::More; + + $x = Math::BigFloat->new(123.4567); + $y = Math::BigFloat->new(123.456789); + Math::BigFloat->accuracy(4); # no more A than 4 + + is ($x->copy()->fround(),123.4); # even rounding + print $x->copy()->fround(),"\n"; # 123.4 + Math::BigFloat->round_mode('odd'); # round to odd + print $x->copy()->fround(),"\n"; # 123.5 + Math::BigFloat->accuracy(5); # no more A than 5 + Math::BigFloat->round_mode('odd'); # round to odd + print $x->copy()->fround(),"\n"; # 123.46 + $y = $x->copy()->fround(4),"\n"; # A = 4: 123.4 + print "$y, ",$y->accuracy(),"\n"; # 123.4, 4 + + Math::BigFloat->accuracy(undef); # A not important now + Math::BigFloat->precision(2); # P important + print $x->copy()->bnorm(),"\n"; # 123.46 + print $x->copy()->fround(),"\n"; # 123.46 + +Examples for converting: + + my $x = Math::BigInt->new('0b1'.'01' x 123); + print "bin: ",$x->as_bin()," hex:",$x->as_hex()," dec: ",$x,"\n"; + +=head1 Autocreating constants + +After C all the B decimal, hexadecimal +and binary constants in the given scope are converted to C. +This conversion happens at compile time. + +In particular, + + perl -MMath::BigInt=:constant -e 'print 2**100,"\n"' + +prints the integer value of C<2**100>. Note that without conversion of +constants the expression 2**100 will be calculated as perl scalar. + +Please note that strings and floating point constants are not affected, +so that + + use Math::BigInt qw/:constant/; + + $x = 1234567890123456789012345678901234567890 + + 123456789123456789; + $y = '1234567890123456789012345678901234567890' + + '123456789123456789'; + +do not work. You need an explicit Math::BigInt->new() around one of the +operands. You should also quote large constants to protect loss of precision: + + use Math::BigInt; + + $x = Math::BigInt->new('1234567889123456789123456789123456789'); + +Without the quotes Perl would convert the large number to a floating point +constant at compile time and then hand the result to BigInt, which results in +an truncated result or a NaN. + +This also applies to integers that look like floating point constants: + + use Math::BigInt ':constant'; + + print ref(123e2),"\n"; + print ref(123.2e2),"\n"; + +will print nothing but newlines. Use either L or L +to get this to work. + +=head1 PERFORMANCE + +Using the form $x += $y; etc over $x = $x + $y is faster, since a copy of $x +must be made in the second case. For long numbers, the copy can eat up to 20% +of the work (in the case of addition/subtraction, less for +multiplication/division). If $y is very small compared to $x, the form +$x += $y is MUCH faster than $x = $x + $y since making the copy of $x takes +more time then the actual addition. + +With a technique called copy-on-write, the cost of copying with overload could +be minimized or even completely avoided. A test implementation of COW did show +performance gains for overloaded math, but introduced a performance loss due +to a constant overhead for all other operations. So Math::BigInt does currently +not COW. + +The rewritten version of this module (vs. v0.01) is slower on certain +operations, like C, C and C. The reason are that it +does now more work and handles much more cases. The time spent in these +operations is usually gained in the other math operations so that code on +the average should get (much) faster. If they don't, please contact the author. + +Some operations may be slower for small numbers, but are significantly faster +for big numbers. Other operations are now constant (O(1), like C, +C etc), instead of O(N) and thus nearly always take much less time. +These optimizations were done on purpose. + +If you find the Calc module to slow, try to install any of the replacement +modules and see if they help you. + +=head2 Alternative math libraries + +You can use an alternative library to drive Math::BigInt. See the section +L for more information. + +For more benchmark results see L. + +=head1 SUBCLASSING + +=head2 Subclassing Math::BigInt + +The basic design of Math::BigInt allows simple subclasses with very little +work, as long as a few simple rules are followed: + +=over + +=item * + +The public API must remain consistent, i.e. if a sub-class is overloading +addition, the sub-class must use the same name, in this case badd(). The +reason for this is that Math::BigInt is optimized to call the object methods +directly. + +=item * + +The private object hash keys like C<< $x->{sign} >> may not be changed, but +additional keys can be added, like C<< $x->{_custom} >>. + +=item * + +Accessor functions are available for all existing object hash keys and should +be used instead of directly accessing the internal hash keys. The reason for +this is that Math::BigInt itself has a pluggable interface which permits it +to support different storage methods. + +=back + +More complex sub-classes may have to replicate more of the logic internal of +Math::BigInt if they need to change more basic behaviors. A subclass that +needs to merely change the output only needs to overload C. + +All other object methods and overloaded functions can be directly inherited +from the parent class. + +At the very minimum, any subclass will need to provide its own C and can +store additional hash keys in the object. There are also some package globals +that must be defined, e.g.: + + # Globals + $accuracy = undef; + $precision = -2; # round to 2 decimal places + $round_mode = 'even'; + $div_scale = 40; + +Additionally, you might want to provide the following two globals to allow +auto-upgrading and auto-downgrading to work correctly: + + $upgrade = undef; + $downgrade = undef; + +This allows Math::BigInt to correctly retrieve package globals from the +subclass, like C<$SubClass::precision>. See t/Math/BigInt/Subclass.pm or +t/Math/BigFloat/SubClass.pm completely functional subclass examples. + +Don't forget to + + use overload; + +in your subclass to automatically inherit the overloading from the parent. If +you like, you can change part of the overloading, look at Math::String for an +example. + +=head1 UPGRADING + +When used like this: + + use Math::BigInt upgrade => 'Foo::Bar'; + +certain operations will 'upgrade' their calculation and thus the result to +the class Foo::Bar. Usually this is used in conjunction with Math::BigFloat: + + use Math::BigInt upgrade => 'Math::BigFloat'; + +As a shortcut, you can use the module L: + + use bignum; + +Also good for one-liners: + + perl -Mbignum -le 'print 2 ** 255' + +This makes it possible to mix arguments of different classes (as in 2.5 + 2) +as well es preserve accuracy (as in sqrt(3)). + +Beware: This feature is not fully implemented yet. + +=head2 Auto-upgrade + +The following methods upgrade themselves unconditionally; that is if upgrade +is in effect, they will always hand up their work: + +=over + +=item bsqrt() + +=item div() + +=item blog() + +=item bexp() + +=back + +Beware: This list is not complete. + +All other methods upgrade themselves only when one (or all) of their +arguments are of the class mentioned in $upgrade (This might change in later +versions to a more sophisticated scheme): + +=head1 EXPORTS + +C exports nothing by default, but can export the following methods: + + bgcd + blcm + +=head1 CAVEATS + +Some things might not work as you expect them. Below is documented what is +known to be troublesome: + +=over + +=item bstr(), bsstr() and 'cmp' + +Both C and C as well as automated stringify via overload now +drop the leading '+'. The old code would return '+3', the new returns '3'. +This is to be consistent with Perl and to make C (especially with +overloading) to work as you expect. It also solves problems with C +and L, which stringify arguments before comparing them. + +Mark Biggar said, when asked about to drop the '+' altogether, or make only +C work: + + I agree (with the first alternative), don't add the '+' on positive + numbers. It's not as important anymore with the new internal + form for numbers. It made doing things like abs and neg easier, + but those have to be done differently now anyway. + +So, the following examples will now work all as expected: + + use Test::More tests => 1; + use Math::BigInt; + + my $x = new Math::BigInt 3*3; + my $y = new Math::BigInt 3*3; + + is ($x,3*3, 'multiplication'); + print "$x eq 9" if $x eq $y; + print "$x eq 9" if $x eq '9'; + print "$x eq 9" if $x eq 3*3; + +Additionally, the following still works: + + print "$x == 9" if $x == $y; + print "$x == 9" if $x == 9; + print "$x == 9" if $x == 3*3; + +There is now a C method to get the string in scientific notation aka +C<1e+2> instead of C<100>. Be advised that overloaded 'eq' always uses bstr() +for comparison, but Perl will represent some numbers as 100 and others +as 1e+308. If in doubt, convert both arguments to Math::BigInt before +comparing them as strings: + + use Test::More tests => 3; + use Math::BigInt; + + $x = Math::BigInt->new('1e56'); $y = 1e56; + is ($x,$y); # will fail + is ($x->bsstr(),$y); # okay + $y = Math::BigInt->new($y); + is ($x,$y); # okay + +Alternatively, simply use C<< <=> >> for comparisons, this will get it +always right. There is not yet a way to get a number automatically represented +as a string that matches exactly the way Perl represents it. + +See also the section about L for problems in +comparing NaNs. + +=item int() + +C will return (at least for Perl v5.7.1 and up) another BigInt, not a +Perl scalar: + + $x = Math::BigInt->new(123); + $y = int($x); # BigInt 123 + $x = Math::BigFloat->new(123.45); + $y = int($x); # BigInt 123 + +In all Perl versions you can use C or C for the same +effect: + + $x = Math::BigFloat->new(123.45); + $y = $x->as_number(); # BigInt 123 + $y = $x->as_int(); # ditto + +This also works for other subclasses, like Math::String. + +If you want a real Perl scalar, use C: + + $y = $x->numify(); # 123 as scalar + +This is seldom necessary, though, because this is done automatically, like +when you access an array: + + $z = $array[$x]; # does work automatically + +=item length() + +The following will probably not do what you expect: + + $c = Math::BigInt->new(123); + print $c->length(),"\n"; # prints 30 + +It prints both the number of digits in the number and in the fraction part +since print calls C in list context. Use something like: + + print scalar $c->length(),"\n"; # prints 3 + +=item bdiv() + +The following will probably not do what you expect: + + print $c->bdiv(10000),"\n"; + +It prints both quotient and remainder since print calls C in list +context. Also, C will modify $c, so be careful. You probably want +to use + + print $c / 10000,"\n"; + +or, if you want to modify $c instead, + + print scalar $c->bdiv(10000),"\n"; + +The quotient is always the greatest integer less than or equal to the +real-valued quotient of the two operands, and the remainder (when it is +non-zero) always has the same sign as the second operand; so, for +example, + + 1 / 4 => ( 0, 1) + 1 / -4 => (-1,-3) + -3 / 4 => (-1, 1) + -3 / -4 => ( 0,-3) + -11 / 2 => (-5,1) + 11 /-2 => (-5,-1) + +As a consequence, the behavior of the operator % agrees with the +behavior of Perl's built-in % operator (as documented in the perlop +manpage), and the equation + + $x == ($x / $y) * $y + ($x % $y) + +holds true for any $x and $y, which justifies calling the two return +values of bdiv() the quotient and remainder. The only exception to this rule +are when $y == 0 and $x is negative, then the remainder will also be +negative. See below under "infinity handling" for the reasoning behind this. + +Perl's 'use integer;' changes the behaviour of % and / for scalars, but will +not change BigInt's way to do things. This is because under 'use integer' Perl +will do what the underlying C thinks is right and this is different for each +system. If you need BigInt's behaving exactly like Perl's 'use integer', bug +the author to implement it ;) + +=item infinity handling + +Here are some examples that explain the reasons why certain results occur while +handling infinity: + +The following table shows the result of the division and the remainder, so that +the equation above holds true. Some "ordinary" cases are strewn in to show more +clearly the reasoning: + + A / B = C, R so that C * B + R = A + ========================================================= + 5 / 8 = 0, 5 0 * 8 + 5 = 5 + 0 / 8 = 0, 0 0 * 8 + 0 = 0 + 0 / inf = 0, 0 0 * inf + 0 = 0 + 0 /-inf = 0, 0 0 * -inf + 0 = 0 + 5 / inf = 0, 5 0 * inf + 5 = 5 + 5 /-inf = 0, 5 0 * -inf + 5 = 5 + -5/ inf = 0, -5 0 * inf + -5 = -5 + -5/-inf = 0, -5 0 * -inf + -5 = -5 + inf/ 5 = inf, 0 inf * 5 + 0 = inf + -inf/ 5 = -inf, 0 -inf * 5 + 0 = -inf + inf/ -5 = -inf, 0 -inf * -5 + 0 = inf + -inf/ -5 = inf, 0 inf * -5 + 0 = -inf + 5/ 5 = 1, 0 1 * 5 + 0 = 5 + -5/ -5 = 1, 0 1 * -5 + 0 = -5 + inf/ inf = 1, 0 1 * inf + 0 = inf + -inf/-inf = 1, 0 1 * -inf + 0 = -inf + inf/-inf = -1, 0 -1 * -inf + 0 = inf + -inf/ inf = -1, 0 1 * -inf + 0 = -inf + 8/ 0 = inf, 8 inf * 0 + 8 = 8 + inf/ 0 = inf, inf inf * 0 + inf = inf + 0/ 0 = NaN + +These cases below violate the "remainder has the sign of the second of the two +arguments", since they wouldn't match up otherwise. + + A / B = C, R so that C * B + R = A + ======================================================== + -inf/ 0 = -inf, -inf -inf * 0 + inf = -inf + -8/ 0 = -inf, -8 -inf * 0 + 8 = -8 + +=item Modifying and = + +Beware of: + + $x = Math::BigFloat->new(5); + $y = $x; + +It will not do what you think, e.g. making a copy of $x. Instead it just makes +a second reference to the B object and stores it in $y. Thus anything +that modifies $x (except overloaded operators) will modify $y, and vice versa. +Or in other words, C<=> is only safe if you modify your BigInts only via +overloaded math. As soon as you use a method call it breaks: + + $x->bmul(2); + print "$x, $y\n"; # prints '10, 10' + +If you want a true copy of $x, use: + + $y = $x->copy(); + +You can also chain the calls like this, this will make first a copy and then +multiply it by 2: + + $y = $x->copy()->bmul(2); + +See also the documentation for overload.pm regarding C<=>. + +=item bpow + +C (and the rounding functions) now modifies the first argument and +returns it, unlike the old code which left it alone and only returned the +result. This is to be consistent with C etc. The first three will +modify $x, the last one won't: + + print bpow($x,$i),"\n"; # modify $x + print $x->bpow($i),"\n"; # ditto + print $x **= $i,"\n"; # the same + print $x ** $i,"\n"; # leave $x alone + +The form C<$x **= $y> is faster than C<$x = $x ** $y;>, though. + +=item Overloading -$x + +The following: + + $x = -$x; + +is slower than + + $x->bneg(); + +since overload calls C instead of C. The first variant +needs to preserve $x since it does not know that it later will get overwritten. +This makes a copy of $x and takes O(N), but $x->bneg() is O(1). + +=item Mixing different object types + +In Perl you will get a floating point value if you do one of the following: + + $float = 5.0 + 2; + $float = 2 + 5.0; + $float = 5 / 2; + +With overloaded math, only the first two variants will result in a BigFloat: + + use Math::BigInt; + use Math::BigFloat; + + $mbf = Math::BigFloat->new(5); + $mbi2 = Math::BigInteger->new(5); + $mbi = Math::BigInteger->new(2); + + # what actually gets called: + $float = $mbf + $mbi; # $mbf->badd() + $float = $mbf / $mbi; # $mbf->bdiv() + $integer = $mbi + $mbf; # $mbi->badd() + $integer = $mbi2 / $mbi; # $mbi2->bdiv() + $integer = $mbi2 / $mbf; # $mbi2->bdiv() + +This is because math with overloaded operators follows the first (dominating) +operand, and the operation of that is called and returns thus the result. So, +Math::BigInt::bdiv() will always return a Math::BigInt, regardless whether +the result should be a Math::BigFloat or the second operant is one. + +To get a Math::BigFloat you either need to call the operation manually, +make sure the operands are already of the proper type or casted to that type +via Math::BigFloat->new(): + + $float = Math::BigFloat->new($mbi2) / $mbi; # = 2.5 + +Beware of simple "casting" the entire expression, this would only convert +the already computed result: + + $float = Math::BigFloat->new($mbi2 / $mbi); # = 2.0 thus wrong! + +Beware also of the order of more complicated expressions like: + + $integer = ($mbi2 + $mbi) / $mbf; # int / float => int + $integer = $mbi2 / Math::BigFloat->new($mbi); # ditto + +If in doubt, break the expression into simpler terms, or cast all operands +to the desired resulting type. + +Scalar values are a bit different, since: + + $float = 2 + $mbf; + $float = $mbf + 2; + +will both result in the proper type due to the way the overloaded math works. + +This section also applies to other overloaded math packages, like Math::String. + +One solution to you problem might be autoupgrading|upgrading. See the +pragmas L, L and L for an easy way to do this. + +=item bsqrt() + +C works only good if the result is a big integer, e.g. the square +root of 144 is 12, but from 12 the square root is 3, regardless of rounding +mode. The reason is that the result is always truncated to an integer. + +If you want a better approximation of the square root, then use: + + $x = Math::BigFloat->new(12); + Math::BigFloat->precision(0); + Math::BigFloat->round_mode('even'); + print $x->copy->bsqrt(),"\n"; # 4 + + Math::BigFloat->precision(2); + print $x->bsqrt(),"\n"; # 3.46 + print $x->bsqrt(3),"\n"; # 3.464 + +=item brsft() + +For negative numbers in base see also L. + +=back + +=head1 BUGS + +Please report any bugs or feature requests to +C, or through the web interface at +L +(requires login). +We will be notified, and then you'll automatically be notified of progress on +your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc Math::BigInt + +You can also look for information at: + +=over 4 + +=item * RT: CPAN's request tracker + +L + +=item * AnnoCPAN: Annotated CPAN documentation + +L + +=item * CPAN Ratings + +L + +=item * Search CPAN + +L + +=item * CPAN Testers Matrix + +L + +=item * The Bignum mailing list + +=over 4 + +=item * Post to mailing list + +C + +=item * View mailing list + +L + +=item * Subscribe/Unsubscribe + +L + +=back + +=back + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 SEE ALSO + +L and L as well as the backends +L, L, and L. + +The pragmas L, L and L also might be of interest +because they solve the autoupgrading/downgrading issue, at least partly. + +=head1 AUTHORS + +Original code by Mark Biggar, overloaded interface by Ilya Zakharevich. +Completely rewritten by Tels http://bloodgate.com in late 2000, 2001 - 2006 +and still at it in 2007. + +Many people contributed in one or more ways to the final beast, see the file +CREDITS for an (incomplete) list. If you miss your name, please drop me a +mail. Thank you! + +=cut diff --git a/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm b/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm new file mode 100644 index 0000000000..ce9bf3ab8b --- /dev/null +++ b/cpan/Math-BigInt/lib/Math/BigInt/Calc.pm @@ -0,0 +1,3029 @@ +package Math::BigInt::Calc; + +use 5.006002; +use strict; +# use warnings; # do not use warnings for older Perls + +our $VERSION = '1.999701'; + +# Package to store unsigned big integers in decimal and do math with them + +# Internally the numbers are stored in an array with at least 1 element, no +# leading zero parts (except the first) and in base 1eX where X is determined +# automatically at loading time to be the maximum possible value + +# todo: +# - fully remove funky $# stuff in div() (maybe - that code scares me...) + +# USE_MUL: due to problems on certain os (os390, posix-bc) "* 1e-5" is used +# instead of "/ 1e5" at some places, (marked with USE_MUL). Other platforms +# BS2000, some Crays need USE_DIV instead. +# The BEGIN block is used to determine which of the two variants gives the +# correct result. + +# Beware of things like: +# $i = $i * $y + $car; $car = int($i / $BASE); $i = $i % $BASE; +# This works on x86, but fails on ARM (SA1100, iPAQ) due to who knows what +# reasons. So, use this instead (slower, but correct): +# $i = $i * $y + $car; $car = int($i / $BASE); $i -= $BASE * $car; + +############################################################################## +# global constants, flags and accessory + +# announce that we are compatible with MBI v1.83 and up +sub api_version () { 2; } + +# constants for easier life +my ($BASE,$BASE_LEN,$RBASE,$MAX_VAL); +my ($AND_BITS,$XOR_BITS,$OR_BITS); +my ($AND_MASK,$XOR_MASK,$OR_MASK); + +sub _base_len + { + # Set/get the BASE_LEN and assorted other, connected values. + # Used only by the testsuite, the set variant is used only by the BEGIN + # block below: + shift; + + my ($b, $int) = @_; + if (defined $b) + { + # avoid redefinitions + undef &_mul; + undef &_div; + + if ($] >= 5.008 && $int && $b > 7) + { + $BASE_LEN = $b; + *_mul = \&_mul_use_div_64; + *_div = \&_div_use_div_64; + $BASE = int("1e".$BASE_LEN); + $MAX_VAL = $BASE-1; + return $BASE_LEN unless wantarray; + return ($BASE_LEN, $BASE, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL,); + } + + # find whether we can use mul or div in mul()/div() + $BASE_LEN = $b+1; + my $caught = 0; + while (--$BASE_LEN > 5) + { + $BASE = int("1e".$BASE_LEN); + $RBASE = abs('1e-'.$BASE_LEN); # see USE_MUL + $caught = 0; + $caught += 1 if (int($BASE * $RBASE) != 1); # should be 1 + $caught += 2 if (int($BASE / $BASE) != 1); # should be 1 + last if $caught != 3; + } + $BASE = int("1e".$BASE_LEN); + $RBASE = abs('1e-'.$BASE_LEN); # see USE_MUL + $MAX_VAL = $BASE-1; + + # ($caught & 1) != 0 => cannot use MUL + # ($caught & 2) != 0 => cannot use DIV + if ($caught == 2) # 2 + { + # must USE_MUL since we cannot use DIV + *_mul = \&_mul_use_mul; + *_div = \&_div_use_mul; + } + else # 0 or 1 + { + # can USE_DIV instead + *_mul = \&_mul_use_div; + *_div = \&_div_use_div; + } + } + return $BASE_LEN unless wantarray; + return ($BASE_LEN, $BASE, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL); + } + +sub _new + { + # (ref to string) return ref to num_array + # Convert a number from string format (without sign) to internal base + # 1ex format. Assumes normalized value as input. + my $il = length($_[1])-1; + + # < BASE_LEN due len-1 above + return [ int($_[1]) ] if $il < $BASE_LEN; # shortcut for short numbers + + # this leaves '00000' instead of int 0 and will be corrected after any op + [ reverse(unpack("a" . ($il % $BASE_LEN+1) + . ("a$BASE_LEN" x ($il / $BASE_LEN)), $_[1])) ]; + } + +BEGIN + { + # from Daniel Pfeiffer: determine largest group of digits that is precisely + # multipliable with itself plus carry + # Test now changed to expect the proper pattern, not a result off by 1 or 2 + my ($e, $num) = 3; # lowest value we will use is 3+1-1 = 3 + do + { + $num = ('9' x ++$e) + 0; + $num *= $num + 1.0; + } while ("$num" =~ /9{$e}0{$e}/); # must be a certain pattern + $e--; # last test failed, so retract one step + # the limits below brush the problems with the test above under the rug: + # the test should be able to find the proper $e automatically + $e = 5 if $^O =~ /^uts/; # UTS get's some special treatment + $e = 5 if $^O =~ /^unicos/; # unicos is also problematic (6 seems to work + # there, but we play safe) + + my $int = 0; + if ($e > 7) + { + use integer; + my $e1 = 7; + $num = 7; + do + { + $num = ('9' x ++$e1) + 0; + $num *= $num + 1; + } while ("$num" =~ /9{$e1}0{$e1}/); # must be a certain pattern + $e1--; # last test failed, so retract one step + if ($e1 > 7) + { + $int = 1; $e = $e1; + } + } + + __PACKAGE__->_base_len($e,$int); # set and store + + use integer; + # find out how many bits _and, _or and _xor can take (old default = 16) + # I don't think anybody has yet 128 bit scalars, so let's play safe. + local $^W = 0; # don't warn about 'nonportable number' + $AND_BITS = 15; $XOR_BITS = 15; $OR_BITS = 15; + + # find max bits, we will not go higher than numberofbits that fit into $BASE + # to make _and etc simpler (and faster for smaller, slower for large numbers) + my $max = 16; + while (2 ** $max < $BASE) { $max++; } + { + no integer; + $max = 16 if $] < 5.006; # older Perls might not take >16 too well + } + my ($x,$y,$z); + do { + $AND_BITS++; + $x = CORE::oct('0b' . '1' x $AND_BITS); $y = $x & $x; + $z = (2 ** $AND_BITS) - 1; + } while ($AND_BITS < $max && $x == $z && $y == $x); + $AND_BITS --; # retreat one step + do { + $XOR_BITS++; + $x = CORE::oct('0b' . '1' x $XOR_BITS); $y = $x ^ 0; + $z = (2 ** $XOR_BITS) - 1; + } while ($XOR_BITS < $max && $x == $z && $y == $x); + $XOR_BITS --; # retreat one step + do { + $OR_BITS++; + $x = CORE::oct('0b' . '1' x $OR_BITS); $y = $x | $x; + $z = (2 ** $OR_BITS) - 1; + } while ($OR_BITS < $max && $x == $z && $y == $x); + $OR_BITS --; # retreat one step + + $AND_MASK = __PACKAGE__->_new( ( 2 ** $AND_BITS )); + $XOR_MASK = __PACKAGE__->_new( ( 2 ** $XOR_BITS )); + $OR_MASK = __PACKAGE__->_new( ( 2 ** $OR_BITS )); + + # We can compute the approximate length no faster than the real length: + *_alen = \&_len; + } + +############################################################################### + +sub _zero + { + # create a zero + [ 0 ]; + } + +sub _one + { + # create a one + [ 1 ]; + } + +sub _two + { + # create a two (used internally for shifting) + [ 2 ]; + } + +sub _ten + { + # create a 10 (used internally for shifting) + [ 10 ]; + } + +sub _1ex + { + # create a 1Ex + my $rem = $_[1] % $BASE_LEN; # remainder + my $parts = $_[1] / $BASE_LEN; # parts + + # 000000, 000000, 100 + [ (0) x $parts, '1' . ('0' x $rem) ]; + } + +sub _copy + { + # make a true copy + [ @{$_[1]} ]; + } + +# catch and throw away +sub import { } + +############################################################################## +# convert back to string and number + +sub _str + { + # (ref to BINT) return num_str + # Convert number from internal base 100000 format to string format. + # internal format is always normalized (no leading zeros, "-0" => "+0") + my $ar = $_[1]; + + my $l = scalar @$ar; # number of parts + if ($l < 1) # should not happen + { + require Carp; + Carp::croak("$_[1] has no elements"); + } + + my $ret = ""; + # handle first one different to strip leading zeros from it (there are no + # leading zero parts in internal representation) + $l --; $ret .= int($ar->[$l]); $l--; + # Interestingly, the pre-padd method uses more time + # the old grep variant takes longer (14 vs. 10 sec) + my $z = '0' x ($BASE_LEN-1); + while ($l >= 0) + { + $ret .= substr($z.$ar->[$l],-$BASE_LEN); # fastest way I could think of + $l--; + } + $ret; + } + +sub _num + { + # Make a Perl scalar number (int/float) from a BigInt object. + my $x = $_[1]; + + return 0 + $x->[0] if scalar @$x == 1; # below $BASE + + # Start with the most significant element and work towards the least + # significant element. Avoid multiplying "inf" (which happens if the number + # overflows) with "0" (if there are zero elements in $x) since this gives + # "nan" which propagates to the output. + + my $num = 0; + for (my $i = $#$x ; $i >= 0 ; --$i) { + $num *= $BASE; + $num += $x -> [$i]; + } + return $num; + } + +############################################################################## +# actual math code + +sub _add + { + # (ref to int_num_array, ref to int_num_array) + # routine to add two base 1eX numbers + # stolen from Knuth Vol 2 Algorithm A pg 231 + # there are separate routines to add and sub as per Knuth pg 233 + # This routine modifies array x, but not y. + + my ($c,$x,$y) = @_; + + return $x if (@$y == 1) && $y->[0] == 0; # $x + 0 => $x + if ((@$x == 1) && $x->[0] == 0) # 0 + $y => $y->copy + { + # twice as slow as $x = [ @$y ], but nec. to retain $x as ref :( + @$x = @$y; return $x; + } + + # for each in Y, add Y to X and carry. If after that, something is left in + # X, foreach in X add carry to X and then return X, carry + # Trades one "$j++" for having to shift arrays + my $i; my $car = 0; my $j = 0; + for $i (@$y) + { + $x->[$j] -= $BASE if $car = (($x->[$j] += $i + $car) >= $BASE) ? 1 : 0; + $j++; + } + while ($car != 0) + { + $x->[$j] -= $BASE if $car = (($x->[$j] += $car) >= $BASE) ? 1 : 0; $j++; + } + $x; + } + +sub _inc + { + # (ref to int_num_array, ref to int_num_array) + # Add 1 to $x, modify $x in place + my ($c,$x) = @_; + + for my $i (@$x) + { + return $x if (($i += 1) < $BASE); # early out + $i = 0; # overflow, next + } + push @$x,1 if (($x->[-1] || 0) == 0); # last overflowed, so extend + $x; + } + +sub _dec + { + # (ref to int_num_array, ref to int_num_array) + # Sub 1 from $x, modify $x in place + my ($c,$x) = @_; + + my $MAX = $BASE-1; # since MAX_VAL based on BASE + for my $i (@$x) + { + last if (($i -= 1) >= 0); # early out + $i = $MAX; # underflow, next + } + pop @$x if $x->[-1] == 0 && @$x > 1; # last underflowed (but leave 0) + $x; + } + +sub _sub + { + # (ref to int_num_array, ref to int_num_array, swap) + # subtract base 1eX numbers -- stolen from Knuth Vol 2 pg 232, $x > $y + # subtract Y from X by modifying x in place + my ($c,$sx,$sy,$s) = @_; + + my $car = 0; my $i; my $j = 0; + if (!$s) + { + for $i (@$sx) + { + last unless defined $sy->[$j] || $car; + $i += $BASE if $car = (($i -= ($sy->[$j] || 0) + $car) < 0); $j++; + } + # might leave leading zeros, so fix that + return __strip_zeros($sx); + } + for $i (@$sx) + { + # we can't do an early out if $x is < than $y, since we + # need to copy the high chunks from $y. Found by Bob Mathews. + #last unless defined $sy->[$j] || $car; + $sy->[$j] += $BASE + if $car = (($sy->[$j] = $i-($sy->[$j]||0) - $car) < 0); + $j++; + } + # might leave leading zeros, so fix that + __strip_zeros($sy); + } + +sub _mul_use_mul + { + # (ref to int_num_array, ref to int_num_array) + # multiply two numbers in internal representation + # modifies first arg, second need not be different from first + my ($c,$xv,$yv) = @_; + + if (@$yv == 1) + { + # shortcut for two very short numbers (improved by Nathan Zook) + # works also if xv and yv are the same reference, and handles also $x == 0 + if (@$xv == 1) + { + if (($xv->[0] *= $yv->[0]) >= $BASE) + { + $xv->[0] = $xv->[0] - ($xv->[1] = int($xv->[0] * $RBASE)) * $BASE; + }; + return $xv; + } + # $x * 0 => 0 + if ($yv->[0] == 0) + { + @$xv = (0); + return $xv; + } + # multiply a large number a by a single element one, so speed up + my $y = $yv->[0]; my $car = 0; + foreach my $i (@$xv) + { + $i = $i * $y + $car; $car = int($i * $RBASE); $i -= $car * $BASE; + } + push @$xv, $car if $car != 0; + return $xv; + } + # shortcut for result $x == 0 => result = 0 + return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); + + # since multiplying $x with $x fails, make copy in this case + $yv = [@$xv] if $xv == $yv; # same references? + + my @prod = (); my ($prod,$car,$cty,$xi,$yi); + + for $xi (@$xv) + { + $car = 0; $cty = 0; + + # slow variant +# for $yi (@$yv) +# { +# $prod = $xi * $yi + ($prod[$cty] || 0) + $car; +# $prod[$cty++] = +# $prod - ($car = int($prod * RBASE)) * $BASE; # see USE_MUL +# } +# $prod[$cty] += $car if $car; # need really to check for 0? +# $xi = shift @prod; + + # faster variant + # looping through this if $xi == 0 is silly - so optimize it away! + $xi = (shift @prod || 0), next if $xi == 0; + for $yi (@$yv) + { + $prod = $xi * $yi + ($prod[$cty] || 0) + $car; +## this is actually a tad slower +## $prod = $prod[$cty]; $prod += ($car + $xi * $yi); # no ||0 here + $prod[$cty++] = + $prod - ($car = int($prod * $RBASE)) * $BASE; # see USE_MUL + } + $prod[$cty] += $car if $car; # need really to check for 0? + $xi = shift @prod || 0; # || 0 makes v5.005_3 happy + } + push @$xv, @prod; + # can't have leading zeros +# __strip_zeros($xv); + $xv; + } + +sub _mul_use_div_64 + { + # (ref to int_num_array, ref to int_num_array) + # multiply two numbers in internal representation + # modifies first arg, second need not be different from first + # works for 64 bit integer with "use integer" + my ($c,$xv,$yv) = @_; + + use integer; + if (@$yv == 1) + { + # shortcut for two small numbers, also handles $x == 0 + if (@$xv == 1) + { + # shortcut for two very short numbers (improved by Nathan Zook) + # works also if xv and yv are the same reference, and handles also $x == 0 + if (($xv->[0] *= $yv->[0]) >= $BASE) + { + $xv->[0] = + $xv->[0] - ($xv->[1] = $xv->[0] / $BASE) * $BASE; + }; + return $xv; + } + # $x * 0 => 0 + if ($yv->[0] == 0) + { + @$xv = (0); + return $xv; + } + # multiply a large number a by a single element one, so speed up + my $y = $yv->[0]; my $car = 0; + foreach my $i (@$xv) + { + #$i = $i * $y + $car; $car = $i / $BASE; $i -= $car * $BASE; + $i = $i * $y + $car; $i -= ($car = $i / $BASE) * $BASE; + } + push @$xv, $car if $car != 0; + return $xv; + } + # shortcut for result $x == 0 => result = 0 + return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); + + # since multiplying $x with $x fails, make copy in this case + $yv = [@$xv] if $xv == $yv; # same references? + + my @prod = (); my ($prod,$car,$cty,$xi,$yi); + for $xi (@$xv) + { + $car = 0; $cty = 0; + # looping through this if $xi == 0 is silly - so optimize it away! + $xi = (shift @prod || 0), next if $xi == 0; + for $yi (@$yv) + { + $prod = $xi * $yi + ($prod[$cty] || 0) + $car; + $prod[$cty++] = $prod - ($car = $prod / $BASE) * $BASE; + } + $prod[$cty] += $car if $car; # need really to check for 0? + $xi = shift @prod || 0; # || 0 makes v5.005_3 happy + } + push @$xv, @prod; + $xv; + } + +sub _mul_use_div + { + # (ref to int_num_array, ref to int_num_array) + # multiply two numbers in internal representation + # modifies first arg, second need not be different from first + my ($c,$xv,$yv) = @_; + + if (@$yv == 1) + { + # shortcut for two small numbers, also handles $x == 0 + if (@$xv == 1) + { + # shortcut for two very short numbers (improved by Nathan Zook) + # works also if xv and yv are the same reference, and handles also $x == 0 + if (($xv->[0] *= $yv->[0]) >= $BASE) + { + $xv->[0] = + $xv->[0] - ($xv->[1] = int($xv->[0] / $BASE)) * $BASE; + }; + return $xv; + } + # $x * 0 => 0 + if ($yv->[0] == 0) + { + @$xv = (0); + return $xv; + } + # multiply a large number a by a single element one, so speed up + my $y = $yv->[0]; my $car = 0; + foreach my $i (@$xv) + { + $i = $i * $y + $car; $car = int($i / $BASE); $i -= $car * $BASE; + # This (together with use integer;) does not work on 32-bit Perls + #$i = $i * $y + $car; $i -= ($car = $i / $BASE) * $BASE; + } + push @$xv, $car if $car != 0; + return $xv; + } + # shortcut for result $x == 0 => result = 0 + return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); + + # since multiplying $x with $x fails, make copy in this case + $yv = [@$xv] if $xv == $yv; # same references? + + my @prod = (); my ($prod,$car,$cty,$xi,$yi); + for $xi (@$xv) + { + $car = 0; $cty = 0; + # looping through this if $xi == 0 is silly - so optimize it away! + $xi = (shift @prod || 0), next if $xi == 0; + for $yi (@$yv) + { + $prod = $xi * $yi + ($prod[$cty] || 0) + $car; + $prod[$cty++] = $prod - ($car = int($prod / $BASE)) * $BASE; + } + $prod[$cty] += $car if $car; # need really to check for 0? + $xi = shift @prod || 0; # || 0 makes v5.005_3 happy + } + push @$xv, @prod; + # can't have leading zeros +# __strip_zeros($xv); + $xv; + } + +sub _div_use_mul + { + # ref to array, ref to array, modify first array and return remainder if + # in list context + + # see comments in _div_use_div() for more explanations + + my ($c,$x,$yorg) = @_; + + # the general div algorithm here is about O(N*N) and thus quite slow, so + # we first check for some special cases and use shortcuts to handle them. + + # This works, because we store the numbers in a chunked format where each + # element contains 5..7 digits (depending on system). + + # if both numbers have only one element: + if (@$x == 1 && @$yorg == 1) + { + # shortcut, $yorg and $x are two small numbers + if (wantarray) + { + my $r = [ $x->[0] % $yorg->[0] ]; + $x->[0] = int($x->[0] / $yorg->[0]); + return ($x,$r); + } + else + { + $x->[0] = int($x->[0] / $yorg->[0]); + return $x; + } + } + + # if x has more than one, but y has only one element: + if (@$yorg == 1) + { + my $rem; + $rem = _mod($c,[ @$x ],$yorg) if wantarray; + + # shortcut, $y is < $BASE + my $j = scalar @$x; my $r = 0; + my $y = $yorg->[0]; my $b; + while ($j-- > 0) + { + $b = $r * $BASE + $x->[$j]; + $x->[$j] = int($b/$y); + $r = $b % $y; + } + pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero + return ($x,$rem) if wantarray; + return $x; + } + + # now x and y have more than one element + + # check whether y has more elements than x, if yet, the result will be 0 + if (@$yorg > @$x) + { + my $rem; + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to original array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; # only x, which is [0] now + } + # check whether the numbers have the same number of elements, in that case + # the result will fit into one element and can be computed efficiently + if (@$yorg == @$x) + { + my $rem; + # if $yorg has more digits than $x (it's leading element is longer than + # the one from $x), the result will also be 0: + if (length(int($yorg->[-1])) > length(int($x->[-1]))) + { + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to org array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # now calculate $x / $yorg + if (length(int($yorg->[-1])) == length(int($x->[-1]))) + { + # same length, so make full compare + + my $a = 0; my $j = scalar @$x - 1; + # manual way (abort if unequal, good for early ne) + while ($j >= 0) + { + last if ($a = $x->[$j] - $yorg->[$j]); $j--; + } + # $a contains the result of the compare between X and Y + # a < 0: x < y, a == 0: x == y, a > 0: x > y + if ($a <= 0) + { + $rem = [ 0 ]; # a = 0 => x == y => rem 0 + $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x + splice(@$x,1); # keep single element + $x->[0] = 0; # if $a < 0 + $x->[0] = 1 if $a == 0; # $x == $y + return ($x,$rem) if wantarray; + return $x; + } + # $x >= $y, so proceed normally + } + } + + # all other cases: + + my $y = [ @$yorg ]; # always make copy to preserve + + my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); + + $car = $bar = $prd = 0; + if (($dd = int($BASE/($y->[-1]+1))) != 1) + { + for $xi (@$x) + { + $xi = $xi * $dd + $car; + $xi -= ($car = int($xi * $RBASE)) * $BASE; # see USE_MUL + } + push(@$x, $car); $car = 0; + for $yi (@$y) + { + $yi = $yi * $dd + $car; + $yi -= ($car = int($yi * $RBASE)) * $BASE; # see USE_MUL + } + } + else + { + push(@$x, 0); + } + @q = (); ($v2,$v1) = @$y[-2,-1]; + $v2 = 0 unless $v2; + while ($#$x > $#$y) + { + ($u2,$u1,$u0) = @$x[-3..-1]; + $u2 = 0 unless $u2; + #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" + # if $v1 == 0; + $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); + --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); + if ($q) + { + ($car, $bar) = (0,0); + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $prd = $q * $y->[$yi] + $car; + $prd -= ($car = int($prd * $RBASE)) * $BASE; # see USE_MUL + $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); + } + if ($x->[-1] < $car + $bar) + { + $car = 0; --$q; + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $x->[$xi] -= $BASE + if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); + } + } + } + pop(@$x); + unshift(@q, $q); + } + if (wantarray) + { + @d = (); + if ($dd != 1) + { + $car = 0; + for $xi (reverse @$x) + { + $prd = $car * $BASE + $xi; + $car = $prd - ($tmp = int($prd / $dd)) * $dd; # see USE_MUL + unshift(@d, $tmp); + } + } + else + { + @d = @$x; + } + @$x = @q; + my $d = \@d; + __strip_zeros($x); + __strip_zeros($d); + return ($x,$d); + } + @$x = @q; + __strip_zeros($x); + $x; + } + +sub _div_use_div_64 + { + # ref to array, ref to array, modify first array and return remainder if + # in list context + # This version works on 64 bit integers + my ($c,$x,$yorg) = @_; + + use integer; + # the general div algorithm here is about O(N*N) and thus quite slow, so + # we first check for some special cases and use shortcuts to handle them. + + # This works, because we store the numbers in a chunked format where each + # element contains 5..7 digits (depending on system). + + # if both numbers have only one element: + if (@$x == 1 && @$yorg == 1) + { + # shortcut, $yorg and $x are two small numbers + if (wantarray) + { + my $r = [ $x->[0] % $yorg->[0] ]; + $x->[0] = int($x->[0] / $yorg->[0]); + return ($x,$r); + } + else + { + $x->[0] = int($x->[0] / $yorg->[0]); + return $x; + } + } + # if x has more than one, but y has only one element: + if (@$yorg == 1) + { + my $rem; + $rem = _mod($c,[ @$x ],$yorg) if wantarray; + + # shortcut, $y is < $BASE + my $j = scalar @$x; my $r = 0; + my $y = $yorg->[0]; my $b; + while ($j-- > 0) + { + $b = $r * $BASE + $x->[$j]; + $x->[$j] = int($b/$y); + $r = $b % $y; + } + pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero + return ($x,$rem) if wantarray; + return $x; + } + # now x and y have more than one element + + # check whether y has more elements than x, if yet, the result will be 0 + if (@$yorg > @$x) + { + my $rem; + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to original array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; # only x, which is [0] now + } + # check whether the numbers have the same number of elements, in that case + # the result will fit into one element and can be computed efficiently + if (@$yorg == @$x) + { + my $rem; + # if $yorg has more digits than $x (it's leading element is longer than + # the one from $x), the result will also be 0: + if (length(int($yorg->[-1])) > length(int($x->[-1]))) + { + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to org array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # now calculate $x / $yorg + + if (length(int($yorg->[-1])) == length(int($x->[-1]))) + { + # same length, so make full compare + + my $a = 0; my $j = scalar @$x - 1; + # manual way (abort if unequal, good for early ne) + while ($j >= 0) + { + last if ($a = $x->[$j] - $yorg->[$j]); $j--; + } + # $a contains the result of the compare between X and Y + # a < 0: x < y, a == 0: x == y, a > 0: x > y + if ($a <= 0) + { + $rem = [ 0 ]; # a = 0 => x == y => rem 0 + $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x + splice(@$x,1); # keep single element + $x->[0] = 0; # if $a < 0 + $x->[0] = 1 if $a == 0; # $x == $y + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # $x >= $y, so proceed normally + + } + } + + # all other cases: + + my $y = [ @$yorg ]; # always make copy to preserve + + my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); + + $car = $bar = $prd = 0; + if (($dd = int($BASE/($y->[-1]+1))) != 1) + { + for $xi (@$x) + { + $xi = $xi * $dd + $car; + $xi -= ($car = int($xi / $BASE)) * $BASE; + } + push(@$x, $car); $car = 0; + for $yi (@$y) + { + $yi = $yi * $dd + $car; + $yi -= ($car = int($yi / $BASE)) * $BASE; + } + } + else + { + push(@$x, 0); + } + + # @q will accumulate the final result, $q contains the current computed + # part of the final result + + @q = (); ($v2,$v1) = @$y[-2,-1]; + $v2 = 0 unless $v2; + while ($#$x > $#$y) + { + ($u2,$u1,$u0) = @$x[-3..-1]; + $u2 = 0 unless $u2; + #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" + # if $v1 == 0; + $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); + --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); + if ($q) + { + ($car, $bar) = (0,0); + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $prd = $q * $y->[$yi] + $car; + $prd -= ($car = int($prd / $BASE)) * $BASE; + $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); + } + if ($x->[-1] < $car + $bar) + { + $car = 0; --$q; + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $x->[$xi] -= $BASE + if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); + } + } + } + pop(@$x); unshift(@q, $q); + } + if (wantarray) + { + @d = (); + if ($dd != 1) + { + $car = 0; + for $xi (reverse @$x) + { + $prd = $car * $BASE + $xi; + $car = $prd - ($tmp = int($prd / $dd)) * $dd; + unshift(@d, $tmp); + } + } + else + { + @d = @$x; + } + @$x = @q; + my $d = \@d; + __strip_zeros($x); + __strip_zeros($d); + return ($x,$d); + } + @$x = @q; + __strip_zeros($x); + $x; + } + +sub _div_use_div + { + # ref to array, ref to array, modify first array and return remainder if + # in list context + my ($c,$x,$yorg) = @_; + + # the general div algorithm here is about O(N*N) and thus quite slow, so + # we first check for some special cases and use shortcuts to handle them. + + # This works, because we store the numbers in a chunked format where each + # element contains 5..7 digits (depending on system). + + # if both numbers have only one element: + if (@$x == 1 && @$yorg == 1) + { + # shortcut, $yorg and $x are two small numbers + if (wantarray) + { + my $r = [ $x->[0] % $yorg->[0] ]; + $x->[0] = int($x->[0] / $yorg->[0]); + return ($x,$r); + } + else + { + $x->[0] = int($x->[0] / $yorg->[0]); + return $x; + } + } + # if x has more than one, but y has only one element: + if (@$yorg == 1) + { + my $rem; + $rem = _mod($c,[ @$x ],$yorg) if wantarray; + + # shortcut, $y is < $BASE + my $j = scalar @$x; my $r = 0; + my $y = $yorg->[0]; my $b; + while ($j-- > 0) + { + $b = $r * $BASE + $x->[$j]; + $x->[$j] = int($b/$y); + $r = $b % $y; + } + pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero + return ($x,$rem) if wantarray; + return $x; + } + # now x and y have more than one element + + # check whether y has more elements than x, if yet, the result will be 0 + if (@$yorg > @$x) + { + my $rem; + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to original array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; # only x, which is [0] now + } + # check whether the numbers have the same number of elements, in that case + # the result will fit into one element and can be computed efficiently + if (@$yorg == @$x) + { + my $rem; + # if $yorg has more digits than $x (it's leading element is longer than + # the one from $x), the result will also be 0: + if (length(int($yorg->[-1])) > length(int($x->[-1]))) + { + $rem = [@$x] if wantarray; # make copy + splice (@$x,1); # keep ref to org array + $x->[0] = 0; # set to 0 + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # now calculate $x / $yorg + + if (length(int($yorg->[-1])) == length(int($x->[-1]))) + { + # same length, so make full compare + + my $a = 0; my $j = scalar @$x - 1; + # manual way (abort if unequal, good for early ne) + while ($j >= 0) + { + last if ($a = $x->[$j] - $yorg->[$j]); $j--; + } + # $a contains the result of the compare between X and Y + # a < 0: x < y, a == 0: x == y, a > 0: x > y + if ($a <= 0) + { + $rem = [ 0 ]; # a = 0 => x == y => rem 0 + $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x + splice(@$x,1); # keep single element + $x->[0] = 0; # if $a < 0 + $x->[0] = 1 if $a == 0; # $x == $y + return ($x,$rem) if wantarray; # including remainder? + return $x; + } + # $x >= $y, so proceed normally + + } + } + + # all other cases: + + my $y = [ @$yorg ]; # always make copy to preserve + + my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); + + $car = $bar = $prd = 0; + if (($dd = int($BASE/($y->[-1]+1))) != 1) + { + for $xi (@$x) + { + $xi = $xi * $dd + $car; + $xi -= ($car = int($xi / $BASE)) * $BASE; + } + push(@$x, $car); $car = 0; + for $yi (@$y) + { + $yi = $yi * $dd + $car; + $yi -= ($car = int($yi / $BASE)) * $BASE; + } + } + else + { + push(@$x, 0); + } + + # @q will accumulate the final result, $q contains the current computed + # part of the final result + + @q = (); ($v2,$v1) = @$y[-2,-1]; + $v2 = 0 unless $v2; + while ($#$x > $#$y) + { + ($u2,$u1,$u0) = @$x[-3..-1]; + $u2 = 0 unless $u2; + #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" + # if $v1 == 0; + $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); + --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); + if ($q) + { + ($car, $bar) = (0,0); + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $prd = $q * $y->[$yi] + $car; + $prd -= ($car = int($prd / $BASE)) * $BASE; + $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); + } + if ($x->[-1] < $car + $bar) + { + $car = 0; --$q; + for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) + { + $x->[$xi] -= $BASE + if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); + } + } + } + pop(@$x); unshift(@q, $q); + } + if (wantarray) + { + @d = (); + if ($dd != 1) + { + $car = 0; + for $xi (reverse @$x) + { + $prd = $car * $BASE + $xi; + $car = $prd - ($tmp = int($prd / $dd)) * $dd; + unshift(@d, $tmp); + } + } + else + { + @d = @$x; + } + @$x = @q; + my $d = \@d; + __strip_zeros($x); + __strip_zeros($d); + return ($x,$d); + } + @$x = @q; + __strip_zeros($x); + $x; + } + +############################################################################## +# testing + +sub _acmp + { + # internal absolute post-normalized compare (ignore signs) + # ref to array, ref to array, return <0, 0, >0 + # arrays must have at least one entry; this is not checked for + my ($c,$cx,$cy) = @_; + + # shortcut for short numbers + return (($cx->[0] <=> $cy->[0]) <=> 0) + if scalar @$cx == scalar @$cy && scalar @$cx == 1; + + # fast comp based on number of array elements (aka pseudo-length) + my $lxy = (scalar @$cx - scalar @$cy) + # or length of first element if same number of elements (aka difference 0) + || + # need int() here because sometimes the last element is '00018' vs '18' + (length(int($cx->[-1])) - length(int($cy->[-1]))); + return -1 if $lxy < 0; # already differs, ret + return 1 if $lxy > 0; # ditto + + # manual way (abort if unequal, good for early ne) + my $a; my $j = scalar @$cx; + while (--$j >= 0) + { + last if ($a = $cx->[$j] - $cy->[$j]); + } + $a <=> 0; + } + +sub _len + { + # compute number of digits in base 10 + + # int() because add/sub sometimes leaves strings (like '00005') instead of + # '5' in this place, thus causing length() to report wrong length + my $cx = $_[1]; + + (@$cx-1)*$BASE_LEN+length(int($cx->[-1])); + } + +sub _digit + { + # Return the nth digit. Zero is rightmost, so _digit(123,0) gives 3. + # Negative values count from the left, so _digit(123, -1) gives 1. + my ($c,$x,$n) = @_; + + my $len = _len('',$x); + + $n += $len if $n < 0; # -1 last, -2 second-to-last + return "0" if $n < 0 || $n >= $len; # return 0 for digits out of range + + my $elem = int($n / $BASE_LEN); # which array element + my $digit = $n % $BASE_LEN; # which digit in this element + substr("$x->[$elem]", -$digit-1, 1); + } + +sub _zeros + { + # return amount of trailing zeros in decimal + # check each array elem in _m for having 0 at end as long as elem == 0 + # Upon finding a elem != 0, stop + my $x = $_[1]; + + return 0 if scalar @$x == 1 && $x->[0] == 0; + + my $zeros = 0; my $elem; + foreach my $e (@$x) + { + if ($e != 0) + { + $elem = "$e"; # preserve x + $elem =~ s/.*?(0*$)/$1/; # strip anything not zero + $zeros *= $BASE_LEN; # elems * 5 + $zeros += length($elem); # count trailing zeros + last; # early out + } + $zeros ++; # real else branch: 50% slower! + } + $zeros; + } + +############################################################################## +# _is_* routines + +sub _is_zero + { + # return true if arg is zero + (((scalar @{$_[1]} == 1) && ($_[1]->[0] == 0))) <=> 0; + } + +sub _is_even + { + # return true if arg is even + (!($_[1]->[0] & 1)) <=> 0; + } + +sub _is_odd + { + # return true if arg is odd + (($_[1]->[0] & 1)) <=> 0; + } + +sub _is_one + { + # return true if arg is one + (scalar @{$_[1]} == 1) && ($_[1]->[0] == 1) <=> 0; + } + +sub _is_two + { + # return true if arg is two + (scalar @{$_[1]} == 1) && ($_[1]->[0] == 2) <=> 0; + } + +sub _is_ten + { + # return true if arg is ten + (scalar @{$_[1]} == 1) && ($_[1]->[0] == 10) <=> 0; + } + +sub __strip_zeros + { + # internal normalization function that strips leading zeros from the array + # args: ref to array + my $s = shift; + + my $cnt = scalar @$s; # get count of parts + my $i = $cnt-1; + push @$s,0 if $i < 0; # div might return empty results, so fix it + + return $s if @$s == 1; # early out + + #print "strip: cnt $cnt i $i\n"; + # '0', '3', '4', '0', '0', + # 0 1 2 3 4 + # cnt = 5, i = 4 + # i = 4 + # i = 3 + # => fcnt = cnt - i (5-2 => 3, cnt => 5-1 = 4, throw away from 4th pos) + # >= 1: skip first part (this can be zero) + while ($i > 0) { last if $s->[$i] != 0; $i--; } + $i++; splice @$s,$i if ($i < $cnt); # $i cant be 0 + $s; + } + +############################################################################### +# check routine to test internal state for corruptions + +sub _check + { + # used by the test suite + my $x = $_[1]; + + return "$x is not a reference" if !ref($x); + + # are all parts are valid? + my $i = 0; my $j = scalar @$x; my ($e,$try); + while ($i < $j) + { + $e = $x->[$i]; $e = 'undef' unless defined $e; + $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e)"; + last if $e !~ /^[+]?[0-9]+$/; + $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e) (stringify)"; + last if "$e" !~ /^[+]?[0-9]+$/; + $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e) (cat-stringify)"; + last if '' . "$e" !~ /^[+]?[0-9]+$/; + $try = ' < 0 || >= $BASE; '."($x, $e)"; + last if $e <0 || $e >= $BASE; + # this test is disabled, since new/bnorm and certain ops (like early out + # in add/sub) are allowed/expected to leave '00000' in some elements + #$try = '=~ /^00+/; '."($x, $e)"; + #last if $e =~ /^00+/; + $i++; + } + return "Illegal part '$e' at pos $i (tested: $try)" if $i < $j; + 0; + } + + +############################################################################### + +sub _mod + { + # if possible, use mod shortcut + my ($c,$x,$yo) = @_; + + # slow way since $y too big + if (scalar @$yo > 1) + { + my ($xo,$rem) = _div($c,$x,$yo); + @$x = @$rem; + return $x; + } + + my $y = $yo->[0]; + + # if both are single element arrays + if (scalar @$x == 1) + { + $x->[0] %= $y; + return $x; + } + + # if @$x has more than one element, but @$y is a single element + my $b = $BASE % $y; + if ($b == 0) + { + # when BASE % Y == 0 then (B * BASE) % Y == 0 + # (B * BASE) % $y + A % Y => A % Y + # so need to consider only last element: O(1) + $x->[0] %= $y; + } + elsif ($b == 1) + { + # else need to go through all elements in @$x: O(N), but loop is a bit + # simplified + my $r = 0; + foreach (@$x) + { + $r = ($r + $_) % $y; # not much faster, but heh... + #$r += $_ % $y; $r %= $y; + } + $r = 0 if $r == $y; + $x->[0] = $r; + } + else + { + # else need to go through all elements in @$x: O(N) + my $r = 0; + my $bm = 1; + foreach (@$x) + { + $r = ($_ * $bm + $r) % $y; + $bm = ($bm * $b) % $y; + + #$r += ($_ % $y) * $bm; + #$bm *= $b; + #$bm %= $y; + #$r %= $y; + } + $r = 0 if $r == $y; + $x->[0] = $r; + } + @$x = $x->[0]; # keep one element of @$x + return $x; + } + +############################################################################## +# shifts + +sub _rsft + { + my ($c,$x,$y,$n) = @_; + + if ($n != 10) + { + $n = _new($c,$n); return _div($c,$x, _pow($c,$n,$y)); + } + + # shortcut (faster) for shifting by 10) + # multiples of $BASE_LEN + my $dst = 0; # destination + my $src = _num($c,$y); # as normal int + my $xlen = (@$x-1)*$BASE_LEN+length(int($x->[-1])); # len of x in digits + if ($src >= $xlen or ($src == $xlen and ! defined $x->[1])) + { + # 12345 67890 shifted right by more than 10 digits => 0 + splice (@$x,1); # leave only one element + $x->[0] = 0; # set to zero + return $x; + } + my $rem = $src % $BASE_LEN; # remainder to shift + $src = int($src / $BASE_LEN); # source + if ($rem == 0) + { + splice (@$x,0,$src); # even faster, 38.4 => 39.3 + } + else + { + my $len = scalar @$x - $src; # elems to go + my $vd; my $z = '0'x $BASE_LEN; + $x->[scalar @$x] = 0; # avoid || 0 test inside loop + while ($dst < $len) + { + $vd = $z.$x->[$src]; + $vd = substr($vd,-$BASE_LEN,$BASE_LEN-$rem); + $src++; + $vd = substr($z.$x->[$src],-$rem,$rem) . $vd; + $vd = substr($vd,-$BASE_LEN,$BASE_LEN) if length($vd) > $BASE_LEN; + $x->[$dst] = int($vd); + $dst++; + } + splice (@$x,$dst) if $dst > 0; # kill left-over array elems + pop @$x if $x->[-1] == 0 && @$x > 1; # kill last element if 0 + } # else rem == 0 + $x; + } + +sub _lsft + { + my ($c,$x,$y,$n) = @_; + + if ($n != 10) + { + $n = _new($c,$n); return _mul($c,$x, _pow($c,$n,$y)); + } + + # shortcut (faster) for shifting by 10) since we are in base 10eX + # multiples of $BASE_LEN: + my $src = scalar @$x; # source + my $len = _num($c,$y); # shift-len as normal int + my $rem = $len % $BASE_LEN; # remainder to shift + my $dst = $src + int($len/$BASE_LEN); # destination + my $vd; # further speedup + $x->[$src] = 0; # avoid first ||0 for speed + my $z = '0' x $BASE_LEN; + while ($src >= 0) + { + $vd = $x->[$src]; $vd = $z.$vd; + $vd = substr($vd,-$BASE_LEN+$rem,$BASE_LEN-$rem); + $vd .= $src > 0 ? substr($z.$x->[$src-1],-$BASE_LEN,$rem) : '0' x $rem; + $vd = substr($vd,-$BASE_LEN,$BASE_LEN) if length($vd) > $BASE_LEN; + $x->[$dst] = int($vd); + $dst--; $src--; + } + # set lowest parts to 0 + while ($dst >= 0) { $x->[$dst--] = 0; } + # fix spurious last zero element + splice @$x,-1 if $x->[-1] == 0; + $x; + } + +sub _pow + { + # power of $x to $y + # ref to array, ref to array, return ref to array + my ($c,$cx,$cy) = @_; + + if (scalar @$cy == 1 && $cy->[0] == 0) + { + splice (@$cx,1); $cx->[0] = 1; # y == 0 => x => 1 + return $cx; + } + if ((scalar @$cx == 1 && $cx->[0] == 1) || # x == 1 + (scalar @$cy == 1 && $cy->[0] == 1)) # or y == 1 + { + return $cx; + } + if (scalar @$cx == 1 && $cx->[0] == 0) + { + splice (@$cx,1); $cx->[0] = 0; # 0 ** y => 0 (if not y <= 0) + return $cx; + } + + my $pow2 = _one(); + + my $y_bin = _as_bin($c,$cy); $y_bin =~ s/^0b//; + my $len = length($y_bin); + while (--$len > 0) + { + _mul($c,$pow2,$cx) if substr($y_bin,$len,1) eq '1'; # is odd? + _mul($c,$cx,$cx); + } + + _mul($c,$cx,$pow2); + $cx; + } + +sub _nok { + # Return binomial coefficient (n over k). + # Given refs to arrays, return ref to array. + # First input argument is modified. + + my ($c, $n, $k) = @_; + + # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as + # nok(n, n-k), to minimize the number if iterations in the loop. + + { + my $twok = _mul($c, _two($c), _copy($c, $k)); # 2 * k + if (_acmp($c, $twok, $n) > 0) { # if 2*k > n + $k = _sub($c, _copy($c, $n), $k); # k = n - k + } + } + + # Example: + # + # / 7 \ 7! 1*2*3*4 * 5*6*7 5 * 6 * 7 6 7 + # | | = --------- = --------------- = --------- = 5 * - * - + # \ 3 / (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3 2 3 + + if (_is_zero($c, $k)) { + @$n = 1; + } + + else { + + # Make a copy of the original n, since we'll be modifying n in-place. + + my $n_orig = _copy($c, $n); + + # n = 5, f = 6, d = 2 (cf. example above) + + _sub($c, $n, $k); + _inc($c, $n); + + my $f = _copy($c, $n); + _inc($c, $f); + + my $d = _two($c); + + # while f <= n (the original n, that is) ... + + while (_acmp($c, $f, $n_orig) <= 0) { + + # n = (n * f / d) == 5 * 6 / 2 (cf. example above) + + _mul($c, $n, $f); + _div($c, $n, $d); + + # f = 7, d = 3 (cf. example above) + + _inc($c, $f); + _inc($c, $d); + } + + } + + return $n; +} + +my @factorials = ( + 1, + 1, + 2, + 2*3, + 2*3*4, + 2*3*4*5, + 2*3*4*5*6, + 2*3*4*5*6*7, +); + +sub _fac + { + # factorial of $x + # ref to array, return ref to array + my ($c,$cx) = @_; + + if ((@$cx == 1) && ($cx->[0] <= 7)) + { + $cx->[0] = $factorials[$cx->[0]]; # 0 => 1, 1 => 1, 2 => 2 etc. + return $cx; + } + + if ((@$cx == 1) && # we do this only if $x >= 12 and $x <= 7000 + ($cx->[0] >= 12 && $cx->[0] < 7000)) + { + + # Calculate (k-j) * (k-j+1) ... k .. (k+j-1) * (k + j) + # See http://blogten.blogspot.com/2007/01/calculating-n.html + # The above series can be expressed as factors: + # k * k - (j - i) * 2 + # We cache k*k, and calculate (j * j) as the sum of the first j odd integers + + # This will not work when N exceeds the storage of a Perl scalar, however, + # in this case the algorithm would be way to slow to terminate, anyway. + + # As soon as the last element of $cx is 0, we split it up and remember + # how many zeors we got so far. The reason is that n! will accumulate + # zeros at the end rather fast. + my $zero_elements = 0; + + # If n is even, set n = n -1 + my $k = _num($c,$cx); my $even = 1; + if (($k & 1) == 0) + { + $even = $k; $k --; + } + # set k to the center point + $k = ($k + 1) / 2; +# print "k $k even: $even\n"; + # now calculate k * k + my $k2 = $k * $k; + my $odd = 1; my $sum = 1; + my $i = $k - 1; + # keep reference to x + my $new_x = _new($c, $k * $even); + @$cx = @$new_x; + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } +# print STDERR "x = ", _str($c,$cx),"\n"; + my $BASE2 = int(sqrt($BASE))-1; + my $j = 1; + while ($j <= $i) + { + my $m = ($k2 - $sum); $odd += 2; $sum += $odd; $j++; + while ($j <= $i && ($m < $BASE2) && (($k2 - $sum) < $BASE2)) + { + $m *= ($k2 - $sum); + $odd += 2; $sum += $odd; $j++; +# print STDERR "\n k2 $k2 m $m sum $sum odd $odd\n"; sleep(1); + } + if ($m < $BASE) + { + _mul($c,$cx,[$m]); + } + else + { + _mul($c,$cx,$c->_new($m)); + } + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } +# print STDERR "Calculate $k2 - $sum = $m (x = ", _str($c,$cx),")\n"; + } + # multiply in the zeros again + unshift @$cx, (0) x $zero_elements; + return $cx; + } + + # go forward until $base is exceeded + # limit is either $x steps (steps == 100 means a result always too high) or + # $base. + my $steps = 100; $steps = $cx->[0] if @$cx == 1; + my $r = 2; my $cf = 3; my $step = 2; my $last = $r; + while ($r*$cf < $BASE && $step < $steps) + { + $last = $r; $r *= $cf++; $step++; + } + if ((@$cx == 1) && $step == $cx->[0]) + { + # completely done, so keep reference to $x and return + $cx->[0] = $r; + return $cx; + } + + # now we must do the left over steps + my $n; # steps still to do + if (scalar @$cx == 1) + { + $n = $cx->[0]; + } + else + { + $n = _copy($c,$cx); + } + + # Set $cx to the last result below $BASE (but keep ref to $x) + $cx->[0] = $last; splice (@$cx,1); + # As soon as the last element of $cx is 0, we split it up and remember + # how many zeors we got so far. The reason is that n! will accumulate + # zeros at the end rather fast. + my $zero_elements = 0; + + # do left-over steps fit into a scalar? + if (ref $n eq 'ARRAY') + { + # No, so use slower inc() & cmp() + # ($n is at least $BASE here) + my $base_2 = int(sqrt($BASE)) - 1; + #print STDERR "base_2: $base_2\n"; + while ($step < $base_2) + { + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + my $b = $step * ($step + 1); $step += 2; + _mul($c,$cx,[$b]); + } + $step = [$step]; + while (_acmp($c,$step,$n) <= 0) + { + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + _mul($c,$cx,$step); _inc($c,$step); + } + } + else + { + # Yes, so we can speed it up slightly + +# print "# left over steps $n\n"; + + my $base_4 = int(sqrt(sqrt($BASE))) - 2; + #print STDERR "base_4: $base_4\n"; + my $n4 = $n - 4; + while ($step < $n4 && $step < $base_4) + { + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + my $b = $step * ($step + 1); $step += 2; $b *= $step * ($step + 1); $step += 2; + _mul($c,$cx,[$b]); + } + my $base_2 = int(sqrt($BASE)) - 1; + my $n2 = $n - 2; + #print STDERR "base_2: $base_2\n"; + while ($step < $n2 && $step < $base_2) + { + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + my $b = $step * ($step + 1); $step += 2; + _mul($c,$cx,[$b]); + } + # do what's left over + while ($step <= $n) + { + _mul($c,$cx,[$step]); $step++; + if ($cx->[0] == 0) + { + $zero_elements ++; shift @$cx; + } + } + } + # multiply in the zeros again + unshift @$cx, (0) x $zero_elements; + $cx; # return result + } + +############################################################################# + +sub _log_int + { + # calculate integer log of $x to base $base + # ref to array, ref to array - return ref to array + my ($c,$x,$base) = @_; + + # X == 0 => NaN + return if (scalar @$x == 1 && $x->[0] == 0); + # BASE 0 or 1 => NaN + return if (scalar @$base == 1 && $base->[0] < 2); + my $cmp = _acmp($c,$x,$base); # X == BASE => 1 + if ($cmp == 0) + { + splice (@$x,1); $x->[0] = 1; + return ($x,1) + } + # X < BASE + if ($cmp < 0) + { + splice (@$x,1); $x->[0] = 0; + return ($x,undef); + } + + my $x_org = _copy($c,$x); # preserve x + splice(@$x,1); $x->[0] = 1; # keep ref to $x + + # Compute a guess for the result based on: + # $guess = int ( length_in_base_10(X) / ( log(base) / log(10) ) ) + my $len = _len($c,$x_org); + my $log = log($base->[-1]) / log(10); + + # for each additional element in $base, we add $BASE_LEN to the result, + # based on the observation that log($BASE,10) is BASE_LEN and + # log(x*y) == log(x) + log(y): + $log += ((scalar @$base)-1) * $BASE_LEN; + + # calculate now a guess based on the values obtained above: + my $res = int($len / $log); + + $x->[0] = $res; + my $trial = _pow ($c, _copy($c, $base), $x); + my $a = _acmp($c,$trial,$x_org); + +# print STDERR "# trial ", _str($c,$x)," was: $a (0 = exact, -1 too small, +1 too big)\n"; + + # found an exact result? + return ($x,1) if $a == 0; + + if ($a > 0) + { + # or too big + _div($c,$trial,$base); _dec($c, $x); + while (($a = _acmp($c,$trial,$x_org)) > 0) + { +# print STDERR "# big _log_int at ", _str($c,$x), "\n"; + _div($c,$trial,$base); _dec($c, $x); + } + # result is now exact (a == 0), or too small (a < 0) + return ($x, $a == 0 ? 1 : 0); + } + + # else: result was to small + _mul($c,$trial,$base); + + # did we now get the right result? + $a = _acmp($c,$trial,$x_org); + + if ($a == 0) # yes, exactly + { + _inc($c, $x); + return ($x,1); + } + return ($x,0) if $a > 0; + + # Result still too small (we should come here only if the estimate above + # was very off base): + + # Now let the normal trial run obtain the real result + # Simple loop that increments $x by 2 in each step, possible overstepping + # the real result + + my $base_mul = _mul($c, _copy($c,$base), $base); # $base * $base + + while (($a = _acmp($c,$trial,$x_org)) < 0) + { +# print STDERR "# small _log_int at ", _str($c,$x), "\n"; + _mul($c,$trial,$base_mul); _add($c, $x, [2]); + } + + my $exact = 1; + if ($a > 0) + { + # overstepped the result + _dec($c, $x); + _div($c,$trial,$base); + $a = _acmp($c,$trial,$x_org); + if ($a > 0) + { + _dec($c, $x); + } + $exact = 0 if $a != 0; # a = -1 => not exact result, a = 0 => exact + } + + ($x,$exact); # return result + } + +# for debugging: + use constant DEBUG => 0; + my $steps = 0; + sub steps { $steps }; + +sub _sqrt + { + # square-root of $x in place + # Compute a guess of the result (by rule of thumb), then improve it via + # Newton's method. + my ($c,$x) = @_; + + if (scalar @$x == 1) + { + # fits into one Perl scalar, so result can be computed directly + $x->[0] = int(sqrt($x->[0])); + return $x; + } + my $y = _copy($c,$x); + # hopefully _len/2 is < $BASE, the -1 is to always undershot the guess + # since our guess will "grow" + my $l = int((_len($c,$x)-1) / 2); + + my $lastelem = $x->[-1]; # for guess + my $elems = scalar @$x - 1; + # not enough digits, but could have more? + if ((length($lastelem) <= 3) && ($elems > 1)) + { + # right-align with zero pad + my $len = length($lastelem) & 1; + print "$lastelem => " if DEBUG; + $lastelem .= substr($x->[-2] . '0' x $BASE_LEN,0,$BASE_LEN); + # former odd => make odd again, or former even to even again + $lastelem = $lastelem / 10 if (length($lastelem) & 1) != $len; + print "$lastelem\n" if DEBUG; + } + + # construct $x (instead of _lsft($c,$x,$l,10) + my $r = $l % $BASE_LEN; # 10000 00000 00000 00000 ($BASE_LEN=5) + $l = int($l / $BASE_LEN); + print "l = $l " if DEBUG; + + splice @$x,$l; # keep ref($x), but modify it + + # we make the first part of the guess not '1000...0' but int(sqrt($lastelem)) + # that gives us: + # 14400 00000 => sqrt(14400) => guess first digits to be 120 + # 144000 000000 => sqrt(144000) => guess 379 + + print "$lastelem (elems $elems) => " if DEBUG; + $lastelem = $lastelem / 10 if ($elems & 1 == 1); # odd or even? + my $g = sqrt($lastelem); $g =~ s/\.//; # 2.345 => 2345 + $r -= 1 if $elems & 1 == 0; # 70 => 7 + + # padd with zeros if result is too short + $x->[$l--] = int(substr($g . '0' x $r,0,$r+1)); + print "now ",$x->[-1] if DEBUG; + print " would have been ", int('1' . '0' x $r),"\n" if DEBUG; + + # If @$x > 1, we could compute the second elem of the guess, too, to create + # an even better guess. Not implemented yet. Does it improve performance? + $x->[$l--] = 0 while ($l >= 0); # all other digits of guess are zero + + print "start x= ",_str($c,$x),"\n" if DEBUG; + my $two = _two(); + my $last = _zero(); + my $lastlast = _zero(); + $steps = 0 if DEBUG; + while (_acmp($c,$last,$x) != 0 && _acmp($c,$lastlast,$x) != 0) + { + $steps++ if DEBUG; + $lastlast = _copy($c,$last); + $last = _copy($c,$x); + _add($c,$x, _div($c,_copy($c,$y),$x)); + _div($c,$x, $two ); + print " x= ",_str($c,$x),"\n" if DEBUG; + } + print "\nsteps in sqrt: $steps, " if DEBUG; + _dec($c,$x) if _acmp($c,$y,_mul($c,_copy($c,$x),$x)) < 0; # overshot? + print " final ",$x->[-1],"\n" if DEBUG; + $x; + } + +sub _root + { + # take n'th root of $x in place (n >= 3) + my ($c,$x,$n) = @_; + + if (scalar @$x == 1) + { + if (scalar @$n > 1) + { + # result will always be smaller than 2 so trunc to 1 at once + $x->[0] = 1; + } + else + { + # fits into one Perl scalar, so result can be computed directly + # cannot use int() here, because it rounds wrongly (try + # (81 ** 3) ** (1/3) to see what I mean) + #$x->[0] = int( $x->[0] ** (1 / $n->[0]) ); + # round to 8 digits, then truncate result to integer + $x->[0] = int ( sprintf ("%.8f", $x->[0] ** (1 / $n->[0]) ) ); + } + return $x; + } + + # we know now that X is more than one element long + + # if $n is a power of two, we can repeatedly take sqrt($X) and find the + # proper result, because sqrt(sqrt($x)) == root($x,4) + my $b = _as_bin($c,$n); + if ($b =~ /0b1(0+)$/) + { + my $count = CORE::length($1); # 0b100 => len('00') => 2 + my $cnt = $count; # counter for loop + unshift (@$x, 0); # add one element, together with one + # more below in the loop this makes 2 + while ($cnt-- > 0) + { + # 'inflate' $X by adding one element, basically computing + # $x * $BASE * $BASE. This gives us more $BASE_LEN digits for result + # since len(sqrt($X)) approx == len($x) / 2. + unshift (@$x, 0); + # calculate sqrt($x), $x is now one element to big, again. In the next + # round we make that two, again. + _sqrt($c,$x); + } + # $x is now one element to big, so truncate result by removing it + splice (@$x,0,1); + } + else + { + # trial computation by starting with 2,4,8,16 etc until we overstep + my $step; + my $trial = _two(); + + # while still to do more than X steps + do + { + $step = _two(); + while (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) < 0) + { + _mul ($c, $step, [2]); + _add ($c, $trial, $step); + } + + # hit exactly? + if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) == 0) + { + @$x = @$trial; # make copy while preserving ref to $x + return $x; + } + # overstepped, so go back on step + _sub($c, $trial, $step); + } while (scalar @$step > 1 || $step->[0] > 128); + + # reset step to 2 + $step = _two(); + # add two, because $trial cannot be exactly the result (otherwise we would + # already have found it) + _add($c, $trial, $step); + + # and now add more and more (2,4,6,8,10 etc) + while (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) < 0) + { + _add ($c, $trial, $step); + } + + # hit not exactly? (overstepped) + if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) > 0) + { + _dec($c,$trial); + } + + # hit not exactly? (overstepped) + # 80 too small, 81 slightly too big, 82 too big + if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) > 0) + { + _dec ($c, $trial); + } + + @$x = @$trial; # make copy while preserving ref to $x + return $x; + } + $x; + } + +############################################################################## +# binary stuff + +sub _and + { + my ($c,$x,$y) = @_; + + # the shortcut makes equal, large numbers _really_ fast, and makes only a + # very small performance drop for small numbers (e.g. something with less + # than 32 bit) Since we optimize for large numbers, this is enabled. + return $x if _acmp($c,$x,$y) == 0; # shortcut + + my $m = _one(); my ($xr,$yr); + my $mask = $AND_MASK; + + my $x1 = $x; + my $y1 = _copy($c,$y); # make copy + $x = _zero(); + my ($b,$xrr,$yrr); + use integer; + while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) + { + ($x1, $xr) = _div($c,$x1,$mask); + ($y1, $yr) = _div($c,$y1,$mask); + + # make ints() from $xr, $yr + # this is when the AND_BITS are greater than $BASE and is slower for + # small (<256 bits) numbers, but faster for large numbers. Disabled + # due to KISS principle + +# $b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } +# $b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } +# _add($c,$x, _mul($c, _new( $c, ($xrr & $yrr) ), $m) ); + + # 0+ due to '&' doesn't work in strings + _add($c,$x, _mul($c, [ 0+$xr->[0] & 0+$yr->[0] ], $m) ); + _mul($c,$m,$mask); + } + $x; + } + +sub _xor + { + my ($c,$x,$y) = @_; + + return _zero() if _acmp($c,$x,$y) == 0; # shortcut (see -and) + + my $m = _one(); my ($xr,$yr); + my $mask = $XOR_MASK; + + my $x1 = $x; + my $y1 = _copy($c,$y); # make copy + $x = _zero(); + my ($b,$xrr,$yrr); + use integer; + while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) + { + ($x1, $xr) = _div($c,$x1,$mask); + ($y1, $yr) = _div($c,$y1,$mask); + # make ints() from $xr, $yr (see _and()) + #$b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } + #$b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } + #_add($c,$x, _mul($c, _new( $c, ($xrr ^ $yrr) ), $m) ); + + # 0+ due to '^' doesn't work in strings + _add($c,$x, _mul($c, [ 0+$xr->[0] ^ 0+$yr->[0] ], $m) ); + _mul($c,$m,$mask); + } + # the loop stops when the shorter of the two numbers is exhausted + # the remainder of the longer one will survive bit-by-bit, so we simple + # multiply-add it in + _add($c,$x, _mul($c, $x1, $m) ) if !_is_zero($c,$x1); + _add($c,$x, _mul($c, $y1, $m) ) if !_is_zero($c,$y1); + + $x; + } + +sub _or + { + my ($c,$x,$y) = @_; + + return $x if _acmp($c,$x,$y) == 0; # shortcut (see _and) + + my $m = _one(); my ($xr,$yr); + my $mask = $OR_MASK; + + my $x1 = $x; + my $y1 = _copy($c,$y); # make copy + $x = _zero(); + my ($b,$xrr,$yrr); + use integer; + while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) + { + ($x1, $xr) = _div($c,$x1,$mask); + ($y1, $yr) = _div($c,$y1,$mask); + # make ints() from $xr, $yr (see _and()) +# $b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } +# $b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } +# _add($c,$x, _mul($c, _new( $c, ($xrr | $yrr) ), $m) ); + + # 0+ due to '|' doesn't work in strings + _add($c,$x, _mul($c, [ 0+$xr->[0] | 0+$yr->[0] ], $m) ); + _mul($c,$m,$mask); + } + # the loop stops when the shorter of the two numbers is exhausted + # the remainder of the longer one will survive bit-by-bit, so we simple + # multiply-add it in + _add($c,$x, _mul($c, $x1, $m) ) if !_is_zero($c,$x1); + _add($c,$x, _mul($c, $y1, $m) ) if !_is_zero($c,$y1); + + $x; + } + +sub _as_hex + { + # convert a decimal number to hex (ref to array, return ref to string) + my ($c,$x) = @_; + + # fits into one element (handle also 0x0 case) + return sprintf("0x%x",$x->[0]) if @$x == 1; + + my $x1 = _copy($c,$x); + + my $es = ''; + my ($xr, $h, $x10000); + if ($] >= 5.006) + { + $x10000 = [ 0x10000 ]; $h = 'h4'; + } + else + { + $x10000 = [ 0x1000 ]; $h = 'h3'; + } + while (@$x1 != 1 || $x1->[0] != 0) # _is_zero() + { + ($x1, $xr) = _div($c,$x1,$x10000); + $es .= unpack($h,pack('V',$xr->[0])); + } + $es = reverse $es; + $es =~ s/^[0]+//; # strip leading zeros + '0x' . $es; # return result prepended with 0x + } + +sub _as_bin + { + # convert a decimal number to bin (ref to array, return ref to string) + my ($c,$x) = @_; + + # fits into one element (and Perl recent enough), handle also 0b0 case + # handle zero case for older Perls + if ($] <= 5.005 && @$x == 1 && $x->[0] == 0) + { + my $t = '0b0'; return $t; + } + if (@$x == 1 && $] >= 5.006) + { + my $t = sprintf("0b%b",$x->[0]); + return $t; + } + my $x1 = _copy($c,$x); + + my $es = ''; + my ($xr, $b, $x10000); + if ($] >= 5.006) + { + $x10000 = [ 0x10000 ]; $b = 'b16'; + } + else + { + $x10000 = [ 0x1000 ]; $b = 'b12'; + } + while (!(@$x1 == 1 && $x1->[0] == 0)) # _is_zero() + { + ($x1, $xr) = _div($c,$x1,$x10000); + $es .= unpack($b,pack('v',$xr->[0])); + } + $es = reverse $es; + $es =~ s/^[0]+//; # strip leading zeros + '0b' . $es; # return result prepended with 0b + } + +sub _as_oct + { + # convert a decimal number to octal (ref to array, return ref to string) + my ($c,$x) = @_; + + # fits into one element (handle also 0 case) + return sprintf("0%o",$x->[0]) if @$x == 1; + + my $x1 = _copy($c,$x); + + my $es = ''; + my $xr; + my $x1000 = [ 0100000 ]; + while (@$x1 != 1 || $x1->[0] != 0) # _is_zero() + { + ($x1, $xr) = _div($c,$x1,$x1000); + $es .= reverse sprintf("%05o", $xr->[0]); + } + $es = reverse $es; + $es =~ s/^[0]+//; # strip leading zeros + '0' . $es; # return result prepended with 0 + } + +sub _from_oct + { + # convert a octal number to decimal (string, return ref to array) + my ($c,$os) = @_; + + # for older Perls, play safe + my $m = [ 0100000 ]; + my $d = 5; # 5 digits at a time + + my $mul = _one(); + my $x = _zero(); + + my $len = int( (length($os)-1)/$d ); # $d digit parts, w/o the '0' + my $val; my $i = -$d; + while ($len >= 0) + { + $val = substr($os,$i,$d); # get oct digits + $val = CORE::oct($val); + $i -= $d; $len --; + my $adder = [ $val ]; + _add ($c, $x, _mul ($c, $adder, $mul ) ) if $val != 0; + _mul ($c, $mul, $m ) if $len >= 0; # skip last mul + } + $x; + } + +sub _from_hex + { + # convert a hex number to decimal (string, return ref to array) + my ($c,$hs) = @_; + + my $m = _new($c, 0x10000000); # 28 bit at a time (<32 bit!) + my $d = 7; # 7 digits at a time + if ($] <= 5.006) + { + # for older Perls, play safe + $m = [ 0x10000 ]; # 16 bit at a time (<32 bit!) + $d = 4; # 4 digits at a time + } + + my $mul = _one(); + my $x = _zero(); + + my $len = int( (length($hs)-2)/$d ); # $d digit parts, w/o the '0x' + my $val; my $i = -$d; + while ($len >= 0) + { + $val = substr($hs,$i,$d); # get hex digits + $val =~ s/^0x// if $len == 0; # for last part only because + $val = CORE::hex($val); # hex does not like wrong chars + $i -= $d; $len --; + my $adder = [ $val ]; + # if the resulting number was to big to fit into one element, create a + # two-element version (bug found by Mark Lakata - Thanx!) + if (CORE::length($val) > $BASE_LEN) + { + $adder = _new($c,$val); + } + _add ($c, $x, _mul ($c, $adder, $mul ) ) if $val != 0; + _mul ($c, $mul, $m ) if $len >= 0; # skip last mul + } + $x; + } + +sub _from_bin + { + # convert a hex number to decimal (string, return ref to array) + my ($c,$bs) = @_; + + # instead of converting X (8) bit at a time, it is faster to "convert" the + # number to hex, and then call _from_hex. + + my $hs = $bs; + $hs =~ s/^[+-]?0b//; # remove sign and 0b + my $l = length($hs); # bits + $hs = '0' x (8-($l % 8)) . $hs if ($l % 8) != 0; # padd left side w/ 0 + my $h = '0x' . unpack('H*', pack ('B*', $hs)); # repack as hex + + $c->_from_hex($h); + } + +############################################################################## +# special modulus functions + +sub _modinv + { + # modular multiplicative inverse + my ($c,$x,$y) = @_; + + # modulo zero + if (_is_zero($c, $y)) { + return (undef, undef); + } + + # modulo one + if (_is_one($c, $y)) { + return (_zero($c), '+'); + } + + my $u = _zero($c); + my $v = _one($c); + my $a = _copy($c,$y); + my $b = _copy($c,$x); + + # Euclid's Algorithm for bgcd(), only that we calc bgcd() ($a) and the result + # ($u) at the same time. See comments in BigInt for why this works. + my $q; + my $sign = 1; + { + ($a, $q, $b) = ($b, _div($c, $a, $b)); # step 1 + last if _is_zero($c, $b); + + my $t = _add($c, # step 2: + _mul($c, _copy($c, $v), $q) , # t = v * q + $u ); # + u + $u = $v; # u = v + $v = $t; # v = t + $sign = -$sign; + redo; + } + + # if the gcd is not 1, then return NaN + return (undef, undef) unless _is_one($c, $a); + + ($v, $sign == 1 ? '+' : '-'); + } + +sub _modpow + { + # modulus of power ($x ** $y) % $z + my ($c,$num,$exp,$mod) = @_; + + # a^b (mod 1) = 0 for all a and b + if (_is_one($c,$mod)) + { + @$num = 0; + return $num; + } + + # 0^a (mod m) = 0 if m != 0, a != 0 + # 0^0 (mod m) = 1 if m != 0 + if (_is_zero($c, $num)) { + if (_is_zero($c, $exp)) { + @$num = 1; + } else { + @$num = 0; + } + return $num; + } + +# $num = _mod($c,$num,$mod); # this does not make it faster + + my $acc = _copy($c,$num); my $t = _one(); + + my $expbin = _as_bin($c,$exp); $expbin =~ s/^0b//; + my $len = length($expbin); + while (--$len >= 0) + { + if ( substr($expbin,$len,1) eq '1') # is_odd + { + _mul($c,$t,$acc); + $t = _mod($c,$t,$mod); + } + _mul($c,$acc,$acc); + $acc = _mod($c,$acc,$mod); + } + @$num = @$t; + $num; + } + +sub _gcd { + # Greatest common divisor. + + my ($c, $x, $y) = @_; + + # gcd(0,0) = 0 + # gcd(0,a) = a, if a != 0 + + if (@$x == 1 && $x->[0] == 0) { + if (@$y == 1 && $y->[0] == 0) { + @$x = 0; + } else { + @$x = @$y; + } + return $x; + } + + # Until $y is zero ... + + until (@$y == 1 && $y->[0] == 0) { + + # Compute remainder. + + _mod($c, $x, $y); + + # Swap $x and $y. + + my $tmp = [ @$x ]; + @$x = @$y; + $y = $tmp; # no deref here; that would modify input $y + } + + return $x; +} + +############################################################################## +############################################################################## + +1; +__END__ + +=pod + +=head1 NAME + +Math::BigInt::Calc - Pure Perl module to support Math::BigInt + +=head1 SYNOPSIS + +This library provides support for big integer calculations. It is not +intended to be used by other modules. Other modules which support the same +API (see below) can also be used to support Math::BigInt, like +Math::BigInt::GMP and Math::BigInt::Pari. + +=head1 DESCRIPTION + +In this library, the numbers are represented in base B = 10**N, where N is +the largest possible value that does not cause overflow in the intermediate +computations. The base B elements are stored in an array, with the least +significant element stored in array element zero. There are no leading zero +elements, except a single zero element when the number is zero. + +For instance, if B = 10000, the number 1234567890 is represented internally +as [3456, 7890, 12]. + +=head1 THE Math::BigInt API + +In order to allow for multiple big integer libraries, Math::BigInt was +rewritten to use a plug-in library for core math routines. Any module which +conforms to the API can be used by Math::BigInt by using this in your program: + + use Math::BigInt lib => 'libname'; + +'libname' is either the long name, like 'Math::BigInt::Pari', or only the short +version, like 'Pari'. + +=head2 General Notes + +A library only needs to deal with unsigned big integers. Testing of input +parameter validity is done by the caller, so there is no need to worry about +underflow (e.g., in C<_sub()> and C<_dec()>) nor about division by zero (e.g., +in C<_div()>) or similar cases. + +For some methods, the first parameter can be modified. That includes the +possibility that you return a reference to a completely different object +instead. Although keeping the reference and just changing its contents is +preferred over creating and returning a different reference. + +Return values are always objects, strings, Perl scalars, or true/false for +comparison routines. + +=head2 API version 1 + +The following methods must be defined in order to support the use by +Math::BigInt v1.70 or later. + +=head3 API version + +=over 4 + +=item I + +Return API version as a Perl scalar, 1 for Math::BigInt v1.70, 2 for +Math::BigInt v1.83. + +=back + +=head3 Constructors + +=over 4 + +=item I<_new(STR)> + +Convert a string representing an unsigned decimal number to an object +representing the same number. The input is normalize, i.e., it matches +C<^(0|[1-9]\d*)$>. + +=item I<_zero()> + +Return an object representing the number zero. + +=item I<_one()> + +Return an object representing the number one. + +=item I<_two()> + +Return an object representing the number two. + +=item I<_ten()> + +Return an object representing the number ten. + +=item I<_from_bin(STR)> + +Return an object given a string representing a binary number. The input has a +'0b' prefix and matches the regular expression C<^0[bB](0|1[01]*)$>. + +=item I<_from_oct(STR)> + +Return an object given a string representing an octal number. The input has a +'0' prefix and matches the regular expression C<^0[1-7]*$>. + +=item I<_from_hex(STR)> + +Return an object given a string representing a hexadecimal number. The input +has a '0x' prefix and matches the regular expression +C<^0x(0|[1-9a-fA-F][\da-fA-F]*)$>. + +=back + +=head3 Mathematical functions + +Each of these methods may modify the first input argument, except I<_bgcd()>, +which shall not modify any input argument, and I<_sub()> which may modify the +second input argument. + +=over 4 + +=item I<_add(OBJ1, OBJ2)> + +Returns the result of adding OBJ2 to OBJ1. + +=item I<_mul(OBJ1, OBJ2)> + +Returns the result of multiplying OBJ2 and OBJ1. + +=item I<_div(OBJ1, OBJ2)> + +Returns the result of dividing OBJ1 by OBJ2 and truncating the result to an +integer. + +=item I<_sub(OBJ1, OBJ2, FLAG)> + +=item I<_sub(OBJ1, OBJ2)> + +Returns the result of subtracting OBJ2 by OBJ1. If C is false or omitted, +OBJ1 might be modified. If C is true, OBJ2 might be modified. + +=item I<_dec(OBJ)> + +Decrement OBJ by one. + +=item I<_inc(OBJ)> + +Increment OBJ by one. + +=item I<_mod(OBJ1, OBJ2)> + +Return OBJ1 modulo OBJ2, i.e., the remainder after dividing OBJ1 by OBJ2. + +=item I<_sqrt(OBJ)> + +Return the square root of the object, truncated to integer. + +=item I<_root(OBJ, N)> + +Return Nth root of the object, truncated to int. N is E= 3. + +=item I<_fac(OBJ)> + +Return factorial of object (1*2*3*4*...). + +=item I<_pow(OBJ1, OBJ2)> + +Return OBJ1 to the power of OBJ2. By convention, 0**0 = 1. + +=item I<_modinv(OBJ1, OBJ2)> + +Return modular multiplicative inverse, i.e., return OBJ3 so that + + (OBJ3 * OBJ1) % OBJ2 = 1 % OBJ2 + +The result is returned as two arguments. If the modular multiplicative +inverse does not exist, both arguments are undefined. Otherwise, the +arguments are a number (object) and its sign ("+" or "-"). + +The output value, with its sign, must either be a positive value in the +range 1,2,...,OBJ2-1 or the same value subtracted OBJ2. For instance, if the +input arguments are objects representing the numbers 7 and 5, the method +must either return an object representing the number 3 and a "+" sign, since +(3*7) % 5 = 1 % 5, or an object representing the number 2 and "-" sign, +since (-2*7) % 5 = 1 % 5. + +=item I<_modpow(OBJ1, OBJ2, OBJ3)> + +Return modular exponentiation, (OBJ1 ** OBJ2) % OBJ3. + +=item I<_rsft(OBJ, N, B)> + +Shift object N digits right in base B and return the resulting object. This is +equivalent to performing integer division by B**N and discarding the remainder, +except that it might be much faster, depending on how the number is represented +internally. + +For instance, if the object $obj represents the hexadecimal number 0xabcde, +then C<_rsft($obj, 2, 16)> returns an object representing the number 0xabc. The +"remainer", 0xde, is discarded and not returned. + +=item I<_lsft(OBJ, N, B)> + +Shift the object N digits left in base B. This is equivalent to multiplying by +B**N, except that it might be much faster, depending on how the number is +represented internally. + +=item I<_log_int(OBJ, B)> + +Return integer log of OBJ to base BASE. This method has two output arguments, +the OBJECT and a STATUS. The STATUS is Perl scalar; it is 1 if OBJ is the exact +result, 0 if the result was truncted to give OBJ, and undef if it is unknown +whether OBJ is the exact result. + +=item I<_gcd(OBJ1, OBJ2)> + +Return the greatest common divisor of OBJ1 and OBJ2. + +=back + +=head3 Bitwise operators + +Each of these methods may modify the first input argument. + +=over 4 + +=item I<_and(OBJ1, OBJ2)> + +Return bitwise and. If necessary, the smallest number is padded with leading +zeros. + +=item I<_or(OBJ1, OBJ2)> + +Return bitwise or. If necessary, the smallest number is padded with leading +zeros. + +=item I<_xor(OBJ1, OBJ2)> + +Return bitwise exclusive or. If necessary, the smallest number is padded +with leading zeros. + +=back + +=head3 Boolean operators + +=over 4 + +=item I<_is_zero(OBJ)> + +Returns a true value if OBJ is zero, and false value otherwise. + +=item I<_is_one(OBJ)> + +Returns a true value if OBJ is one, and false value otherwise. + +=item I<_is_two(OBJ)> + +Returns a true value if OBJ is two, and false value otherwise. + +=item I<_is_ten(OBJ)> + +Returns a true value if OBJ is ten, and false value otherwise. + +=item I<_is_even(OBJ)> + +Return a true value if OBJ is an even integer, and a false value otherwise. + +=item I<_is_odd(OBJ)> + +Return a true value if OBJ is an even integer, and a false value otherwise. + +=item I<_acmp(OBJ1, OBJ2)> + +Compare OBJ1 and OBJ2 and return -1, 0, or 1, if OBJ1 is less than, equal +to, or larger than OBJ2, respectively. + +=back + +=head3 String conversion + +=over 4 + +=item I<_str(OBJ)> + +Return a string representing the object. The returned string should have no +leading zeros, i.e., it should match C<^(0|[1-9]\d*)$>. + +=item I<_as_bin(OBJ)> + +Return the binary string representation of the number. The string must have a +'0b' prefix. + +=item I<_as_oct(OBJ)> + +Return the octal string representation of the number. The string must have +a '0x' prefix. + +Note: This method was required from Math::BigInt version 1.78, but the required +API version number was not incremented, so there are older libraries that +support API version 1, but do not support C<_as_oct()>. + +=item I<_as_hex(OBJ)> + +Return the hexadecimal string representation of the number. The string must +have a '0x' prefix. + +=back + +=head3 Numeric conversion + +=over 4 + +=item I<_num(OBJ)> + +Given an object, return a Perl scalar number (int/float) representing this +number. + +=back + +=head3 Miscellaneous + +=over 4 + +=item I<_copy(OBJ)> + +Return a true copy of the object. + +=item I<_len(OBJ)> + +Returns the number of the decimal digits in the number. The output is a +Perl scalar. + +=item I<_zeros(OBJ)> + +Return the number of trailing decimal zeros. The output is a Perl scalar. + +=item I<_digit(OBJ, N)> + +Return the Nth digit as a Perl scalar. N is a Perl scalar, where zero refers to +the rightmost (least significant) digit, and negative values count from the +left (most significant digit). If $obj represents the number 123, then +I<_digit($obj, 0)> is 3 and I<_digit(123, -1)> is 1. + +=item I<_check(OBJ)> + +Return a true value if the object is OK, and a false value otherwise. This is a +check routine to test the internal state of the object for corruption. + +=back + +=head2 API version 2 + +The following methods are required for an API version of 2 or greater. + +=head3 Constructors + +=over 4 + +=item I<_1ex(N)> + +Return an object representing the number 10**N where N E= 0 is a Perl +scalar. + +=back + +=head3 Mathematical functions + +=over 4 + +=item I<_nok(OBJ1, OBJ2)> + +Return the binomial coefficient OBJ1 over OBJ1. + +=back + +=head3 Miscellaneous + +=over 4 + +=item I<_alen(OBJ)> + +Return the approximate number of decimal digits of the object. The +output is one Perl scalar. This estimate must be greater than or equal +to what C<_len()> returns. + +=back + +=head2 API optional methods + +The following methods are optional, and can be defined if the underlying lib +has a fast way to do them. If undefined, Math::BigInt will use pure Perl (hence +slow) fallback routines to emulate these: + +=head3 Signed bitwise operators. + +Each of these methods may modify the first input argument. + +=over 4 + +=item I<_signed_or(OBJ1, OBJ2, SIGN1, SIGN2)> + +Return the signed bitwise or. + +=item I<_signed_and(OBJ1, OBJ2, SIGN1, SIGN2)> + +Return the signed bitwise and. + +=item I<_signed_xor(OBJ1, OBJ2, SIGN1, SIGN2)> + +Return the signed bitwise exclusive or. + +=back + +=head1 WRAP YOUR OWN + +If you want to port your own favourite c-lib for big numbers to the +Math::BigInt interface, you can take any of the already existing modules as +a rough guideline. You should really wrap up the latest BigInt and BigFloat +testsuites with your module, and replace in them any of the following: + + use Math::BigInt; + +by this: + + use Math::BigInt lib => 'yourlib'; + +This way you ensure that your library really works 100% within Math::BigInt. + +=head1 BUGS + +Please report any bugs or feature requests to +C, or through the web interface at +L +(requires login). +We will be notified, and then you'll automatically be notified of progress on +your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc Math::BigInt::Calc + +You can also look for information at: + +=over 4 + +=item * RT: CPAN's request tracker + +L + +=item * AnnoCPAN: Annotated CPAN documentation + +L + +=item * CPAN Ratings + +L + +=item * Search CPAN + +L + +=item * CPAN Testers Matrix + +L + +=item * The Bignum mailing list + +=over 4 + +=item * Post to mailing list + +C + +=item * View mailing list + +L + +=item * Subscribe/Unsubscribe + +L + +=back + +=back + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHORS + +=over 4 + +=item * + +Original math code by Mark Biggar, rewritten by Tels L +in late 2000. + +=item * + +Separated from BigInt and shaped API with the help of John Peacock. + +=item * + +Fixed, speed-up, streamlined and enhanced by Tels 2001 - 2007. + +=item * + +API documentation corrected and extended by Peter John Acklam, +Epjacklam@online.noE + +=back + +=head1 SEE ALSO + +L, L, +L, L and L. + +=cut diff --git a/cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm b/cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm new file mode 100644 index 0000000000..0ff9dcca17 --- /dev/null +++ b/cpan/Math-BigInt/lib/Math/BigInt/CalcEmu.pm @@ -0,0 +1,395 @@ +package Math::BigInt::CalcEmu; + +use 5.006002; +use strict; +# use warnings; # do not use warnings for older Perls +use vars qw/$VERSION/; + +$VERSION = '1.999701'; + +package Math::BigInt; + +# See SYNOPSIS below. + +my $CALC_EMU; + +BEGIN + { + $CALC_EMU = Math::BigInt->config()->{'lib'}; + # register us with MBI to get notified of future lib changes + Math::BigInt::_register_callback( __PACKAGE__, sub { $CALC_EMU = $_[0]; } ); + } + +sub __emu_band + { + my ($self,$x,$y,$sx,$sy,@r) = @_; + + return $x->bzero(@r) if $y->is_zero() || $x->is_zero(); + + my $sign = 0; # sign of result + $sign = 1 if $sx == -1 && $sy == -1; + + my ($bx,$by); + + if ($sx == -1) # if x is negative + { + # two's complement: inc and flip all "bits" in $bx + $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $bx =~ s/-?0x//; + $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $bx = $x->as_hex(); # get binary representation + $bx =~ s/-?0x//; + $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + if ($sy == -1) # if y is negative + { + # two's complement: inc and flip all "bits" in $by + $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $by =~ s/-?0x//; + $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $by = $y->as_hex(); # get binary representation + $by =~ s/-?0x//; + $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + # now we have bit-strings from X and Y, reverse them for padding + $bx = reverse $bx; + $by = reverse $by; + + # padd the shorter string + my $xx = "\x00"; $xx = "\x0f" if $sx == -1; + my $yy = "\x00"; $yy = "\x0f" if $sy == -1; + my $diff = CORE::length($bx) - CORE::length($by); + if ($diff > 0) + { + # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by + $by .= $yy x $diff; + } + elsif ($diff < 0) + { + # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx + $bx .= $xx x abs($diff); + } + + # and the strings together + my $r = $bx & $by; + + # and reverse the result again + $bx = reverse $r; + + # One of $x or $y was negative, so need to flip bits in the result. + # In both cases (one or two of them negative, or both positive) we need + # to get the characters back. + if ($sign == 1) + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; + } + else + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; + } + + # leading zeros will be stripped by _from_hex() + $bx = '0x' . $bx; + $x->{value} = $CALC_EMU->_from_hex( $bx ); + + # calculate sign of result + $x->{sign} = '+'; + $x->{sign} = '-' if $sign == 1 && !$x->is_zero(); + + $x->bdec() if $sign == 1; + + $x->round(@r); + } + +sub __emu_bior + { + my ($self,$x,$y,$sx,$sy,@r) = @_; + + return $x->round(@r) if $y->is_zero(); + + my $sign = 0; # sign of result + $sign = 1 if ($sx == -1) || ($sy == -1); + + my ($bx,$by); + + if ($sx == -1) # if x is negative + { + # two's complement: inc and flip all "bits" in $bx + $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $bx =~ s/-?0x//; + $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $bx = $x->as_hex(); # get binary representation + $bx =~ s/-?0x//; + $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + if ($sy == -1) # if y is negative + { + # two's complement: inc and flip all "bits" in $by + $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $by =~ s/-?0x//; + $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $by = $y->as_hex(); # get binary representation + $by =~ s/-?0x//; + $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + # now we have bit-strings from X and Y, reverse them for padding + $bx = reverse $bx; + $by = reverse $by; + + # padd the shorter string + my $xx = "\x00"; $xx = "\x0f" if $sx == -1; + my $yy = "\x00"; $yy = "\x0f" if $sy == -1; + my $diff = CORE::length($bx) - CORE::length($by); + if ($diff > 0) + { + $by .= $yy x $diff; + } + elsif ($diff < 0) + { + $bx .= $xx x abs($diff); + } + + # or the strings together + my $r = $bx | $by; + + # and reverse the result again + $bx = reverse $r; + + # one of $x or $y was negative, so need to flip bits in the result + # in both cases (one or two of them negative, or both positive) we need + # to get the characters back. + if ($sign == 1) + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; + } + else + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; + } + + # leading zeros will be stripped by _from_hex() + $bx = '0x' . $bx; + $x->{value} = $CALC_EMU->_from_hex( $bx ); + + # calculate sign of result + $x->{sign} = '+'; + $x->{sign} = '-' if $sign == 1 && !$x->is_zero(); + + # if one of X or Y was negative, we need to decrement result + $x->bdec() if $sign == 1; + + $x->round(@r); + } + +sub __emu_bxor + { + my ($self,$x,$y,$sx,$sy,@r) = @_; + + return $x->round(@r) if $y->is_zero(); + + my $sign = 0; # sign of result + $sign = 1 if $x->{sign} ne $y->{sign}; + + my ($bx,$by); + + if ($sx == -1) # if x is negative + { + # two's complement: inc and flip all "bits" in $bx + $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $bx =~ s/-?0x//; + $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $bx = $x->as_hex(); # get binary representation + $bx =~ s/-?0x//; + $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + if ($sy == -1) # if y is negative + { + # two's complement: inc and flip all "bits" in $by + $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc + $by =~ s/-?0x//; + $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + else + { + $by = $y->as_hex(); # get binary representation + $by =~ s/-?0x//; + $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; + } + # now we have bit-strings from X and Y, reverse them for padding + $bx = reverse $bx; + $by = reverse $by; + + # padd the shorter string + my $xx = "\x00"; $xx = "\x0f" if $sx == -1; + my $yy = "\x00"; $yy = "\x0f" if $sy == -1; + my $diff = CORE::length($bx) - CORE::length($by); + if ($diff > 0) + { + $by .= $yy x $diff; + } + elsif ($diff < 0) + { + $bx .= $xx x abs($diff); + } + + # xor the strings together + my $r = $bx ^ $by; + + # and reverse the result again + $bx = reverse $r; + + # one of $x or $y was negative, so need to flip bits in the result + # in both cases (one or two of them negative, or both positive) we need + # to get the characters back. + if ($sign == 1) + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; + } + else + { + $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; + } + + # leading zeros will be stripped by _from_hex() + $bx = '0x' . $bx; + $x->{value} = $CALC_EMU->_from_hex( $bx ); + + # calculate sign of result + $x->{sign} = '+'; + $x->{sign} = '-' if $sx != $sy && !$x->is_zero(); + + $x->bdec() if $sign == 1; + + $x->round(@r); + } + +############################################################################## +############################################################################## + +1; + +__END__ + +=pod + +=head1 NAME + +Math::BigInt::CalcEmu - Emulate low-level math with BigInt code + +=head1 SYNOPSIS + + use Math::BigInt::CalcEmu; + +=head1 DESCRIPTION + +Contains routines that emulate low-level math functions in BigInt, e.g. +optional routines the low-level math package does not provide on its own. + +Will be loaded on demand and called automatically by BigInt. + +Stuff here is really low-priority to optimize, since it is far better to +implement the operation in the low-level math library directly, possible even +using a call to the native lib. + +=head1 METHODS + +=over + +=item __emu_bxor + +=item __emu_band + +=item __emu_bior + +=back + +=head1 BUGS + +Please report any bugs or feature requests to +C, or through the web interface at +L +(requires login). +We will be notified, and then you'll automatically be notified of progress on +your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc Math::BigInt::CalcEmu + +You can also look for information at: + +=over 4 + +=item * RT: CPAN's request tracker + +L + +=item * AnnoCPAN: Annotated CPAN documentation + +L + +=item * CPAN Ratings + +L + +=item * Search CPAN + +L + +=item * CPAN Testers Matrix + +L + +=item * The Bignum mailing list + +=over 4 + +=item * Post to mailing list + +C + +=item * View mailing list + +L + +=item * Subscribe/Unsubscribe + +L + +=back + +=back + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHORS + +(c) Tels http://bloodgate.com 2003, 2004 - based on BigInt code by +Tels from 2001-2003. + +=head1 SEE ALSO + +L, L, +L and L. + +=cut diff --git a/cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm b/cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm new file mode 100644 index 0000000000..94d3f2a624 --- /dev/null +++ b/cpan/Math-BigInt/t/Math/BigFloat/Subclass.pm @@ -0,0 +1,49 @@ +#!/usr/bin/perl -w + +# for testing subclassing Math::BigFloat + +package Math::BigFloat::Subclass; + +require 5.005_02; +use strict; + +use Exporter; +use Math::BigFloat(1.38); +use vars qw($VERSION @ISA $PACKAGE + $accuracy $precision $round_mode $div_scale); + +@ISA = qw(Exporter Math::BigFloat); + +$VERSION = 0.05; + +use overload; # inherit overload from BigInt + +# Globals +$accuracy = $precision = undef; +$round_mode = 'even'; +$div_scale = 40; + +sub new +{ + my $proto = shift; + my $class = ref($proto) || $proto; + + my $value = shift; + my $a = $accuracy; $a = $_[0] if defined $_[0]; + my $p = $precision; $p = $_[1] if defined $_[1]; + # Store the floating point value + my $self = Math::BigFloat->new($value,$a,$p,$round_mode); + bless $self, $class; + $self->{'_custom'} = 1; # make sure this never goes away + return $self; +} + +BEGIN + { + *objectify = \&Math::BigInt::objectify; + # to allow Math::BigFloat::Subclass::bgcd( ... ) style calls + *bgcd = \&Math::BigFloat::bgcd; + *blcm = \&Math::BigFloat::blcm; + } + +1; diff --git a/cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm b/cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm new file mode 100644 index 0000000000..0bbe861cf8 --- /dev/null +++ b/cpan/Math-BigInt/t/Math/BigInt/BareCalc.pm @@ -0,0 +1,44 @@ +package Math::BigInt::BareCalc; + +use 5.005; +use strict; +# use warnings; # dont use warnings for older Perls + +require Exporter; +use vars qw/@ISA $VERSION/; +@ISA = qw(Exporter); + +$VERSION = '0.05'; + +sub api_version () { 1; } + +# Package to to test Bigint's simulation of Calc + +# uses Calc, but only features the strictly necc. methods. + +use Math::BigInt::Calc '0.51'; + +BEGIN + { + no strict 'refs'; + foreach (qw/ + base_len new zero one two ten copy str num add sub mul div mod inc dec + acmp alen len digit zeros + rsft lsft + fac pow gcd log_int sqrt root + is_zero is_one is_odd is_even is_one is_two is_ten check + as_hex as_bin as_oct from_hex from_bin from_oct + modpow modinv + and xor or + /) + { + my $name = "Math::BigInt::Calc::_$_"; + *{"Math::BigInt::BareCalc::_$_"} = \&$name; + } + print "# BareCalc using Calc v$Math::BigInt::Calc::VERSION\n"; + } + +# catch and throw away +sub import { } + +1; diff --git a/cpan/Math-BigInt/t/Math/BigInt/Scalar.pm b/cpan/Math-BigInt/t/Math/BigInt/Scalar.pm new file mode 100644 index 0000000000..c20a3e377e --- /dev/null +++ b/cpan/Math-BigInt/t/Math/BigInt/Scalar.pm @@ -0,0 +1,355 @@ +############################################################################### +# core math lib for BigInt, representing big numbers by normal int/float's +# for testing only, will fail any bignum test if range is exceeded + +package Math::BigInt::Scalar; + +use 5.005; +use strict; +# use warnings; # dont use warnings for older Perls + +require Exporter; + +use vars qw/@ISA $VERSION/; +@ISA = qw(Exporter); + +$VERSION = '0.13'; + +sub api_version() { 1; } + +############################################################################## +# global constants, flags and accessory + +# constants for easier life +my $nan = 'NaN'; + +############################################################################## +# create objects from various representations + +sub _new + { + # create scalar ref from string + my $d = $_[1]; + my $x = $d; # make copy + \$x; + } + +sub _from_hex + { + # not used + } + +sub _from_oct + { + # not used + } + +sub _from_bin + { + # not used + } + +sub _zero + { + my $x = 0; \$x; + } + +sub _one + { + my $x = 1; \$x; + } + +sub _two + { + my $x = 2; \$x; + } + +sub _ten + { + my $x = 10; \$x; + } + +sub _copy + { + my $x = $_[1]; + my $z = $$x; + \$z; + } + +# catch and throw away +sub import { } + +############################################################################## +# convert back to string and number + +sub _str + { + # make string + "${$_[1]}"; + } + +sub _num + { + # make a number + 0+${$_[1]}; + } + +sub _zeros + { + my $x = $_[1]; + + $x =~ /\d(0*)$/; + length($1 || ''); + } + +sub _rsft + { + # not used + } + +sub _lsft + { + # not used + } + +sub _mod + { + # not used + } + +sub _gcd + { + # not used + } + +sub _sqrt + { + # not used + } + +sub _root + { + # not used + } + +sub _fac + { + # not used + } + +sub _modinv + { + # not used + } + +sub _modpow + { + # not used + } + +sub _log_int + { + # not used + } + +sub _as_hex + { + sprintf("0x%x",${$_[1]}); + } + +sub _as_bin + { + sprintf("0b%b",${$_[1]}); + } + +sub _as_oct + { + sprintf("0%o",${$_[1]}); + } + +############################################################################## +# actual math code + +sub _add + { + my ($c,$x,$y) = @_; + $$x += $$y; + return $x; + } + +sub _sub + { + my ($c,$x,$y) = @_; + $$x -= $$y; + return $x; + } + +sub _mul + { + my ($c,$x,$y) = @_; + $$x *= $$y; + return $x; + } + +sub _div + { + my ($c,$x,$y) = @_; + + my $u = int($$x / $$y); my $r = $$x % $$y; $$x = $u; + return ($x,\$r) if wantarray; + return $x; + } + +sub _pow + { + my ($c,$x,$y) = @_; + my $u = $$x ** $$y; $$x = $u; + return $x; + } + +sub _and + { + my ($c,$x,$y) = @_; + my $u = int($$x) & int($$y); $$x = $u; + return $x; + } + +sub _xor + { + my ($c,$x,$y) = @_; + my $u = int($$x) ^ int($$y); $$x = $u; + return $x; + } + +sub _or + { + my ($c,$x,$y) = @_; + my $u = int($$x) | int($$y); $$x = $u; + return $x; + } + +sub _inc + { + my ($c,$x) = @_; + my $u = int($$x)+1; $$x = $u; + return $x; + } + +sub _dec + { + my ($c,$x) = @_; + my $u = int($$x)-1; $$x = $u; + return $x; + } + +############################################################################## +# testing + +sub _acmp + { + my ($c,$x, $y) = @_; + return ($$x <=> $$y); + } + +sub _len + { + return length("${$_[1]}"); + } + +sub _digit + { + # return the nth digit, negative values count backward + # 0 is the rightmost digit + my ($c,$x,$n) = @_; + + $n ++; # 0 => 1, 1 => 2 + return substr($$x,-$n,1); # 1 => -1, -2 => 2 etc + } + +############################################################################## +# _is_* routines + +sub _is_zero + { + # return true if arg is zero + my ($c,$x) = @_; + ($$x == 0) <=> 0; + } + +sub _is_even + { + # return true if arg is even + my ($c,$x) = @_; + (!($$x & 1)) <=> 0; + } + +sub _is_odd + { + # return true if arg is odd + my ($c,$x) = @_; + ($$x & 1) <=> 0; + } + +sub _is_one + { + # return true if arg is one + my ($c,$x) = @_; + ($$x == 1) <=> 0; + } + +sub _is_two + { + # return true if arg is one + my ($c,$x) = @_; + ($$x == 2) <=> 0; + } + +sub _is_ten + { + # return true if arg is one + my ($c,$x) = @_; + ($$x == 10) <=> 0; + } + +############################################################################### +# check routine to test internal state of corruptions + +sub _check + { + # no checks yet, pull it out from the test suite + my ($c,$x) = @_; + return "$x is not a reference" if !ref($x); + return 0; + } + +1; +__END__ + +=head1 NAME + +Math::BigInt::Scalar - Pure Perl module to test Math::BigInt with scalars + +=head1 SYNOPSIS + +Provides support for big integer calculations via means of 'small' int/floats. +Only for testing purposes, since it will fail at large values. But it is simple +enough not to introduce bugs on it's own and to serve as a testbed. + +=head1 DESCRIPTION + +Please see Math::BigInt::Calc. + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 AUTHOR + +Tels http://bloodgate.com in 2001 - 2007. + +=head1 SEE ALSO + +L, L. + +=cut diff --git a/cpan/Math-BigInt/t/Math/BigInt/Subclass.pm b/cpan/Math-BigInt/t/Math/BigInt/Subclass.pm new file mode 100644 index 0000000000..d45e9e53ad --- /dev/null +++ b/cpan/Math-BigInt/t/Math/BigInt/Subclass.pm @@ -0,0 +1,90 @@ +#!/usr/bin/perl -w + +package Math::BigInt::Subclass; + +require 5.005_02; +use strict; + +use Exporter; +use Math::BigInt (1.64); +# $lib is for the "lib => " test +use vars qw($VERSION @ISA $PACKAGE @EXPORT_OK + $lib + $accuracy $precision $round_mode $div_scale); + +@ISA = qw(Exporter Math::BigInt); +@EXPORT_OK = qw(bgcd objectify); + +$VERSION = 0.04; + +use overload; # inherit overload from BigInt + +# Globals +$accuracy = $precision = undef; +$round_mode = 'even'; +$div_scale = 40; +$lib = ''; + +sub new +{ + my $proto = shift; + my $class = ref($proto) || $proto; + + my $value = shift; + my $a = $accuracy; $a = $_[0] if defined $_[0]; + my $p = $precision; $p = $_[1] if defined $_[1]; + my $self = Math::BigInt->new($value,$a,$p,$round_mode); + bless $self,$class; + $self->{'_custom'} = 1; # make sure this never goes away + return $self; +} + +sub bgcd + { + Math::BigInt::bgcd(@_); + } + +sub blcm + { + Math::BigInt::blcm(@_); + } + +sub as_int + { + Math::BigInt->new($_[0]); + } + +BEGIN + { + *objectify = \&Math::BigInt::objectify; + + # these are called by AUTOLOAD from BigFloat, so we need at least these. + # We cheat, of course.. + *bneg = \&Math::BigInt::bneg; + *babs = \&Math::BigInt::babs; + *bnan = \&Math::BigInt::bnan; + *binf = \&Math::BigInt::binf; + *bzero = \&Math::BigInt::bzero; + *bone = \&Math::BigInt::bone; + } + +sub import + { + my $self = shift; + + my @a; my $t = 0; + foreach (@_) + { + # remove the "lib => foo" parameters and store it + $lib = $_, $t = 0, next if $t == 1; + if ($_ eq 'lib') + { + $t = 1; next; + } + push @a,$_; + } + $self->SUPER::import(@a); # need it for subclasses + $self->export_to_level(1,$self,@a); # need this ? + } + +1; diff --git a/cpan/Math-BigInt/t/_e_math.t b/cpan/Math-BigInt/t/_e_math.t new file mode 100644 index 0000000000..bae9e2df09 --- /dev/null +++ b/cpan/Math-BigInt/t/_e_math.t @@ -0,0 +1,85 @@ +#!/usr/bin/perl -w + +# test the helper math routines in Math::BigFloat + +use strict; +use Test::More tests => 26; + +use Math::BigFloat lib => 'Calc'; + +############################################################################# +# add + +my $a = Math::BigInt::Calc->_new("123"); +my $b = Math::BigInt::Calc->_new("321"); + +my ($x, $xs) = Math::BigFloat::_e_add($a,$b,'+','+'); +is (_str($x,$xs), '+444', 'add two positive numbers'); +is (_str($a,''), '444', 'a modified'); + +($x,$xs) = _add (123,321,'+','+'); +is (_str($x,$xs), '+444', 'add two positive numbers'); + +($x,$xs) = _add (123,321,'+','-'); +is (_str($x,$xs), '-198', 'add +x + -y'); +($x,$xs) = _add (123,321,'-','+'); +is (_str($x,$xs), '+198', 'add -x + +y'); + +($x,$xs) = _add (321,123,'-','+'); +is (_str($x,$xs), '-198', 'add -x + +y'); +($x,$xs) = _add (321,123,'+','-'); +is (_str($x,$xs), '+198', 'add +x + -y'); + +($x,$xs) = _add (10,1,'+','-'); +is (_str($x,$xs), '+9', 'add 10 + -1'); +($x,$xs) = _add (10,1,'-','+'); +is (_str($x,$xs), '-9', 'add -10 + +1'); +($x,$xs) = _add (1,10,'-','+'); +is (_str($x,$xs), '+9', 'add -1 + 10'); +($x,$xs) = _add (1,10,'+','-'); +is (_str($x,$xs), '-9', 'add 1 + -10'); + +############################################################################# +# sub + +$a = Math::BigInt::Calc->_new("123"); +$b = Math::BigInt::Calc->_new("321"); +($x, $xs) = Math::BigFloat::_e_sub($b,$a,'+','+'); +is (_str($x,$xs), '+198', 'sub two positive numbers'); +is (_str($b,''), '198', 'a modified'); + +($x,$xs) = _sub (123,321,'+','-'); +is (_str($x,$xs), '+444', 'sub +x + -y'); +($x,$xs) = _sub (123,321,'-','+'); +is (_str($x,$xs), '-444', 'sub -x + +y'); + +sub _add + { + my ($a,$b,$as,$bs) = @_; + + my $aa = Math::BigInt::Calc->_new($a); + my $bb = Math::BigInt::Calc->_new($b); + my ($x, $xs) = Math::BigFloat::_e_add($aa,$bb,$as,$bs); + is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), + 'param0 modified'); + ($x,$xs); + } + +sub _sub + { + my ($a,$b,$as,$bs) = @_; + + my $aa = Math::BigInt::Calc->_new($a); + my $bb = Math::BigInt::Calc->_new($b); + my ($x, $xs) = Math::BigFloat::_e_sub($aa,$bb,$as,$bs); + is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), + 'param0 modified'); + ($x,$xs); + } + +sub _str + { + my ($x,$s) = @_; + + $s . Math::BigInt::Calc->_str($x); + } diff --git a/cpan/Math-BigInt/t/alias.inc b/cpan/Math-BigInt/t/alias.inc new file mode 100644 index 0000000000..746a20c99e --- /dev/null +++ b/cpan/Math-BigInt/t/alias.inc @@ -0,0 +1,12 @@ + +# alias subroutine testing, included by sub_ali.t and mbi_ali.t + +my $x = $CL->new(123); + +is ($x->is_pos(), 1, '123 is positive'); +is ($x->is_neg(), 0, '123 is not negative'); +is ($x->as_int(), 123, '123 is 123 as int'); +is (ref($x->as_int()), 'Math::BigInt', "as_int(123) is of class Math::BigInt"); +$x->bneg(); +is ($x->is_pos(), 0, '-123 is not positive'); +is ($x->is_neg(), 1, '-123 is negative'); diff --git a/cpan/Math-BigInt/t/bare_mbf.t b/cpan/Math-BigInt/t/bare_mbf.t new file mode 100644 index 0000000000..69dcc80f92 --- /dev/null +++ b/cpan/Math-BigInt/t/bare_mbf.t @@ -0,0 +1,14 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 2340; + +BEGIN { unshift @INC, 't'; } + +use Math::BigFloat lib => 'BareCalc'; + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigFloat"; +$CL = "Math::BigInt::BareCalc"; + +require 't/bigfltpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/bare_mbi.t b/cpan/Math-BigInt/t/bare_mbi.t new file mode 100644 index 0000000000..8aedf4350d --- /dev/null +++ b/cpan/Math-BigInt/t/bare_mbi.t @@ -0,0 +1,18 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 3649; + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt lib => 'BareCalc'; + +print "# ",Math::BigInt->config()->{lib},"\n"; + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigInt"; +$CL = "Math::BigInt::BareCalc"; + +my $version = '1.84'; # for $VERSION tests, match current release (by hand!) + +require 't/bigintpm.inc'; # perform same tests as bigintpm diff --git a/cpan/Math-BigInt/t/bare_mif.t b/cpan/Math-BigInt/t/bare_mif.t new file mode 100644 index 0000000000..2e533241ea --- /dev/null +++ b/cpan/Math-BigInt/t/bare_mif.t @@ -0,0 +1,24 @@ +#!/usr/bin/perl -w + +# test rounding, accuracy, precision and fallback, round_mode and mixing +# of classes under BareCalc + +use strict; +use Test::More tests => 684 + + 1; # our own tests + +BEGIN { unshift @INC, 't'; } + +print "# ",Math::BigInt->config()->{lib},"\n"; + +use Math::BigInt lib => 'BareCalc'; +use Math::BigFloat lib => 'BareCalc'; + +use vars qw/$mbi $mbf/; + +$mbi = 'Math::BigInt'; +$mbf = 'Math::BigFloat'; + +is (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); + +require 't/mbimbf.inc'; diff --git a/cpan/Math-BigInt/t/big_pi_e.t b/cpan/Math-BigInt/t/big_pi_e.t new file mode 100644 index 0000000000..9cc4751aa4 --- /dev/null +++ b/cpan/Math-BigInt/t/big_pi_e.t @@ -0,0 +1,33 @@ +#!/usr/bin/perl -w + +# Test bpi() and bexp() + +use strict; +use Test::More tests => 8; + +use Math::BigFloat; + +############################################################################# + +my $pi = Math::BigFloat::bpi(); + +ok (!exists $pi->{_a}, 'A not set'); +ok (!exists $pi->{_p}, 'P not set'); + +$pi = Math::BigFloat->bpi(); + +ok (!exists $pi->{_a}, 'A not set'); +ok (!exists $pi->{_p}, 'P not set'); + +$pi = Math::BigFloat->bpi(10); + +is ($pi->{_a}, 10, 'A set'); +is ($pi->{_p}, undef, 'P not set'); + +############################################################################# +my $e = Math::BigFloat->new(1)->bexp(); + +ok (!exists $e->{_a}, 'A not set'); +ok (!exists $e->{_p}, 'P not set'); + + diff --git a/cpan/Math-BigInt/t/bigfltpm.inc b/cpan/Math-BigInt/t/bigfltpm.inc new file mode 100644 index 0000000000..3eb2e21e0d --- /dev/null +++ b/cpan/Math-BigInt/t/bigfltpm.inc @@ -0,0 +1,1836 @@ +#include this file into another test for subclass testing... + +is ($class->config()->{lib},$CL); + +use strict; + +my $z; + +while () + { + $_ =~ s/[\n\r]//g; # remove newlines + $_ =~ s/#.*$//; # remove comments + $_ =~ s/\s+$//; # trailing spaces + next if /^$/; # skip empty lines & comments + if (s/^&//) + { + $f = $_; + } + elsif (/^\$/) + { + $setup = $_; $setup =~ s/\$/\$${class}::/g; # round_mode, div_scale + #print "\$setup== $setup\n"; + } + else + { + if (m|^(.*?):(/.+)$|) + { + $ans = $2; + @args = split(/:/,$1,99); + } + else + { + @args = split(/:/,$_,99); $ans = pop(@args); + } + $try = "\$x = $class->new(\"$args[0]\");"; + if ($f eq "fnorm") + { + $try .= "\$x;"; + } elsif ($f eq "finf") { + $try .= "\$x->finf('$args[1]');"; + } elsif ($f eq "is_inf") { + $try .= "\$x->is_inf('$args[1]');"; + } elsif ($f eq "fone") { + $try .= "\$x->bone('$args[1]');"; + } elsif ($f eq "fstr") { + $try .= "\$x->accuracy($args[1]); \$x->precision($args[2]);"; + $try .= '$x->fstr();'; + } elsif ($f eq "parts") { + # ->bstr() to see if an object is returned + $try .= '($a,$b) = $x->parts(); $a = $a->bstr(); $b = $b->bstr();'; + $try .= '"$a $b";'; + } elsif ($f eq "exponent") { + # ->bstr() to see if an object is returned + $try .= '$x->exponent()->bstr();'; + } elsif ($f eq "mantissa") { + # ->bstr() to see if an object is returned + $try .= '$x->mantissa()->bstr();'; + } elsif ($f =~ /^(numify|length|as_number|as_hex|as_bin)$/) { + $try .= "\$x->$f();"; + # some unary ops (test the fxxx form, since that is done by AUTOLOAD) + } elsif ($f =~ /^f(nan|sstr|neg|floor|ceil|int|abs)$/) { + $try .= "\$x->f$1();"; + # some is_xxx test function + } elsif ($f =~ /^is_(zero|one|negative|positive|odd|even|nan|int)$/) { + $try .= "\$x->$f();"; + } elsif ($f eq "bpi") { + $try .= '$class->bpi($x);'; + } elsif ($f eq "finc") { + $try .= '++$x;'; + } elsif ($f eq "fdec") { + $try .= '--$x;'; + }elsif ($f eq "fround") { + $try .= "$setup; \$x->fround($args[1]);"; + } elsif ($f eq "ffround") { + $try .= "$setup; \$x->ffround($args[1]);"; + } elsif ($f eq "fsqrt") { + $try .= "$setup; \$x->fsqrt();"; + } elsif ($f eq "ffac") { + $try .= "$setup; \$x->ffac();"; + } elsif ($f eq "flog") { + if (defined $args[1] && $args[1] ne '') + { + $try .= "\$y = $class->new($args[1]);"; + $try .= "$setup; \$x->flog(\$y);"; + } + else + { + $try .= "$setup; \$x->flog();"; + } + } + else + { + $try .= "\$y = $class->new(\"$args[1]\");"; + + if ($f eq "bgcd") + { + if (defined $args[2]) + { + $try .= " \$z = $class->new(\"$args[2]\"); "; + } + $try .= "$class\::bgcd(\$x, \$y"; + $try .= ", \$z" if (defined $args[2]); + $try .= " );"; + } + elsif ($f eq "blcm") + { + if (defined $args[2]) + { + $try .= " \$z = $class->new(\"$args[2]\"); "; + } + $try .= "$class\::blcm(\$x, \$y"; + $try .= ", \$z" if (defined $args[2]); + $try .= " );"; + } elsif ($f eq "fcmp") { + $try .= '$x->fcmp($y);'; + } elsif ($f eq "facmp") { + $try .= '$x->facmp($y);'; + } elsif ($f eq "fpow") { + $try .= '$x ** $y;'; + } elsif ($f eq "bnok") { + $try .= '$x->bnok($y);'; + } elsif ($f eq "bcos") { + $try .= '$x->bcos($y);'; + } elsif ($f eq "bsin") { + $try .= '$x->bsin($y);'; + } elsif ($f eq "batan") { + $try .= '$x->batan($y);'; + } elsif ($f eq "froot") { + $try .= "$setup; \$x->froot(\$y);"; + } elsif ($f eq "fadd") { + $try .= '$x + $y;'; + } elsif ($f eq "fsub") { + $try .= '$x - $y;'; + } elsif ($f eq "fmul") { + $try .= '$x * $y;'; + } elsif ($f eq "fdiv") { + $try .= "$setup; \$x / \$y;"; + } elsif ($f eq "fdiv-list") { + $try .= "$setup; join(',',\$x->fdiv(\$y));"; + } elsif ($f eq "frsft") { + $try .= '$x >> $y;'; + } elsif ($f eq "flsft") { + $try .= '$x << $y;'; + } elsif ($f eq "fmod") { + $try .= '$x % $y;'; + } else { + # Functions with three arguments + $try .= "\$z = $class->new(\"$args[2]\");"; + + if( $f eq "bmodpow") { + $try .= '$x->bmodpow($y,$z);'; + } elsif ($f eq "bmuladd"){ + $try .= '$x->bmuladd($y,$z);'; + } elsif ($f eq "batan2"){ + $try .= '$x->batan2($y,$z);'; + } else { warn "Unknown op '$f'"; } + } + } + # print "# Trying: '$try'\n"; + $ans1 = eval $try; + print "# Error: $@\n" if $@; + if ($ans =~ m|^/(.*)$|) + { + my $pat = $1; + if ($ans1 =~ /$pat/) + { + is (1,1); + } + else + { + print "# '$try' expected: /$pat/ got: '$ans1'\n" if !is (1,0); + } + } + else + { + if ($ans eq "") + { + is ($ans1, undef); + } + else + { + print "# Tried: '$try'\n" if !is ($ans1, $ans); + if (ref($ans1) eq "$class") + { + # float numbers are normalized (for now), so mantissa shouldn't have + # trailing zeros + #print $ans1->_trailing_zeros(),"\n"; + print "# Has trailing zeros after '$try'\n" + if !is ($CL->_zeros( $ans1->{_m}), 0); + } + } + } # end pattern or string + } + } # end while + +# check whether $class->new( Math::BigInt->new()) destroys it +# ($y == 12 in this case) +$x = Math::BigInt->new(1200); $y = $class->new($x); +is ($y,1200); is ($x,1200); + +############################################################################### +# Really huge, big, ultra-mega-biggy-monster exponents +# Technically, the exponents should not be limited (they are BigInts), but +# practically there are a few places were they are limited to a Perl scalar. +# This is sometimes for speed, sometimes because otherwise the number wouldn't +# fit into your memory (just think of 1e123456789012345678901234567890 + 1!) +# anyway. We don't test everything here, but let's make sure it just basically +# works. + +my $monster = '1e1234567890123456789012345678901234567890'; + +# new and exponent +is ($class->new($monster)->bsstr(), + '1e+1234567890123456789012345678901234567890'); +is ($class->new($monster)->exponent(), + '1234567890123456789012345678901234567890'); +# cmp +is ($class->new($monster) > 0,1); + +# sub/mul +is ($class->new($monster)->bsub( $monster),0); +is ($class->new($monster)->bmul(2)->bsstr(), + '2e+1234567890123456789012345678901234567890'); + +# mantissa +$monster = '1234567890123456789012345678901234567890e2'; +is ($class->new($monster)->mantissa(), + '123456789012345678901234567890123456789'); + +############################################################################### +# zero,inf,one,nan + +$x = $class->new(2); $x->fzero(); is ($x->{_a}, undef); is ($x->{_p}, undef); +$x = $class->new(2); $x->finf(); is ($x->{_a}, undef); is ($x->{_p}, undef); +$x = $class->new(2); $x->fone(); is ($x->{_a}, undef); is ($x->{_p}, undef); +$x = $class->new(2); $x->fnan(); is ($x->{_a}, undef); is ($x->{_p}, undef); + +############################################################################### +# bone/binf etc as plain calls (Lite failed them) + +is ($class->fzero(),0); +is ($class->fone(),1); +is ($class->fone('+'),1); +is ($class->fone('-'),-1); +is ($class->fnan(),'NaN'); +is ($class->finf(),'inf'); +is ($class->finf('+'),'inf'); +is ($class->finf('-'),'-inf'); +is ($class->finf('-inf'),'-inf'); + +$class->accuracy(undef); $class->precision(undef); # reset + +############################################################################### +# bug in bsstr()/numify() showed up in after-rounding in bdiv() + +$x = $class->new('0.008'); $y = $class->new(2); +$x->bdiv(3,$y); +is ($x,'0.0027'); + +############################################################################### +# Verify that numify() returns a normalized value, and underflows and +# overflows when given "extreme" values. + +like($class->new("12345e67")->numify(), qr/^1\.2345e\+?0*71$/); +like($class->new("1e-9999")->numify(), qr/^\+?0$/); # underflow +unlike($class->new("1e9999")->numify(), qr/^1(\.0*)?e\+?9+$/); # overflow + +############################################################################### +# fsqrt() with set global A/P or A/P enabled on $x, also a test whether fsqrt() +# correctly modifies $x + + +$x = $class->new(12); $class->precision(-2); $x->fsqrt(); is ($x,'3.46'); + +$class->precision(undef); +$x = $class->new(12); $class->precision(0); $x->fsqrt(); is ($x,'3'); + +$class->precision(-3); $x = $class->new(12); $x->fsqrt(); is ($x,'3.464'); + +{ + no strict 'refs'; + # A and P set => NaN + ${${class}.'::accuracy'} = 4; $x = $class->new(12); + $x->fsqrt(3); is ($x,'NaN'); + # supplied arg overrides set global + $class->precision(undef); $x = $class->new(12); $x->fsqrt(3); is ($x,'3.46'); + $class->accuracy(undef); $class->precision(undef); # reset for further tests +} + +############################################################################# +# can we call objectify (broken until v1.52) + +{ + no strict; + $try = + '@args' . " = $class" . "::objectify(2,$class,4,5);".'join(" ",@args);'; + $ans = eval $try; + is ($ans,"$class 4 5"); +} + +############################################################################# +# is_one('-') (broken until v1.64) + +is ($class->new(-1)->is_one(),0); +is ($class->new(-1)->is_one('-'),1); + +############################################################################# +# bug 1/0.5 leaving 2e-0 instead of 2e0 + +is ($class->new(1)->fdiv('0.5')->bsstr(),'2e+0'); + +############################################################################### +# [perl #30609] bug with $x -= $x not being 0, but 2*$x + +$x = $class->new(3); $x -= $x; is ($x, 0); +$x = $class->new(-3); $x -= $x; is ($x, 0); +$x = $class->new(3); $x += $x; is ($x, 6); +$x = $class->new(-3); $x += $x; is ($x, -6); + +$x = $class->new('NaN'); $x -= $x; is ($x->is_nan(), 1); +$x = $class->new('inf'); $x -= $x; is ($x->is_nan(), 1); +$x = $class->new('-inf'); $x -= $x; is ($x->is_nan(), 1); + +$x = $class->new('NaN'); $x += $x; is ($x->is_nan(), 1); +$x = $class->new('inf'); $x += $x; is ($x->is_inf(), 1); +$x = $class->new('-inf'); $x += $x; is ($x->is_inf('-'), 1); + +$x = $class->new('3.14'); $x -= $x; is ($x, 0); +$x = $class->new('-3.14'); $x -= $x; is ($x, 0); +$x = $class->new('3.14'); $x += $x; is ($x, '6.28'); +$x = $class->new('-3.14'); $x += $x; is ($x, '-6.28'); + +$x = $class->new('3.14'); $x *= $x; is ($x, '9.8596'); +$x = $class->new('-3.14'); $x *= $x; is ($x, '9.8596'); +$x = $class->new('3.14'); $x /= $x; is ($x, '1'); +$x = $class->new('-3.14'); $x /= $x; is ($x, '1'); +$x = $class->new('3.14'); $x %= $x; is ($x, '0'); +$x = $class->new('-3.14'); $x %= $x; is ($x, '0'); + +############################################################################### +# the following two were reported by "kenny" via hotmail.com: + +#perl -MMath::BigFloat -wle 'print Math::BigFloat->new(0)->bpow(".1")' +#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. + +$x = $class->new(0); $y = $class->new('0.1'); +is ($x ** $y, 0, 'no warnings and zero result'); + +#perl -MMath::BigFloat -lwe 'print Math::BigFloat->new(".222222222222222222222222222222222222222222")->bceil()' +#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. + +$x = $class->new(".222222222222222222222222222222222222222222"); +is ($x->bceil(), 1, 'no warnings and one as result'); + +############################################################################### +# test **=, <<=, >>= + +# ((2^148)-1)/17 +$x = $class->new(2); $x **= 148; $x++; $x->bdiv(17, 60)->bfloor(); $x->accuracy(undef); +is ($x,"20988936657440586486151264256610222593863921"); +is ($x->length(),length "20988936657440586486151264256610222593863921"); + +$x = $class->new('2'); +my $y = $class->new('18'); +is ($x <<= $y, 2 << 18); +is ($x, 2 << 18); +is ($x >>= $y, 2); +is ($x, 2); + +$x = $class->new('2'); +$y = $class->new('18.2'); +$x <<= $y; # 2 * (2 ** 18.2); + +is ($x->copy()->bfround(-9), '602248.763144685'); +is ($x >>= $y, 2); # 2 * (2 ** 18.2) / (2 ** 18.2) => 2 +is ($x, 2); + +1; # all done + +__DATA__ +&bgcd +inf:12:NaN +-inf:12:NaN +12:inf:NaN +12:-inf:NaN +inf:inf:NaN +inf:-inf:NaN +-inf:-inf:NaN +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++0:+0:0 ++0:+1:1 ++1:+0:1 ++1:+1:1 ++2:+3:1 ++3:+2:1 +-3:+2:1 +-3:-2:1 +-144:-60:12 +144:-60:12 +144:60:12 +100:625:25 +4096:81:1 +1034:804:2 +27:90:56:1 +27:90:54:9 +&blcm +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++0:+0:NaN ++1:+0:0 ++0:+1:0 ++27:+90:270 ++1034:+804:415668 +$div_scale = 40; +&bcos +1.2:10:0.3623577545 +2.4:12:-0.737393715541 +0:10:1 +0:20:1 +1:10:0.5403023059 +1:12:0.540302305868 +&bsin +1:10:0.8414709848 +0:10:0 +0:20:0 +2.1:12:0.863209366649 +1.2:13:0.9320390859672 +0.2:13:0.1986693307951 +3.2:12:-0.0583741434276 +&batan +NaN:10:NaN +inf:14:1.5707963267949 +-inf:14:-1.5707963267949 +0.2:13:0.1973955598499 +0.2:14:0.19739555984988 +0:10:0 +1:14:0.78539816339744 +-1:14:-0.78539816339744 +# test an argument X > 1 +2:14:1.1071487177941 +&batan2 +NaN:1:10:NaN +NaN:NaN:10:NaN +1:NaN:10:NaN +inf:1:14:1.5707963267949 +-inf:1:14:-1.5707963267949 +0:-inf:14:3.1415926535898 +-1:-inf:14:-3.1415926535898 +1:-inf:14:3.1415926535898 +0:inf:14:0 +inf:-inf:14:2.3561944901923 +-inf:-inf:14:-2.3561944901923 +inf:+inf:14:0.7853981633974 +-inf:+inf:14:-0.7853981633974 +1:5:13:0.1973955598499 +1:5:14:0.19739555984988 +0:0:10:0 +0:1:14:0 +0:2:14:0 +1:0:14:1.5707963267949 +5:0:14:1.5707963267949 +-1:0:11:-1.5707963268 +-2:0:77:-1.5707963267948966192313216916397514420985846996875529104874722961539082031431 +2:0:77:1.5707963267948966192313216916397514420985846996875529104874722961539082031431 +-1:5:14:-0.19739555984988 +1:5:14:0.19739555984988 +-1:8:14:-0.12435499454676 +1:8:14:0.12435499454676 +-1:1:14:-0.78539816339744 +# test an argument X > 1 and one X < 1 +1:2:24:0.463647609000806116214256 +2:1:14:1.1071487177941 +-2:1:14:-1.1071487177941 +&bpi +150:3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940813 +77:3.1415926535897932384626433832795028841971693993751058209749445923078164062862 ++0:3.141592653589793238462643383279502884197 +11:3.1415926536 +&bnok ++inf:10:inf +NaN:NaN:NaN +NaN:1:NaN +1:NaN:NaN +1:1:1 +# k > n +1:2:0 +2:3:0 +# k < 0 +1:-2:0 +# 7 over 3 = 35 +7:3:35 +7:6:7 +100:90:17310309456440 +100:95:75287520 +2:0:1 +7:0:1 +2:1:2 +&flog +0::NaN +-1::NaN +-2::NaN +# base > 0, base != 1 +2:-1:NaN +2:0:NaN +2:1:NaN +# log(1) is always 1, regardless of $base +1::0 +1:1:0 +1:2:0 +2::0.6931471805599453094172321214581765680755 +2.718281828::0.9999999998311266953289851340574956564911 +$div_scale = 20; +2.718281828::0.99999999983112669533 +$div_scale = 15; +123::4.81218435537242 +10::2.30258509299405 +1000::6.90775527898214 +100::4.60517018598809 +2::0.693147180559945 +3.1415::1.14470039286086 +12345::9.42100640177928 +0.001::-6.90775527898214 +# bug until v1.71: +10:10:1 +100:100:1 +# reset for further tests +$div_scale = 40; +1::0 +&frsft +NaNfrsft:2:NaN +0:2:0 +1:1:0.5 +2:1:1 +4:1:2 +123:1:61.5 +32:3:4 +&flsft +NaNflsft:0:NaN +2:1:4 +4:3:32 +5:3:40 +1:2:4 +0:5:0 +&fnorm +1:1 +-0:0 +fnormNaN:NaN ++inf:inf +-inf:-inf +123:123 +-123.4567:-123.4567 +# invalid inputs +1__2:NaN +1E1__2:NaN +11__2E2:NaN +.2E-3.:NaN +1e3e4:NaN +# strange, but valid +.2E2:20 +1.E3:1000 +# some inputs that result in zero +0e0:0 ++0e0:0 ++0e+0:0 +-0e+0:0 +0e-0:0 +-0e-0:0 ++0e-0:0 +000:0 +00e2:0 +00e02:0 +000e002:0 +000e1230:0 +00e-3:0 +00e+3:0 +00e-03:0 +00e+03:0 +-000:0 +-00e2:0 +-00e02:0 +-000e002:0 +-000e1230:0 +-00e-3:0 +-00e+3:0 +-00e-03:0 +-00e+03:0 +&as_number +0:0 +1:1 +1.2:1 +2.345:2 +-2:-2 +-123.456:-123 +-200:-200 +-inf:-inf +inf:inf +NaN:NaN +71243225429896467497217836789578596379:71243225429896467497217836789578596379 +# test for bug in brsft() not handling cases that return 0 +0.000641:0 +0.0006412:0 +0.00064123:0 +0.000641234:0 +0.0006412345:0 +0.00064123456:0 +0.000641234567:0 +0.0006412345678:0 +0.00064123456789:0 +0.1:0 +0.01:0 +0.001:0 +0.0001:0 +0.00001:0 +0.000001:0 +0.0000001:0 +0.00000001:0 +0.000000001:0 +0.0000000001:0 +0.00000000001:0 +0.12345:0 +0.123456:0 +0.1234567:0 +0.12345678:0 +0.123456789:0 +&finf +1:+:inf +2:-:-inf +3:abc:inf +&as_hex ++inf:inf +-inf:-inf +hexNaN:NaN +0:0x0 +5:0x5 +-5:-0x5 +&as_bin ++inf:inf +-inf:-inf +hexNaN:NaN +0:0b0 +5:0b101 +-5:-0b101 +&numify +# uses bsstr() so 5 => 5e+0 to be compatible w/ Perls output +0:0 ++1:1 +1234:1234 +-5:-5 +100:100 +-100:-100 +&fnan +abc:NaN +2:NaN +-2:NaN +0:NaN +&fone +2:+:1 +-2:-:-1 +-2:+:1 +2:-:-1 +0::1 +-2::1 +abc::1 +2:abc:1 +&fsstr ++inf:inf +-inf:-inf +abcfsstr:NaN +-abcfsstr:NaN +1234.567:1234567e-3 +123:123e+0 +-5:-5e+0 +-100:-1e+2 +&fstr ++inf:::inf +-inf:::-inf +abcfstr:::NaN +1234.567:9::1234.56700 +1234.567::-6:1234.567000 +12345:5::12345 +0.001234:6::0.00123400 +0.001234::-8:0.00123400 +0:4::0 +0::-4:0.0000 +&fnorm +inf:inf ++inf:inf +-inf:-inf ++infinity:NaN ++-inf:NaN +abc:NaN + 1 a:NaN +1bcd2:NaN +11111b:NaN ++1z:NaN +-1z:NaN +0e999:0 +0e-999:0 +-0e999:0 +-0e-999:0 +0:0 ++0:0 ++00:0 ++0_0_0:0 +000000_0000000_00000:0 +-0:0 +-0000:0 ++1:1 ++01:1 ++001:1 ++00000100000:100000 +123456789:123456789 +-1:-1 +-01:-1 +-001:-1 +-123456789:-123456789 +-00000100000:-100000 +123.456a:NaN +123.456:123.456 +0.01:0.01 +.002:0.002 ++.2:0.2 +-0.0003:-0.0003 +-.0000000004:-0.0000000004 +123456E2:12345600 +123456E-2:1234.56 +-123456E2:-12345600 +-123456E-2:-1234.56 +1e1:10 +2e-11:0.00000000002 +# exercise _split + .02e-1:0.002 + 000001:1 + -00001:-1 + -1:-1 + 000.01:0.01 + -000.0023:-0.0023 + 1.1e1:11 +-3e111:-3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-4e-1111:-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004 +&fpow +NaN:1:NaN +1:NaN:NaN +NaN:-1:NaN +-1:NaN:NaN +NaN:-21:NaN +-21:NaN:NaN +NaN:21:NaN +21:NaN:NaN +0:0:1 +0:1:0 +0:9:0 +0:-2:inf +2:2:4 +1:2:1 +1:3:1 +-1:2:1 +-1:3:-1 +123.456:2:15241.383936 +2:-2:0.25 +2:-3:0.125 +128:-2:0.00006103515625 +abc:123.456:NaN +123.456:abc:NaN ++inf:123.45:inf +-inf:123.45:-inf ++inf:-123.45:inf +-inf:-123.45:-inf +-2:2:4 +-2:3:-8 +-2:4:16 +-2:5:-32 +-3:2:9 +-3:3:-27 +-3:4:81 +-3:5:-243 +# 2 ** 0.5 == sqrt(2) +# 1.41..7 and not 1.4170 since fallback (bsqrt(9) is '3', not 3.0...0) +2:0.5:1.41421356237309504880168872420969807857 +#2:0.2:1.148698354997035006798626946777927589444 +#6:1.5:14.6969384566990685891837044482353483518 +$div_scale = 20; +#62.5:12.5:26447206647554886213592.3959144 +$div_scale = 40; +&fneg +fnegNaN:NaN ++inf:-inf +-inf:inf ++0:0 ++1:-1 +-1:1 ++123456789:-123456789 +-123456789:123456789 ++123.456789:-123.456789 +-123456.789:123456.789 +&fabs +fabsNaN:NaN ++inf:inf +-inf:inf ++0:0 ++1:1 +-1:1 ++123456789:123456789 +-123456789:123456789 ++123.456789:123.456789 +-123456.789:123456.789 +&fround +$round_mode = "trunc" ++inf:5:inf +-inf:5:-inf +0:5:0 +NaNfround:5:NaN ++10123456789:5:10123000000 +-10123456789:5:-10123000000 ++10123456789.123:5:10123000000 +-10123456789.123:5:-10123000000 ++10123456789:9:10123456700 +-10123456789:9:-10123456700 ++101234500:6:101234000 +-101234500:6:-101234000 +$round_mode = "zero" ++20123456789:5:20123000000 +-20123456789:5:-20123000000 ++20123456789.123:5:20123000000 +-20123456789.123:5:-20123000000 ++20123456789:9:20123456800 +-20123456789:9:-20123456800 ++201234500:6:201234000 +-201234500:6:-201234000 +$round_mode = "+inf" ++30123456789:5:30123000000 +-30123456789:5:-30123000000 ++30123456789.123:5:30123000000 +-30123456789.123:5:-30123000000 ++30123456789:9:30123456800 +-30123456789:9:-30123456800 ++301234500:6:301235000 +-301234500:6:-301234000 +$round_mode = "-inf" ++40123456789:5:40123000000 +-40123456789:5:-40123000000 ++40123456789.123:5:40123000000 +-40123456789.123:5:-40123000000 ++40123456789:9:40123456800 +-40123456789:9:-40123456800 ++401234500:6:401234000 +-401234500:6:-401235000 +$round_mode = "odd" ++50123456789:5:50123000000 +-50123456789:5:-50123000000 ++50123456789.123:5:50123000000 +-50123456789.123:5:-50123000000 ++50123456789:9:50123456800 +-50123456789:9:-50123456800 ++501234500:6:501235000 +-501234500:6:-501235000 +$round_mode = "even" ++60123456789:5:60123000000 +-60123456789:5:-60123000000 ++60123456789:9:60123456800 +-60123456789:9:-60123456800 ++601234500:6:601234000 +-601234500:6:-601234000 ++60123456789.0123:5:60123000000 +-60123456789.0123:5:-60123000000 +$round_mode = "common" ++60123456789:5:60123000000 +-60123456789:5:-60123000000 ++60123456789:6:60123500000 +-60123456789:6:-60123500000 ++60123456789:9:60123456800 +-60123456789:9:-60123456800 ++601234500:6:601235000 +-601234500:6:-601235000 ++601234400:6:601234000 +-601234400:6:-601234000 ++601234600:6:601235000 +-601234600:6:-601235000 ++601234300:6:601234000 ++60123456789.0123:5:60123000000 +-60123456789.0123:5:-60123000000 +&ffround +$round_mode = "trunc" ++inf:5:inf +-inf:5:-inf +0:5:0 +NaNffround:5:NaN ++1.23:-1:1.2 ++1.234:-1:1.2 ++1.2345:-1:1.2 ++1.23:-2:1.23 ++1.234:-2:1.23 ++1.2345:-2:1.23 ++1.23:-3:1.230 ++1.234:-3:1.234 ++1.2345:-3:1.234 +-1.23:-1:-1.2 ++1.27:-1:1.2 +-1.27:-1:-1.2 ++1.25:-1:1.2 +-1.25:-1:-1.2 ++1.35:-1:1.3 +-1.35:-1:-1.3 +-0.0061234567890:-1:0.0 +-0.0061:-1:0.0 +-0.00612:-1:0.0 +-0.00612:-2:0.00 +-0.006:-1:0.0 +-0.006:-2:0.00 +-0.0006:-2:0.00 +-0.0006:-3:0.000 +-0.0065:-3:/-0\.006|-6e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:0 +0.51:0:0 +0.41:0:0 +$round_mode = "zero" ++2.23:-1:/2.2(?:0{5}\d+)? +-2.23:-1:/-2.2(?:0{5}\d+)? ++2.27:-1:/2.(?:3|29{5}\d+) +-2.27:-1:/-2.(?:3|29{5}\d+) ++2.25:-1:/2.2(?:0{5}\d+)? +-2.25:-1:/-2.2(?:0{5}\d+)? ++2.35:-1:/2.(?:3|29{5}\d+) +-2.35:-1:/-2.(?:3|29{5}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.006|-6e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:0 +0.51:0:1 +0.41:0:0 +$round_mode = "+inf" ++3.23:-1:/3.2(?:0{5}\d+)? +-3.23:-1:/-3.2(?:0{5}\d+)? ++3.27:-1:/3.(?:3|29{5}\d+) +-3.27:-1:/-3.(?:3|29{5}\d+) ++3.25:-1:/3.(?:3|29{5}\d+) +-3.25:-1:/-3.2(?:0{5}\d+)? ++3.35:-1:/3.(?:4|39{5}\d+) +-3.35:-1:/-3.(?:3|29{5}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.006|-6e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:1 +0.51:0:1 +0.41:0:0 +$round_mode = "-inf" ++4.23:-1:/4.2(?:0{5}\d+)? +-4.23:-1:/-4.2(?:0{5}\d+)? ++4.27:-1:/4.(?:3|29{5}\d+) +-4.27:-1:/-4.(?:3|29{5}\d+) ++4.25:-1:/4.2(?:0{5}\d+)? +-4.25:-1:/-4.(?:3|29{5}\d+) ++4.35:-1:/4.(?:3|29{5}\d+) +-4.35:-1:/-4.(?:4|39{5}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.007|-7e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:0 +0.51:0:1 +0.41:0:0 +$round_mode = "odd" ++5.23:-1:/5.2(?:0{5}\d+)? +-5.23:-1:/-5.2(?:0{5}\d+)? ++5.27:-1:/5.(?:3|29{5}\d+) +-5.27:-1:/-5.(?:3|29{5}\d+) ++5.25:-1:/5.(?:3|29{5}\d+) +-5.25:-1:/-5.(?:3|29{5}\d+) ++5.35:-1:/5.(?:3|29{5}\d+) +-5.35:-1:/-5.(?:3|29{5}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.007|-7e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:1 +0.51:0:1 +0.41:0:0 +$round_mode = "even" ++6.23:-1:/6.2(?:0{5}\d+)? +-6.23:-1:/-6.2(?:0{5}\d+)? ++6.27:-1:/6.(?:3|29{5}\d+) +-6.27:-1:/-6.(?:3|29{5}\d+) ++6.25:-1:/6.(?:2(?:0{5}\d+)?|29{5}\d+) +-6.25:-1:/-6.(?:2(?:0{5}\d+)?|29{5}\d+) ++6.35:-1:/6.(?:4|39{5}\d+|29{8}\d+) +-6.35:-1:/-6.(?:4|39{5}\d+|29{8}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.006|-7e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:0 +0.51:0:1 +0.41:0:0 +0.01234567:-3:0.012 +0.01234567:-4:0.0123 +0.01234567:-5:0.01235 +0.01234567:-6:0.012346 +0.01234567:-7:0.0123457 +0.01234567:-8:0.01234567 +0.01234567:-9:0.012345670 +0.01234567:-12:0.012345670000 +&fcmp +fcmpNaN:fcmpNaN: +fcmpNaN:+0: ++0:fcmpNaN: ++0:+0:0 +-1:+0:-1 ++0:-1:1 ++1:+0:1 ++0:+1:-1 +-1:+1:-1 ++1:-1:1 +-1:-1:0 ++1:+1:0 +-1.1:0:-1 ++0:-1.1:1 ++1.1:+0:1 ++0:+1.1:-1 ++123:+123:0 ++123:+12:1 ++12:+123:-1 +-123:-123:0 +-123:-12:-1 +-12:-123:1 ++123:+124:-1 ++124:+123:1 +-123:-124:1 +-124:-123:-1 +0:0.01:-1 +0:0.0001:-1 +0:-0.0001:1 +0:-0.1:1 +0.1:0:1 +0.00001:0:1 +-0.0001:0:-1 +-0.1:0:-1 +0:0.0001234:-1 +0:-0.0001234:1 +0.0001234:0:1 +-0.0001234:0:-1 +0.0001:0.0005:-1 +0.0005:0.0001:1 +0.005:0.0001:1 +0.001:0.0005:1 +0.000001:0.0005:-1 +0.00000123:0.0005:-1 +0.00512:0.0001:1 +0.005:0.000112:1 +0.00123:0.0005:1 +1.5:2:-1 +2:1.5:1 +1.54321:234:-1 +234:1.54321:1 +1e1234567890987654321:1e1234567890987654320:1 +1e-1234567890987654321:1e-1234567890987654320:-1 +# infinity +-inf:5432112345:-1 ++inf:5432112345:1 +-inf:-5432112345:-1 ++inf:-5432112345:1 +-inf:54321.12345:-1 ++inf:54321.12345:1 +-inf:-54321.12345:-1 ++inf:-54321.12345:1 ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:1 +-inf:+inf:-1 +# return undef ++inf:NaN: +NaN:inf: +-inf:NaN: +NaN:-inf: +&facmp +fcmpNaN:fcmpNaN: +fcmpNaN:+0: ++0:fcmpNaN: ++0:+0:0 +-1:+0:1 ++0:-1:-1 ++1:+0:1 ++0:+1:-1 +-1:+1:0 ++1:-1:0 +-1:-1:0 ++1:+1:0 +-1.1:0:1 ++0:-1.1:-1 ++1.1:+0:1 ++0:+1.1:-1 ++123:+123:0 ++123:+12:1 ++12:+123:-1 +-123:-123:0 +-123:-12:1 +-12:-123:-1 ++123:+124:-1 ++124:+123:1 +-123:-124:-1 +-124:-123:1 +0:0.01:-1 +0:0.0001:-1 +0:-0.0001:-1 +0:-0.1:-1 +0.1:0:1 +0.00001:0:1 +-0.0001:0:1 +-0.1:0:1 +0:0.0001234:-1 +0:-0.0001234:-1 +0.0001234:0:1 +-0.0001234:0:1 +0.0001:0.0005:-1 +0.0005:0.0001:1 +0.005:0.0001:1 +0.001:0.0005:1 +0.000001:0.0005:-1 +0.00000123:0.0005:-1 +0.00512:0.0001:1 +0.005:0.000112:1 +0.00123:0.0005:1 +1.5:2:-1 +2:1.5:1 +1.54321:234:-1 +234:1.54321:1 +# infinity +-inf:5432112345:1 ++inf:5432112345:1 +-inf:-5432112345:1 ++inf:-5432112345:1 +-inf:54321.12345:1 ++inf:54321.12345:1 +-inf:-54321.12345:1 ++inf:-54321.12345:1 ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:0 +-inf:+inf:0 +5:inf:-1 +-1:inf:-1 +5:-inf:-1 +-1:-inf:-1 +# return undef ++inf:facmpNaN: +facmpNaN:inf: +-inf:facmpNaN: +facmpNaN:-inf: +&fdec +fdecNaN:NaN ++inf:inf +-inf:-inf ++0:-1 ++1:0 +-1:-2 +1.23:0.23 +-1.23:-2.23 +100:99 +101:100 +-100:-101 +-99:-100 +-98:-99 +99:98 +&finc +fincNaN:NaN ++inf:inf +-inf:-inf ++0:1 ++1:2 +-1:0 +1.23:2.23 +-1.23:-0.23 +100:101 +-100:-99 +-99:-98 +-101:-100 +99:100 +&fadd +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:-inf:NaN +-inf:+inf:NaN ++inf:+inf:inf +-inf:-inf:-inf +baddNaN:+inf:NaN +baddNaN:+inf:NaN ++inf:baddNaN:NaN +-inf:baddNaN:NaN ++0:+0:0 ++1:+0:1 ++0:+1:1 ++1:+1:2 +-1:+0:-1 ++0:-1:-1 +-1:-1:-2 +-1:+1:0 ++1:-1:0 ++9:+1:10 ++99:+1:100 ++999:+1:1000 ++9999:+1:10000 ++99999:+1:100000 ++999999:+1:1000000 ++9999999:+1:10000000 ++99999999:+1:100000000 ++999999999:+1:1000000000 ++9999999999:+1:10000000000 ++99999999999:+1:100000000000 ++10:-1:9 ++100:-1:99 ++1000:-1:999 ++10000:-1:9999 ++100000:-1:99999 ++1000000:-1:999999 ++10000000:-1:9999999 ++100000000:-1:99999999 ++1000000000:-1:999999999 ++10000000000:-1:9999999999 ++123456789:+987654321:1111111110 +-123456789:+987654321:864197532 +-123456789:-987654321:-1111111110 ++123456789:-987654321:-864197532 +0.001234:0.0001234:0.0013574 +&fsub +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:-inf:inf +-inf:+inf:-inf ++inf:+inf:NaN +-inf:-inf:NaN +baddNaN:+inf:NaN +baddNaN:+inf:NaN ++inf:baddNaN:NaN +-inf:baddNaN:NaN ++0:+0:0 ++1:+0:1 ++0:+1:-1 ++1:+1:0 +-1:+0:-1 ++0:-1:1 +-1:-1:0 +-1:+1:-2 ++1:-1:2 ++9:+1:8 ++99:+1:98 ++999:+1:998 ++9999:+1:9998 ++99999:+1:99998 ++999999:+1:999998 ++9999999:+1:9999998 ++99999999:+1:99999998 ++999999999:+1:999999998 ++9999999999:+1:9999999998 ++99999999999:+1:99999999998 ++10:-1:11 ++100:-1:101 ++1000:-1:1001 ++10000:-1:10001 ++100000:-1:100001 ++1000000:-1:1000001 ++10000000:-1:10000001 ++100000000:-1:100000001 ++1000000000:-1:1000000001 ++10000000000:-1:10000000001 ++123456789:+987654321:-864197532 +-123456789:+987654321:-1111111110 +-123456789:-987654321:864197532 ++123456789:-987654321:1111111110 +&bmuladd +abc:abc:0:NaN +abc:+0:0:NaN ++0:abc:0:NaN ++0:0:abc:NaN +NaNmul:+inf:0:NaN +NaNmul:-inf:0:NaN +-inf:NaNmul:0:NaN ++inf:NaNmul:0:NaN ++inf:+inf:0:inf ++inf:-inf:0:-inf +-inf:+inf:0:-inf +-inf:-inf:0:inf ++0:+0:0:0 ++0:+1:0:0 ++1:+0:0:0 ++0:-1:0:0 +-1:+0:0:0 +123456789123456789:0:0:0 +0:123456789123456789:0:0 +-1:-1:0:1 +-1:-1:0:1 +-1:+1:0:-1 ++1:-1:0:-1 ++1:+1:0:1 ++2:+3:0:6 +-2:+3:0:-6 ++2:-3:0:-6 +-2:-3:0:6 +111:111:0:12321 +10101:10101:0:102030201 +1001001:1001001:0:1002003002001 +100010001:100010001:0:10002000300020001 +10000100001:10000100001:0:100002000030000200001 +11111111111:9:0:99999999999 +22222222222:9:0:199999999998 +33333333333:9:0:299999999997 +44444444444:9:0:399999999996 +55555555555:9:0:499999999995 +66666666666:9:0:599999999994 +77777777777:9:0:699999999993 +88888888888:9:0:799999999992 +99999999999:9:0:899999999991 +11111111111:9:1:100000000000 +22222222222:9:1:199999999999 +33333333333:9:1:299999999998 +44444444444:9:1:399999999997 +55555555555:9:1:499999999996 +66666666666:9:1:599999999995 +77777777777:9:1:699999999994 +88888888888:9:1:799999999993 +99999999999:9:1:899999999992 +-3:-4:-5:7 +3:-4:-5:-17 +-3:4:-5:-17 +3:4:-5:7 +-3:4:5:-7 +3:-4:5:-7 +9999999999999999999:10000000000000000000:1234567890:99999999999999999990000000001234567890 +3.2:5.7:8.9:27.14 +-3.2:5.197:6.05:-10.5804 +&bmodpow +3:4:8:1 +3:4:7:4 +3:4:7:4 +77777:777:123456789:99995084 +3.2:6.2:5.2:2.970579856718063040273642739529400818 +&fmul +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:NaNmul:NaN ++inf:NaNmul:NaN +NaNmul:+inf:NaN +NaNmul:-inf:NaN ++inf:+inf:inf ++inf:-inf:-inf ++inf:-inf:-inf ++inf:+inf:inf ++inf:123.34:inf ++inf:-123.34:-inf +-inf:123.34:-inf +-inf:-123.34:inf +123.34:+inf:inf +-123.34:+inf:-inf +123.34:-inf:-inf +-123.34:-inf:inf ++0:+0:0 ++0:+1:0 ++1:+0:0 ++0:-1:0 +-1:+0:0 ++123456789123456789:+0:0 ++0:+123456789123456789:0 +-1:-1:1 +-1:+1:-1 ++1:-1:-1 ++1:+1:1 ++2:+3:6 +-2:+3:-6 ++2:-3:-6 +-2:-3:6 ++111:+111:12321 ++10101:+10101:102030201 ++1001001:+1001001:1002003002001 ++100010001:+100010001:10002000300020001 ++10000100001:+10000100001:100002000030000200001 ++11111111111:+9:99999999999 ++22222222222:+9:199999999998 ++33333333333:+9:299999999997 ++44444444444:+9:399999999996 ++55555555555:+9:499999999995 ++66666666666:+9:599999999994 ++77777777777:+9:699999999993 ++88888888888:+9:799999999992 ++99999999999:+9:899999999991 +6:120:720 +10:10000:100000 +&fdiv-list +0:0:NaN,0 +0:1:0,0 +9:4:2,1 +9:5:1,4 +# bug in v1.74 with bdiv in list context, when $y is 1 or -1 +2.1:-1:-2.1,0 +2.1:1:2.1,0 +-2.1:-1:2.1,0 +-2.1:1:-2.1,0 +&fdiv +$div_scale = 40; $round_mode = 'even' +abc:abc:NaN +abc:+1:abc:NaN ++1:abc:NaN +-1:abc:NaN +0:abc:NaN ++0:+0:NaN ++0:+1:0 ++1:+0:inf ++3214:+0:inf ++0:-1:0 +-1:+0:-inf +-3214:+0:-inf ++1:+1:1 +-1:-1:1 ++1:-1:-1 +-1:+1:-1 ++1:+2:0.5 ++2:+1:2 +123:+inf:0 +123:-inf:0 ++10:+5:2 ++100:+4:25 ++1000:+8:125 ++10000:+16:625 ++10000:-16:-625 ++999999999999:+9:111111111111 ++999999999999:+99:10101010101 ++999999999999:+999:1001001001 ++999999999999:+9999:100010001 ++999999999999999:+99999:10000100001 ++1000000000:+9:111111111.1111111111111111111111111111111 ++2000000000:+9:222222222.2222222222222222222222222222222 ++3000000000:+9:333333333.3333333333333333333333333333333 ++4000000000:+9:444444444.4444444444444444444444444444444 ++5000000000:+9:555555555.5555555555555555555555555555556 ++6000000000:+9:666666666.6666666666666666666666666666667 ++7000000000:+9:777777777.7777777777777777777777777777778 ++8000000000:+9:888888888.8888888888888888888888888888889 ++9000000000:+9:1000000000 ++35500000:+113:314159.2920353982300884955752212389380531 ++71000000:+226:314159.2920353982300884955752212389380531 ++106500000:+339:314159.2920353982300884955752212389380531 ++1000000000:+3:333333333.3333333333333333333333333333333 +2:25.024996000799840031993601279744051189762:0.07992009269196593320152084692285869265447 +123456:1:123456 +$div_scale = 20 ++1000000000:+9:111111111.11111111111 ++2000000000:+9:222222222.22222222222 ++3000000000:+9:333333333.33333333333 ++4000000000:+9:444444444.44444444444 ++5000000000:+9:555555555.55555555556 ++6000000000:+9:666666666.66666666667 ++7000000000:+9:777777777.77777777778 ++8000000000:+9:888888888.88888888889 ++9000000000:+9:1000000000 +1:10:0.1 +1:100:0.01 +1:1000:0.001 +1:10000:0.0001 +1:504:0.001984126984126984127 +2:1.987654321:1.0062111801179738436 +123456789.123456789123456789123456789:1:123456789.12345678912 +# the next two cases are the "old" behaviour, but are now (>v0.01) different +#+35500000:+113:314159.292035398230088 +#+71000000:+226:314159.292035398230088 ++35500000:+113:314159.29203539823009 ++71000000:+226:314159.29203539823009 ++106500000:+339:314159.29203539823009 ++1000000000:+3:333333333.33333333333 +$div_scale = 1 +# round to accuracy 1 after bdiv ++124:+3:40 +123456789.1234:1:100000000 +# reset scale for further tests +$div_scale = 40 +&fmod ++9:4:1 ++9:5:4 ++9000:56:40 ++56:9000:56 +# inf handling, see table in doc +0:inf:0 +0:-inf:0 +5:inf:5 +5:-inf:-inf +-5:inf:inf +-5:-inf:-5 +inf:5:NaN +-inf:5:NaN +inf:-5:NaN +-inf:-5:NaN +5:5:0 +-5:-5:0 +inf:inf:NaN +-inf:-inf:NaN +-inf:inf:NaN +inf:-inf:NaN +8:0:8 +inf:0:inf +-inf:0:-inf +-8:0:-8 +0:0:0 +abc:abc:NaN +abc:1:abc:NaN +1:abc:NaN +0:1:0 +1:0:1 +0:-1:0 +-1:0:-1 +1:1:0 +-1:-1:0 +1:-1:0 +-1:1:0 +1:2:1 +2:1:0 +1000000000:9:1 +2000000000:9:2 +3000000000:9:3 +4000000000:9:4 +5000000000:9:5 +6000000000:9:6 +7000000000:9:7 +8000000000:9:8 +9000000000:9:0 +35500000:113:33 +71000000:226:66 +106500000:339:99 +1000000000:3:1 +10:5:0 +100:4:0 +1000:8:0 +10000:16:0 +999999999999:9:0 +999999999999:99:0 +999999999999:999:0 +999999999999:9999:0 +999999999999999:99999:0 +-9:+5:1 ++9:-5:-1 +-9:-5:-4 +-5:3:1 +-2:3:1 +4:3:1 +1:3:1 +-5:-3:-2 +-2:-3:-2 +4:-3:-2 +1:-3:-2 +4095:4095:0 +100041000510123:3:0 +152403346:12345:4321 +87654321:87654321:0 +# now some floating point tests +123:2.5:0.5 +1230:2.5:0 +123.4:2.5:0.9 +123e1:25:5 +-2.1:1:0.9 +2.1:1:0.1 +-2.1:-1:-0.1 +2.1:-1:-0.9 +-3:1:0 +3:1:0 +-3:-1:0 +3:-1:0 +&ffac +Nanfac:NaN +-1:NaN ++inf:inf +-inf:NaN +0:1 +1:1 +2:2 +3:6 +4:24 +5:120 +6:720 +10:3628800 +11:39916800 +12:479001600 +&froot +# sqrt() ++0:2:0 ++1:2:1 +-1:2:NaN +# -$x ** (1/2) => -$y, but not in froot() +-123.456:2:NaN ++inf:2:inf +-inf:2:NaN +2:2:1.41421356237309504880168872420969807857 +-2:2:NaN +4:2:2 +9:2:3 +16:2:4 +100:2:10 +123.456:2:11.11107555549866648462149404118219234119 +15241.38393:2:123.4559999756998444766131352122991626468 +1.44:2:1.2 +12:2:3.464101615137754587054892683011744733886 +0.49:2:0.7 +0.0049:2:0.07 +# invalid ones +1:NaN:NaN +-1:NaN:NaN +0:NaN:NaN +-inf:NaN:NaN ++inf:NaN:NaN +NaN:0:NaN +NaN:2:NaN +NaN:inf:NaN +NaN:inf:NaN +12:-inf:NaN +12:inf:NaN ++0:0:NaN ++1:0:NaN +-1:0:NaN +-2:0:NaN +-123.45:0:NaN ++inf:0:NaN +12:1:12 +-12:1:NaN +8:-1:NaN +-8:-1:NaN +# cubic root +8:3:2 +-8:3:NaN +# fourths root +16:4:2 +81:4:3 +# see t/bigroot() for more tests +&fsqrt ++0:0 +-1:NaN +-2:NaN +-16:NaN +-123.45:NaN +nanfsqrt:NaN ++inf:inf +-inf:NaN +1:1 +2:1.41421356237309504880168872420969807857 +4:2 +9:3 +16:4 +100:10 +123.456:11.11107555549866648462149404118219234119 +15241.38393:123.4559999756998444766131352122991626468 +1.44:1.2 +# sqrt(1.44) = 1.2, sqrt(e10) = e5 => 12e4 +1.44E10:120000 +2e10:141421.356237309504880168872420969807857 +144e20:120000000000 +# proved to be an endless loop under 7-9 +12:3.464101615137754587054892683011744733886 +0.49:0.7 +0.0049:0.07 +&is_nan +123:0 +abc:1 +NaN:1 +-123:0 +&is_inf ++inf::1 +-inf::1 +abc::0 +1::0 +NaN::0 +-1::0 ++inf:-:0 ++inf:+:1 +-inf:-:1 +-inf:+:0 +# it must be exactly /^[+-]inf$/ ++infinity::0 +-infinity::0 +&is_odd +abc:0 +0:0 +-1:1 +-3:1 +1:1 +3:1 +1000001:1 +1000002:0 ++inf:0 +-inf:0 +123.45:0 +-123.45:0 +2:0 +&is_int +NaNis_int:0 +0:1 +1:1 +2:1 +-2:1 +-1:1 +-inf:0 ++inf:0 +123.4567:0 +-0.1:0 +-0.002:0 +&is_even +abc:0 +0:1 +-1:0 +-3:0 +1:0 +3:0 +1000001:0 +1000002:1 +2:1 ++inf:0 +-inf:0 +123.456:0 +-123.456:0 +0.01:0 +-0.01:0 +120:1 +1200:1 +-1200:1 +&is_positive +0:0 +1:1 +-1:0 +-123:0 +NaN:0 +-inf:0 ++inf:1 +&is_negative +0:0 +1:0 +-1:1 +-123:1 +NaN:0 +-inf:1 ++inf:0 +&parts +0:0 1 +1:1 0 +123:123 0 +-123:-123 0 +-1200:-12 2 +NaNparts:NaN NaN ++inf:inf inf +-inf:-inf inf +&exponent +0:1 +1:0 +123:0 +-123:0 +-1200:2 ++inf:inf +-inf:inf +NaNexponent:NaN +&mantissa +0:0 +1:1 +123:123 +-123:-123 +-1200:-12 ++inf:inf +-inf:-inf +NaNmantissa:NaN +&length +123:3 +-123:3 +0:1 +1:1 +12345678901234567890:20 +&is_zero +NaNzero:0 ++inf:0 +-inf:0 +0:1 +-1:0 +1:0 +&is_one +NaNone:0 ++inf:0 +-inf:0 +0:0 +2:0 +1:1 +-1:0 +-2:0 +&ffloor +0:0 +abc:NaN ++inf:inf +-inf:-inf +1:1 +-51:-51 +-51.2:-52 +12.2:12 +0.12345:0 +0.123456:0 +0.1234567:0 +0.12345678:0 +0.123456789:0 +&fceil +0:0 +abc:NaN ++inf:inf +-inf:-inf +1:1 +-51:-51 +-51.2:-51 +12.2:13 +-0.4:0 +&fint +0:0 +NaN:NaN ++inf:inf +-inf:-inf +1:1 +-51:-51 +-51.2:-51 +12.2:12 +-0.4:0 diff --git a/cpan/Math-BigInt/t/bigfltpm.t b/cpan/Math-BigInt/t/bigfltpm.t new file mode 100644 index 0000000000..8653f77ad1 --- /dev/null +++ b/cpan/Math-BigInt/t/bigfltpm.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 2340 + + 5; # own tests + + +use Math::BigInt lib => 'Calc'; +use Math::BigFloat; + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigFloat"; +$CL = "Math::BigInt::Calc"; + +is ($class->config()->{class},$class); +is ($class->config()->{with}, $CL); + +# bug #17447: Can't call method Math::BigFloat->bsub, not a valid method +my $c = Math::BigFloat->new( '123.3' ); +is ($c->fsub(123), '0.3'); # calling fsub on a BigFloat works + +# Bug until BigInt v1.86, the scale wasn't treated as a scalar: +$c = Math::BigFloat->new('0.008'); my $d = Math::BigFloat->new(3); +my $e = $c->bdiv(Math::BigFloat->new(3),$d); + +is ($e,'0.00267'); # '0.008 / 3 => 0.0027'); +is (ref($e->{_e}->[0]), ''); # 'Not a BigInt'); + +require 't/bigfltpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/bigintc.t b/cpan/Math-BigInt/t/bigintc.t new file mode 100644 index 0000000000..d5837f0890 --- /dev/null +++ b/cpan/Math-BigInt/t/bigintc.t @@ -0,0 +1,454 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 379; + +use Math::BigInt::Calc; + +my ($BASE_LEN, undef, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL) = + Math::BigInt::Calc->_base_len(); + +print "# BASE_LEN = $BASE_LEN\n"; +print "# MAX_VAL = $MAX_VAL\n"; +print "# AND_BITS = $AND_BITS\n"; +print "# XOR_BITS = $XOR_BITS\n"; +print "# IOR_BITS = $OR_BITS\n"; + +# testing of Math::BigInt::Calc + +my $C = 'Math::BigInt::Calc'; # pass classname to sub's + +# _new and _str +my $x = $C->_new("123"); my $y = $C->_new("321"); +is (ref($x),'ARRAY'); is ($C->_str($x),123); is ($C->_str($y),321); + +############################################################################### +# _add, _sub, _mul, _div +is ($C->_str($C->_add($x,$y)),444); +is ($C->_str($C->_sub($x,$y)),123); +is ($C->_str($C->_mul($x,$y)),39483); +is ($C->_str($C->_div($x,$y)),123); + +############################################################################### +# check that mul/div doesn't change $y +# and returns the same reference, not something new +is ($C->_str($C->_mul($x,$y)),39483); +is ($C->_str($x),39483); is ($C->_str($y),321); + +is ($C->_str($C->_div($x,$y)),123); +is ($C->_str($x),123); is ($C->_str($y),321); + +$x = $C->_new("39483"); +my ($x1,$r1) = $C->_div($x,$y); +is ("$x1","$x"); +$C->_inc($x1); +is ("$x1","$x"); +is ($C->_str($r1),'0'); + +$x = $C->_new("39483"); # reset + +############################################################################### +my $z = $C->_new("2"); +is ($C->_str($C->_add($x,$z)),39485); +my ($re,$rr) = $C->_div($x,$y); + +is ($C->_str($re),123); is ($C->_str($rr),2); + +# is_zero, _is_one, _one, _zero +is ($C->_is_zero($x)||0,0); +is ($C->_is_one($x)||0,0); + +is ($C->_str($C->_zero()),"0"); +is ($C->_str($C->_one()),"1"); + +# _two() and _ten() +is ($C->_str($C->_two()),"2"); +is ($C->_str($C->_ten()),"10"); +is ($C->_is_ten($C->_two()),0); +is ($C->_is_two($C->_two()),1); +is ($C->_is_ten($C->_ten()),1); +is ($C->_is_two($C->_ten()),0); + +is ($C->_is_one($C->_one()),1); +is ($C->_is_one($C->_two()),0); +is ($C->_is_one($C->_ten()),0); + +is ($C->_is_one($C->_zero()) || 0,0); + +is ($C->_is_zero($C->_zero()),1); + +is ($C->_is_zero($C->_one()) || 0,0); + +# is_odd, is_even +is ($C->_is_odd($C->_one()),1); is ($C->_is_odd($C->_zero())||0,0); +is ($C->_is_even($C->_one()) || 0,0); is ($C->_is_even($C->_zero()),1); + +# _len +for my $method (qw/_alen _len/) + { + $x = $C->_new("1"); is ($C->$method($x),1); + $x = $C->_new("12"); is ($C->$method($x),2); + $x = $C->_new("123"); is ($C->$method($x),3); + $x = $C->_new("1234"); is ($C->$method($x),4); + $x = $C->_new("12345"); is ($C->$method($x),5); + $x = $C->_new("123456"); is ($C->$method($x),6); + $x = $C->_new("1234567"); is ($C->$method($x),7); + $x = $C->_new("12345678"); is ($C->$method($x),8); + $x = $C->_new("123456789"); is ($C->$method($x),9); + + $x = $C->_new("8"); is ($C->$method($x),1); + $x = $C->_new("21"); is ($C->$method($x),2); + $x = $C->_new("321"); is ($C->$method($x),3); + $x = $C->_new("4321"); is ($C->$method($x),4); + $x = $C->_new("54321"); is ($C->$method($x),5); + $x = $C->_new("654321"); is ($C->$method($x),6); + $x = $C->_new("7654321"); is ($C->$method($x),7); + $x = $C->_new("87654321"); is ($C->$method($x),8); + $x = $C->_new("987654321"); is ($C->$method($x),9); + + $x = $C->_new("0"); is ($C->$method($x),1); + $x = $C->_new("20"); is ($C->$method($x),2); + $x = $C->_new("320"); is ($C->$method($x),3); + $x = $C->_new("4320"); is ($C->$method($x),4); + $x = $C->_new("54320"); is ($C->$method($x),5); + $x = $C->_new("654320"); is ($C->$method($x),6); + $x = $C->_new("7654320"); is ($C->$method($x),7); + $x = $C->_new("87654320"); is ($C->$method($x),8); + $x = $C->_new("987654320"); is ($C->$method($x),9); + + for (my $i = 1; $i < 9; $i++) + { + my $a = "$i" . '0' x ($i-1); + $x = $C->_new($a); + print "# Tried len '$a'\n" unless is ($C->_len($x),$i); + } + } + +# _digit +$x = $C->_new("123456789"); +is ($C->_digit($x,0),9); +is ($C->_digit($x,1),8); +is ($C->_digit($x,2),7); +is ($C->_digit($x,8),1); +is ($C->_digit($x,9),0); +is ($C->_digit($x,-1),1); +is ($C->_digit($x,-2),2); +is ($C->_digit($x,-3),3); +is ($C->_digit($x,-9),9); +is ($C->_digit($x,-10),0); + +# _copy +foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/) + { + $x = $C->_new("$_"); + is ($C->_str($C->_copy($x)),"$_"); + is ($C->_str($x),"$_"); # did _copy destroy original x? + } + +# _zeros +$x = $C->_new("1256000000"); is ($C->_zeros($x),6); +$x = $C->_new("152"); is ($C->_zeros($x),0); +$x = $C->_new("123000"); is ($C->_zeros($x),3); +$x = $C->_new("0"); is ($C->_zeros($x),0); + +# _lsft, _rsft +$x = $C->_new("10"); $y = $C->_new("3"); +is ($C->_str($C->_lsft($x,$y,10)),10000); +$x = $C->_new("20"); $y = $C->_new("3"); +is ($C->_str($C->_lsft($x,$y,10)),20000); + +$x = $C->_new("128"); $y = $C->_new("4"); +is ($C->_str($C->_lsft($x,$y,2)), 128 << 4); + +$x = $C->_new("1000"); $y = $C->_new("3"); +is ($C->_str($C->_rsft($x,$y,10)),1); +$x = $C->_new("20000"); $y = $C->_new("3"); +is ($C->_str($C->_rsft($x,$y,10)),20); +$x = $C->_new("256"); $y = $C->_new("4"); +is ($C->_str($C->_rsft($x,$y,2)),256 >> 4); + +$x = $C->_new("6411906467305339182857313397200584952398"); +$y = $C->_new("45"); +is ($C->_str($C->_rsft($x,$y,10)),0); + +# _acmp +$x = $C->_new("123456789"); +$y = $C->_new("987654321"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); +$x = $C->_new("12"); +$y = $C->_new("12"); +is ($C->_acmp($x,$y),0); +$x = $C->_new("21"); +is ($C->_acmp($x,$y),1); +is ($C->_acmp($y,$x),-1); +$x = $C->_new("123456789"); +$y = $C->_new("1987654321"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),+1); + +$x = $C->_new("1234567890123456789"); +$y = $C->_new("987654321012345678"); +is ($C->_acmp($x,$y),1); +is ($C->_acmp($y,$x),-1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); + +$x = $C->_new("1234"); +$y = $C->_new("987654321012345678"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); + +# _modinv +$x = $C->_new("8"); +$y = $C->_new("5033"); +my ($xmod,$sign) = $C->_modinv($x,$y); +is ($C->_str($xmod),'629'); # -629 % 5033 == 4404 +is ($sign, '-'); + +# _div +$x = $C->_new("3333"); $y = $C->_new("1111"); +is ($C->_str(scalar $C->_div($x,$y)),3); +$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); +is ($C->_str($x),30); is ($C->_str($y),3); +$x = $C->_new("123"); $y = $C->_new("1111"); +($x,$y) = $C->_div($x,$y); is ($C->_str($x),0); is ($C->_str($y),123); + +# _num +foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/) + { + $x = $C->_new("$_"); + is (ref($x),'ARRAY'); is ($C->_str($x),"$_"); + $x = $C->_num($x); is (ref($x),''); is ($x,$_); + } + +# _sqrt +$x = $C->_new("144"); is ($C->_str($C->_sqrt($x)),'12'); +$x = $C->_new("144000000000000"); is ($C->_str($C->_sqrt($x)),'12000000'); + +# _root +$x = $C->_new("81"); my $n = $C->_new("3"); # 4*4*4 = 64, 5*5*5 = 125 +is ($C->_str($C->_root($x,$n)),'4'); # 4.xx => 4.0 +$x = $C->_new("81"); $n = $C->_new("4"); # 3*3*3*3 == 81 +is ($C->_str($C->_root($x,$n)),'3'); + +# _pow (and _root) +$x = $C->_new("0"); $n = $C->_new("3"); # 0 ** y => 0 +is ($C->_str($C->_pow($x,$n)), 0); +$x = $C->_new("3"); $n = $C->_new("0"); # x ** 0 => 1 +is ($C->_str($C->_pow($x,$n)), 1); +$x = $C->_new("1"); $n = $C->_new("3"); # 1 ** y => 1 +is ($C->_str($C->_pow($x,$n)), 1); +$x = $C->_new("5"); $n = $C->_new("1"); # x ** 1 => x +is ($C->_str($C->_pow($x,$n)), 5); + +$x = $C->_new("81"); $n = $C->_new("3"); # 81 ** 3 == 531441 +is ($C->_str($C->_pow($x,$n)),81 ** 3); + +is ($C->_str($C->_root($x,$n)),81); + +$x = $C->_new("81"); +is ($C->_str($C->_pow($x,$n)),81 ** 3); +is ($C->_str($C->_pow($x,$n)),'150094635296999121'); # 531441 ** 3 == + +is ($C->_str($C->_root($x,$n)),'531441'); +is ($C->_str($C->_root($x,$n)),'81'); + +$x = $C->_new("81"); $n = $C->_new("14"); +is ($C->_str($C->_pow($x,$n)),'523347633027360537213511521'); +is ($C->_str($C->_root($x,$n)),'81'); + +$x = $C->_new("523347633027360537213511520"); +is ($C->_str($C->_root($x,$n)),'80'); + +$x = $C->_new("523347633027360537213511522"); +is ($C->_str($C->_root($x,$n)),'81'); + +my $res = [ qw/9 31 99 316 999 3162 9999 31622 99999/ ]; + +# 99 ** 2 = 9801, 999 ** 2 = 998001 etc +for my $i (2 .. 9) + { + $x = '9' x $i; $x = $C->_new($x); + $n = $C->_new("2"); + my $rc = '9' x ($i-1). '8' . '0' x ($i-1) . '1'; + print "# _pow( ", '9' x $i, ", 2) \n" unless + is ($C->_str($C->_pow($x,$n)),$rc); + + # if $i > $BASE_LEN, the test takes a really long time: + if ($i <= $BASE_LEN) + { + $x = '9' x $i; $x = $C->_new($x); + $n = '9' x $i; $n = $C->_new($n); + print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n"; + print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless + is ($C->_str($C->_root($x,$n)),'1'); + + $x = '9' x $i; $x = $C->_new($x); + $n = $C->_new("2"); + print "# BASE_LEN $BASE_LEN _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless + is ($C->_str($C->_root($x,$n)), $res->[$i-2]); + } + else + { + is ("skipped $i", "skipped $i"); + is ("skipped $i", "skipped $i"); + } + } + +############################################################################## +# _fac +$x = $C->_new("0"); is ($C->_str($C->_fac($x)),'1'); +$x = $C->_new("1"); is ($C->_str($C->_fac($x)),'1'); +$x = $C->_new("2"); is ($C->_str($C->_fac($x)),'2'); +$x = $C->_new("3"); is ($C->_str($C->_fac($x)),'6'); +$x = $C->_new("4"); is ($C->_str($C->_fac($x)),'24'); +$x = $C->_new("5"); is ($C->_str($C->_fac($x)),'120'); +$x = $C->_new("10"); is ($C->_str($C->_fac($x)),'3628800'); +$x = $C->_new("11"); is ($C->_str($C->_fac($x)),'39916800'); +$x = $C->_new("12"); is ($C->_str($C->_fac($x)),'479001600'); +$x = $C->_new("13"); is ($C->_str($C->_fac($x)),'6227020800'); + +# test that _fac modifies $x in place for small arguments +$x = $C->_new("3"); $C->_fac($x); is ($C->_str($x),'6'); +$x = $C->_new("13"); $C->_fac($x); is ($C->_str($x),'6227020800'); + +############################################################################## +# _inc and _dec +foreach (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/) + { + $x = $C->_new("$_"); $C->_inc($x); + print "# \$x = ",$C->_str($x),"\n" + unless is ($C->_str($x),substr($_,0,length($_)-1) . '2'); + $C->_dec($x); is ($C->_str($x),$_); + } +foreach (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/) + { + $x = $C->_new("$_"); $C->_inc($x); + print "# \$x = ",$C->_str($x),"\n" + unless is ($C->_str($x),substr($_,0,length($_)-2) . '20'); + $C->_dec($x); is ($C->_str($x),$_); + } +foreach (qw/999 9999 99999 9999999 99999999 999999999 9999999999 99999999999/) + { + $x = $C->_new("$_"); $C->_inc($x); + print "# \$x = ",$C->_str($x),"\n" + unless is ($C->_str($x), '1' . '0' x (length($_))); + $C->_dec($x); is ($C->_str($x),$_); + } + +$x = $C->_new("1000"); $C->_inc($x); is ($C->_str($x),'1001'); +$C->_dec($x); is ($C->_str($x),'1000'); + +my $BL; +{ + no strict 'refs'; + $BL = &{"$C"."::_base_len"}(); +} + +$x = '1' . '0' x $BL; +$z = '1' . '0' x ($BL-1); $z .= '1'; +$x = $C->_new($x); $C->_inc($x); is ($C->_str($x),$z); + +$x = '1' . '0' x $BL; $z = '9' x $BL; +$x = $C->_new($x); $C->_dec($x); is ($C->_str($x),$z); + +# should not happen: +# $x = $C->_new("-2"); $y = $C->_new("4"); is ($C->_acmp($x,$y),-1); + +############################################################################### +# _mod +$x = $C->_new("1000"); $y = $C->_new("3"); +is ($C->_str(scalar $C->_mod($x,$y)),1); +$x = $C->_new("1000"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_mod($x,$y)),0); + +# _and, _or, _xor +$x = $C->_new("5"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_xor($x,$y)),7); +$x = $C->_new("5"); $y = $C->_new("2"); +is ($C->_str(scalar $C->_or($x,$y)),7); +$x = $C->_new("5"); $y = $C->_new("3"); +is ($C->_str(scalar $C->_and($x,$y)),1); + +# _from_hex, _from_bin, _from_oct +is ($C->_str( $C->_from_hex("0xFf")),255); +is ($C->_str( $C->_from_bin("0b10101011")),160+11); +is ($C->_str( $C->_from_oct("0100")), 8*8); +is ($C->_str( $C->_from_oct("01000")), 8*8*8); +is ($C->_str( $C->_from_oct("010001")), 8*8*8*8+1); +is ($C->_str( $C->_from_oct("010007")), 8*8*8*8+7); + +# _as_hex, _as_bin, as_oct +is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("128")))), 128); +is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("128")))), 128); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("128")))), 128); + +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456")))), 123456); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456789")))), "123456789"); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("1234567890123")))), "1234567890123"); + +my $long = '123456789012345678901234567890'; +is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new($long)))), $long); +is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new($long)))), $long); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new($long)))), $long); +is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("0")))), 0); +is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("0")))), 0); +is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("0")))), 0); +is ($C->_as_hex( $C->_new("0")), '0x0'); +is ($C->_as_bin( $C->_new("0")), '0b0'); +is ($C->_as_oct( $C->_new("0")), '00'); +is ($C->_as_hex( $C->_new("12")), '0xc'); +is ($C->_as_bin( $C->_new("12")), '0b1100'); +is ($C->_as_oct( $C->_new("64")), '0100'); + +# _1ex +is ($C->_str($C->_1ex(0)), "1"); +is ($C->_str($C->_1ex(1)), "10"); +is ($C->_str($C->_1ex(2)), "100"); +is ($C->_str($C->_1ex(12)), "1000000000000"); +is ($C->_str($C->_1ex(16)), "10000000000000000"); + +# _check +$x = $C->_new("123456789"); +is ($C->_check($x),0); +is ($C->_check(123),'123 is not a reference'); + +############################################################################### +# __strip_zeros + +{ + no strict 'refs'; + # correct empty arrays + $x = &{$C."::__strip_zeros"}([]); is (@$x,1); is ($x->[0],0); + # don't strip single elements + $x = &{$C."::__strip_zeros"}([0]); is (@$x,1); is ($x->[0],0); + $x = &{$C."::__strip_zeros"}([1]); is (@$x,1); is ($x->[0],1); + # don't strip non-zero elements + $x = &{$C."::__strip_zeros"}([0,1]); + is (@$x,2); is ($x->[0],0); is ($x->[1],1); + $x = &{$C."::__strip_zeros"}([0,1,2]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + # but strip leading zeros + $x = &{$C."::__strip_zeros"}([0,1,2,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + $x = &{$C."::__strip_zeros"}([0,1,2,0,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]); + is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); + + # collapse multiple zeros + $x = &{$C."::__strip_zeros"}([0,0,0,0]); + is (@$x,1); is ($x->[0],0); +} + +# done + +1; diff --git a/cpan/Math-BigInt/t/bigintpm.inc b/cpan/Math-BigInt/t/bigintpm.inc new file mode 100644 index 0000000000..08a98acae5 --- /dev/null +++ b/cpan/Math-BigInt/t/bigintpm.inc @@ -0,0 +1,2707 @@ +#include this file into another for subclass testing + +my $version = ${"$class\::VERSION"}; + +use strict; + +############################################################################## +# for testing inheritance of _swap + +package Math::Foo; + +use Math::BigInt lib => $main::CL; +use vars qw/@ISA/; +@ISA = (qw/Math::BigInt/); + +use overload +# customized overload for sub, since original does not use swap there +'-' => sub { my @a = ref($_[0])->_swap(@_); + $a[0]->bsub($a[1])}; + +sub _swap + { + # a fake _swap, which reverses the params + my $self = shift; # for override in subclass + if ($_[2]) + { + my $c = ref ($_[0] ) || 'Math::Foo'; + return ( $_[0]->copy(), $_[1] ); + } + else + { + return ( Math::Foo->new($_[1]), $_[0] ); + } + } + +############################################################################## +package main; + +my $CALC = $class->config()->{lib}; is ($CALC,$CL); + +my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class); + +while () + { + $_ =~ s/[\n\r]//g; # remove newlines + next if /^#/; # skip comments + if (s/^&//) + { + $f = $_; next; + } + elsif (/^\$/) + { + $round_mode = $_; $round_mode =~ s/^\$/$class\->/; next; + } + + @args = split(/:/,$_,99); $ans = pop(@args); + $expected_class = $class; + if ($ans =~ /(.*?)=(.*)/) + { + $expected_class = $2; $ans = $1; + } + $try = "\$x = $class->new(\"$args[0]\");"; + if ($f eq "bnorm") + { + $try = "\$x = $class->bnorm(\"$args[0]\");"; + # some is_xxx tests + } elsif ($f =~ /^is_(zero|one|odd|even|negative|positive|nan|int)$/) { + $try .= "\$x->$f() || 0;"; + } elsif ($f eq "is_inf") { + $try .= "\$x->is_inf('$args[1]');"; + } elsif ($f eq "binf") { + $try .= "\$x->binf('$args[1]');"; + } elsif ($f eq "bone") { + $try .= "\$x->bone('$args[1]');"; + # some unary ops + } elsif ($f =~ /^b(nan|floor|ceil|int|sstr|neg|abs|sgn|inc|dec|not|sqrt|fac)$/) { + $try .= "\$x->$f();"; + } elsif ($f =~ /^(numify|length|stringify|as_hex|as_bin)$/) { + $try .= "\$x->$f();"; + } elsif ($f eq "exponent"){ + # ->bstr() to see if an object is returned + $try .= '$x = $x->exponent()->bstr();'; + } elsif ($f eq "mantissa"){ + # ->bstr() to see if an object is returned + $try .= '$x = $x->mantissa()->bstr();'; + } elsif ($f eq "parts"){ + $try .= '($m,$e) = $x->parts();'; + # ->bstr() to see if an object is returned + $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;'; + $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;'; + $try .= '"$m,$e";'; + }elsif ($f eq "bexp"){ + $try .= "\$x->bexp();"; + } elsif ($f eq "bpi"){ + $try .= "$class\->bpi(\$x);"; + } else { + # binary ops + $try .= "\$y = $class->new('$args[1]');"; + if ($f eq "bcmp") + { + $try .= '$x->bcmp($y);'; + } elsif ($f eq "bround") { + $try .= "$round_mode; \$x->bround(\$y);"; + } elsif ($f eq "bacmp"){ + $try .= '$x->bacmp($y);'; + } elsif ($f eq "badd"){ + $try .= '$x + $y;'; + } elsif ($f eq "bsub"){ + $try .= '$x - $y;'; + } elsif ($f eq "bmul"){ + $try .= '$x * $y;'; + } elsif ($f eq "bdiv"){ + $try .= '$x / $y;'; + } elsif ($f eq "bdiv-list"){ + $try .= 'join (",",$x->bdiv($y));'; + # overload via x= + } elsif ($f =~ /^.=$/){ + $try .= "\$x $f \$y;"; + # overload via x + } elsif ($f =~ /^.$/){ + $try .= "\$x $f \$y;"; + } elsif ($f eq "bmod"){ + $try .= '$x % $y;'; + } elsif ($f eq "bgcd") + { + if (defined $args[2]) + { + $try .= " \$z = $class->new('$args[2]'); "; + } + $try .= "$class\::bgcd(\$x, \$y"; + $try .= ", \$z" if (defined $args[2]); + $try .= " );"; + } + elsif ($f eq "blcm") + { + if (defined $args[2]) + { + $try .= " \$z = $class->new('$args[2]'); "; + } + $try .= "$class\::blcm(\$x, \$y"; + $try .= ", \$z" if (defined $args[2]); + $try .= " );"; + }elsif ($f eq "blsft"){ + if (defined $args[2]) + { + $try .= "\$x->blsft(\$y,$args[2]);"; + } + else + { + $try .= "\$x << \$y;"; + } + }elsif ($f eq "brsft"){ + if (defined $args[2]) + { + $try .= "\$x->brsft(\$y,$args[2]);"; + } + else + { + $try .= "\$x >> \$y;"; + } + }elsif ($f eq "bnok"){ + $try .= "\$x->bnok(\$y);"; + }elsif ($f eq "broot"){ + $try .= "\$x->broot(\$y);"; + }elsif ($f eq "blog"){ + $try .= "\$x->blog(\$y);"; + }elsif ($f eq "band"){ + $try .= "\$x & \$y;"; + }elsif ($f eq "bior"){ + $try .= "\$x | \$y;"; + }elsif ($f eq "bxor"){ + $try .= "\$x ^ \$y;"; + }elsif ($f eq "bpow"){ + $try .= "\$x ** \$y;"; + } elsif( $f eq "bmodinv") { + $try .= "\$x->bmodinv(\$y);"; + }elsif ($f eq "digit"){ + $try .= "\$x->digit(\$y);"; + }elsif ($f eq "batan2"){ + $try .= "\$x->batan2(\$y);"; + } else { + # Functions with three arguments + $try .= "\$z = $class->new(\"$args[2]\");"; + + if( $f eq "bmodpow") { + $try .= "\$x->bmodpow(\$y,\$z);"; + } elsif ($f eq "bmuladd"){ + $try .= "\$x->bmuladd(\$y,\$z);"; + } else { warn "Unknown op '$f'"; } + } + } # end else all other ops + + $ans1 = eval $try; + # convert hex/binary targets to decimal + if ($ans =~ /^(0x0x|0b0b)/) + { + $ans =~ s/^0[xb]//; $ans = Math::BigInt->new($ans)->bstr(); + } + if ($ans eq "") + { + is ($ans1, undef); + } + else + { + # print "try: $try ans: $ans1 $ans\n"; + print "# Tried: '$try'\n" if !is ($ans1, $ans); + is (ref($ans),$expected_class) if $expected_class ne $class; + } + # check internal state of number objects + is_valid($ans1,$f) if ref $ans1; + } # endwhile data tests +close DATA; + +# test some more +@a = (); +for (my $i = 1; $i < 10; $i++) + { + push @a, $i; + } +is("@a", "1 2 3 4 5 6 7 8 9"); + +# test whether self-multiplication works correctly (result is 2**64) +$try = "\$x = $class->new('4294967296');"; +$try .= '$a = $x->bmul($x);'; +$ans1 = eval $try; +print "# Tried: '$try'\n" if !is ($ans1, $class->new(2) ** 64); +# test self-pow +$try = "\$x = $class->new(10);"; +$try .= '$a = $x->bpow($x);'; +$ans1 = eval $try; +print "# Tried: '$try'\n" if !is ($ans1, $class->new(10) ** 10); + +############################################################################### +# test whether op destroys args or not (should better not) + +$x = $class->new(3); +$y = $class->new(4); +$z = $x & $y; +is ($x,3); +is ($y,4); +is ($z,0); +$z = $x | $y; +is ($x,3); +is ($y,4); +is ($z,7); +$x = $class->new(1); +$y = $class->new(2); +$z = $x | $y; +is ($x,1); +is ($y,2); +is ($z,3); + +$x = $class->new(5); +$y = $class->new(4); +$z = $x ^ $y; +is ($x,5); +is ($y,4); +is ($z,1); + +$x = $class->new(-5); $y = -$x; +is ($x, -5); + +$x = $class->new(-5); $y = abs($x); +is ($x, -5); + +$x = $class->new(8); +$y = $class->new(-1); +$z = $class->new(5033); +my $u = $x->copy()->bmodpow($y,$z); +is ($u,4404); +is ($y,-1); +is ($z,5033); + +$x = $class->new(-5); $y = -$x; is ($x,-5); is ($y,5); +$x = $class->new(-5); $y = $x->copy()->bneg(); is ($x,-5); is ($y,5); + +$x = $class->new(-5); $y = $class->new(3); $x->bmul($y); is ($x,-15); is ($y,3); +$x = $class->new(-5); $y = $class->new(3); $x->badd($y); is ($x,-2); is ($y,3); +$x = $class->new(-5); $y = $class->new(3); $x->bsub($y); is ($x,-8); is ($y,3); +$x = $class->new(-15); $y = $class->new(3); $x->bdiv($y); is ($x,-5); is ($y,3); +$x = $class->new(-5); $y = $class->new(3); $x->bmod($y); is ($x,1); is ($y,3); + +$x = $class->new(5); $y = $class->new(3); $x->bmul($y); is ($x,15); is ($y,3); +$x = $class->new(5); $y = $class->new(3); $x->badd($y); is ($x,8); is ($y,3); +$x = $class->new(5); $y = $class->new(3); $x->bsub($y); is ($x,2); is ($y,3); +$x = $class->new(15); $y = $class->new(3); $x->bdiv($y); is ($x,5); is ($y,3); +$x = $class->new(5); $y = $class->new(3); $x->bmod($y); is ($x,2); is ($y,3); + +$x = $class->new(5); $y = $class->new(-3); $x->bmul($y); is ($x,-15); is ($y,-3); +$x = $class->new(5); $y = $class->new(-3); $x->badd($y); is ($x,2); is ($y,-3); +$x = $class->new(5); $y = $class->new(-3); $x->bsub($y); is ($x,8); is ($y,-3); +$x = $class->new(15); $y = $class->new(-3); $x->bdiv($y); is ($x,-5); is ($y,-3); +$x = $class->new(5); $y = $class->new(-3); $x->bmod($y); is ($x,-1); is ($y,-3); + +############################################################################### +# check whether overloading cmp works +$try = "\$x = $class->new(0);"; +$try .= "\$y = 10;"; +$try .= "'false' if \$x ne \$y;"; +$ans = eval $try; +print "# For '$try'\n" if (!is ("$ans" , "false") ); + +# we cant test for working cmpt with other objects here, we would need a dummy +# object with stringify overload for this. see Math::String tests as example + +############################################################################### +# check reversed order of arguments + +$try = "\$x = $class->new(10); \$x = 2 ** \$x;"; +$try .= "'ok' if \$x == 1024;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +$try = "\$x = $class->new(10); \$x = 2 * \$x;"; +$try .= "'ok' if \$x == 20;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +$try = "\$x = $class->new(10); \$x = 2 + \$x;"; +$try .= "'ok' if \$x == 12;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +$try = "\$x = $class\->new(10); \$x = 2 - \$x;"; +$try .= "'ok' if \$x == -8;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +$try = "\$x = $class\->new(10); \$x = 20 / \$x;"; +$try .= "'ok' if \$x == 2;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +$try = "\$x = $class\->new(3); \$x = 20 % \$x;"; +$try .= "'ok' if \$x == 2;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +$try = "\$x = $class\->new(7); \$x = 20 & \$x;"; +$try .= "'ok' if \$x == 4;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +$try = "\$x = $class\->new(7); \$x = 0x20 | \$x;"; +$try .= "'ok' if \$x == 0x27;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +$try = "\$x = $class\->new(7); \$x = 0x20 ^ \$x;"; +$try .= "'ok' if \$x == 0x27;"; $ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +############################################################################### +# check badd(4,5) form + +$try = "\$x = $class\->badd(4,5);"; +$try .= "'ok' if \$x == 9;"; +$ans = eval $try; +print "# For '$try'\n" if (!ok "$ans" , "ok" ); + +############################################################################### +# check undefs: NOT DONE YET + +############################################################################### +# bool + +$x = $class->new(1); if ($x) { is (1,1); } else { is ($x,'to be true') } +$x = $class->new(0); if (!$x) { is (1,1); } else { is ($x,'to be false') } + +############################################################################### +# objectify() + +@args = Math::BigInt::objectify(2,4,5); +is (scalar @args,3); # $class, 4, 5 +like ($args[0], qr/^Math::BigInt/); +is ($args[1],4); +is ($args[2],5); + +@args = Math::BigInt::objectify(0,4,5); +is (scalar @args,3); # $class, 4, 5 +like ($args[0], qr/^Math::BigInt/); +is ($args[1],4); +is ($args[2],5); + +@args = Math::BigInt::objectify(2,4,5); +is (scalar @args,3); # $class, 4, 5 +like ($args[0], qr/^Math::BigInt/); +is ($args[1],4); +is ($args[2],5); + +@args = Math::BigInt::objectify(2,4,5,6,7); +is (scalar @args,5); # $class, 4, 5, 6, 7 +like ($args[0], qr/^Math::BigInt/); +is ($args[1],4); is (ref($args[1]),$args[0]); +is ($args[2],5); is (ref($args[2]),$args[0]); +is ($args[3],6); is (ref($args[3]),''); +is ($args[4],7); is (ref($args[4]),''); + +@args = Math::BigInt::objectify(2,$class,4,5,6,7); +is (scalar @args,5); # $class, 4, 5, 6, 7 +is ($args[0],$class); +is ($args[1],4); is (ref($args[1]),$args[0]); +is ($args[2],5); is (ref($args[2]),$args[0]); +is ($args[3],6); is (ref($args[3]),''); +is ($args[4],7); is (ref($args[4]),''); + +############################################################################### +# test whether an opp calls objectify properly or not (or at least does what +# it should do given non-objects, w/ or w/o objectify()) + +is ($class->new(123)->badd(123),246); +is ($class->badd(123,321),444); +is ($class->badd(123,$class->new(321)),444); + +is ($class->new(123)->bsub(122),1); +is ($class->bsub(321,123),198); +is ($class->bsub(321,$class->new(123)),198); + +is ($class->new(123)->bmul(123),15129); +is ($class->bmul(123,123),15129); +is ($class->bmul(123,$class->new(123)),15129); + +is ($class->new(15129)->bdiv(123),123); +is ($class->bdiv(15129,123),123); +is ($class->bdiv(15129,$class->new(123)),123); + +is ($class->new(15131)->bmod(123),2); +is ($class->bmod(15131,123),2); +is ($class->bmod(15131,$class->new(123)),2); + +is ($class->new(2)->bpow(16),65536); +is ($class->bpow(2,16),65536); +is ($class->bpow(2,$class->new(16)),65536); + +is ($class->new(2**15)->brsft(1),2**14); +is ($class->brsft(2**15,1),2**14); +is ($class->brsft(2**15,$class->new(1)),2**14); + +is ($class->new(2**13)->blsft(1),2**14); +is ($class->blsft(2**13,1),2**14); +is ($class->blsft(2**13,$class->new(1)),2**14); + +############################################################################### +# test for floating-point input (other tests in bnorm() below) + +$z = 1050000000000000; # may be int on systems with 64bit? +$x = $class->new($z); is ($x->bsstr(),'105e+13'); # not 1.05e+15 +$z = 1e+129; # definitely a float (may fail on UTS) +# don't compare to $z, since some Perl versions stringify $z into something +# like '1.e+129' or something equally ugly +$x = $class->new($z); is ($x->bsstr(),'1e+129'); + +############################################################################### +# test for whitespace including newlines to be handled correctly + +# is ($Math::BigInt::strict,1); # the default + +foreach my $c ( + qw/1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567890/) + { + my $m = $class->new($c); + is ($class->new("$c"),$m); + is ($class->new(" $c"),$m); + is ($class->new("$c "),$m); + is ($class->new(" $c "),$m); + is ($class->new("\n$c"),$m); + is ($class->new("$c\n"),$m); + is ($class->new("\n$c\n"),$m); + is ($class->new(" \n$c\n"),$m); + is ($class->new(" \n$c \n"),$m); + is ($class->new(" \n$c\n "),$m); + is ($class->new(" \n$c\n1"),'NaN'); + is ($class->new("1 \n$c\n1"),'NaN'); + } + +############################################################################### +# prime number tests, also test for **= and length() +# found on: http://www.utm.edu/research/primes/notes/by_year.html + +# ((2^148)-1)/17 +$x = $class->new(2); $x **= 148; $x++; $x = $x / 17; +is ($x,"20988936657440586486151264256610222593863921"); +is ($x->length(),length "20988936657440586486151264256610222593863921"); + +# MM7 = 2^127-1 +$x = $class->new(2); $x **= 127; $x--; +is ($x,"170141183460469231731687303715884105727"); + +$x = $class->new('215960156869840440586892398248'); +($x,$y) = $x->length(); +is ($x,30); is ($y,0); + +$x = $class->new('1_000_000_000_000'); +($x,$y) = $x->length(); +is ($x,13); is ($y,0); + +# test <<=, >>= +$x = $class->new('2'); +my $y = $class->new('18'); +is ($x <<= $y, 2 << 18); +is ($x, 2 << 18); +is ($x >>= $y, 2); +is ($x, 2); + +# I am afraid the following is not yet possible due to slowness +# Also, testing for 2 meg output is a bit hard ;) +#$x = $class->new(2); $x **= 6972593; $x--; + +# 593573509*2^332162+1 has exactly 1,000,000 digits +# takes about 24 mins on 300 Mhz, so cannot be done yet ;) +#$x = $class->new(2); $x **= 332162; $x *= "593573509"; $x++; +#is ($x->length(),1_000_000); + +############################################################################### +# inheritance and overriding of _swap + +$x = Math::Foo->new(5); +$x = $x - 8; # 8 - 5 instead of 5-8 +is ($x,3); +is (ref($x),'Math::Foo'); + +$x = Math::Foo->new(5); +$x = 8 - $x; # 5 - 8 instead of 8 - 5 +is ($x,-3); +is (ref($x),'Math::Foo'); + +############################################################################### +# Test whether +inf eq inf +# This tried to test whether BigInt inf equals Perl inf. Unfortunately, Perl +# hasn't (before 5.7.3 at least) a consistent way to say inf, and some things +# like 1e100000 crash on some platforms. So simple test for the string 'inf' +$x = $class->new('+inf'); is ($x,'inf'); + +############################################################################### +# numify() and 64 bit integer support + +require Config; +SKIP: { + skip("no 64 bit integer support", 4) + unless $Config::Config{use64bitint} || $Config::Config{use64bitall}; + + # The following should not give "1.84467440737096e+19". + + $x = $class -> new(2) -> bpow(64) -> bdec(); + is($x -> bstr(), "18446744073709551615", "bigint 2**64-1 as string"); + is($x -> numify(), "18446744073709551615", "bigint 2**64-1 as number"); + + # The following should not give "-9.22337203685478e+18". + + $x = $class -> new(2) -> bpow(63) -> bneg(); + is($x -> bstr(), "-9223372036854775808", "bigint -2**63 as string"); + is($x -> numify(), "-9223372036854775808", "bigint -2**63 as number"); +}; + +############################################################################### +############################################################################### +# the following tests only make sense with Math::BigInt::Calc or BareCalc or +# FastCalc + +exit if $CALC !~ /^Math::BigInt::(|Bare|Fast)Calc$/; # for Pari et al. + +############################################################################### +# check proper length of internal arrays + +my $bl = $CL->_base_len(); +my $BASE = '9' x $bl; +my $MAX = $BASE; +$BASE++; + +$x = $class->new($MAX); is_valid($x); # f.i. 9999 +$x += 1; is ($x,$BASE); is_valid($x); # 10000 +$x -= 1; is ($x,$MAX); is_valid($x); # 9999 again + +############################################################################### +# check numify + +$x = $class->new($BASE-1); is ($x->numify(),$BASE-1); +$x = $class->new(-($BASE-1)); is ($x->numify(),-($BASE-1)); + +# +0 is to protect from 1e15 vs 100000000 (stupid to_string aaaarglburblll...) +$x = $class->new($BASE); is ($x->numify()+0,$BASE+0); +$x = $class->new(-$BASE); is ($x->numify(),-$BASE); +$x = $class->new( -($BASE*$BASE*1+$BASE*1+1) ); +is ($x->numify(),-($BASE*$BASE*1+$BASE*1+1)); + +############################################################################### +# test bug in _digits with length($c[-1]) where $c[-1] was "00001" instead of 1 + +$x = $class->new($BASE-2); $x++; $x++; $x++; $x++; +if ($x > $BASE) { is (1,1) } else { is ("$x < $BASE","$x > $BASE"); } + +$x = $class->new($BASE+3); $x++; +if ($x > $BASE) { is (1,1) } else { is ("$x > $BASE","$x < $BASE"); } + +# test for +0 instead of int(): +$x = $class->new($MAX); is ($x->length(), length($MAX)); + +############################################################################### +# test bug that $class->digit($string) did not work + +is ($class->digit(123,2),1); + +############################################################################### +# bug in sub where number with at least 6 trailing zeros after any op failed + +$x = $class->new(123456); $z = $class->new(10000); $z *= 10; $x -= $z; +is ($z, 100000); +is ($x, 23456); + +############################################################################### +# bug in shortcut in mul() + +# construct a number with a zero-hole of BASE_LEN_SMALL +{ + my @bl = $CL->_base_len(); my $bl = $bl[5]; + + $x = '1' x $bl . '0' x $bl . '1' x $bl . '0' x $bl; + $y = '1' x (2*$bl); + $x = $class->new($x)->bmul($y); + # result is 123..$bl . $bl x (3*bl-1) . $bl...321 . '0' x $bl + $y = ''; my $d = ''; + for (my $i = 1; $i <= $bl; $i++) + { + $y .= $i; $d = $i.$d; + } + $y .= $bl x (3*$bl-1) . $d . '0' x $bl; + is ($x,$y); + + + ############################################################################# + # see if mul shortcut for small numbers works + + $x = '9' x $bl; + $x = $class->new($x); + # 999 * 999 => 998 . 001, 9999*9999 => 9998 . 0001 + is ($x*$x, '9' x ($bl-1) . '8' . '0' x ($bl-1) . '1'); +} + +############################################################################### +# bug with rest "-0" in div, causing further div()s to fail + +$x = $class->new('-322056000'); ($x,$y) = $x->bdiv('-12882240'); + +is ($y,'0'); is_valid($y); # $y not '-0' + +############################################################################### +# bug in $x->bmod($y) + +# if $x < 0 and $y > 0 +$x = $class->new('-629'); is ($x->bmod(5033),4404); + +############################################################################### +# bone/binf etc as plain calls (Lite failed them) + +is ($class->bzero(),0); +is ($class->bone(),1); +is ($class->bone('+'),1); +is ($class->bone('-'),-1); +is ($class->bnan(),'NaN'); +is ($class->binf(),'inf'); +is ($class->binf('+'),'inf'); +is ($class->binf('-'),'-inf'); +is ($class->binf('-inf'),'-inf'); + +############################################################################### +# is_one('-') + +is ($class->new(1)->is_one('-'),0); +is ($class->new(-1)->is_one('-'),1); +is ($class->new(1)->is_one(),1); +is ($class->new(-1)->is_one(),0); + +############################################################################### +# [perl #30609] bug with $x -= $x not being 0, but 2*$x + +$x = $class->new(3); $x -= $x; is ($x, 0); +$x = $class->new(-3); $x -= $x; is ($x, 0); +$x = $class->new('NaN'); $x -= $x; is ($x->is_nan(), 1); +$x = $class->new('inf'); $x -= $x; is ($x->is_nan(), 1); +$x = $class->new('-inf'); $x -= $x; is ($x->is_nan(), 1); + +$x = $class->new('NaN'); $x += $x; is ($x->is_nan(), 1); +$x = $class->new('inf'); $x += $x; is ($x->is_inf(), 1); +$x = $class->new('-inf'); $x += $x; is ($x->is_inf('-'), 1); +$x = $class->new(3); $x += $x; is ($x, 6); +$x = $class->new(-3); $x += $x; is ($x, -6); + +$x = $class->new(3); $x *= $x; is ($x, 9); +$x = $class->new(-3); $x *= $x; is ($x, 9); +$x = $class->new(3); $x /= $x; is ($x, 1); +$x = $class->new(-3); $x /= $x; is ($x, 1); +$x = $class->new(3); $x %= $x; is ($x, 0); +$x = $class->new(-3); $x %= $x; is ($x, 0); + +############################################################################### +# all tests done + +1; + +############################################################################### +# sub to check validity of a BigInt internally, to ensure that no op leaves a +# number object in an invalid state (f.i. "-0") + +sub is_valid + { + my ($x,$f) = @_; + + my $e = 0; # error? + + # allow the check to pass for all Lite, and all MBI and subclasses + # ok as reference? + $e = 'Not a reference to Math::BigInt' if ref($x) !~ /^Math::BigInt/; + + if (ref($x) ne 'Math::BigInt::Lite') + { + # has ok sign? + $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" + if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; + + $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; + $e = $CALC->_check($x->{value}) if $e eq '0'; + } + + # test done, see if error did crop up + is (1,1), return if ($e eq '0'); + + is (1,$e." after op '$f'"); + } + +__DATA__ +&.= +1234:-345:1234-345 +&+= +1:2:3 +-1:-2:-3 +&-= +1:2:-1 +-1:-2:1 +&*= +2:3:6 +-1:5:-5 +&%= +100:3:1 +8:9:8 +-629:5033:4404 +&/= +100:3:33 +-8:2:-4 +&|= +2:1:3 +&&= +5:7:5 +&^= +5:7:2 +&blog +NaNlog:2:NaN +122:NaNlog:NaN +NaNlog1:NaNlog:NaN +122:inf:NaN +inf:122:NaN +122:-inf:NaN +-inf:122:NaN +-inf:-inf:NaN +inf:inf:NaN +0:4:NaN +-21:4:NaN +21:-21:NaN +# normal results +1024:2:10 +81:3:4 +# 3.01.. truncate +82:3:4 +# 3.9... truncate +80:3:3 +15625:5:6 +15626:5:6 +15624:5:5 +1000:10:3 +10000:10:4 +100000:10:5 +1000000:10:6 +10000000:10:7 +100000000:10:8 +8916100448256:12:12 +8916100448257:12:12 +8916100448255:12:11 +2251799813685248:8:17 +72057594037927936:2:56 +144115188075855872:2:57 +288230376151711744:2:58 +576460752303423488:2:59 +4096:2:12 +1329227995784915872903807060280344576:2:120 +# $x == $base => result 1 +3:3:1 +# $x < $base => result 0 ($base ** 0 <= $x) +3:4:0 +# $x == 1 => result 0 +1:5:0 +&is_negative +0:0 +-1:1 +1:0 ++inf:0 +-inf:1 +NaNneg:0 +&is_positive +0:0 +-1:0 +1:1 ++inf:1 +-inf:0 +NaNneg:0 +&is_int +-inf:0 ++inf:0 +NaNis_int:0 +1:1 +0:1 +123e12:1 +&is_odd +abc:0 +0:0 +1:1 +3:1 +-1:1 +-3:1 +10000001:1 +10000002:0 +2:0 +120:0 +121:1 +&is_even +abc:0 +0:1 +1:0 +3:0 +-1:0 +-3:0 +10000001:0 +10000002:1 +2:1 +120:1 +121:0 +&bacmp ++0:-0:0 ++0:+1:-1 +-1:+1:0 ++1:-1:0 +-1:+2:-1 ++2:-1:1 +-123456789:+987654321:-1 ++123456789:-987654321:-1 ++987654321:+123456789:1 +-987654321:+123456789:1 +-123:+4567889:-1 +# NaNs +acmpNaN:123: +123:acmpNaN: +acmpNaN:acmpNaN: +# infinity ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:0 +-inf:+inf:0 ++inf:123:1 +-inf:123:1 ++inf:-123:1 +-inf:-123:1 +123:-inf:-1 +-123:inf:-1 +-123:-inf:-1 +123:inf:-1 +# return undef ++inf:NaN: +NaN:inf: +-inf:NaN: +NaN:-inf: +&bnorm +0e999:0 +0e-999:0 +-0e999:0 +-0e-999:0 +123:123 +# binary input +0babc:NaN +0b123:NaN +0b0:0 +-0b0:0 +-0b1:-1 +0b0001:1 +0b001:1 +0b011:3 +0b101:5 +0b1001:9 +0b10001:17 +0b100001:33 +0b1000001:65 +0b10000001:129 +0b100000001:257 +0b1000000001:513 +0b10000000001:1025 +0b100000000001:2049 +0b1000000000001:4097 +0b10000000000001:8193 +0b100000000000001:16385 +0b1000000000000001:32769 +0b10000000000000001:65537 +0b100000000000000001:131073 +0b1000000000000000001:262145 +0b10000000000000000001:524289 +0b100000000000000000001:1048577 +0b1000000000000000000001:2097153 +0b10000000000000000000001:4194305 +0b100000000000000000000001:8388609 +0b1000000000000000000000001:16777217 +0b10000000000000000000000001:33554433 +0b100000000000000000000000001:67108865 +0b1000000000000000000000000001:134217729 +0b10000000000000000000000000001:268435457 +0b100000000000000000000000000001:536870913 +0b1000000000000000000000000000001:1073741825 +0b10000000000000000000000000000001:2147483649 +0b100000000000000000000000000000001:4294967297 +0b1000000000000000000000000000000001:8589934593 +0b10000000000000000000000000000000001:17179869185 +0b__101:NaN +0b1_0_1:5 +0b0_0_0_1:1 +# hex input +-0x0:0 +0xabcdefgh:NaN +0x1234:4660 +0xabcdef:11259375 +-0xABCDEF:-11259375 +-0x1234:-4660 +0x12345678:305419896 +0x1_2_3_4_56_78:305419896 +0xa_b_c_d_e_f:11259375 +0x__123:NaN +0x9:9 +0x11:17 +0x21:33 +0x41:65 +0x81:129 +0x101:257 +0x201:513 +0x401:1025 +0x801:2049 +0x1001:4097 +0x2001:8193 +0x4001:16385 +0x8001:32769 +0x10001:65537 +0x20001:131073 +0x40001:262145 +0x80001:524289 +0x100001:1048577 +0x200001:2097153 +0x400001:4194305 +0x800001:8388609 +0x1000001:16777217 +0x2000001:33554433 +0x4000001:67108865 +0x8000001:134217729 +0x10000001:268435457 +0x20000001:536870913 +0x40000001:1073741825 +0x80000001:2147483649 +0x100000001:4294967297 +0x200000001:8589934593 +0x400000001:17179869185 +0x800000001:34359738369 +# bug found by Mark Lakata in Calc.pm creating too big one-element numbers in _from_hex() +0x2dd59e18a125dbed30a6ab1d93e9c855569f44f75806f0645dc9a2e98b808c3:1295719234436071846486578237372801883390756472611551858964079371952886122691 +# inf input +inf:inf ++inf:inf +-inf:-inf +0inf:NaN +# abnormal input +:NaN +abc:NaN + 1 a:NaN +1bcd2:NaN +11111b:NaN ++1z:NaN +-1z:NaN +# only one underscore between two digits +_123:NaN +_123_:NaN +123_:NaN +1__23:NaN +1E1__2:NaN +1_E12:NaN +1E_12:NaN +1_E_12:NaN ++_1E12:NaN ++0_1E2:100 ++0_0_1E2:100 +-0_0_1E2:-100 +-0_0_1E+0_0_2:-100 +E1:NaN +E23:NaN +1.23E1:NaN +1.23E-1:NaN +# bug with two E's in number being valid +1e2e3:NaN +1e2r:NaN +1e2.0:NaN +# bug with two '.' in number being valid +1.2.2:NaN +1.2.3e1:NaN +-1.2.3:NaN +-1.2.3e-4:NaN +1.2e3.4:NaN +1.2e-3.4:NaN +1.2.3.4:NaN +1.2.t:NaN +1..2:NaN +1..2e1:NaN +1..2e1..1:NaN +12e1..1:NaN +..2:NaN +.-2:NaN +# leading zeros +012:12 +0123:123 +01234:1234 +012345:12345 +0123456:123456 +01234567:1234567 +012345678:12345678 +0123456789:123456789 +01234567891:1234567891 +012345678912:12345678912 +0123456789123:123456789123 +01234567891234:1234567891234 +# some inputs that result in zero +0e0:0 ++0e0:0 ++0e+0:0 +-0e+0:0 +0e-0:0 +-0e-0:0 ++0e-0:0 +000:0 +00e2:0 +00e02:0 +000e002:0 +000e1230:0 +00e-3:0 +00e+3:0 +00e-03:0 +00e+03:0 +-000:0 +-00e2:0 +-00e02:0 +-000e002:0 +-000e1230:0 +-00e-3:0 +-00e+3:0 +-00e-03:0 +-00e+03:0 +# normal input +0:0 ++0:0 ++00:0 ++000:0 +000000000000000000:0 +-0:0 +-0000:0 ++1:1 ++01:1 ++001:1 ++00000100000:100000 +123456789:123456789 +-1:-1 +-01:-1 +-001:-1 +-123456789:-123456789 +-00000100000:-100000 +1_2_3:123 +10000000000E-1_0:1 +1E2:100 +1E1:10 +1E0:1 +1.23E2:123 +100E-1:10 +# floating point input +# .2e2:20 +1.E3:1000 +1.01E2:101 +1010E-1:101 +-1010E0:-1010 +-1010E1:-10100 +1234.00:1234 +# non-integer numbers +-1010E-2:NaN +-1.01E+1:NaN +-1.01E-1:NaN +1E-999999:NaN +0.5:NaN +&bnan +1:NaN +2:NaN +abc:NaN +&bone +2:+:1 +2:-:-1 +boneNaN:-:-1 +boneNaN:+:1 +2:abc:1 +3::1 +&binf +1:+:inf +2:-:-inf +3:abc:inf +&is_nan +123:0 +abc:1 +NaN:1 +-123:0 +&is_inf ++inf::1 +-inf::1 +abc::0 +1::0 +NaN::0 +-1::0 ++inf:-:0 ++inf:+:1 +-inf:-:1 +-inf:+:0 +-inf:-inf:1 +-inf:+inf:0 ++inf:-inf:0 ++inf:+inf:1 +# it must be exactly /^[+-]inf$/ ++infinity::0 +-infinity::0 +&blsft +abc:abc:NaN ++2:+2:8 ++1:+32:4294967296 ++1:+48:281474976710656 ++8:-2:NaN +# exercise base 10 ++12345:4:10:123450000 +-1234:0:10:-1234 ++1234:0:10:1234 ++2:2:10:200 ++12:2:10:1200 ++1234:-3:10:NaN +1234567890123:12:10:1234567890123000000000000 +-3:1:2:-6 +-5:1:2:-10 +-2:1:2:-4 +-102533203:1:2:-205066406 +&brsft +abc:abc:NaN ++8:+2:2 ++4294967296:+32:1 ++281474976710656:+48:1 ++2:-2:NaN +# exercise base 10 +-1234:0:10:-1234 ++1234:0:10:1234 ++200:2:10:2 ++1234:3:10:1 ++1234:2:10:12 ++1234:-3:10:NaN +310000:4:10:31 +12300000:5:10:123 +1230000000000:10:10:123 +09876123456789067890:12:10:9876123 +1234561234567890123:13:10:123456 +820265627:1:2:410132813 +# test shifting negative numbers in base 2 +-15:1:2:-8 +-14:1:2:-7 +-13:1:2:-7 +-12:1:2:-6 +-11:1:2:-6 +-10:1:2:-5 +-9:1:2:-5 +-8:1:2:-4 +-7:1:2:-4 +-6:1:2:-3 +-5:1:2:-3 +-4:1:2:-2 +-3:1:2:-2 +-2:1:2:-1 +-1:1:2:-1 +-1640531254:2:2:-410132814 +-1640531254:1:2:-820265627 +-820265627:1:2:-410132814 +-205066405:1:2:-102533203 +&bsstr ++inf:inf +-inf:-inf +1e+34:1e+34 +123.456E3:123456e+0 +100:1e+2 +bsstrabc:NaN +-5:-5e+0 +-100:-1e+2 +&numify +numifyabc:NaN ++inf:inf +-inf:-inf +5:5 +-5:-5 +100:100 +-100:-100 +&bneg +bnegNaN:NaN ++inf:-inf +-inf:inf +abd:NaN +0:0 +1:-1 +-1:1 ++123456789:-123456789 +-123456789:123456789 +&babs +babsNaN:NaN ++inf:inf +-inf:inf +0:0 +1:1 +-1:1 ++123456789:123456789 +-123456789:123456789 +&bsgn +NaN:NaN ++inf:1 +-inf:-1 +0:0 ++123456789:1 +-123456789:-1 +&bcmp +bcmpNaN:bcmpNaN: +bcmpNaN:0: +0:bcmpNaN: +0:0:0 +-1:0:-1 +0:-1:1 +1:0:1 +0:1:-1 +-1:1:-1 +1:-1:1 +-1:-1:0 +1:1:0 +123:123:0 +123:12:1 +12:123:-1 +-123:-123:0 +-123:-12:-1 +-12:-123:1 +123:124:-1 +124:123:1 +-123:-124:1 +-124:-123:-1 +100:5:1 +-123456789:987654321:-1 ++123456789:-987654321:1 +-987654321:123456789:-1 +-inf:5432112345:-1 ++inf:5432112345:1 +-inf:-5432112345:-1 ++inf:-5432112345:1 ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:1 +-inf:+inf:-1 +5:inf:-1 +5:inf:-1 +-5:-inf:1 +-5:-inf:1 +# return undef ++inf:NaN: +NaN:inf: +-inf:NaN: +NaN:-inf: +&binc +abc:NaN ++inf:inf +-inf:-inf ++0:1 ++1:2 +-1:0 +&bdec +abc:NaN ++inf:inf +-inf:-inf ++0:-1 ++1:0 +-1:-2 +&badd +abc:abc:NaN +abc:0:NaN ++0:abc:NaN ++inf:-inf:NaN +-inf:+inf:NaN ++inf:+inf:inf +-inf:-inf:-inf +baddNaN:+inf:NaN +baddNaN:+inf:NaN ++inf:baddNaN:NaN +-inf:baddNaN:NaN +0:0:0 +1:0:1 +0:1:1 +1:1:2 +-1:0:-1 +0:-1:-1 +-1:-1:-2 +-1:+1:0 ++1:-1:0 ++9:+1:10 ++99:+1:100 ++999:+1:1000 ++9999:+1:10000 ++99999:+1:100000 ++999999:+1:1000000 ++9999999:+1:10000000 ++99999999:+1:100000000 ++999999999:+1:1000000000 ++9999999999:+1:10000000000 ++99999999999:+1:100000000000 ++10:-1:9 ++100:-1:99 ++1000:-1:999 ++10000:-1:9999 ++100000:-1:99999 ++1000000:-1:999999 ++10000000:-1:9999999 ++100000000:-1:99999999 ++1000000000:-1:999999999 ++10000000000:-1:9999999999 ++123456789:987654321:1111111110 +-123456789:987654321:864197532 +-123456789:-987654321:-1111111110 ++123456789:-987654321:-864197532 +-1:10001:10000 +-1:100001:100000 +-1:1000001:1000000 +-1:10000001:10000000 +-1:100000001:100000000 +-1:1000000001:1000000000 +-1:10000000001:10000000000 +-1:100000000001:100000000000 +-1:1000000000001:1000000000000 +-1:10000000000001:10000000000000 +-1:-10001:-10002 +-1:-100001:-100002 +-1:-1000001:-1000002 +-1:-10000001:-10000002 +-1:-100000001:-100000002 +-1:-1000000001:-1000000002 +-1:-10000000001:-10000000002 +-1:-100000000001:-100000000002 +-1:-1000000000001:-1000000000002 +-1:-10000000000001:-10000000000002 +&bsub +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:-inf:inf +-inf:+inf:-inf ++inf:+inf:NaN +-inf:-inf:NaN ++0:+0:0 ++1:+0:1 ++0:+1:-1 ++1:+1:0 +-1:+0:-1 ++0:-1:1 +-1:-1:0 +-1:+1:-2 ++1:-1:2 ++9:+1:8 ++99:+1:98 ++999:+1:998 ++9999:+1:9998 ++99999:+1:99998 ++999999:+1:999998 ++9999999:+1:9999998 ++99999999:+1:99999998 ++999999999:+1:999999998 ++9999999999:+1:9999999998 ++99999999999:+1:99999999998 ++10:-1:11 ++100:-1:101 ++1000:-1:1001 ++10000:-1:10001 ++100000:-1:100001 ++1000000:-1:1000001 ++10000000:-1:10000001 ++100000000:-1:100000001 ++1000000000:-1:1000000001 ++10000000000:-1:10000000001 ++123456789:+987654321:-864197532 +-123456789:+987654321:-1111111110 +-123456789:-987654321:864197532 ++123456789:-987654321:1111111110 +10001:1:10000 +100001:1:100000 +1000001:1:1000000 +10000001:1:10000000 +100000001:1:100000000 +1000000001:1:1000000000 +10000000001:1:10000000000 +100000000001:1:100000000000 +1000000000001:1:1000000000000 +10000000000001:1:10000000000000 +10001:-1:10002 +100001:-1:100002 +1000001:-1:1000002 +10000001:-1:10000002 +100000001:-1:100000002 +1000000001:-1:1000000002 +10000000001:-1:10000000002 +100000000001:-1:100000000002 +1000000000001:-1:1000000000002 +10000000000001:-1:10000000000002 +&bmuladd +abc:abc:0:NaN +abc:+0:0:NaN ++0:abc:0:NaN ++0:0:abc:NaN +NaNmul:+inf:0:NaN +NaNmul:-inf:0:NaN +-inf:NaNmul:0:NaN ++inf:NaNmul:0:NaN ++inf:+inf:0:inf ++inf:-inf:0:-inf +-inf:+inf:0:-inf +-inf:-inf:0:inf ++0:+0:0:0 ++0:+1:0:0 ++1:+0:0:0 ++0:-1:0:0 +-1:+0:0:0 +123456789123456789:0:0:0 +0:123456789123456789:0:0 +-1:-1:0:1 +-1:-1:0:1 +-1:+1:0:-1 ++1:-1:0:-1 ++1:+1:0:1 ++2:+3:0:6 +-2:+3:0:-6 ++2:-3:0:-6 +-2:-3:0:6 +111:111:0:12321 +10101:10101:0:102030201 +1001001:1001001:0:1002003002001 +100010001:100010001:0:10002000300020001 +10000100001:10000100001:0:100002000030000200001 +11111111111:9:0:99999999999 +22222222222:9:0:199999999998 +33333333333:9:0:299999999997 +44444444444:9:0:399999999996 +55555555555:9:0:499999999995 +66666666666:9:0:599999999994 +77777777777:9:0:699999999993 +88888888888:9:0:799999999992 +99999999999:9:0:899999999991 +11111111111:9:1:100000000000 +22222222222:9:1:199999999999 +33333333333:9:1:299999999998 +44444444444:9:1:399999999997 +55555555555:9:1:499999999996 +66666666666:9:1:599999999995 +77777777777:9:1:699999999994 +88888888888:9:1:799999999993 +99999999999:9:1:899999999992 +-3:-4:-5:7 +3:-4:-5:-17 +-3:4:-5:-17 +3:4:-5:7 +-3:4:5:-7 +3:-4:5:-7 +9999999999999999999:10000000000000000000:1234567890:99999999999999999990000000001234567890 +2:3:12345678901234567890:12345678901234567896 +&bmul +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN +NaNmul:+inf:NaN +NaNmul:-inf:NaN +-inf:NaNmul:NaN ++inf:NaNmul:NaN ++inf:+inf:inf ++inf:-inf:-inf +-inf:+inf:-inf +-inf:-inf:inf ++0:+0:0 ++0:+1:0 ++1:+0:0 ++0:-1:0 +-1:+0:0 +123456789123456789:0:0 +0:123456789123456789:0 +-1:-1:1 +-1:+1:-1 ++1:-1:-1 ++1:+1:1 ++2:+3:6 +-2:+3:-6 ++2:-3:-6 +-2:-3:6 +111:111:12321 +10101:10101:102030201 +1001001:1001001:1002003002001 +100010001:100010001:10002000300020001 +10000100001:10000100001:100002000030000200001 +11111111111:9:99999999999 +22222222222:9:199999999998 +33333333333:9:299999999997 +44444444444:9:399999999996 +55555555555:9:499999999995 +66666666666:9:599999999994 +77777777777:9:699999999993 +88888888888:9:799999999992 +99999999999:9:899999999991 ++25:+25:625 ++12345:+12345:152399025 ++99999:+11111:1111088889 +9999:10000:99990000 +99999:100000:9999900000 +999999:1000000:999999000000 +9999999:10000000:99999990000000 +99999999:100000000:9999999900000000 +999999999:1000000000:999999999000000000 +9999999999:10000000000:99999999990000000000 +99999999999:100000000000:9999999999900000000000 +999999999999:1000000000000:999999999999000000000000 +9999999999999:10000000000000:99999999999990000000000000 +99999999999999:100000000000000:9999999999999900000000000000 +999999999999999:1000000000000000:999999999999999000000000000000 +9999999999999999:10000000000000000:99999999999999990000000000000000 +99999999999999999:100000000000000000:9999999999999999900000000000000000 +999999999999999999:1000000000000000000:999999999999999999000000000000000000 +9999999999999999999:10000000000000000000:99999999999999999990000000000000000000 +&bdiv-list +100:20:5,0 +4095:4095:1,0 +-4095:-4095:1,0 +4095:-4095:-1,0 +-4095:4095:-1,0 +123:2:61,1 +9:5:1,4 +9:4:2,1 +# inf handling and general remainder +5:8:0,5 +0:8:0,0 +11:2:5,1 +11:-2:-6,-1 +-11:2:-6,1 +# see table in documentation in MBI +0:inf:0,0 +0:-inf:0,0 +5:inf:0,5 +5:-inf:-1,-inf +-5:inf:-1,inf +-5:-inf:0,-5 +inf:5:inf,NaN +-inf:5:-inf,NaN +inf:-5:-inf,NaN +-inf:-5:inf,NaN +5:5:1,0 +-5:-5:1,0 +inf:inf:NaN,NaN +-inf:-inf:NaN,NaN +-inf:inf:NaN,NaN +inf:-inf:NaN,NaN +8:0:inf,8 +inf:0:inf,inf +# exceptions to remainder rule +-8:0:-inf,-8 +-inf:0:-inf,-inf +0:0:NaN,0 +# test the shortcut in Calc if @$x == @$yorg +1234567812345678:123456712345678:10,688888898 +12345671234567:1234561234567:10,58888897 +123456123456:12345123456:10,4888896 +1234512345:123412345:10,388895 +1234567890999999999:1234567890:1000000000,999999999 +1234567890000000000:1234567890:1000000000,0 +1234567890999999999:9876543210:124999998,9503086419 +1234567890000000000:9876543210:124999998,8503086420 +96969696969696969696969696969678787878626262626262626262626262:484848484848484848484848486666666666666689898989898989898989:199,484848484848484848484848123012121211954972727272727272727451 +# bug in v1.76 +1267650600228229401496703205375:1267650600228229401496703205376:0,1267650600228229401496703205375 +# exercise shortcut for numbers of the same length in div +999999999999999999999999999999999:999999999999999999999999999999999:1,0 +999999999999999999999999999999999:888888888888888888888888888888888:1,111111111111111111111111111111111 +999999999999999999999999999999999:777777777777777777777777777777777:1,222222222222222222222222222222222 +999999999999999999999999999999999:666666666666666666666666666666666:1,333333333333333333333333333333333 +999999999999999999999999999999999:555555555555555555555555555555555:1,444444444444444444444444444444444 +999999999999999999999999999999999:444444444444444444444444444444444:2,111111111111111111111111111111111 +999999999999999999999999999999999:333333333333333333333333333333333:3,0 +999999999999999999999999999999999:222222222222222222222222222222222:4,111111111111111111111111111111111 +999999999999999999999999999999999:111111111111111111111111111111111:9,0 +9999999_9999999_9999999_9999999:3333333_3333333_3333333_3333333:3,0 +9999999_9999999_9999999_9999999:3333333_0000000_0000000_0000000:3,999999999999999999999 +9999999_9999999_9999999_9999999:3000000_0000000_0000000_0000000:3,999999999999999999999999999 +9999999_9999999_9999999_9999999:2000000_0000000_0000000_0000000:4,1999999999999999999999999999 +9999999_9999999_9999999_9999999:1000000_0000000_0000000_0000000:9,999999999999999999999999999 +9999999_9999999_9999999_9999999:100000_0000000_0000000_0000000:99,99999999999999999999999999 +9999999_9999999_9999999_9999999:10000_0000000_0000000_0000000:999,9999999999999999999999999 +9999999_9999999_9999999_9999999:1000_0000000_0000000_0000000:9999,999999999999999999999999 +9999999_9999999_9999999_9999999:100_0000000_0000000_0000000:99999,99999999999999999999999 +9999999_9999999_9999999_9999999:10_0000000_0000000_0000000:999999,9999999999999999999999 +9999999_9999999_9999999_9999999:1_0000000_0000000_0000000:9999999,999999999999999999999 +&bdiv +abc:abc:NaN +abc:1:NaN +1:abc:NaN +0:0:NaN +# inf handling (see table in doc) +0:inf:0 +0:-inf:0 +5:inf:0 +5:-inf:-1 +-5:inf:-1 +-5:-inf:0 +inf:5:inf +-inf:5:-inf +inf:-5:-inf +-inf:-5:inf +5:5:1 +-5:-5:1 +inf:inf:NaN +-inf:-inf:NaN +-inf:inf:NaN +inf:-inf:NaN +8:0:inf +inf:0:inf +-8:0:-inf +-inf:0:-inf +0:0:NaN +11:2:5 +-11:-2:5 +-11:2:-6 +11:-2:-6 +0:1:0 +0:-1:0 +1:1:1 +-1:-1:1 +1:-1:-1 +-1:1:-1 +1:2:0 +2:1:2 +1:26:0 +1000000000:9:111111111 +2000000000:9:222222222 +3000000000:9:333333333 +4000000000:9:444444444 +5000000000:9:555555555 +6000000000:9:666666666 +7000000000:9:777777777 +8000000000:9:888888888 +9000000000:9:1000000000 +35500000:113:314159 +71000000:226:314159 +106500000:339:314159 +1000000000:3:333333333 ++10:+5:2 ++100:+4:25 ++1000:+8:125 ++10000:+16:625 +999999999999:9:111111111111 +999999999999:99:10101010101 +999999999999:999:1001001001 +999999999999:9999:100010001 +999999999999999:99999:10000100001 ++1111088889:99999:11111 +-5:-3:1 +-5:3:-2 +4:3:1 +4:-3:-2 +1:3:0 +1:-3:-1 +-2:-3:0 +-2:3:-1 +8:3:2 +-8:3:-3 +14:-3:-5 +-14:3:-5 +-14:-3:4 +14:3:4 +# bug in Calc with '99999' vs $BASE-1 +10000000000000000000000000000000000000000000000000000000000000000000000000000000000:10000000375084540248994272022843165711074:999999962491547381984643365663244474111576 +# test the shortcut in Calc if @$x == @$yorg +1234567812345678:123456712345678:10 +12345671234567:1234561234567:10 +123456123456:12345123456:10 +1234512345:123412345:10 +1234567890999999999:1234567890:1000000000 +1234567890000000000:1234567890:1000000000 +1234567890999999999:9876543210:124999998 +1234567890000000000:9876543210:124999998 +96969696969696969696969696969678787878626262626262626262626262:484848484848484848484848486666666666666689898989898989898989:199 +# bug up to v0.35 in Calc (--$q one too many) +84696969696969696956565656566184292929292929292847474747436308080808080808086765396464646464646465:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999999 +84696969696969696943434343434871161616161616161452525252486813131313131313143230042929292929292930:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999998 +84696969696969696969696969697497424242424242424242424242385803030303030303030300750000000000000000:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6450000000000000000 +84696969696969696930303030303558030303030303030057575757537318181818181818199694689393939393939395:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999997 +# exercise shortcut for numbers of the same length in div +999999999999999999999999999999999:999999999999999999999999999999999:1 +999999999999999999999999999999999:888888888888888888888888888888888:1 +999999999999999999999999999999999:777777777777777777777777777777777:1 +999999999999999999999999999999999:666666666666666666666666666666666:1 +999999999999999999999999999999999:555555555555555555555555555555555:1 +999999999999999999999999999999999:444444444444444444444444444444444:2 +999999999999999999999999999999999:333333333333333333333333333333333:3 +999999999999999999999999999999999:222222222222222222222222222222222:4 +999999999999999999999999999999999:111111111111111111111111111111111:9 +9999999_9999999_9999999_9999999:3333333_3333333_3333333_3333333:3 +9999999_9999999_9999999_9999999:3333333_0000000_0000000_0000000:3 +9999999_9999999_9999999_9999999:3000000_0000000_0000000_0000000:3 +9999999_9999999_9999999_9999999:2000000_0000000_0000000_0000000:4 +9999999_9999999_9999999_9999999:1000000_0000000_0000000_0000000:9 +9999999_9999999_9999999_9999999:100000_0000000_0000000_0000000:99 +9999999_9999999_9999999_9999999:10000_0000000_0000000_0000000:999 +9999999_9999999_9999999_9999999:1000_0000000_0000000_0000000:9999 +9999999_9999999_9999999_9999999:100_0000000_0000000_0000000:99999 +9999999_9999999_9999999_9999999:10_0000000_0000000_0000000:999999 +9999999_9999999_9999999_9999999:1_0000000_0000000_0000000:9999999 +# bug with shortcut in Calc 0.44 +949418181818187070707070707070707070:181818181853535353535353535353535353:5 +&bmodinv +# format: number:modulus:result +# bmodinv Data errors +abc:abc:NaN +abc:5:NaN +5:abc:NaN +# bmodinv Expected Results from normal use +1:5:1 +3:5:2 +3:-5:-3 +-2:5:2 +8:5033:4404 +1234567891:13:6 +-1234567891:13:7 +324958749843759385732954874325984357439658735983745:2348249874968739:1741662881064902 +-2:1:0 +-1:1:0 +0:1:0 +1:1:0 +2:1:0 +3:1:0 +4:1:0 +-2:3:1 +-1:3:2 +0:3:NaN +1:3:1 +2:3:2 +3:3:NaN +4:3:1 +-2:4:NaN +-1:4:3 +0:4:NaN +1:4:1 +2:4:NaN +3:4:3 +4:4:NaN +## bmodinv Error cases / useless use of function +inf:5:NaN +5:inf:NaN +-inf:5:NaN +5:-inf:NaN +&bmodpow +# format: number:exponent:modulus:result +# bmodpow Data errors +abc:abc:abc:NaN +5:abc:abc:NaN +abc:5:abc:NaN +abc:abc:5:NaN +5:5:abc:NaN +5:abc:5:NaN +abc:5:5:NaN +3:5:0:3 +# bmodpow Expected results +0:0:2:1 +1:0:2:1 +0:3:5:0 +-2:-2:1:0 +-1:-2:1:0 +0:-2:1:0 +1:-2:1:0 +2:-2:1:0 +3:-2:1:0 +4:-2:1:0 +-2:-1:1:0 +-1:-1:1:0 +0:-1:1:0 +1:-1:1:0 +2:-1:1:0 +3:-1:1:0 +4:-1:1:0 +-2:0:1:0 +-1:0:1:0 +0:0:1:0 +1:0:1:0 +2:0:1:0 +3:0:1:0 +4:0:1:0 +-2:1:1:0 +-1:1:1:0 +0:1:1:0 +1:1:1:0 +2:1:1:0 +3:1:1:0 +4:1:1:0 +-2:2:1:0 +-1:2:1:0 +0:2:1:0 +1:2:1:0 +2:2:1:0 +3:2:1:0 +4:2:1:0 +-2:3:1:0 +-1:3:1:0 +0:3:1:0 +1:3:1:0 +2:3:1:0 +3:3:1:0 +4:3:1:0 +-2:4:1:0 +-1:4:1:0 +0:4:1:0 +1:4:1:0 +2:4:1:0 +3:4:1:0 +4:4:1:0 +-2:-2:3:1 +-1:-2:3:1 +0:-2:3:NaN +1:-2:3:1 +2:-2:3:1 +3:-2:3:NaN +4:-2:3:1 +-2:-1:3:1 +-1:-1:3:2 +0:-1:3:NaN +1:-1:3:1 +2:-1:3:2 +3:-1:3:NaN +4:-1:3:1 +-2:0:3:1 +-1:0:3:1 +0:0:3:1 +1:0:3:1 +2:0:3:1 +3:0:3:1 +4:0:3:1 +-2:1:3:1 +-1:1:3:2 +0:1:3:0 +1:1:3:1 +2:1:3:2 +3:1:3:0 +4:1:3:1 +-2:2:3:1 +-1:2:3:1 +0:2:3:0 +1:2:3:1 +2:2:3:1 +3:2:3:0 +4:2:3:1 +-2:3:3:1 +-1:3:3:2 +0:3:3:0 +1:3:3:1 +2:3:3:2 +3:3:3:0 +4:3:3:1 +-2:4:3:1 +-1:4:3:1 +0:4:3:0 +1:4:3:1 +2:4:3:1 +3:4:3:0 +4:4:3:1 +-2:-2:4:NaN +-1:-2:4:1 +0:-2:4:NaN +1:-2:4:1 +2:-2:4:NaN +3:-2:4:1 +4:-2:4:NaN +-2:-1:4:NaN +-1:-1:4:3 +0:-1:4:NaN +1:-1:4:1 +2:-1:4:NaN +3:-1:4:3 +4:-1:4:NaN +-2:0:4:1 +-1:0:4:1 +0:0:4:1 +1:0:4:1 +2:0:4:1 +3:0:4:1 +4:0:4:1 +-2:1:4:2 +-1:1:4:3 +0:1:4:0 +1:1:4:1 +2:1:4:2 +3:1:4:3 +4:1:4:0 +-2:2:4:0 +-1:2:4:1 +0:2:4:0 +1:2:4:1 +2:2:4:0 +3:2:4:1 +4:2:4:0 +-2:3:4:0 +-1:3:4:3 +0:3:4:0 +1:3:4:1 +2:3:4:0 +3:3:4:3 +4:3:4:0 +-2:4:4:0 +-1:4:4:1 +0:4:4:0 +1:4:4:1 +2:4:4:0 +3:4:4:1 +4:4:4:0 +8:-1:16:NaN +8:-1:5033:4404 +8:7:5032:3840 +8:8:-5:-4 +1e50:1:1:0 +98436739867439843769485798542749827593285729587325:43698764986460981048259837659386739857456983759328457:6943857329857295827698367:3104744730915914415259518 +# bmodpow Error cases +inf:5:13:NaN +5:inf:13:NaN +&bmod +# inf handling, see table in doc +0:inf:0 +0:-inf:0 +5:inf:5 +5:-inf:-inf +-5:inf:inf +-5:-inf:-5 +inf:5:NaN +-inf:5:NaN +inf:-5:NaN +-inf:-5:NaN +5:5:0 +-5:-5:0 +inf:inf:NaN +-inf:-inf:NaN +-inf:inf:NaN +inf:-inf:NaN +8:0:8 +inf:0:inf +-inf:0:-inf +-8:0:-8 +0:0:0 +abc:abc:NaN +abc:1:abc:NaN +1:abc:NaN +0:1:0 +1:0:1 +0:-1:0 +-1:0:-1 +1:1:0 +-1:-1:0 +1:-1:0 +-1:1:0 +1:2:1 +2:1:0 +1000000000:9:1 +2000000000:9:2 +3000000000:9:3 +4000000000:9:4 +5000000000:9:5 +6000000000:9:6 +7000000000:9:7 +8000000000:9:8 +9000000000:9:0 +35500000:113:33 +71000000:226:66 +106500000:339:99 +1000000000:3:1 +10:5:0 +100:4:0 +1000:8:0 +10000:16:0 +999999999999:9:0 +999999999999:99:0 +999999999999:999:0 +999999999999:9999:0 +999999999999999:99999:0 +-9:+5:1 ++9:-5:-1 +-9:-5:-4 +-5:3:1 +-2:3:1 +4:3:1 +1:3:1 +-5:-3:-2 +-2:-3:-2 +4:-3:-2 +1:-3:-2 +4095:4095:0 +100041000510123:3:0 +152403346:12345:4321 +9:5:4 +# test shortcuts in Calc +# 1ex % 9 is always == 1, 1ex % 113 is != 1 for x = (4..9), 1ex % 10 = 0 +1234:9:1 +123456:9:3 +12345678:9:0 +1234567891:9:1 +123456789123:9:6 +12345678912345:9:6 +1234567891234567:9:1 +123456789123456789:9:0 +1234:10:4 +123456:10:6 +12345678:10:8 +1234567891:10:1 +123456789123:10:3 +12345678912345:10:5 +1234567891234567:10:7 +123456789123456789:10:9 +1234:113:104 +123456:113:60 +12345678:113:89 +1234567891:113:64 +123456789123:113:95 +12345678912345:113:53 +1234567891234567:113:56 +123456789123456789:113:39 +# bug in bmod() not modifying the variable in place +-629:5033:4404 +# bug in bmod() in Calc in the _div_use_div() shortcut code path, +# when X == X and X was big +111111111111111111111111111111:111111111111111111111111111111:0 +12345678901234567890:12345678901234567890:0 +&bgcd +inf:12:NaN +-inf:12:NaN +12:inf:NaN +12:-inf:NaN +inf:inf:NaN +inf:-inf:NaN +-inf:-inf:NaN +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++0:+0:0 ++0:+1:1 ++1:+0:1 ++1:+1:1 ++2:+3:1 ++3:+2:1 +-3:+2:1 +-3:-2:1 +-144:-60:12 +144:-60:12 +144:60:12 +100:625:25 +4096:81:1 +1034:804:2 +27:90:56:1 +27:90:54:9 +&blcm +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++0:+0:NaN ++1:+0:0 ++0:+1:0 ++27:+90:270 ++1034:+804:415668 +&band +abc:abc:NaN +abc:0:NaN +0:abc:NaN +1:2:0 +3:2:2 ++8:+2:0 ++281474976710656:0:0 ++281474976710656:1:0 ++281474976710656:+281474976710656:281474976710656 +281474976710656:-1:281474976710656 +-2:-3:-4 +-1:-1:-1 +-6:-6:-6 +-7:-4:-8 +-7:4:0 +-4:7:4 +# negative argument is bitwise shorter than positive [perl #26559] +30:-3:28 +123:-1:123 +# equal arguments are treated special, so also do some test with unequal ones +0xFFFF:0xFFFF:0x0xFFFF +0xFFFFFF:0xFFFFFF:0x0xFFFFFF +0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF +0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF +0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF +0xF0F0:0xF0F0:0x0xF0F0 +0x0F0F:0x0F0F:0x0x0F0F +0xF0F0F0:0xF0F0F0:0x0xF0F0F0 +0x0F0F0F:0x0F0F0F:0x0x0F0F0F +0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 +0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F +0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 +0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F +0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 +0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F +0x1F0F0F0F0F0F:0x3F0F0F0F0F0F:0x0x1F0F0F0F0F0F +&bior +abc:abc:NaN +abc:0:NaN +0:abc:NaN +1:2:3 ++8:+2:10 ++281474976710656:0:281474976710656 ++281474976710656:1:281474976710657 ++281474976710656:281474976710656:281474976710656 +-2:-3:-1 +-1:-1:-1 +-6:-6:-6 +-7:4:-3 +-4:7:-1 ++281474976710656:-1:-1 +30:-3:-1 +30:-4:-2 +300:-76:-68 +-76:300:-68 +# equal arguments are treated special, so also do some test with unequal ones +0xFFFF:0xFFFF:0x0xFFFF +0xFFFFFF:0xFFFFFF:0x0xFFFFFF +0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF +0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF +0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF +0:0xFFFF:0x0xFFFF +0:0xFFFFFF:0x0xFFFFFF +0:0xFFFFFFFF:0x0xFFFFFFFF +0:0xFFFFFFFFFF:0x0xFFFFFFFFFF +0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF +0xFFFF:0:0x0xFFFF +0xFFFFFF:0:0x0xFFFFFF +0xFFFFFFFF:0:0x0xFFFFFFFF +0xFFFFFFFFFF:0:0x0xFFFFFFFFFF +0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF +0xF0F0:0xF0F0:0x0xF0F0 +0x0F0F:0x0F0F:0x0x0F0F +0xF0F0:0x0F0F:0x0xFFFF +0xF0F0F0:0xF0F0F0:0x0xF0F0F0 +0x0F0F0F:0x0F0F0F:0x0x0F0F0F +0x0F0F0F:0xF0F0F0:0x0xFFFFFF +0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 +0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F +0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF +0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 +0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F +0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF +0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 +0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F +0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF +0x1F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF +&bxor +abc:abc:NaN +abc:0:NaN +0:abc:NaN +1:2:3 ++8:+2:10 ++281474976710656:0:281474976710656 ++281474976710656:1:281474976710657 ++281474976710656:281474976710656:0 +-2:-3:3 +-1:-1:0 +-6:-6:0 +-7:4:-3 +-4:7:-5 +4:-7:-3 +-4:-7:5 +30:-3:-29 +30:-4:-30 +300:-76:-360 +-76:300:-360 +# equal arguments are treated special, so also do some test with unequal ones +0xFFFF:0xFFFF:0 +0xFFFFFF:0xFFFFFF:0 +0xFFFFFFFF:0xFFFFFFFF:0 +0xFFFFFFFFFF:0xFFFFFFFFFF:0 +0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0 +0:0xFFFF:0x0xFFFF +0:0xFFFFFF:0x0xFFFFFF +0:0xFFFFFFFF:0x0xFFFFFFFF +0:0xFFFFFFFFFF:0x0xFFFFFFFFFF +0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF +0xFFFF:0:0x0xFFFF +0xFFFFFF:0:0x0xFFFFFF +0xFFFFFFFF:0:0x0xFFFFFFFF +0xFFFFFFFFFF:0:0x0xFFFFFFFFFF +0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF +0xF0F0:0xF0F0:0 +0x0F0F:0x0F0F:0 +0xF0F0:0x0F0F:0x0xFFFF +0xF0F0F0:0xF0F0F0:0 +0x0F0F0F:0x0F0F0F:0 +0x0F0F0F:0xF0F0F0:0x0xFFFFFF +0xF0F0F0F0:0xF0F0F0F0:0 +0x0F0F0F0F:0x0F0F0F0F:0 +0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF +0xF0F0F0F0F0:0xF0F0F0F0F0:0 +0x0F0F0F0F0F:0x0F0F0F0F0F:0 +0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF +0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0 +0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0 +0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF +&bnot +abc:NaN ++0:-1 ++8:-9 ++281474976710656:-281474976710657 +-1:0 +-2:1 +-12:11 +&digit +0:0:0 +12:0:2 +12:1:1 +123:0:3 +123:1:2 +123:2:1 +123:-1:1 +123:-2:2 +123:-3:3 +123456:0:6 +123456:1:5 +123456:2:4 +123456:3:3 +123456:4:2 +123456:5:1 +123456:-1:1 +123456:-2:2 +123456:-3:3 +100000:-3:0 +100000:0:0 +100000:1:0 +&mantissa +abc:NaN +1e4:1 +2e0:2 +123:123 +-1:-1 +-2:-2 ++inf:inf +-inf:-inf +&exponent +abc:NaN +1e4:4 +2e0:0 +123:0 +-1:0 +-2:0 +0:1 ++inf:inf +-inf:inf +&parts +abc:NaN,NaN +1e4:1,4 +2e0:2,0 +123:123,0 +-1:-1,0 +-2:-2,0 +0:0,1 ++inf:inf,inf +-inf:-inf,inf +&bfac +-1:NaN +NaNfac:NaN ++inf:inf +-inf:NaN +0:1 +1:1 +2:2 +3:6 +4:24 +5:120 +6:720 +7:5040 +8:40320 +9:362880 +10:3628800 +11:39916800 +12:479001600 +20:2432902008176640000 +22:1124000727777607680000 +69:171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000 +&bpow +abc:12:NaN +12:abc:NaN +0:0:1 +0:1:0 +0:2:0 +0:-1:inf +0:-2:inf +1:0:1 +1:1:1 +1:2:1 +1:3:1 +1:-1:1 +1:-2:1 +1:-3:1 +2:0:1 +2:1:2 +2:2:4 +2:3:8 +3:3:27 +-2:2:4 +-2:3:-8 +-2:4:16 +-2:5:-32 +2:-1:NaN +-2:-1:NaN +2:-2:NaN +-2:-2:NaN +# inf tests ++inf:1234500012:inf +-inf:1234500012:inf +-inf:1234500013:-inf ++inf:-12345000123:inf +-inf:-12345000123:-inf +# -inf * -inf = inf +-inf:2:inf +-inf:0:NaN +-inf:-1:0 +-inf:inf:NaN +2:inf:inf +2:-inf:0 +0:inf:0 +0:-inf:inf +-1:-inf:NaN +-1:inf:NaN +-2:inf:NaN +-2:-inf:0 +NaN:inf:NaN +NaN:-inf:NaN +-inf:NaN:NaN +inf:NaN:NaN +inf:-inf:NaN +1:inf:1 +1:-inf:1 +# 1 ** -x => 1 / (1 ** x) +-1:0:1 +-2:0:1 +-1:1:-1 +-1:2:1 +-1:3:-1 +-1:4:1 +-1:5:-1 +-1:-1:-1 +-1:-2:1 +-1:-3:-1 +-1:-4:1 +10:2:100 +10:3:1000 +10:4:10000 +10:5:100000 +10:6:1000000 +10:7:10000000 +10:8:100000000 +10:9:1000000000 +10:20:100000000000000000000 +123456:2:15241383936 +-2:2:4 +-2:3:-8 +-2:4:16 +-2:5:-32 +-3:2:9 +-3:3:-27 +-3:4:81 +-3:5:-243 +&length +100:3 +10:2 +1:1 +0:1 +12345:5 +10000000000000000:17 +-123:3 +215960156869840440586892398248:30 +&broot +# sqrt() ++0:2:0 ++1:2:1 +-1:2:NaN +# -$x ** (1/2) => -$y, but not in froot() +-123:2:NaN ++inf:2:inf +-inf:2:NaN +2:2:1 +-2:2:NaN +4:2:2 +9:2:3 +16:2:4 +100:2:10 +123:2:11 +15241:2:123 +144:2:12 +12:2:3 +# invalid ones +1:NaN:NaN +-1:NaN:NaN +0:NaN:NaN +-inf:NaN:NaN ++inf:NaN:NaN +NaN:0:NaN +NaN:2:NaN +NaN:inf:NaN +NaN:inf:NaN +12:-inf:NaN +12:inf:NaN ++0:0:NaN ++1:0:NaN +-1:0:NaN +-2:0:NaN +-123.45:0:NaN ++inf:0:NaN +12:1:12 +-12:1:NaN +8:-1:NaN +-8:-1:NaN +# cubic root +8:3:2 +-8:3:NaN +# fourths root +16:4:2 +81:4:3 +# 2 ** 64 +18446744073709551616:4:65536 +18446744073709551616:8:256 +18446744073709551616:16:16 +18446744073709551616:32:4 +18446744073709551616:64:2 +18446744073709551616:128:1 +# 213 ** 15 +84274086103068221283760416414557757:15:213 +# see t/bigroot.t for more tests +&bsqrt +145:12 +144:12 +143:11 +16:4 +170:13 +169:13 +168:12 +4:2 +3:1 +2:1 +9:3 +12:3 +256:16 +100000000:10000 +4000000000000:2000000 +152399026:12345 +152399025:12345 +152399024:12344 +# 2 ** 64 => 2 ** 32 +18446744073709551616:4294967296 +84274086103068221283760416414557757:290299993288095377 +1:1 +0:0 +-2:NaN +-123:NaN +Nan:NaN ++inf:inf +-inf:NaN +# see t/biglog.t for more tests +&bexp +NaN:NaN +inf:inf +1:2 +2:7 +&batan2 +NaN:1:10:NaN +NaN:NaN:10:NaN +1:NaN:10:NaN +inf:1:14:1 +-inf:1:14:-1 +0:-inf:14:3 +-1:-inf:14:-3 +1:-inf:14:3 +0:inf:14:0 +inf:-inf:14:2 +-inf:-inf:14:-2 +# +- 0.78.... +inf:+inf:14:0 +-inf:+inf:14:0 +1:5:13:0 +1:5:14:0 +0:0:10:0 +0:1:14:0 +0:2:14:0 +1:0:14:1 +5:0:14:1 +-1:0:11:-1 +-2:0:77:-1 +2:0:77:1 +-1:5:14:0 +1:5:14:0 +-1:8:14:0 +1:8:14:0 +-1:1:14:0 +&bpi +77:3 ++0:3 +11:3 +# see t/bignok.t for more tests +&bnok ++inf:10:inf +NaN:NaN:NaN +NaN:1:NaN +1:NaN:NaN +1:1:1 +# k > n +1:2:0 +2:3:0 +# k < 0 +1:-2:0 +# 7 over 3 = 35 +7:3:35 +7:6:7 +100:90:17310309456440 +100:95:75287520 +2:0:1 +7:0:1 +2:1:2 +&bround +$round_mode('trunc') +0:12:0 +NaNbround:12:NaN ++inf:12:inf +-inf:12:-inf +1234:0:1234 +1234:2:1200 +123456:4:123400 +123456:5:123450 +123456:6:123456 ++10123456789:5:10123000000 +-10123456789:5:-10123000000 ++10123456789:9:10123456700 +-10123456789:9:-10123456700 ++101234500:6:101234000 +-101234500:6:-101234000 +#+101234500:-4:101234000 +#-101234500:-4:-101234000 +$round_mode('zero') ++20123456789:5:20123000000 +-20123456789:5:-20123000000 ++20123456789:9:20123456800 +-20123456789:9:-20123456800 ++201234500:6:201234000 +-201234500:6:-201234000 +#+201234500:-4:201234000 +#-201234500:-4:-201234000 ++12345000:4:12340000 +-12345000:4:-12340000 +$round_mode('+inf') ++30123456789:5:30123000000 +-30123456789:5:-30123000000 ++30123456789:9:30123456800 +-30123456789:9:-30123456800 ++301234500:6:301235000 +-301234500:6:-301234000 +#+301234500:-4:301235000 +#-301234500:-4:-301234000 ++12345000:4:12350000 +-12345000:4:-12340000 +$round_mode('-inf') ++40123456789:5:40123000000 +-40123456789:5:-40123000000 ++40123456789:9:40123456800 +-40123456789:9:-40123456800 ++401234500:6:401234000 ++401234500:6:401234000 +#-401234500:-4:-401235000 +#-401234500:-4:-401235000 ++12345000:4:12340000 +-12345000:4:-12350000 +$round_mode('odd') ++50123456789:5:50123000000 +-50123456789:5:-50123000000 ++50123456789:9:50123456800 +-50123456789:9:-50123456800 ++501234500:6:501235000 +-501234500:6:-501235000 +#+501234500:-4:501235000 +#-501234500:-4:-501235000 ++12345000:4:12350000 +-12345000:4:-12350000 +$round_mode('even') ++60123456789:5:60123000000 +-60123456789:5:-60123000000 ++60123456789:9:60123456800 +-60123456789:9:-60123456800 ++601234500:6:601234000 +-601234500:6:-601234000 +#+601234500:-4:601234000 +#-601234500:-4:-601234000 +#-601234500:-9:0 +#-501234500:-9:0 +#-601234500:-8:0 +#-501234500:-8:0 ++1234567:7:1234567 ++1234567:6:1234570 ++12345000:4:12340000 +-12345000:4:-12340000 +$round_mode('common') ++60123456789:5:60123000000 ++60123199999:5:60123000000 ++60123299999:5:60123000000 ++60123399999:5:60123000000 ++60123499999:5:60123000000 ++60123500000:5:60124000000 ++60123600000:5:60124000000 ++60123700000:5:60124000000 ++60123800000:5:60124000000 ++60123900000:5:60124000000 +-60123456789:5:-60123000000 +-60123199999:5:-60123000000 +-60123299999:5:-60123000000 +-60123399999:5:-60123000000 +-60123499999:5:-60123000000 +-60123500000:5:-60124000000 +-60123600000:5:-60124000000 +-60123700000:5:-60124000000 +-60123800000:5:-60124000000 +-60123900000:5:-60124000000 +&is_zero +0:1 +NaNzero:0 ++inf:0 +-inf:0 +123:0 +-1:0 +1:0 +&is_one +0:0 +NaNone:0 ++inf:0 +-inf:0 +1:1 +2:0 +-1:0 +-2:0 +# floor, ceil, and int are pretty pointless in integer space, but play safe +&bfloor +0:0 +NaNfloor:NaN ++inf:inf +-inf:-inf +-1:-1 +-2:-2 +2:2 +3:3 +abc:NaN +&bceil +NaNceil:NaN ++inf:inf +-inf:-inf +0:0 +-1:-1 +-2:-2 +2:2 +3:3 +abc:NaN +&bint +NaN:NaN ++inf:inf +-inf:-inf +0:0 +-1:-1 +-2:-2 +2:2 +3:3 +&as_hex +128:0x80 +-128:-0x80 +0:0x0 +-0:0x0 +1:0x1 +0x123456789123456789:0x123456789123456789 ++inf:inf +-inf:-inf +NaNas_hex:NaN +&as_bin +128:0b10000000 +-128:-0b10000000 +0:0b0 +-0:0b0 +1:0b1 +0b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101 +0x123456789123456789:0b100100011010001010110011110001001000100100011010001010110011110001001 ++inf:inf +-inf:-inf +NaNas_bin:NaN diff --git a/cpan/Math-BigInt/t/bigintpm.t b/cpan/Math-BigInt/t/bigintpm.t new file mode 100644 index 0000000000..a03710edf5 --- /dev/null +++ b/cpan/Math-BigInt/t/bigintpm.t @@ -0,0 +1,36 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 3649 + 6; + +use Math::BigInt lib => 'Calc'; + +use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigInt"; +$CL = "Math::BigInt::Calc"; + +############################################################################# +# from_hex(), from_bin() and from_oct() tests + +my $x = Math::BigInt->from_hex('0xcafe'); +is ($x, "51966", 'from_hex() works'); + +$x = Math::BigInt->from_hex('0xcafebabedead'); +is ($x, "223195403574957", 'from_hex() works with long numbers'); + +$x = Math::BigInt->from_bin('0b1001'); +is ($x, "9", 'from_bin() works'); + +$x = Math::BigInt->from_bin('0b1001100110011001100110011001'); +is ($x, "161061273", 'from_bin() works with big numbers'); + +$x = Math::BigInt->from_oct('0775'); +is ($x, "509", 'from_oct() works'); + +$x = Math::BigInt->from_oct('07777777777777711111111222222222'); +is ($x, "9903520314281112085086151826", 'from_oct() works with big numbers'); + +############################################################################# +# all the other tests + +require 't/bigintpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/bigints.t b/cpan/Math-BigInt/t/bigints.t new file mode 100644 index 0000000000..a61696877b --- /dev/null +++ b/cpan/Math-BigInt/t/bigints.t @@ -0,0 +1,99 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 51; + +BEGIN { unshift @INC, 't'; } + +# testing of Math::BigInt:Scalar (used by the testsuite), +# primarily for interface/api and not for the math functionality + +use Math::BigInt::Scalar; + +my $C = 'Math::BigInt::Scalar'; # pass classname to sub's + +# _new and _str +my $x = $C->_new("123"); my $y = $C->_new("321"); +is (ref($x),'SCALAR'); is ($C->_str($x),123); is ($C->_str($y),321); + +# _add, _sub, _mul, _div + +is ($C->_str($C->_add($x,$y)),444); +is ($C->_str($C->_sub($x,$y)),123); +is ($C->_str($C->_mul($x,$y)),39483); +is ($C->_str($C->_div($x,$y)),123); + +is ($C->_str($C->_mul($x,$y)),39483); +is ($C->_str($x),39483); +is ($C->_str($y),321); +my $z = $C->_new("2"); +is ($C->_str($C->_add($x,$z)),39485); +my ($re,$rr) = $C->_div($x,$y); + +is ($C->_str($re),123); is ($C->_str($rr),2); + +# is_zero, _is_one, _one, _zero +is ($C->_is_zero($x),0); +is ($C->_is_one($x),0); + +is ($C->_is_one($C->_one()),1); is ($C->_is_one($C->_zero()),0); +is ($C->_is_zero($C->_zero()),1); is ($C->_is_zero($C->_one()),0); + +# is_odd, is_even +is ($C->_is_odd($C->_one()),1); is ($C->_is_odd($C->_zero()),0); +is ($C->_is_even($C->_one()),0); is ($C->_is_even($C->_zero()),1); + +# _digit +$x = $C->_new("123456789"); +is ($C->_digit($x,0),9); +is ($C->_digit($x,1),8); +is ($C->_digit($x,2),7); +is ($C->_digit($x,-1),1); +is ($C->_digit($x,-2),2); +is ($C->_digit($x,-3),3); + +# _copy +$x = $C->_new("12356"); +is ($C->_str($C->_copy($x)),12356); + +# _acmp +$x = $C->_new("123456789"); +$y = $C->_new("987654321"); +is ($C->_acmp($x,$y),-1); +is ($C->_acmp($y,$x),1); +is ($C->_acmp($x,$x),0); +is ($C->_acmp($y,$y),0); + +# _div +$x = $C->_new("3333"); $y = $C->_new("1111"); +is ($C->_str( scalar $C->_div($x,$y)),3); +$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); +is ($C->_str($x),30); is ($C->_str($y),3); +$x = $C->_new("123"); $y = $C->_new("1111"); +($x,$y) = $C->_div($x,$y); is ($C->_str($x),0); is ($C->_str($y),123); + +# _num +$x = $C->_new("12345"); $x = $C->_num($x); is (ref($x)||'',''); is ($x,12345); + +# _len +$x = $C->_new("12345"); $x = $C->_len($x); is (ref($x)||'',''); is ($x,5); + +# _and, _or, _xor +$x = $C->_new("3"); $y = $C->_new("4"); is ($C->_str( $C->_or($x,$y)),7); +$x = $C->_new("1"); $y = $C->_new("4"); is ($C->_str( $C->_xor($x,$y)),5); +$x = $C->_new("7"); $y = $C->_new("3"); is ($C->_str( $C->_and($x,$y)),3); + +# _pow +$x = $C->_new("2"); $y = $C->_new("4"); is ($C->_str( $C->_pow($x,$y)),16); +$x = $C->_new("2"); $y = $C->_new("5"); is ($C->_str( $C->_pow($x,$y)),32); +$x = $C->_new("3"); $y = $C->_new("3"); is ($C->_str( $C->_pow($x,$y)),27); + + +# _check +$x = $C->_new("123456789"); +is ($C->_check($x),0); +is ($C->_check(123),'123 is not a reference'); + +# done + +1; diff --git a/cpan/Math-BigInt/t/biglog.t b/cpan/Math-BigInt/t/biglog.t new file mode 100644 index 0000000000..7c3b618ce3 --- /dev/null +++ b/cpan/Math-BigInt/t/biglog.t @@ -0,0 +1,187 @@ +#!/usr/bin/perl -w + +# Test blog function (and bpow, since it uses blog), as well as bexp(). + +# It is too slow to be simple included in bigfltpm.inc, where it would get +# executed 3 times. One time would be under BareCalc, which shouldn't make any +# difference since there is no CALC->_log() function, and one time under a +# subclass, which *should* work. + +# But it is better to test the numerical functionality, instead of not testing +# it at all (which did lead to wrong answers for 0 < $x < 1 in blog() in +# versions up to v1.63, and for bsqrt($x) when $x << 1 for instance). + +use strict; +use Test::More tests => 70; + +use Math::BigFloat; +use Math::BigInt; + +my $cl = "Math::BigInt"; + +############################################################################# +# test log($n) in BigInt (broken until 1.80) + +is ($cl->new(2)->blog(), '0', "blog(2)"); +is ($cl->new(288)->blog(), '5',"blog(288)"); +is ($cl->new(2000)->blog(), '7', "blog(2000)"); + +############################################################################# +# test exp($n) in BigInt + +is ($cl->new(1)->bexp(), '2', "bexp(1)"); +is ($cl->new(2)->bexp(), '7',"bexp(2)"); +is ($cl->new(3)->bexp(), '20', "bexp(3)"); + +############################################################################# +############################################################################# +# BigFloat tests + +############################################################################# +# test log(2, N) where N > 67 (broken until 1.82) + +$cl = "Math::BigFloat"; + +# These tests can take quite a while, but are nec. Maybe protect them with +# some alarm()? + +# this triggers the calculation and caching of ln(2): +is ($cl->new(5)->blog(undef,71), +'1.6094379124341003746007593332261876395256013542685177219126478914741790'); + +# if the cache was correct, we should get this result, fast: +is ($cl->new(2)->blog(undef,71), +'0.69314718055994530941723212145817656807550013436025525412068000949339362'); + +is ($cl->new(11)->blog(undef,71), +'2.3978952727983705440619435779651292998217068539374171752185677091305736'); + +is ($cl->new(21)->blog(undef,71), +'3.0445224377234229965005979803657054342845752874046106401940844835750742'); + +############################################################################# + +# These tests are now really fast, since they collapse to blog(10), basically +# Don't attempt to run them with older versions. You are warned. + +# $x < 0 => NaN +is ($cl->new(-2)->blog(), 'NaN'); +is ($cl->new(-1)->blog(), 'NaN'); +is ($cl->new(-10)->blog(), 'NaN'); +is ($cl->new(-2,2)->blog(), 'NaN'); + +my $ten = $cl->new(10)->blog(); + +# 10 is cached (up to 75 digits) +is ($cl->new(10)->blog(), '2.302585092994045684017991454684364207601'); + +# 0.1 is using the cached value for log(10), too + +is ($cl->new(0.1)->blog(), -$ten); +is ($cl->new(0.01)->blog(), -$ten * 2); +is ($cl->new(0.001)->blog(), -$ten * 3); +is ($cl->new(0.0001)->blog(), -$ten * 4); + +# also cached +is ($cl->new(2)->blog(), '0.6931471805599453094172321214581765680755'); +is ($cl->new(4)->blog(), $cl->new(2)->blog * 2); + +# These are still slow, so do them only to 10 digits + +is ($cl->new('0.2')->blog(undef,10), '-1.609437912'); +is ($cl->new('0.3')->blog(undef,10), '-1.203972804'); +is ($cl->new('0.4')->blog(undef,10), '-0.9162907319'); +is ($cl->new('0.5')->blog(undef,10), '-0.6931471806'); +is ($cl->new('0.6')->blog(undef,10), '-0.5108256238'); +is ($cl->new('0.7')->blog(undef,10), '-0.3566749439'); +is ($cl->new('0.8')->blog(undef,10), '-0.2231435513'); +is ($cl->new('0.9')->blog(undef,10), '-0.1053605157'); + +is ($cl->new('9')->blog(undef,10), '2.197224577'); + +is ($cl->new('10')->blog(10,10), '1.000000000'); +is ($cl->new('20')->blog(20,10), '1.000000000'); +is ($cl->new('100')->blog(100,10), '1.000000000'); + +is ($cl->new('100')->blog(10,10), '2.000000000'); # 10 ** 2 == 100 +is ($cl->new('400')->blog(20,10), '2.000000000'); # 20 ** 2 == 400 + +is ($cl->new('4')->blog(2,10), '2.000000000'); # 2 ** 2 == 4 +is ($cl->new('16')->blog(2,10), '4.000000000'); # 2 ** 4 == 16 + +is ($cl->new('1.2')->bpow('0.3',10), '1.056219968'); +is ($cl->new('10')->bpow('0.6',10), '3.981071706'); + +# blog should handle bigint input +is (Math::BigFloat::blog(Math::BigInt->new(100),10), 2, "blog(100)"); + +############################################################################# +# some integer results +is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32"); +is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32"); +is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65"); + +my $x = Math::BigInt->new( '777' ) ** 256; +my $base = Math::BigInt->new( '12345678901234' ); +is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)'); + +$x = Math::BigInt->new( '777' ) ** 777; +$base = Math::BigInt->new( '777' ); +is ($x->copy()->blog($base), 777, 'blog(777**777, 777)'); + +############################################################################# +# test for bug in bsqrt() not taking negative _e into account +test_bpow ('200','0.5',10, '14.14213562'); +test_bpow ('20','0.5',10, '4.472135955'); +test_bpow ('2','0.5',10, '1.414213562'); +test_bpow ('0.2','0.5',10, '0.4472135955'); +test_bpow ('0.02','0.5',10, '0.1414213562'); +test_bpow ('0.49','0.5',undef , '0.7'); +test_bpow ('0.49','0.5',10 , '0.7000000000'); +test_bpow ('0.002','0.5',10, '0.04472135955'); +test_bpow ('0.0002','0.5',10, '0.01414213562'); +test_bpow ('0.0049','0.5',undef,'0.07'); +test_bpow ('0.0049','0.5',10 , '0.07000000000'); +test_bpow ('0.000002','0.5',10, '0.001414213562'); +test_bpow ('0.021','0.5',10, '0.1449137675'); +test_bpow ('1.2','0.5',10, '1.095445115'); +test_bpow ('1.23','0.5',10, '1.109053651'); +test_bpow ('12.3','0.5',10, '3.507135583'); + +test_bpow ('9.9','0.5',10, '3.146426545'); +test_bpow ('9.86902225','0.5',10, '3.141500000'); +test_bpow ('9.86902225','0.5',undef, '3.1415'); + +test_bpow ('0.2','0.41',10, '0.5169187652'); + +############################################################################# +# test bexp() with cached results + +is ($cl->new(1)->bexp(), '2.718281828459045235360287471352662497757', 'bexp(1)'); +is ($cl->new(2)->bexp(40), $cl->new(1)->bexp(45)->bpow(2,40), 'bexp(2)'); + +is ($cl->new("12.5")->bexp(61), $cl->new(1)->bexp(65)->bpow(12.5,61), 'bexp(12.5)'); + +############################################################################# +# test bexp() with big values (non-cached) + +is ($cl->new(1)->bexp(100), + '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', + 'bexp(100)'); + +is ($cl->new("12.5")->bexp(91), $cl->new(1)->bexp(95)->bpow(12.5,91), + 'bexp(12.5) to 91 digits'); + +# all done +1; + +############################################################################# +sub test_bpow + { + my ($x,$y,$scale,$result) = @_; + + print "# Tried: $x->bpow($y,$scale);\n" + unless ok ($cl->new($x)->bpow($y,$scale),$result); + } + + diff --git a/cpan/Math-BigInt/t/bigroot.t b/cpan/Math-BigInt/t/bigroot.t new file mode 100644 index 0000000000..c90d5ae9af --- /dev/null +++ b/cpan/Math-BigInt/t/bigroot.t @@ -0,0 +1,43 @@ +#!/usr/bin/perl -w + +# Test broot function (and bsqrt() function, since it is used by broot()). + +# It is too slow to be simple included in bigfltpm.inc, where it would get +# executed 3 times. + +# But it is better to test the numerical functionality, instead of not testing +# it at all. + +use strict; +use Test::More tests => 4 * 2; + +use Math::BigFloat; +use Math::BigInt; + +my $cl = "Math::BigFloat"; +my $c = "Math::BigInt"; + +# 2 ** 240 = +# 1766847064778384329583297500742918515827483896875618958121606201292619776 + +# takes way too long +#test_broot ('2','240', 8, undef, '1073741824'); +#test_broot ('2','240', 9, undef, '106528681.3099908308759836475139583940127'); +#test_broot ('2','120', 9, undef, '10321.27324073880096577298929482324664787'); +#test_broot ('2','120', 17, undef, '133.3268493632747279600707813049418888729'); + +test_broot ('2','120', 8, undef, '32768'); +test_broot ('2','60', 8, undef, '181.0193359837561662466161566988413540569'); +test_broot ('2','60', 9, undef, '101.5936673259647663841091609134277286651'); +test_broot ('2','60', 17, undef, '11.54672461623965153271017217302844672562'); + +sub test_broot + { + my ($x,$n,$y,$scale,$result) = @_; + + my $s = $scale || 'undef'; + is ($cl->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $cl $x->bpow($n)->broot($y,$s) == $result"); + $result =~ s/\..*//; + is ($c->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $c $x->bpow($n)->broot($y,$s) == $result"); + } + diff --git a/cpan/Math-BigInt/t/calling.t b/cpan/Math-BigInt/t/calling.t new file mode 100644 index 0000000000..6cdb4ac92f --- /dev/null +++ b/cpan/Math-BigInt/t/calling.t @@ -0,0 +1,149 @@ +#!/usr/bin/perl -w + +# test calling conventions, and :constant overloading + +use strict; +use Test::More tests => 160; + +BEGIN { unshift @INC, 't'; } + +package Math::BigInt::Test; + +use Math::BigInt; +use vars qw/@ISA/; +@ISA = qw/Math::BigInt/; # child of MBI +use overload; + +package Math::BigFloat::Test; + +use Math::BigFloat; +use vars qw/@ISA/; +@ISA = qw/Math::BigFloat/; # child of MBI +use overload; + +package main; + +use Math::BigInt try => 'Calc'; +use Math::BigFloat; + +my ($x,$y,$z,$u); +my $version = '1.76'; # adjust manually to match latest release + +############################################################################### +# check whether op's accept normal strings, even when inherited by subclasses + +# do one positive and one negative test to avoid false positives by "accident" + +my ($func,@args,$ans,$rc,$class,$try); +while () + { + $_ =~ s/[\n\r]//g; # remove newlines + next if /^#/; # skip comments + if (s/^&//) + { + $func = $_; + } + else + { + @args = split(/:/,$_,99); + $ans = pop @args; + foreach $class (qw/ + Math::BigInt Math::BigFloat Math::BigInt::Test Math::BigFloat::Test/) + { + $try = "'$args[0]'"; # quote it + $try = $args[0] if $args[0] =~ /'/; # already quoted + $try = '' if $args[0] eq ''; # undef, no argument + $try = "$class\->$func($try);"; + $rc = eval $try; + print "# Tried: '$try'\n" if !is ($rc, $ans); + } + } + + } + +$class = 'Math::BigInt'; + +# XXX TODO this test does not work/fail. +# test whether use Math::BigInt qw/version/ works +#$try = "use $class ($version.'1');"; +#$try .= ' $x = $class->new(123); $x = "$x";'; +#eval $try; +#is ( $x, undef ); # should result in error! + +# test whether fallback to calc works +$try = "use $class ($version,'try','foo, bar , ');"; +$try .= "$class\->config()->{lib};"; +$ans = eval $try; +like ( $ans, qr/^Math::BigInt::(Fast)?Calc\z/); + +# test whether constant works or not, also test for qw($version) +# bgcd() is present in subclass, too +$try = "use Math::BigInt ($version,'bgcd',':constant');"; +$try .= ' $x = 2**150; bgcd($x); $x = "$x";'; +$ans = eval $try; +is ( $ans, "1427247692705959881058285969449495136382746624"); + +# test whether Math::BigInt::Scalar via use works (w/ dff. spellings of calc) +$try = "use $class ($version,'lib','Scalar');"; +$try .= ' $x = 2**10; $x = "$x";'; +$ans = eval $try; is ( $ans, "1024"); +$try = "use $class ($version,'lib','$class\::Scalar');"; +$try .= ' $x = 2**10; $x = "$x";'; +$ans = eval $try; is ( $ans, "1024"); + +# all done + +__END__ +&is_zero +1:0 +0:1 +&is_one +1:1 +0:0 +&is_positive +1:1 +-1:0 +&is_negative +1:0 +-1:1 +&is_nan +abc:1 +1:0 +&is_inf +inf:1 +0:0 +&bstr +5:5 +10:10 +-10:-10 +abc:NaN +'+inf':inf +'-inf':-inf +&bsstr +1:1e+0 +0:0e+1 +2:2e+0 +200:2e+2 +-5:-5e+0 +-100:-1e+2 +abc:NaN +'+inf':inf +&babs +-1:1 +1:1 +&bnot +-2:1 +1:-2 +&bzero +:0 +&bnan +:NaN +abc:NaN +&bone +:1 +'+':1 +'-':-1 +&binf +:inf +'+':inf +'-':-inf diff --git a/cpan/Math-BigInt/t/config.t b/cpan/Math-BigInt/t/config.t new file mode 100644 index 0000000000..2d079b99ec --- /dev/null +++ b/cpan/Math-BigInt/t/config.t @@ -0,0 +1,128 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 55; + +# test whether Math::BigInt->config() and Math::BigFloat->config() works + +use Math::BigInt lib => 'Calc'; +use Math::BigFloat; + +my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat'; + +############################################################################## +# BigInt + +ok ($mbi->can('config')); + +my $cfg = $mbi->config(); + +ok (ref($cfg),'HASH'); + +is ($cfg->{lib},'Math::BigInt::Calc', 'lib'); +is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version'); +is ($cfg->{class},$mbi,'class'); +is ($cfg->{upgrade}||'','', 'upgrade'); +is ($cfg->{div_scale},40, 'div_Scale'); + +is ($cfg->{precision}||0,0, 'precision'); # should test for undef +is ($cfg->{accuracy}||0,0,'accuracy'); +is ($cfg->{round_mode},'even','round_mode'); + +is ($cfg->{trap_nan},0, 'trap_nan'); +is ($cfg->{trap_inf},0, 'trap_inf'); + +is ($mbi->config('lib'), 'Math::BigInt::Calc', 'config("lib")'); + +# can set via hash ref? +$cfg = $mbi->config( { trap_nan => 1 } ); +is ($cfg->{trap_nan},1, 'can set via hash ref'); + +# reset for later +$mbi->config( trap_nan => 0 ); + +############################################################################## +# BigFloat + +ok ($mbf->can('config')); + +$cfg = $mbf->config(); + +ok (ref($cfg),'HASH'); + +is ($cfg->{lib},'Math::BigInt::Calc', 'lib'); +is ($cfg->{with},'Math::BigInt::Calc', 'with'); +is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version'); +is ($cfg->{class},$mbf,'class'); +is ($cfg->{upgrade}||'','', 'upgrade'); +is ($cfg->{div_scale},40, 'div_Scale'); + +is ($cfg->{precision}||0,0, 'precision'); # should test for undef +is ($cfg->{accuracy}||0,0,'accuracy'); +is ($cfg->{round_mode},'even','round_mode'); + +is ($cfg->{trap_nan},0, 'trap_nan'); +is ($cfg->{trap_inf},0, 'trap_inf'); + +is ($mbf->config('lib'), 'Math::BigInt::Calc', 'config("lib")'); + +# can set via hash ref? +$cfg = $mbf->config( { trap_nan => 1 } ); +is ($cfg->{trap_nan},1, 'can set via hash ref'); + +# reset for later +$mbf->config( trap_nan => 0 ); + +############################################################################## +# test setting values + +my $test = { + trap_nan => 1, + trap_inf => 1, + accuracy => 2, + precision => 3, + round_mode => 'zero', + div_scale => '100', + upgrade => 'Math::BigInt::SomeClass', + downgrade => 'Math::BigInt::SomeClass', + }; + +my $c; + +foreach my $key (keys %$test) + { + # see if setting in MBI works + eval ( "$mbi\->config( $key => '$test->{$key}' );" ); + $c = $mbi->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}"); + $c = $mbf->config(); + # see if setting it in MBI leaves MBF alone + if (($c->{$key}||0) ne $test->{$key}) + { + is (1,1); + } + else + { + is ("$key eq $c->{$key}","$key ne $test->{$key}", "$key"); + } + + # see if setting in MBF works + eval ( "$mbf\->config( $key => '$test->{$key}' );" ); + $c = $mbf->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}"); + } + +############################################################################## +# test setting illegal keys (should croak) + +$@ = ""; my $never_reached = 0; +eval ("$mbi\->config( 'some_garbage' => 1 ); $never_reached = 1;"); +is ($never_reached,0); + +$@ = ""; $never_reached = 0; +eval ("$mbf\->config( 'some_garbage' => 1 ); $never_reached = 1;"); +is ($never_reached,0); + +# this does not work. Why? +#ok ($@ eq "Illegal keys 'some_garbage' passed to Math::BigInt->config() at ./config.t line 104", 1); + +# all tests done + diff --git a/cpan/Math-BigInt/t/const_mbf.t b/cpan/Math-BigInt/t/const_mbf.t new file mode 100644 index 0000000000..84f7a8cf99 --- /dev/null +++ b/cpan/Math-BigInt/t/const_mbf.t @@ -0,0 +1,14 @@ +#!/usr/bin/perl -w + +# test BigFloat constants alone (w/o BigInt loading) + +use strict; +use Test::More tests => 2; + +use Math::BigFloat ':constant'; + +is (1.0 / 3.0, '0.3333333333333333333333333333333333333333'); + +# BigInt was not loaded with ':constant', so only floats are handled +is (ref(2 ** 2),''); + diff --git a/cpan/Math-BigInt/t/constant.t b/cpan/Math-BigInt/t/constant.t new file mode 100644 index 0000000000..ad8afeed2d --- /dev/null +++ b/cpan/Math-BigInt/t/constant.t @@ -0,0 +1,35 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 7; + +use Math::BigInt ':constant'; + +is (2 ** 255,'57896044618658097711785492504343953926634992332820282019728792003956564819968'); + +{ + no warnings 'portable'; # protect against "non-portable" warnings +# hexadecimal constants +is (0x123456789012345678901234567890, + Math::BigInt->new('0x123456789012345678901234567890')); +# binary constants +is (0b01010100011001010110110001110011010010010110000101101101, + Math::BigInt->new( + '0b01010100011001010110110001110011010010010110000101101101')); +} + +use Math::BigFloat ':constant'; +is (1.0 / 3.0, '0.3333333333333333333333333333333333333333'); + +# stress-test Math::BigFloat->import() + +Math::BigFloat->import( qw/:constant/ ); +is (1,1); + +Math::BigFloat->import( qw/:constant upgrade Math::BigRat/ ); +is (1,1); + +Math::BigFloat->import( qw/upgrade Math::BigRat :constant/ ); +is (1,1); + +# all tests done diff --git a/cpan/Math-BigInt/t/downgrade.t b/cpan/Math-BigInt/t/downgrade.t new file mode 100644 index 0000000000..f6b011e5a0 --- /dev/null +++ b/cpan/Math-BigInt/t/downgrade.t @@ -0,0 +1,45 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 15; + +use Math::BigInt upgrade => 'Math::BigFloat'; +use Math::BigFloat downgrade => 'Math::BigInt', upgrade => 'Math::BigInt'; + +use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup + $ECL $CL); +$class = "Math::BigInt"; +$CL = "Math::BigInt::Calc"; +$ECL = "Math::BigFloat"; + +# simplistic test for now +is (Math::BigFloat->downgrade(),'Math::BigInt'); +is (Math::BigFloat->upgrade(),'Math::BigInt'); + +# these downgrade +is (ref(Math::BigFloat->new('inf')),'Math::BigInt'); +is (ref(Math::BigFloat->new('-inf')),'Math::BigInt'); +is (ref(Math::BigFloat->new('NaN')),'Math::BigInt'); +is (ref(Math::BigFloat->new('0')),'Math::BigInt'); +is (ref(Math::BigFloat->new('1')),'Math::BigInt'); +is (ref(Math::BigFloat->new('10')),'Math::BigInt'); +is (ref(Math::BigFloat->new('-10')),'Math::BigInt'); +is (ref(Math::BigFloat->new('-10.0E1')),'Math::BigInt'); + +# bug until v1.67: +is (Math::BigFloat->new('0.2E0'), '0.2'); +is (Math::BigFloat->new('0.2E1'), '2'); +# until v1.67 resulted in 200: +is (Math::BigFloat->new('0.2E2'), '20'); + +# disable, otherwise it screws calculations +Math::BigFloat->upgrade(undef); +is (Math::BigFloat->upgrade()||'',''); + +Math::BigFloat->div_scale(20); # make it a bit faster +my $x = Math::BigFloat->new(2); # downgrades +# the following test upgrade for bsqrt() and also makes new() NOT downgrade +# for the bpow() side +is (Math::BigFloat->bpow('2','0.5'),$x->bsqrt()); + +#require 'upgrade.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/inf_nan.t b/cpan/Math-BigInt/t/inf_nan.t new file mode 100644 index 0000000000..270689bac6 --- /dev/null +++ b/cpan/Math-BigInt/t/inf_nan.t @@ -0,0 +1,404 @@ +#!/usr/bin/perl -w + +# test inf/NaN handling all in one place +# Thanx to Jarkko for the excellent explanations and the tables + +use strict; + +use Test::More + tests => 7 * 6 * 5 * 4 * 2 + + 7 * 6 * 2 * 4 * 1 # bmod +; +# see bottom: + 4 * 10; # 4 classes * 10 NaN == NaN tests + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt; +use Math::BigFloat; +use Math::BigInt::Subclass; +use Math::BigFloat::Subclass; + +my @biclasses = + qw/ Math::BigInt Math::BigInt::Subclass /; +my @bfclasses = + qw/ Math::BigFloat Math::BigFloat::Subclass /; + +my (@args,$x,$y,$z); + +# + +foreach (qw/ + -inf:-inf:-inf + -1:-inf:-inf + -0:-inf:-inf + 0:-inf:-inf + 1:-inf:-inf + inf:-inf:NaN + NaN:-inf:NaN + + -inf:-1:-inf + -1:-1:-2 + -0:-1:-1 + 0:-1:-1 + 1:-1:0 + inf:-1:inf + NaN:-1:NaN + + -inf:0:-inf + -1:0:-1 + -0:0:0 + 0:0:0 + 1:0:1 + inf:0:inf + NaN:0:NaN + + -inf:1:-inf + -1:1:0 + -0:1:1 + 0:1:1 + 1:1:2 + inf:1:inf + NaN:1:NaN + + -inf:inf:NaN + -1:inf:inf + -0:inf:inf + 0:inf:inf + 1:inf:inf + inf:inf:inf + NaN:inf:NaN + + -inf:NaN:NaN + -1:NaN:NaN + -0:NaN:NaN + 0:NaN:NaN + 1:NaN:NaN + inf:NaN:NaN + NaN:NaN:NaN + /) + { + @args = split /:/,$_; + for my $class (@biclasses, @bfclasses) + { + $x = $class->new($args[0]); + $y = $class->new($args[1]); + $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 + my $r = $x->badd($y); + + is($x->bstr(),$args[2],"x $class $args[0] + $args[1]"); + is($x->bstr(),$args[2],"r $class $args[0] + $args[1]"); + } + } + +# - +foreach (qw/ + -inf:-inf:NaN + -1:-inf:inf + -0:-inf:inf + 0:-inf:inf + 1:-inf:inf + inf:-inf:inf + NaN:-inf:NaN + + -inf:-1:-inf + -1:-1:0 + -0:-1:1 + 0:-1:1 + 1:-1:2 + inf:-1:inf + NaN:-1:NaN + + -inf:0:-inf + -1:0:-1 + -0:0:-0 + 0:0:0 + 1:0:1 + inf:0:inf + NaN:0:NaN + + -inf:1:-inf + -1:1:-2 + -0:1:-1 + 0:1:-1 + 1:1:0 + inf:1:inf + NaN:1:NaN + + -inf:inf:-inf + -1:inf:-inf + -0:inf:-inf + 0:inf:-inf + 1:inf:-inf + inf:inf:NaN + NaN:inf:NaN + + -inf:NaN:NaN + -1:NaN:NaN + -0:NaN:NaN + 0:NaN:NaN + 1:NaN:NaN + inf:NaN:NaN + NaN:NaN:NaN + /) + { + @args = split /:/,$_; + for my $class (@biclasses, @bfclasses) + { + $x = $class->new($args[0]); + $y = $class->new($args[1]); + $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 + my $r = $x->bsub($y); + + is($x->bstr(),$args[2],"x $class $args[0] - $args[1]"); + is($r->bstr(),$args[2],"r $class $args[0] - $args[1]"); + } + } + +# * +foreach (qw/ + -inf:-inf:inf + -1:-inf:inf + -0:-inf:NaN + 0:-inf:NaN + 1:-inf:-inf + inf:-inf:-inf + NaN:-inf:NaN + + -inf:-1:inf + -1:-1:1 + -0:-1:0 + 0:-1:-0 + 1:-1:-1 + inf:-1:-inf + NaN:-1:NaN + + -inf:0:NaN + -1:0:-0 + -0:0:-0 + 0:0:0 + 1:0:0 + inf:0:NaN + NaN:0:NaN + + -inf:1:-inf + -1:1:-1 + -0:1:-0 + 0:1:0 + 1:1:1 + inf:1:inf + NaN:1:NaN + + -inf:inf:-inf + -1:inf:-inf + -0:inf:NaN + 0:inf:NaN + 1:inf:inf + inf:inf:inf + NaN:inf:NaN + + -inf:NaN:NaN + -1:NaN:NaN + -0:NaN:NaN + 0:NaN:NaN + 1:NaN:NaN + inf:NaN:NaN + NaN:NaN:NaN + /) + { + @args = split /:/,$_; + for my $class (@biclasses, @bfclasses) + { + $x = $class->new($args[0]); + $y = $class->new($args[1]); + $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 + $args[2] = '0' if $args[2] eq '-0'; # BigInt hasn't got -0 + my $r = $x->bmul($y); + + is($x->bstr(),$args[2],"x $class $args[0] * $args[1]"); + is($r->bstr(),$args[2],"r $class $args[0] * $args[1]"); + } + } + +# / +foreach (qw/ + -inf:-inf:NaN + -1:-inf:0 + -0:-inf:0 + 0:-inf:-0 + 1:-inf:-1 + inf:-inf:NaN + NaN:-inf:NaN + + -inf:-1:inf + -1:-1:1 + -0:-1:0 + 0:-1:-0 + 1:-1:-1 + inf:-1:-inf + NaN:-1:NaN + + -inf:0:-inf + -1:0:-inf + -0:0:NaN + 0:0:NaN + 1:0:inf + inf:0:inf + NaN:0:NaN + + -inf:1:-inf + -1:1:-1 + -0:1:-0 + 0:1:0 + 1:1:1 + inf:1:inf + NaN:1:NaN + + -inf:inf:NaN + -1:inf:-1 + -0:inf:-0 + 0:inf:0 + 1:inf:0 + inf:inf:NaN + NaN:inf:NaN + + -inf:NaN:NaN + -1:NaN:NaN + -0:NaN:NaN + 0:NaN:NaN + 1:NaN:NaN + inf:NaN:NaN + NaN:NaN:NaN + /) + { + @args = split /:/,$_; + for my $class (@biclasses, @bfclasses) + { + $x = $class->new($args[0]); + $y = $class->new($args[1]); + $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 + + my $t = $x->copy(); + my $tmod = $t->copy(); + + # bdiv in scalar context + unless ($class =~ /^Math::BigFloat/) { + my $r = $x->bdiv($y); + is($x->bstr(),$args[2],"x $class $args[0] / $args[1]"); + is($r->bstr(),$args[2],"r $class $args[0] / $args[1]"); + } + + # bmod and bdiv in list context + my ($d,$rem) = $t->bdiv($y); + + # bdiv in list context + is($t->bstr(),$args[2],"t $class $args[0] / $args[1]"); + is($d->bstr(),$args[2],"d $class $args[0] / $args[1]"); + + # bmod + my $m = $tmod->bmod($y); + + # bmod() agrees with bdiv? + is($m->bstr(),$rem->bstr(),"m $class $args[0] % $args[1]"); + # bmod() return agrees with set value? + is($tmod->bstr(),$m->bstr(),"o $class $args[0] % $args[1]"); + + } + } + +# / +foreach (qw/ + -inf:-inf:NaN + -1:-inf:0 + -0:-inf:0 + 0:-inf:-0 + 1:-inf:-0 + inf:-inf:NaN + NaN:-inf:NaN + + -inf:-1:inf + -1:-1:1 + -0:-1:0 + 0:-1:-0 + 1:-1:-1 + inf:-1:-inf + NaN:-1:NaN + + -inf:0:-inf + -1:0:-inf + -0:0:NaN + 0:0:NaN + 1:0:inf + inf:0:inf + NaN:0:NaN + + -inf:1:-inf + -1:1:-1 + -0:1:-0 + 0:1:0 + 1:1:1 + inf:1:inf + NaN:1:NaN + + -inf:inf:NaN + -1:inf:-0 + -0:inf:-0 + 0:inf:0 + 1:inf:0 + inf:inf:NaN + NaN:inf:NaN + + -inf:NaN:NaN + -1:NaN:NaN + -0:NaN:NaN + 0:NaN:NaN + 1:NaN:NaN + inf:NaN:NaN + NaN:NaN:NaN + /) + { + @args = split /:/,$_; + for my $class (@bfclasses) + { + $x = $class->new($args[0]); + $y = $class->new($args[1]); + $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 + + my $t = $x->copy(); + my $tmod = $t->copy(); + + # bdiv in scalar context + my $r = $x->bdiv($y); + is($x->bstr(),$args[2],"x $class $args[0] / $args[1]"); + is($r->bstr(),$args[2],"r $class $args[0] / $args[1]"); + + } + } + +############################################################################# +# overloaded comparisons + +# these are disabled for now, since Perl itself can't seem to make up it's +# mind what NaN actually is, see [perl #33106]. + +# +#foreach my $c (@biclasses, @bfclasses) +# { +# my $x = $c->bnan(); +# my $y = $c->bnan(); # test with two different objects, too +# my $a = $c->bzero(); +# +# is ($x == $y, undef, 'NaN == NaN: undef'); +# is ($x != $y, 1, 'NaN != NaN: 1'); +# +# is ($x == $x, undef, 'NaN == NaN: undef'); +# is ($x != $x, 1, 'NaN != NaN: 1'); +# +# is ($a != $x, 1, '0 != NaN: 1'); +# is ($a == $x, undef, '0 == NaN: undef'); +# +# is ($a < $x, undef, '0 < NaN: undef'); +# is ($a <= $x, undef, '0 <= NaN: undef'); +# is ($a >= $x, undef, '0 >= NaN: undef'); +# is ($a > $x, undef, '0 > NaN: undef'); +# } + +# All done. diff --git a/cpan/Math-BigInt/t/isa.t b/cpan/Math-BigInt/t/isa.t new file mode 100644 index 0000000000..0bdf66fda2 --- /dev/null +++ b/cpan/Math-BigInt/t/isa.t @@ -0,0 +1,34 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 7; + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt::Subclass; +use Math::BigFloat::Subclass; +use Math::BigInt; +use Math::BigFloat; + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigInt::Subclass"; +$CL = "Math::BigInt::Calc"; + +# Check that a subclass is still considered a BigInt +isa_ok ($class->new(123), 'Math::BigInt'); + +# ditto for plain Math::BigInt +isa_ok (Math::BigInt->new(123), 'Math::BigInt'); + +# But Math::BigFloats aren't +isnt (Math::BigFloat->new(123)->isa('Math::BigInt'), 1); + +# see what happens if we feed a Math::BigFloat into new() +$x = Math::BigInt->new(Math::BigFloat->new(123)); +is (ref($x),'Math::BigInt'); +isa_ok ($x, 'Math::BigInt'); + +# ditto for subclass +$x = Math::BigInt->new(Math::BigFloat->new(123)); +is (ref($x),'Math::BigInt'); +isa_ok ($x, 'Math::BigInt'); diff --git a/cpan/Math-BigInt/t/lib_load.t b/cpan/Math-BigInt/t/lib_load.t new file mode 100644 index 0000000000..65a913ac35 --- /dev/null +++ b/cpan/Math-BigInt/t/lib_load.t @@ -0,0 +1,29 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 4; + +BEGIN { unshift @INC, 't'; } + +# first load BigInt with Calc +use Math::BigInt lib => 'Calc'; + +# BigFloat will remember that we loaded Calc +require Math::BigFloat; +is (Math::BigFloat::config()->{lib}, 'Math::BigInt::Calc', 'BigFloat got Calc'); + +# now load BigInt again with a different lib +Math::BigInt->import( lib => 'BareCalc' ); + +# and finally test that BigFloat knows about BareCalc + +is (Math::BigFloat::config()->{lib}, 'Math::BigInt::BareCalc', 'BigFloat was notified'); + +# See that Math::BigFloat supports "only" +eval "Math::BigFloat->import('only' => 'Calc')"; +is (Math::BigFloat::config()->{lib}, 'Math::BigInt::Calc', '"only" worked'); + +# See that Math::BigFloat supports "try" +eval "Math::BigFloat->import('try' => 'BareCalc')"; +is (Math::BigFloat::config()->{lib}, 'Math::BigInt::BareCalc', '"try" worked'); + diff --git a/cpan/Math-BigInt/t/mbf_ali.t b/cpan/Math-BigInt/t/mbf_ali.t new file mode 100644 index 0000000000..845fbe94e1 --- /dev/null +++ b/cpan/Math-BigInt/t/mbf_ali.t @@ -0,0 +1,14 @@ +#!/usr/bin/perl -w + +# test that the new alias names work + +use strict; +use Test::More tests => 6; + +use Math::BigFloat; + +use vars qw/$x $CL/; + +$CL = 'Math::BigFloat'; + +require 't/alias.inc'; diff --git a/cpan/Math-BigInt/t/mbi_ali.t b/cpan/Math-BigInt/t/mbi_ali.t new file mode 100644 index 0000000000..d52812bec9 --- /dev/null +++ b/cpan/Math-BigInt/t/mbi_ali.t @@ -0,0 +1,14 @@ +#!/usr/bin/perl -w + +# test that the new alias names work + +use strict; +use Test::More tests => 6; + +use Math::BigInt; + +use vars qw/$x $CL/; + +$CL = 'Math::BigInt'; + +require 't/alias.inc'; diff --git a/cpan/Math-BigInt/t/mbi_rand.t b/cpan/Math-BigInt/t/mbi_rand.t new file mode 100644 index 0000000000..a6e3b21c8b --- /dev/null +++ b/cpan/Math-BigInt/t/mbi_rand.t @@ -0,0 +1,86 @@ +#!/usr/bin/perl -w + +use strict; + +my $count; + +BEGIN { + $count = 128; +} + +use Test::More tests => $count*4; + +use Math::BigInt; +my $c = 'Math::BigInt'; + +my $length = 128; + +# If you get a failure here, please re-run the test with the printed seed +# value as input "perl t/mbi_rand.t seed" and send me the output + +my $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(1165537)); +print "# seed: $seed\n"; srand($seed); + +print "# lib: ", Math::BigInt->config()->{lib},"\n"; +if (Math::BigInt->config()->{lib} =~ /::Calc/) + { + print "# base len: ", scalar Math::BigInt::Calc->_base_len(),"\n"; + } + +my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb); +my $two = Math::BigInt->new(2); +for (my $i = 0; $i < $count; $i++) + { + # length of A and B + $la = int(rand($length)+1); $lb = int(rand($length)+1); + $As = ''; $Bs = ''; + + # we create the numbers from "patterns", e.g. get a random number and a + # random count and string them together. This means things like + # "100000999999999999911122222222" are much more likely. If we just strung + # together digits, we would end up with "1272398823211223" etc. It also means + # that we get more frequently equal numbers or other special cases. + while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); } + while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); } + + $As =~ s/^0+//; $Bs =~ s/^0+//; + $As = $As || '0'; $Bs = $Bs || '0'; +# print "# As $As\n# Bs $Bs\n"; + $A = $c->new($As); $B = $c->new($Bs); + print "# A $A\n# B $B\n"; + if ($A->is_zero() || $B->is_zero()) + { + for (1..4) { is (1,1, 'skipped this test'); } next; + } + + # check that int(A/B)*B + A % B == A holds for all inputs + + # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B); + + ($ADB,$AMB) = $A->copy()->bdiv($B); + print "# ($A / $B, $A % $B ) = $ADB $AMB\n"; + + print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n". + "# tried $ADB * $B + $two*$AMB - $AMB\n" + unless is ($ADB*$B+$two*$AMB-$AMB,$As, "ADB * B + 2 * AMB - AMB == A"); + if (is ($ADB*$B/$B,$ADB, "ADB * B / B == ADB")) + { + print "# seed: $seed, \$ADB * \$B / \$B = ", $ADB * $B / $B, " != $ADB (\$B=$B)\n"; + if (Math::BigInt->config()->{lib} =~ /::Calc/) + { + print "# ADB->[-1]: ", $ADB->{value}->[-1], " B->[-1]: ", $B->{value}->[-1],"\n"; + } + } + # swap 'em and try this, too + # $X = ($B/$A)*$A + $B % $A; + ($ADB,$AMB) = $B->copy()->bdiv($A); + # print "check: $ADB $AMB"; + print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n". + "# tried $ADB * $A + $two*$AMB - $AMB\n" + unless is ($ADB*$A+$two*$AMB-$AMB,$Bs, "ADB * A + 2 * AMB - AMB == B"); + print "# +$two * $AMB = ",$ADB * $A + $two * $AMB,"\n"; + print "# -$AMB = ",$ADB * $A + $two * $AMB - $AMB,"\n"; + print "# seed $seed, \$ADB * \$A / \$A = ", $ADB * $A / $A, " != $ADB (\$A=$A)\n" + unless is ($ADB*$A/$A,$ADB, "ADB * A/A == ADB"); + } + diff --git a/cpan/Math-BigInt/t/mbimbf.inc b/cpan/Math-BigInt/t/mbimbf.inc new file mode 100644 index 0000000000..7b2c94613c --- /dev/null +++ b/cpan/Math-BigInt/t/mbimbf.inc @@ -0,0 +1,951 @@ +# test rounding, accuracy, precision and fallback, round_mode and mixing +# of classes + +# Make sure you always quote any bare floating-point values, lest 123.46 will +# be stringified to 123.4599999999 due to limited float prevision. + +use strict; +my ($x,$y,$z,$u,$rc); + +############################################################################### +# test defaults and set/get + +{ + no strict 'refs'; + is (${"$mbi\::accuracy"}, undef); + is (${"$mbi\::precision"}, undef); + is ($mbi->accuracy(), undef); + is ($mbi->precision(), undef); + is (${"$mbi\::div_scale"},40); + is (${"$mbi\::round_mode"},'even'); + is ($mbi->round_mode(),'even'); + + is (${"$mbf\::accuracy"}, undef); + is (${"$mbf\::precision"}, undef); + is ($mbf->precision(), undef); + is ($mbf->precision(), undef); + is (${"$mbf\::div_scale"},40); + is (${"$mbf\::round_mode"},'even'); + is ($mbf->round_mode(),'even'); +} + +# accessors +foreach my $class ($mbi,$mbf) + { + is ($class->accuracy(), undef); + is ($class->precision(), undef); + is ($class->round_mode(),'even'); + is ($class->div_scale(),40); + + is ($class->div_scale(20),20); + $class->div_scale(40); is ($class->div_scale(),40); + + is ($class->round_mode('odd'),'odd'); + $class->round_mode('even'); is ($class->round_mode(),'even'); + + is ($class->accuracy(2),2); + $class->accuracy(3); is ($class->accuracy(),3); + is ($class->accuracy(undef), undef); + + is ($class->precision(2),2); + is ($class->precision(-2),-2); + $class->precision(3); is ($class->precision(),3); + is ($class->precision(undef), undef); + } + +{ + no strict 'refs'; + # accuracy + foreach (qw/5 42 -1 0/) + { + is (${"$mbf\::accuracy"} = $_,$_); + is (${"$mbi\::accuracy"} = $_,$_); + } + is (${"$mbf\::accuracy"} = undef, undef); + is (${"$mbi\::accuracy"} = undef, undef); + + # precision + foreach (qw/5 42 -1 0/) + { + is (${"$mbf\::precision"} = $_,$_); + is (${"$mbi\::precision"} = $_,$_); + } + is (${"$mbf\::precision"} = undef, undef); + is (${"$mbi\::precision"} = undef, undef); + + # fallback + foreach (qw/5 42 1/) + { + is (${"$mbf\::div_scale"} = $_,$_); + is (${"$mbi\::div_scale"} = $_,$_); + } + # illegal values are possible for fallback due to no accessor + + # round_mode + foreach (qw/odd even zero trunc +inf -inf/) + { + is (${"$mbf\::round_mode"} = $_,$_); + is (${"$mbi\::round_mode"} = $_,$_); + } + ${"$mbf\::round_mode"} = 'zero'; + is (${"$mbf\::round_mode"},'zero'); + is (${"$mbi\::round_mode"},'-inf'); # from above + + # reset for further tests + ${"$mbi\::accuracy"} = undef; + ${"$mbi\::precision"} = undef; + ${"$mbf\::div_scale"} = 40; +} + +# local copies +$x = $mbf->new('123.456'); +is ($x->accuracy(), undef); +is ($x->accuracy(5),5); +is ($x->accuracy(undef),undef, undef); +is ($x->precision(), undef); +is ($x->precision(5),5); +is ($x->precision(undef),undef, undef); + +{ + no strict 'refs'; + # see if MBF changes MBIs values + is (${"$mbi\::accuracy"} = 42,42); + is (${"$mbf\::accuracy"} = 64,64); + is (${"$mbi\::accuracy"},42); # should be still 42 + is (${"$mbf\::accuracy"},64); # should be now 64 +} + +############################################################################### +# see if creating a number under set A or P will round it + +{ + no strict 'refs'; + ${"$mbi\::accuracy"} = 4; + ${"$mbi\::precision"} = undef; + + is ($mbi->new(123456),123500); # with A + ${"$mbi\::accuracy"} = undef; + ${"$mbi\::precision"} = 3; + is ($mbi->new(123456),123000); # with P + + ${"$mbf\::accuracy"} = 4; + ${"$mbf\::precision"} = undef; + ${"$mbi\::precision"} = undef; + + is ($mbf->new('123.456'),'123.5'); # with A + ${"$mbf\::accuracy"} = undef; + ${"$mbf\::precision"} = -1; + is ($mbf->new('123.456'),'123.5'); # with P from MBF, not MBI! + + ${"$mbf\::precision"} = undef; # reset +} + +############################################################################### +# see if MBI leaves MBF's private parts alone + +{ + no strict 'refs'; + ${"$mbi\::precision"} = undef; ${"$mbf\::precision"} = undef; + ${"$mbi\::accuracy"} = 4; ${"$mbf\::accuracy"} = undef; + is ($mbf->new('123.456'),'123.456'); + ${"$mbi\::accuracy"} = undef; # reset +} + +############################################################################### +# see if setting accuracy/precision actually rounds the number + +$x = $mbf->new('123.456'); $x->accuracy(4); is ($x,'123.5'); +$x = $mbf->new('123.456'); $x->precision(-2); is ($x,'123.46'); + +$x = $mbi->new(123456); $x->accuracy(4); is ($x,123500); +$x = $mbi->new(123456); $x->precision(2); is ($x,123500); + +############################################################################### +# test actual rounding via round() + +$x = $mbf->new('123.456'); +is ($x->copy()->round(5),'123.46'); +is ($x->copy()->round(4),'123.5'); +is ($x->copy()->round(5,2),'NaN'); +is ($x->copy()->round(undef,-2),'123.46'); +is ($x->copy()->round(undef,2),120); + +$x = $mbi->new('123'); +is ($x->round(5,2),'NaN'); + +$x = $mbf->new('123.45000'); +is ($x->copy()->round(undef,-1,'odd'),'123.5'); + +# see if rounding is 'sticky' +$x = $mbf->new('123.4567'); +$y = $x->copy()->bround(); # no-op since nowhere A or P defined + +is ($y,123.4567); +$y = $x->copy()->round(5); +is ($y->accuracy(),5); +is ($y->precision(), undef); # A has precedence, so P still unset +$y = $x->copy()->round(undef,2); +is ($y->precision(),2); +is ($y->accuracy(), undef); # P has precedence, so A still unset + +# see if setting A clears P and vice versa +$x = $mbf->new('123.4567'); +is ($x,'123.4567'); +is ($x->accuracy(4),4); +is ($x->precision(-2),-2); # clear A +is ($x->accuracy(), undef); + +$x = $mbf->new('123.4567'); +is ($x,'123.4567'); +is ($x->precision(-2),-2); +is ($x->accuracy(4),4); # clear P +is ($x->precision(), undef); + +# does copy work? +$x = $mbf->new(123.456); $x->accuracy(4); $x->precision(2); +$z = $x->copy(); is ($z->accuracy(),undef); is ($z->precision(),2); + +# does $x->bdiv($y,d) work when $d > div_scale? +$x = $mbf->new('0.008'); $x->accuracy(8); + +for my $e ( 4, 8, 16, 32 ) + { + print "# Tried: $x->bdiv(3,$e)\n" + unless is (scalar $x->copy()->bdiv(3,$e), '0.002' . ('6' x ($e-2)) . '7'); + } + +# does accuracy()/precision work on zeros? +foreach my $c ($mbi,$mbf) + { + $x = $c->bzero(); $x->accuracy(5); is ($x->{_a},5); + $x = $c->bzero(); $x->precision(5); is ($x->{_p},5); + $x = $c->new(0); $x->accuracy(5); is ($x->{_a},5); + $x = $c->new(0); $x->precision(5); is ($x->{_p},5); + + $x = $c->bzero(); $x->round(5); is ($x->{_a},5); + $x = $c->bzero(); $x->round(undef,5); is ($x->{_p},5); + $x = $c->new(0); $x->round(5); is ($x->{_a},5); + $x = $c->new(0); $x->round(undef,5); is ($x->{_p},5); + + # see if trying to increasing A in bzero() doesn't do something + $x = $c->bzero(); $x->{_a} = 3; $x->round(5); is ($x->{_a},3); + } + +############################################################################### +# test whether an opp calls objectify properly or not (or at least does what +# it should do given non-objects, w/ or w/o objectify()) + +foreach my $c ($mbi,$mbf) + { +# ${"$c\::precision"} = undef; # reset +# ${"$c\::accuracy"} = undef; # reset + + is ($c->new(123)->badd(123),246); + is ($c->badd(123,321),444); + is ($c->badd(123,$c->new(321)),444); + + is ($c->new(123)->bsub(122),1); + is ($c->bsub(321,123),198); + is ($c->bsub(321,$c->new(123)),198); + + is ($c->new(123)->bmul(123),15129); + is ($c->bmul(123,123),15129); + is ($c->bmul(123,$c->new(123)),15129); + +# is ($c->new(15129)->bdiv(123),123); +# is ($c->bdiv(15129,123),123); +# is ($c->bdiv(15129,$c->new(123)),123); + + is ($c->new(15131)->bmod(123),2); + is ($c->bmod(15131,123),2); + is ($c->bmod(15131,$c->new(123)),2); + + is ($c->new(2)->bpow(16),65536); + is ($c->bpow(2,16),65536); + is ($c->bpow(2,$c->new(16)),65536); + + is ($c->new(2**15)->brsft(1),2**14); + is ($c->brsft(2**15,1),2**14); + is ($c->brsft(2**15,$c->new(1)),2**14); + + is ($c->new(2**13)->blsft(1),2**14); + is ($c->blsft(2**13,1),2**14); + is ($c->blsft(2**13,$c->new(1)),2**14); + } + +############################################################################### +# test whether operations round properly afterwards +# These tests are not complete, since they do not exercise every "return" +# statement in the op's. But heh, it's better than nothing... + +$x = $mbf->new('123.456'); +$y = $mbf->new('654.321'); +$x->{_a} = 5; # $x->accuracy(5) would round $x straight away +$y->{_a} = 4; # $y->accuracy(4) would round $x straight away + +$z = $x + $y; is ($z,'777.8'); +$z = $y - $x; is ($z,'530.9'); +$z = $y * $x; is ($z,'80780'); +$z = $x ** 2; is ($z,'15241'); +$z = $x * $x; is ($z,'15241'); + +# not: $z = -$x; is ($z,'-123.46'); is ($x,'123.456'); +$z = $x->copy(); $z->{_a} = 2; $z = $z / 2; is ($z,62); +$x = $mbf->new(123456); $x->{_a} = 4; +$z = $x->copy; $z++; is ($z,123500); + +$x = $mbi->new(123456); +$y = $mbi->new(654321); +$x->{_a} = 5; # $x->accuracy(5) would round $x straight away +$y->{_a} = 4; # $y->accuracy(4) would round $x straight away + +$z = $x + $y; is ($z,777800); +$z = $y - $x; is ($z,530900); +$z = $y * $x; is ($z,80780000000); +$z = $x ** 2; is ($z,15241000000); +# not yet: $z = -$x; is ($z,-123460); is ($x,123456); +$z = $x->copy; $z++; is ($z,123460); +$z = $x->copy(); $z->{_a} = 2; $z = $z / 2; is ($z,62000); + +$x = $mbi->new(123400); $x->{_a} = 4; +is ($x->bnot(),-123400); # not -1234001 + +# both babs() and bneg() don't need to round, since the input will already +# be rounded (either as $x or via new($string)), and they don't change the +# value. The two tests below peek at this by using _a (illegally) directly +$x = $mbi->new(-123401); $x->{_a} = 4; is ($x->babs(),123401); +$x = $mbi->new(-123401); $x->{_a} = 4; is ($x->bneg(),123401); + +# test fdiv rounding to A and R (bug in v1.48 and maybe earlier versions) +$mbf->round_mode('even'); +$x = $mbf->new('740.7')->fdiv('6',4,undef,'zero'); is ($x,'123.4'); + +$x = $mbi->new('123456'); $y = $mbi->new('123456'); $y->{_a} = 6; +is ($x->bdiv($y),1); is ($x->{_a},6); # carried over + +$x = $mbi->new('123456'); $y = $mbi->new('123456'); $x->{_a} = 6; +is ($x->bdiv($y),1); is ($x->{_a},6); # carried over + +$x = $mbi->new('123456'); $y = $mbi->new('223456'); $y->{_a} = 6; +is ($x->bdiv($y),0); is ($x->{_a},6); # carried over + +$x = $mbi->new('123456'); $y = $mbi->new('223456'); $x->{_a} = 6; +is ($x->bdiv($y),0); is ($x->{_a},6); # carried over + +############################################################################### +# test that bop(0) does the same than bop(undef) + +$x = $mbf->new('1234567890'); +is ($x->copy()->bsqrt(0),$x->copy()->bsqrt(undef)); +is ($x->copy->bsqrt(0),'35136.41828644462161665823116758077037159'); + +is ($x->{_a}, undef); + +# test that bsqrt() modifies $x and does not just return something else +# (especially under BareCalc) +$z = $x->bsqrt(); +is ($z,$x); is ($x,'35136.41828644462161665823116758077037159'); + +$x = $mbf->new('1.234567890123456789'); +is ($x->copy()->bpow('0.5',0),$x->copy()->bpow('0.5',undef)); +is ($x->copy()->bpow('0.5',0),$x->copy()->bsqrt(undef)); +is ($x->copy()->bpow('2',0),'1.524157875323883675019051998750190521'); + +############################################################################### +# test (also under Bare) that bfac() rounds at last step + +is ($mbi->new(12)->bfac(),'479001600'); +is ($mbi->new(12)->bfac(2),'480000000'); +$x = $mbi->new(12); $x->accuracy(2); is ($x->bfac(),'480000000'); +$x = $mbi->new(13); $x->accuracy(2); is ($x->bfac(),'6200000000'); +$x = $mbi->new(13); $x->accuracy(3); is ($x->bfac(),'6230000000'); +$x = $mbi->new(13); $x->accuracy(4); is ($x->bfac(),'6227000000'); +# this does 1,2,3...9,10,11,12...20 +$x = $mbi->new(20); $x->accuracy(1); is ($x->bfac(),'2000000000000000000'); + +############################################################################### +# test bsqrt) rounding to given A/P/R (bug prior to v1.60) +$x = $mbi->new('123456')->bsqrt(2,undef); is ($x,'350'); # not 351 +$x = $mbi->new('3')->bsqrt(2,undef); is ($x->accuracy(),2); + +$mbi->round_mode('even'); $x = $mbi->new('126025')->bsqrt(2,undef,'+inf'); +is ($x,'360'); # not 355 nor 350 + +$x = $mbi->new('126025')->bsqrt(undef,2); is ($x,'400'); # not 355 + + +############################################################################### +# test mixed arguments + +$x = $mbf->new(10); +$u = $mbf->new(2.5); +$y = $mbi->new(2); + +$z = $x + $y; is ($z,12); is (ref($z),$mbf); +$z = $x / $y; is ($z,5); is (ref($z),$mbf); +$z = $u * $y; is ($z,5); is (ref($z),$mbf); + +$y = $mbi->new(12345); +$z = $u->copy()->bmul($y,2,undef,'odd'); is ($z,31000); +$z = $u->copy()->bmul($y,3,undef,'odd'); is ($z,30900); +$z = $u->copy()->bmul($y,undef,0,'odd'); is ($z,30863); +$z = $u->copy()->bmul($y,undef,1,'odd'); is ($z,30863); +$z = $u->copy()->bmul($y,undef,2,'odd'); is ($z,30860); +$z = $u->copy()->bmul($y,undef,3,'odd'); is ($z,30900); +$z = $u->copy()->bmul($y,undef,-1,'odd'); is ($z,30862.5); + +my $warn = ''; $SIG{__WARN__} = sub { $warn = shift; }; +# these should no longer warn, even tho '3.17' is a NaN in BigInt (>= returns +# now false, bug until v1.80) +$warn = ''; eval "\$z = 3.17 <= \$y"; is ($z, ''); +unlike ($warn, qr/^Use of uninitialized value (\$y )?(in numeric le \(<=\) |)at/); +$warn = ''; eval "\$z = \$y >= 3.17"; is ($z, ''); +unlike ($warn, qr/^Use of uninitialized value (\$y )?(in numeric ge \(>=\) |)at/); + +# XXX TODO breakage: +# $z = $y->copy()->bmul($u,2,0,'odd'); is ($z,31000); +# $z = $y * $u; is ($z,5); is (ref($z),$mbi); +# $z = $y + $x; is ($z,12); is (ref($z),$mbi); +# $z = $y / $x; is ($z,0); is (ref($z),$mbi); + +############################################################################### +# rounding in bdiv with fallback and already set A or P + +{ + no strict 'refs'; + ${"$mbf\::accuracy"} = undef; + ${"$mbf\::precision"} = undef; + ${"$mbf\::div_scale"} = 40; +} + + $x = $mbf->new(10); $x->{_a} = 4; + is ($x->bdiv(3),'3.333'); + is ($x->{_a},4); # set's it since no fallback + +$x = $mbf->new(10); $x->{_a} = 4; $y = $mbf->new(3); +is ($x->bdiv($y),'3.333'); +is ($x->{_a},4); # set's it since no fallback + +# rounding to P of x +$x = $mbf->new(10); $x->{_p} = -2; +is ($x->bdiv(3),'3.33'); + +# round in div with requested P +$x = $mbf->new(10); +is ($x->bdiv(3,undef,-2),'3.33'); + +# round in div with requested P greater than fallback +{ + no strict 'refs'; + ${"$mbf\::div_scale"} = 5; + $x = $mbf->new(10); + is ($x->bdiv(3,undef,-8),'3.33333333'); + ${"$mbf\::div_scale"} = 40; +} + +$x = $mbf->new(10); $y = $mbf->new(3); $y->{_a} = 4; +is ($x->bdiv($y),'3.333'); +is ($x->{_a},4); is ($y->{_a},4); # set's it since no fallback +is ($x->{_p}, undef); is ($y->{_p}, undef); + +# rounding to P of y +$x = $mbf->new(10); $y = $mbf->new(3); $y->{_p} = -2; +is ($x->bdiv($y),'3.33'); +is ($x->{_p},-2); + is ($y->{_p},-2); +is ($x->{_a}, undef); is ($y->{_a}, undef); + +############################################################################### +# test whether bround(-n) fails in MBF (undocumented in MBI) +eval { $x = $mbf->new(1); $x->bround(-2); }; +like ($@, qr/^bround\(\) needs positive accuracy/); + +# test whether rounding to higher accuracy is no-op +$x = $mbf->new(1); $x->{_a} = 4; +is ($x,'1.000'); +$x->bround(6); # must be no-op +is ($x->{_a},4); +is ($x,'1.000'); + +$x = $mbi->new(1230); $x->{_a} = 3; +is ($x,'1230'); +$x->bround(6); # must be no-op +is ($x->{_a},3); +is ($x,'1230'); + +# bround(n) should set _a +$x->bround(2); # smaller works +is ($x,'1200'); +is ($x->{_a},2); + +# bround(-n) is undocumented and only used by MBF +# bround(-n) should set _a +$x = $mbi->new(12345); +$x->bround(-1); +is ($x,'12300'); +is ($x->{_a},4); + +# bround(-n) should set _a +$x = $mbi->new(12345); +$x->bround(-2); +is ($x,'12000'); +is ($x->{_a},3); + +# bround(-n) should set _a +$x = $mbi->new(12345); $x->{_a} = 5; +$x->bround(-3); +is ($x,'10000'); +is ($x->{_a},2); + +# bround(-n) should set _a +$x = $mbi->new(12345); $x->{_a} = 5; +$x->bround(-4); +is ($x,'0'); +is ($x->{_a},1); + +# bround(-n) should be noop if n too big +$x = $mbi->new(12345); +$x->bround(-5); +is ($x,'0'); # scale to "big" => 0 +is ($x->{_a},0); + +# bround(-n) should be noop if n too big +$x = $mbi->new(54321); +$x->bround(-5); +is ($x,'100000'); # used by MBF to round 0.0054321 at 0.0_6_00000 +is ($x->{_a},0); + +# bround(-n) should be noop if n too big +$x = $mbi->new(54321); $x->{_a} = 5; +$x->bround(-6); +is ($x,'100000'); # no-op +is ($x->{_a},0); + +# bround(n) should set _a +$x = $mbi->new(12345); $x->{_a} = 5; +$x->bround(5); # must be no-op +is ($x,'12345'); +is ($x->{_a},5); + +# bround(n) should set _a +$x = $mbi->new(12345); $x->{_a} = 5; +$x->bround(6); # must be no-op +is ($x,'12345'); + +$x = $mbf->new('0.0061'); $x->bfround(-2); is ($x,'0.01'); +$x = $mbf->new('0.004'); $x->bfround(-2); is ($x,'0.00'); +$x = $mbf->new('0.005'); $x->bfround(-2); is ($x,'0.00'); + +$x = $mbf->new('12345'); $x->bfround(2); is ($x,'12340'); +$x = $mbf->new('12340'); $x->bfround(2); is ($x,'12340'); + +# MBI::bfround should clear A for negative P +$x = $mbi->new('1234'); $x->accuracy(3); $x->bfround(-2); +is ($x->{_a}, undef); + +# test that bfround() and bround() work with large numbers + +$x = $mbf->new(1)->bdiv(5678,undef,-63); +is ($x, '0.000176118351532229658330398027474462839027826699542092286016203'); + +$x = $mbf->new(1)->bdiv(5678,undef,-90); +is ($x, '0.000176118351532229658330398027474462839027826699542092286016202888340965128566396618527651'); + +$x = $mbf->new(1)->bdiv(5678,80); +is ($x, '0.00017611835153222965833039802747446283902782669954209228601620288834096512856639662'); + +############################################################################### +# rounding with already set precision/accuracy + +$x = $mbf->new(1); $x->{_p} = -5; +is ($x,'1.00000'); + +# further rounding donw +is ($x->bfround(-2),'1.00'); +is ($x->{_p},-2); + +$x = $mbf->new(12345); $x->{_a} = 5; +is ($x->bround(2),'12000'); +is ($x->{_a},2); + +$x = $mbf->new('1.2345'); $x->{_a} = 5; +is ($x->bround(2),'1.2'); +is ($x->{_a},2); + +# mantissa/exponent format and A/P +$x = $mbf->new('12345.678'); $x->accuracy(4); +is ($x,'12350'); is ($x->{_a},4); is ($x->{_p}, undef); + +#is ($x->{_m}->{_a}, undef); is ($x->{_e}->{_a}, undef); +#is ($x->{_m}->{_p}, undef); is ($x->{_e}->{_p}, undef); + +# check for no A/P in case of fallback +# result +$x = $mbf->new(100) / 3; +is ($x->{_a}, undef); is ($x->{_p}, undef); + +# result & remainder +$x = $mbf->new(100) / 3; ($x,$y) = $x->bdiv(3); +is ($x->{_a}, undef); is ($x->{_p}, undef); +is ($y->{_a}, undef); is ($y->{_p}, undef); + +############################################################################### +# math with two numbers with different A and P + +$x = $mbf->new(12345); $x->accuracy(4); # '12340' +$y = $mbf->new(12345); $y->accuracy(2); # '12000' +is ($x+$y,24000); # 12340+12000=> 24340 => 24000 + +$x = $mbf->new(54321); $x->accuracy(4); # '12340' +$y = $mbf->new(12345); $y->accuracy(3); # '12000' +is ($x-$y,42000); # 54320+12300=> 42020 => 42000 + +$x = $mbf->new('1.2345'); $x->precision(-2); # '1.23' +$y = $mbf->new('1.2345'); $y->precision(-4); # '1.2345' +is ($x+$y,'2.46'); # 1.2345+1.2300=> 2.4645 => 2.46 + +############################################################################### +# round should find and use proper class + +#$x = Foo->new(); +#is ($x->round($Foo::accuracy),'a' x $Foo::accuracy); +#is ($x->round(undef,$Foo::precision),'p' x $Foo::precision); +#is ($x->bfround($Foo::precision),'p' x $Foo::precision); +#is ($x->bround($Foo::accuracy),'a' x $Foo::accuracy); + +############################################################################### +# find out whether _find_round_parameters is doing what's it's supposed to do + +{ + no strict 'refs'; + ${"$mbi\::accuracy"} = undef; + ${"$mbi\::precision"} = undef; + ${"$mbi\::div_scale"} = 40; + ${"$mbi\::round_mode"} = 'odd'; +} + +$x = $mbi->new(123); +my @params = $x->_find_round_parameters(); +is (scalar @params,1); # nothing to round + +@params = $x->_find_round_parameters(1); +is (scalar @params,4); # a=1 +is ($params[0],$x); # self +is ($params[1],1); # a +is ($params[2], undef); # p +is ($params[3],'odd'); # round_mode + +@params = $x->_find_round_parameters(undef,2); +is (scalar @params,4); # p=2 +is ($params[0],$x); # self +is ($params[1], undef); # a +is ($params[2],2); # p +is ($params[3],'odd'); # round_mode + +eval { @params = $x->_find_round_parameters(undef,2,'foo'); }; +like ($@, qr/^Unknown round mode 'foo'/); + +@params = $x->_find_round_parameters(undef,2,'+inf'); +is (scalar @params,4); # p=2 +is ($params[0],$x); # self +is ($params[1], undef); # a +is ($params[2],2); # p +is ($params[3],'+inf'); # round_mode + +@params = $x->_find_round_parameters(2,-2,'+inf'); +is (scalar @params,1); # error, A and P defined +is ($params[0],$x); # self + +{ + no strict 'refs'; + ${"$mbi\::accuracy"} = 1; + @params = $x->_find_round_parameters(undef,-2); + is (scalar @params,1); # error, A and P defined + is ($params[0],$x); # self + is ($x->is_nan(),1); # and must be NaN + + ${"$mbi\::accuracy"} = undef; + ${"$mbi\::precision"} = 1; + @params = $x->_find_round_parameters(1,undef); + is (scalar @params,1); # error, A and P defined + is ($params[0],$x); # self + is ($x->is_nan(),1); # and must be NaN + + ${"$mbi\::precision"} = undef; # reset +} + +############################################################################### +# test whether bone/bzero take additional A & P, or reset it etc + +foreach my $c ($mbi,$mbf) + { + $x = $c->new(2)->bzero(); is ($x->{_a}, undef); is ($x->{_p}, undef); + $x = $c->new(2)->bone(); is ($x->{_a}, undef); is ($x->{_p}, undef); + $x = $c->new(2)->binf(); is ($x->{_a}, undef); is ($x->{_p}, undef); + $x = $c->new(2)->bnan(); is ($x->{_a}, undef); is ($x->{_p}, undef); + + $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->bnan(); + is ($x->{_a}, undef); is ($x->{_p}, undef); + $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->binf(); + is ($x->{_a}, undef); is ($x->{_p}, undef); + + $x = $c->new(2,1); is ($x->{_a},1); is ($x->{_p}, undef); + $x = $c->new(2,undef,1); is ($x->{_a}, undef); is ($x->{_p},1); + + $x = $c->new(2,1)->bzero(); is ($x->{_a},1); is ($x->{_p}, undef); + $x = $c->new(2,undef,1)->bzero(); is ($x->{_a}, undef); is ($x->{_p},1); + + $x = $c->new(2,1)->bone(); is ($x->{_a},1); is ($x->{_p}, undef); + $x = $c->new(2,undef,1)->bone(); is ($x->{_a}, undef); is ($x->{_p},1); + + $x = $c->new(2); $x->bone('+',2,undef); is ($x->{_a},2); is ($x->{_p}, undef); + $x = $c->new(2); $x->bone('+',undef,2); is ($x->{_a}, undef); is ($x->{_p},2); + $x = $c->new(2); $x->bone('-',2,undef); is ($x->{_a},2); is ($x->{_p}, undef); + $x = $c->new(2); $x->bone('-',undef,2); is ($x->{_a}, undef); is ($x->{_p},2); + + $x = $c->new(2); $x->bzero(2,undef); is ($x->{_a},2); is ($x->{_p}, undef); + $x = $c->new(2); $x->bzero(undef,2); is ($x->{_a}, undef); is ($x->{_p},2); + } + +############################################################################### +# test whether bone/bzero honour globals + +for my $c ($mbi,$mbf) + { + $c->accuracy(2); + $x = $c->bone(); is ($x->accuracy(),2); + $x = $c->bzero(); is ($x->accuracy(),2); + $c->accuracy(undef); + + $c->precision(-2); + $x = $c->bone(); is ($x->precision(),-2); + $x = $c->bzero(); is ($x->precision(),-2); + $c->precision(undef); + } + +############################################################################### +# check whether mixing A and P creates a NaN + +# new with set accuracy/precision and with parameters +{ + no strict 'refs'; + foreach my $c ($mbi,$mbf) + { + is ($c->new(123,4,-3),'NaN'); # with parameters + ${"$c\::accuracy"} = 42; + ${"$c\::precision"} = 2; + is ($c->new(123),'NaN'); # with globals + ${"$c\::accuracy"} = undef; + ${"$c\::precision"} = undef; + } +} + +# binary ops +foreach my $class ($mbi,$mbf) + { + foreach (qw/add sub mul pow mod/) + #foreach (qw/add sub mul div pow mod/) + { + my $try = "my \$x = $class->new(1234); \$x->accuracy(5); "; + $try .= "my \$y = $class->new(12); \$y->precision(-3); "; + $try .= "\$x->b$_(\$y);"; + $rc = eval $try; + print "# Tried: '$try'\n" if !is ($rc, 'NaN'); + } + } + +# unary ops +foreach (qw/new bsqrt/) + { + my $try = 'my $x = $mbi->$_(1234,5,-3); '; + $rc = eval $try; + print "# Tried: '$try'\n" if !is ($rc, 'NaN'); + } + +# see if $x->bsub(0) and $x->badd(0) really round +foreach my $class ($mbi,$mbf) + { + $x = $class->new(123); $class->accuracy(2); $x->bsub(0); + is ($x,120); + $class->accuracy(undef); + $x = $class->new(123); $class->accuracy(2); $x->badd(0); + is ($x,120); + $class->accuracy(undef); + } + +############################################################################### +# test whether shortcuts returning zero/one preserve A and P + +my ($ans1,$f,$a,$p,$xp,$yp,$xa,$ya,$try,$ans,@args); +my $CALC = Math::BigInt->config()->{lib}; +while () + { + $_ =~ s/[\n\r]//g; # remove newlines + next if /^\s*(#|$)/; # skip comments and empty lines + if (s/^&//) + { + $f = $_; next; # function + } + @args = split(/:/,$_,99); + my $ans = pop(@args); + + ($x,$xa,$xp) = split (/,/,$args[0]); + $xa = $xa || ''; $xp = $xp || ''; + $try = "\$x = $mbi->new('$x'); "; + $try .= "\$x->accuracy($xa); " if $xa ne ''; + $try .= "\$x->precision($xp); " if $xp ne ''; + + ($y,$ya,$yp) = split (/,/,$args[1]); + $ya = $ya || ''; $yp = $yp || ''; + $try .= "\$y = $mbi->new('$y'); "; + $try .= "\$y->accuracy($ya); " if $ya ne ''; + $try .= "\$y->precision($yp); " if $yp ne ''; + + $try .= "\$x->$f(\$y);"; + + # print "trying $try\n"; + $rc = eval $try; + # convert hex/binary targets to decimal + if ($ans =~ /^(0x0x|0b0b)/) + { + $ans =~ s/^0[xb]//; + $ans = $mbi->new($ans)->bstr(); + } + print "# Tried: '$try'\n" if !is ($rc, $ans); + # check internal state of number objects + is_valid($rc,$f) if ref $rc; + + # now check whether A and P are set correctly + # only one of $a or $p will be set (no crossing here) + $a = $xa || $ya; $p = $xp || $yp; + + # print "Check a=$a p=$p\n"; + # print "# Tried: '$try'\n"; + if ($a ne '') + { + if (!(is ($x->{_a}, $a) && is ($x->{_p}, undef))) + { + print "# Check: A=$a and P=undef\n"; + print "# Tried: '$try'\n"; + } + } + if ($p ne '') + { + if (!(is ($x->{_p}, $p) && is($x->{_a}, undef))) + { + print "# Check: A=undef and P=$p\n"; + print "# Tried: '$try'\n"; + } + } + } + +# all done +1; + +############################################################################### +# sub to check validity of a BigInt internally, to ensure that no op leaves a +# number object in an invalid state (f.i. "-0") + +sub is_valid + { + my ($x,$f) = @_; + + my $e = 0; # error? + # ok as reference? + $e = 'Not a reference' if !ref($x); + + # has ok sign? + $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" + if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; + + $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; + $e = $CALC->_check($x->{value}) if $e eq '0'; + + # test done, see if error did crop up + is (1,1), return if ($e eq '0'); + + is (1,$e." after op '$f'"); + } + +# format is: +# x,A,P:x,A,P:result +# 123,,3 means 123 with precision 3 (A is undef) +# the A or P of the result is calculated automatically +__DATA__ +&badd +123,,:123,,:246 +123,3,:0,,:123 +123,,-3:0,,:123 +123,,:0,3,:123 +123,,:0,,-3:123 +&bmul +123,,:1,,:123 +123,3,:0,,:0 +123,,-3:0,,:0 +123,,:0,3,:0 +123,,:0,,-3:0 +123,3,:1,,:123 +123,,-3:1,,:123 +123,,:1,3,:123 +123,,:1,,-3:123 +1,3,:123,,:123 +1,,-3:123,,:123 +1,,:123,3,:123 +1,,:123,,-3:123 +&bdiv +123,,:1,,:123 +123,4,:1,,:123 +123,,:1,4,:123 +123,,:1,,-4:123 +123,,-4:1,,:123 +1,4,:123,,:0 +1,,:123,4,:0 +1,,:123,,-4:0 +1,,-4:123,,:0 +&band +1,,:3,,:1 +1234,1,:0,,:0 +1234,,:0,1,:0 +1234,,-1:0,,:0 +1234,,:0,,-1:0 +0xFF,,:0x10,,:0x0x10 +0xFF,2,:0xFF,,:250 +0xFF,,:0xFF,2,:250 +0xFF,,1:0xFF,,:250 +0xFF,,:0xFF,,1:250 +&bxor +1,,:3,,:2 +1234,1,:0,,:1000 +1234,,:0,1,:1000 +1234,,3:0,,:1000 +1234,,:0,,3:1000 +0xFF,,:0x10,,:239 +# 250 ^ 255 => 5 +0xFF,2,:0xFF,,:5 +0xFF,,:0xFF,2,:5 +0xFF,,1:0xFF,,:5 +0xFF,,:0xFF,,1:5 +# 250 ^ 4095 = 3845 => 3800 +0xFF,2,:0xFFF,,:3800 +# 255 ^ 4100 = 4347 => 4300 +0xFF,,:0xFFF,2,:4300 +0xFF,,2:0xFFF,,:3800 +# 255 ^ 4100 = 10fb => 4347 => 4300 +0xFF,,:0xFFF,,2:4300 +&bior +1,,:3,,:3 +1234,1,:0,,:1000 +1234,,:0,1,:1000 +1234,,3:0,,:1000 +1234,,:0,,3:1000 +0xFF,,:0x10,,:0x0xFF +# FF | FA = FF => 250 +250,2,:0xFF,,:250 +0xFF,,:250,2,:250 +0xFF,,1:0xFF,,:250 +0xFF,,:0xFF,,1:250 +&bpow +2,,:3,,:8 +2,,:0,,:1 +2,2,:0,,:1 +2,,:0,2,:1 diff --git a/cpan/Math-BigInt/t/mbimbf.t b/cpan/Math-BigInt/t/mbimbf.t new file mode 100644 index 0000000000..7d8afb07c9 --- /dev/null +++ b/cpan/Math-BigInt/t/mbimbf.t @@ -0,0 +1,78 @@ +#!/usr/bin/perl -w + +# test rounding, accuracy, precision and fallback, round_mode and mixing +# of classes + +use strict; +use Test::More tests => 684 + + 26; # own tests + +use Math::BigInt 1.70; +use Math::BigFloat 1.43; + +use vars qw/$mbi $mbf/; + +$mbi = 'Math::BigInt'; +$mbf = 'Math::BigFloat'; + +require 't/mbimbf.inc'; + +# some tests that won't work with subclasses, since the things are only +# guaranteed in the Math::BigInt/BigFloat (unless subclass chooses to support +# this) + +Math::BigInt->round_mode('even'); # reset for tests +Math::BigFloat->round_mode('even'); # reset for tests + +is ($Math::BigInt::rnd_mode,'even'); +is ($Math::BigFloat::rnd_mode,'even'); + +my $x = eval '$mbi->round_mode("huhmbi");'; +like ($@, qr/^Unknown round mode 'huhmbi' at/); + +$x = eval '$mbf->round_mode("huhmbf");'; +like ($@, qr/^Unknown round mode 'huhmbf' at/); + +# old way (now with test for validity) +$x = eval '$Math::BigInt::rnd_mode = "huhmbi";'; +like ($@, qr/^Unknown round mode 'huhmbi' at/); +$x = eval '$Math::BigFloat::rnd_mode = "huhmbf";'; +like ($@, qr/^Unknown round mode 'huhmbf' at/); +# see if accessor also changes old variable +$mbi->round_mode('odd'); is ($Math::BigInt::rnd_mode,'odd'); +$mbf->round_mode('odd'); is ($Math::BigInt::rnd_mode,'odd'); + +foreach my $class (qw/Math::BigInt Math::BigFloat/) + { + is ($class->accuracy(5),5); # set A + is ($class->precision(), undef); # and now P must be cleared + is ($class->precision(5),5); # set P + is ($class->accuracy(), undef); # and now A must be cleared + } + +foreach my $class (qw/Math::BigInt Math::BigFloat/) + { + $class->accuracy(42); + my $x = $class->new(123); # $x gets A of 42, too! + is ($x->accuracy(),42); # really? + is ($x->accuracy(undef),42); # $x has no A, but the + # global is still in effect for $x + # so the return value of that operation should + # be 42, not undef + is ($x->accuracy(),42); # so $x should still have A = 42 + $class->accuracy(undef); # reset for further tests + $class->precision(undef); + } +# bug with flog(Math::BigFloat,Math::BigInt) +$x = Math::BigFloat->new(100); +$x = $x->blog(Math::BigInt->new(10)); + +is ($x,2); + +# bug until v1.88 for sqrt() with enough digits +for my $i (80,88,100) + { + $x = Math::BigFloat->new("1." . ("0" x $i) . "1"); + $x = $x->bsqrt; + is ($x, 1); + } diff --git a/cpan/Math-BigInt/t/nan_cmp.t b/cpan/Math-BigInt/t/nan_cmp.t new file mode 100644 index 0000000000..983edcbddc --- /dev/null +++ b/cpan/Math-BigInt/t/nan_cmp.t @@ -0,0 +1,36 @@ +#!/usr/bin/perl -w + +# test that overloaded compare works when NaN are involved + +use strict; +use Test::More tests => 26; + +use Math::BigInt; +use Math::BigFloat; + +compare (Math::BigInt->bnan(), Math::BigInt->bone() ); +compare (Math::BigFloat->bnan(), Math::BigFloat->bone() ); + +sub compare + { + my ($nan, $one) = @_; + + is ($one, $one, '1 == 1'); + + is ($one != $nan, 1, "1 != NaN"); + is ($nan != $one, 1, "NaN != 1"); + is ($nan != $nan, 1, "NaN != NaN"); + + is ($nan == $one, '', "NaN == 1"); + is ($one == $nan, '', "1 == NaN"); + is ($nan == $nan, '', "NaN == NaN"); + + is ($nan <= $one, '', "NaN <= 1"); + is ($one <= $nan, '', "1 <= NaN"); + is ($nan <= $nan, '', "NaN <= NaN"); + + is ($nan >= $one, '', "NaN >= 1"); + is ($one >= $nan, '', "1 >= NaN"); + is ($nan >= $nan, '', "NaN >= NaN"); + } + diff --git a/cpan/Math-BigInt/t/new_overloaded.t b/cpan/Math-BigInt/t/new_overloaded.t new file mode 100644 index 0000000000..08708dc557 --- /dev/null +++ b/cpan/Math-BigInt/t/new_overloaded.t @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +# Math::BigFloat->new had a bug where it would assume any object is a +# BigInt which broke overloaded non-BigInts. + +use Test::More tests => 4; + + +package Overloaded::Num; + +use overload '0+' => sub { ${$_[0]} }, + fallback => 1; +sub new { + my($class, $num) = @_; + return bless \$num, $class; +} + + +package main; + +use Math::BigFloat; + +my $overloaded_num = Overloaded::Num->new(2.23); +is $overloaded_num, 2.23; + +my $bigfloat = Math::BigFloat->new($overloaded_num); +is $bigfloat, 2.23, 'BigFloat->new accepts overloaded numbers'; + +my $bigint = Math::BigInt->new(Overloaded::Num->new(3)); +is $bigint, 3, 'BigInt->new accepts overloaded numbers'; + +is( Math::BigFloat->new($bigint), 3, 'BigFloat from BigInt' ); diff --git a/cpan/Math-BigInt/t/req_mbf0.t b/cpan/Math-BigInt/t/req_mbf0.t new file mode 100644 index 0000000000..4df4d4a24b --- /dev/null +++ b/cpan/Math-BigInt/t/req_mbf0.t @@ -0,0 +1,13 @@ +#!/usr/bin/perl -w + +# check that simple requiring BigFloat and then bzero() works + +use strict; +use Test::More tests => 1; + +require Math::BigFloat; +my $x = Math::BigFloat->bzero(); $x++; +is ($x,1, '$x is 1'); + +# all tests done + diff --git a/cpan/Math-BigInt/t/req_mbf1.t b/cpan/Math-BigInt/t/req_mbf1.t new file mode 100644 index 0000000000..ac8375c309 --- /dev/null +++ b/cpan/Math-BigInt/t/req_mbf1.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl -w + +# check that simple requiring BigFloat and then bone() works + +use strict; +use Test::More tests => 1; + +require Math::BigFloat; my $x = Math::BigFloat->bone(); is ($x,1); + +# all tests done diff --git a/cpan/Math-BigInt/t/req_mbfa.t b/cpan/Math-BigInt/t/req_mbfa.t new file mode 100644 index 0000000000..eb4d5e10cb --- /dev/null +++ b/cpan/Math-BigInt/t/req_mbfa.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl -w + +# check that simple requiring BigFloat and then bnan() works + +use strict; +use Test::More tests => 1; + +require Math::BigFloat; my $x = Math::BigFloat->bnan(1); is ($x,'NaN'); + +# all tests done diff --git a/cpan/Math-BigInt/t/req_mbfi.t b/cpan/Math-BigInt/t/req_mbfi.t new file mode 100644 index 0000000000..1ea5224c23 --- /dev/null +++ b/cpan/Math-BigInt/t/req_mbfi.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl -w + +# check that simple requiring BigFloat and then binf() works + +use strict; +use Test::More tests => 1; + +require Math::BigFloat; my $x = Math::BigFloat->binf(); is ($x,'inf'); + +# all tests done diff --git a/cpan/Math-BigInt/t/req_mbfn.t b/cpan/Math-BigInt/t/req_mbfn.t new file mode 100644 index 0000000000..1db441798a --- /dev/null +++ b/cpan/Math-BigInt/t/req_mbfn.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl -w + +# check that simple requiring BigFloat and then new() works + +use strict; +use Test::More tests => 1; + +require Math::BigFloat; my $x = Math::BigFloat->new(1); ++$x; is ($x,2); + +# all tests done diff --git a/cpan/Math-BigInt/t/req_mbfw.t b/cpan/Math-BigInt/t/req_mbfw.t new file mode 100644 index 0000000000..9b075c0a74 --- /dev/null +++ b/cpan/Math-BigInt/t/req_mbfw.t @@ -0,0 +1,23 @@ +#!/usr/bin/perl -w + +# check that requiring BigFloat and then calling import() works + +use strict; +use Test::More tests => 3; + +BEGIN { unshift @INC, 't'; } + +# normal require that calls import automatically (we thus have MBI afterwards) +require Math::BigFloat; +my $x = Math::BigFloat->new(1); ++$x; +is ($x,2, '$x is 2'); + +like (Math::BigFloat->config()->{with}, qr/^Math::BigInt::(Fast)?Calc\z/, 'with ignored' ); + +# now override +Math::BigFloat->import ( with => 'Math::BigInt::Subclass' ); + +# the "with" argument is ignored +like (Math::BigFloat->config()->{with}, qr/^Math::BigInt::(Fast)?Calc\z/, 'with ignored' ); + +# all tests done diff --git a/cpan/Math-BigInt/t/require.t b/cpan/Math-BigInt/t/require.t new file mode 100644 index 0000000000..66d9687a13 --- /dev/null +++ b/cpan/Math-BigInt/t/require.t @@ -0,0 +1,15 @@ +#!/usr/bin/perl -w + +# check that simple requiring BigInt works + +use strict; +use Test::More tests => 1; + +my ($x); + +require Math::BigInt; $x = Math::BigInt->new(1); ++$x; + +is ($x,2); + +# all tests done + diff --git a/cpan/Math-BigInt/t/round.t b/cpan/Math-BigInt/t/round.t new file mode 100644 index 0000000000..078e2d055b --- /dev/null +++ b/cpan/Math-BigInt/t/round.t @@ -0,0 +1,94 @@ +#!/usr/bin/perl -w + +# test rounding with non-integer A and P parameters + +use strict; +use Test::More tests => 95; + +use Math::BigFloat; + +my $cf = 'Math::BigFloat'; +my $ci = 'Math::BigInt'; + +my $x = $cf->new('123456.123456'); + +# unary ops with A +_do_a($x, 'round', 3, '123000'); +_do_a($x, 'bfround', 3, '123500'); +_do_a($x, 'bfround', 2, '123460'); +_do_a($x, 'bfround', -2, '123456.12'); +_do_a($x, 'bfround', -3, '123456.123'); + +_do_a($x, 'bround', 4, '123500'); +_do_a($x, 'bround', 3, '123000'); +_do_a($x, 'bround', 2, '120000'); + +_do_a($x, 'bsqrt', 4, '351.4'); +_do_a($x, 'bsqrt', 3, '351'); +_do_a($x, 'bsqrt', 2, '350'); + +# setting P +_do_p($x, 'bsqrt', 2, '350'); +_do_p($x, 'bsqrt', -2, '351.36'); + +# binary ops +_do_2_a($x, 'bdiv', 2, 6, '61728.1'); +_do_2_a($x, 'bdiv', 2, 4, '61730'); +_do_2_a($x, 'bdiv', 2, 3, '61700'); + +_do_2_p($x, 'bdiv', 2, -6, '61728.061728'); +_do_2_p($x, 'bdiv', 2, -4, '61728.0617'); +_do_2_p($x, 'bdiv', 2, -3, '61728.062'); + +# all tests done + +############################################################################# + +sub _do_a + { + my ($x, $method, $A, $result) = @_; + + is ($x->copy->$method($A), $result, "$method($A)"); + is ($x->copy->$method($A.'.1'), $result, "$method(${A}.1)"); + is ($x->copy->$method($A.'.5'), $result, "$method(${A}.5)"); + is ($x->copy->$method($A.'.6'), $result, "$method(${A}.6)"); + is ($x->copy->$method($A.'.9'), $result, "$method(${A}.9)"); + } + +sub _do_p + { + my ($x, $method, $P, $result) = @_; + + is ($x->copy->$method(undef,$P), $result, "$method(undef,$P)"); + is ($x->copy->$method(undef,$P.'.1'), $result, "$method(undef,${P}.1)"); + is ($x->copy->$method(undef,$P.'.5'), $result, "$method(undef.${P}.5)"); + is ($x->copy->$method(undef,$P.'.6'), $result, "$method(undef,${P}.6)"); + is ($x->copy->$method(undef,$P.'.9'), $result, "$method(undef,${P}.9)"); + } + +sub _do_2_a + { + my ($x, $method, $y, $A, $result) = @_; + + my $cy = $cf->new($y); + + is ($x->copy->$method($cy,$A), $result, "$method($cy,$A)"); + is ($x->copy->$method($cy,$A.'.1'), $result, "$method($cy,${A}.1)"); + is ($x->copy->$method($cy,$A.'.5'), $result, "$method($cy,${A}.5)"); + is ($x->copy->$method($cy,$A.'.6'), $result, "$method($cy,${A}.6)"); + is ($x->copy->$method($cy,$A.'.9'), $result, "$method($cy,${A}.9)"); + } + +sub _do_2_p + { + my ($x, $method, $y, $P, $result) = @_; + + my $cy = $cf->new($y); + + is ($x->copy->$method($cy,undef,$P), $result, "$method(undef,$P)"); + is ($x->copy->$method($cy,undef,$P.'.1'), $result, "$method($cy,undef,${P}.1)"); + is ($x->copy->$method($cy,undef,$P.'.5'), $result, "$method($cy,undef.${P}.5)"); + is ($x->copy->$method($cy,undef,$P.'.6'), $result, "$method($cy,undef,${P}.6)"); + is ($x->copy->$method($cy,undef,$P.'.9'), $result, "$method($cy,undef,${P}.9)"); + } + diff --git a/cpan/Math-BigInt/t/rt-16221.t b/cpan/Math-BigInt/t/rt-16221.t new file mode 100644 index 0000000000..a1dc2c6a3a --- /dev/null +++ b/cpan/Math-BigInt/t/rt-16221.t @@ -0,0 +1,77 @@ +#!/usr/bin/perl +# +# Verify that +# - Math::BigInt::objectify() calls as_int() (or as_number(), as a fallback) +# if the target object class is Math::BigInt. +# - Math::BigInt::objectify() calls as_float() if the target object class is +# Math::BigFloat. +# +# See RT #16221 and RT #52124. + +use strict; +use warnings; + +package main; + +use Test::More tests => 2; +use Math::BigInt; +use Math::BigFloat; + +############################################################################ + +my $int = Math::BigInt->new(10); +my $int_percent = My::Percent::Float->new(100); + +is($int * $int_percent, 10); + +############################################################################ + +my $float = Math::BigFloat->new(10); +my $float_percent = My::Percent::Float->new(100); + +is($float * $float_percent, 10); + +############################################################################ + +package My::Percent::Int; + +sub new { + my $class = shift; + my $num = shift; + return bless \$num, $class; +} + +sub as_number { + my $self = shift; + return Math::BigInt->new($$self / 100); +} + +sub as_string { + my $self = shift; + return $$self; +} + +############################################################################ + +package My::Percent::Float; + +sub new { + my $class = shift; + my $num = shift; + return bless \$num, $class; +} + +sub as_int { + my $self = shift; + return Math::BigInt->new($$self / 100); +} + +sub as_float { + my $self = shift; + return Math::BigFloat->new($$self / 100); +} + +sub as_string { + my $self = shift; + return $$self; +} diff --git a/cpan/Math-BigInt/t/sub_ali.t b/cpan/Math-BigInt/t/sub_ali.t new file mode 100644 index 0000000000..04512abd6c --- /dev/null +++ b/cpan/Math-BigInt/t/sub_ali.t @@ -0,0 +1,15 @@ +#!/usr/bin/perl -w + +# test that the new alias names work + +use strict; +use Test::More tests => 6; + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt::Subclass; + +use vars qw/$CL $x/; +$CL = 'Math::BigInt::Subclass'; + +require 't/alias.inc'; diff --git a/cpan/Math-BigInt/t/sub_mbf.t b/cpan/Math-BigInt/t/sub_mbf.t new file mode 100644 index 0000000000..fec4d0708a --- /dev/null +++ b/cpan/Math-BigInt/t/sub_mbf.t @@ -0,0 +1,33 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 2340 + + 6; # + our own tests + + +BEGIN { unshift @INC, 't'; } + +use Math::BigFloat::Subclass; + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigFloat::Subclass"; +$CL = Math::BigFloat->config()->{lib}; # "Math::BigInt::Calc"; or FastCalc + +require 't/bigfltpm.inc'; # perform same tests as bigfltpm + +############################################################################### +# Now do custom tests for Subclass itself +my $ms = $class->new(23); +print "# Missing custom attribute \$ms->{_custom}" if !is (1, $ms->{_custom}); + +# Check that subclass is a Math::BigFloat, but not a Math::Bigint +isa_ok ($ms, 'Math::BigFloat'); +isnt ($ms->isa('Math::BigInt'), 1); + +use Math::BigFloat; + +my $bf = Math::BigFloat->new(23); # same as other +$ms += $bf; +print "# Tried: \$ms += \$bf, got $ms" if !is (46, $ms); +print "# Missing custom attribute \$ms->{_custom}" if !is (1, $ms->{_custom}); +print "# Wrong class: ref(\$ms) was ".ref($ms) if !is ($class, ref($ms)); diff --git a/cpan/Math-BigInt/t/sub_mbi.t b/cpan/Math-BigInt/t/sub_mbi.t new file mode 100644 index 0000000000..b8e0a027ea --- /dev/null +++ b/cpan/Math-BigInt/t/sub_mbi.t @@ -0,0 +1,34 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 3649 + + 5; # +5 own tests + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt::Subclass; + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigInt::Subclass"; +$CL = "Math::BigInt::Calc"; + +my $version = '0.02'; # for $VERSION tests, match current release (by hand!) + +require 't/bigintpm.inc'; # perform same tests as bigintpm + +############################################################################### +# Now do custom tests for Subclass itself + +my $ms = $class->new(23); +print "# Missing custom attribute \$ms->{_custom}" if !is (1, $ms->{_custom}); + +# Check that a subclass is still considered a BigInt +isa_ok ($ms, 'Math::BigInt'); + +use Math::BigInt; + +my $bi = Math::BigInt->new(23); # same as other +$ms += $bi; +print "# Tried: \$ms += \$bi, got $ms" if !is (46, $ms); +print "# Missing custom attribute \$ms->{_custom}" if !is (1, $ms->{_custom}); +print "# Wrong class: ref(\$ms) was ".ref($ms) if !is ($class, ref($ms)); diff --git a/cpan/Math-BigInt/t/sub_mif.t b/cpan/Math-BigInt/t/sub_mif.t new file mode 100644 index 0000000000..6317e97cf3 --- /dev/null +++ b/cpan/Math-BigInt/t/sub_mif.t @@ -0,0 +1,19 @@ +#!/usr/bin/perl -w + +# test rounding, accuracy, precision and fallback, round_mode and mixing +# of classes + +use strict; +use Test::More tests => 684; + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt::Subclass; +use Math::BigFloat::Subclass; + +use vars qw/$mbi $mbf/; + +$mbi = 'Math::BigInt::Subclass'; +$mbf = 'Math::BigFloat::Subclass'; + +require 't/mbimbf.inc'; diff --git a/cpan/Math-BigInt/t/trap.t b/cpan/Math-BigInt/t/trap.t new file mode 100644 index 0000000000..c3348b3d1f --- /dev/null +++ b/cpan/Math-BigInt/t/trap.t @@ -0,0 +1,84 @@ +#!/usr/bin/perl -w + +# test that config ( trap_nan => 1, trap_inf => 1) really works/dies + +use Test::More tests => 43; +use strict; + +use Math::BigInt; +use Math::BigFloat; + +my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat'; +my ($cfg,$x); + +foreach my $class ($mbi, $mbf) + { + # can do and defaults are okay? + ok ($class->can('config'), 'can config()'); + is ($class->config()->{trap_nan}, 0, 'trap_nan defaults to 0'); + is ($class->config()->{trap_inf}, 0, 'trap_inf defaults to 0'); + + # can set? + $cfg = $class->config( trap_nan => 1 ); + is ($cfg->{trap_nan},1, 'trap_nan now true'); + + # also test that new() still works normally + eval ("\$x = \$class->new('42'); \$x->bnan();"); + like ($@, qr/^Tried to set/, 'died'); + is ($x,42,'$x after new() never modified'); + + # can reset? + $cfg = $class->config( trap_nan => 0 ); + is ($cfg->{trap_nan}, 0, 'trap_nan disabled'); + + # can set? + $cfg = $class->config( trap_inf => 1 ); + is ($cfg->{trap_inf}, 1, 'trap_inf enabled'); + + eval ("\$x = \$class->new('4711'); \$x->binf();"); + like ($@, qr/^Tried to set/, 'died'); + is ($x,4711,'$x after new() never modified'); + + eval ("\$x = \$class->new('inf');"); + like ($@, qr/^Tried to set/, 'died'); + is ($x,4711,'$x after new() never modified'); + + eval ("\$x = \$class->new('-inf');"); + like ($@, qr/^Tried to set/, 'died'); + is ($x,4711,'$x after new() never modified'); + + # +$x/0 => +inf + eval ("\$x = \$class->new('4711'); \$x->bdiv(0);"); + like ($@, qr/^Tried to set/, 'died'); + is ($x,4711,'$x after new() never modified'); + + # -$x/0 => -inf + eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);"); + like ($@, qr/^Tried to set/, 'died'); + is ($x,'-815', '$x after new not modified'); + + $cfg = $class->config( trap_nan => 1 ); + # 0/0 => NaN + eval ("\$x = \$class->new('0'); \$x->bdiv(0);"); + like ($@, qr/^Tried to set/, 'died'); + is ($x,'0', '$x after new not modified'); + } + +############################################################################## +# BigInt + +$x = Math::BigInt->new(2); +eval ("\$x = \$mbi->new('0.1');"); +is ($x,2,'never modified since it dies'); +eval ("\$x = \$mbi->new('0a.1');"); +is ($x,2,'never modified since it dies'); + +############################################################################## +# BigFloat + +$x = Math::BigFloat->new(2); +eval ("\$x = \$mbf->new('0.1a');"); +is ($x,2,'never modified since it dies'); + +# all tests done + diff --git a/cpan/Math-BigInt/t/upgrade.inc b/cpan/Math-BigInt/t/upgrade.inc new file mode 100644 index 0000000000..16ca05e44a --- /dev/null +++ b/cpan/Math-BigInt/t/upgrade.inc @@ -0,0 +1,1494 @@ +# include this file into another for subclass testing + +# This file is nearly identical to bigintpm.t, except that certain results +# are _requird_ to be different due to "upgrading" or "promoting" to BigFloat. +# The reverse is not true, any unmarked results can be either BigInt or +# BigFloat, depending on how good the internal optimization is (e.g. it +# is usually desirable to have 2 ** 2 return a BigInt, not a BigFloat). + +# Results that are required to be BigFloat are marked with C<^> at the end. + +# Please note that the testcount goes up by two for each extra result marked +# with ^, since then we test whether it has the proper class and that it left +# the upgrade variable alone. + +my $version = ${"$class\::VERSION"}; + +############################################################################## +# for testing inheritance of _swap + +package Math::Foo; + +use Math::BigInt lib => $main::CL; +use vars qw/@ISA/; +@ISA = (qw/Math::BigInt/); + +use overload +# customized overload for sub, since original does not use swap there +'-' => sub { my @a = ref($_[0])->_swap(@_); + $a[0]->bsub($a[1])}; + +sub _swap + { + # a fake _swap, which reverses the params + my $self = shift; # for override in subclass + if ($_[2]) + { + my $c = ref ($_[0] ) || 'Math::Foo'; + return ( $_[0]->copy(), $_[1] ); + } + else + { + return ( Math::Foo->new($_[1]), $_[0] ); + } + } + +############################################################################## +package main; + +my $CALC = $class->config()->{lib}; is ($CALC,$CL); + +my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class); + +while () + { + $_ =~ s/[\n\r]//g; # remove newlines + next if /^#/; # skip comments + if (s/^&//) + { + $f = $_; next; + } + elsif (/^\$/) + { + $round_mode = $_; $round_mode =~ s/^\$/$class\->/; next; + } + + @args = split(/:/,$_,99); $ans = pop(@args); + $expected_class = $class; + if ($ans =~ /\^$/) + { + $expected_class = $ECL; $ans =~ s/\^$//; + } + $try = "\$x = $class->new(\"$args[0]\");"; + if ($f eq "bnorm") + { + $try = "\$x = $class->bnorm(\"$args[0]\");"; + # some is_xxx tests + } elsif ($f =~ /^is_(zero|one|odd|even|negative|positive|nan|int)$/) { + $try .= "\$x->$f();"; + } elsif ($f eq "as_hex") { + $try .= '$x->as_hex();'; + } elsif ($f eq "as_bin") { + $try .= '$x->as_bin();'; + } elsif ($f eq "is_inf") { + $try .= "\$x->is_inf('$args[1]');"; + } elsif ($f eq "binf") { + $try .= "\$x->binf('$args[1]');"; + } elsif ($f eq "bone") { + $try .= "\$x->bone('$args[1]');"; + # some unary ops + } elsif ($f =~ /^b(nan|floor|ceil|int|sstr|neg|abs|inc|dec|not|sqrt|fac)$/) { + $try .= "\$x->$f();"; + } elsif ($f eq "length") { + $try .= '$x->length();'; + } elsif ($f eq "exponent"){ + # ->bstr() to see if an object is returned + $try .= '$x = $x->exponent()->bstr();'; + } elsif ($f eq "mantissa"){ + # ->bstr() to see if an object is returned + $try .= '$x = $x->mantissa()->bstr();'; + } elsif ($f eq "parts"){ + $try .= '($m,$e) = $x->parts();'; + # ->bstr() to see if an object is returned + $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;'; + $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;'; + $try .= '"$m,$e";'; + } else { + if ($args[1] !~ /\./) + { + $try .= "\$y = $class->new(\"$args[1]\");"; # BigInt + } + else + { + $try .= "\$y = $ECL->new(\"$args[1]\");"; # BigFloat + } + if ($f eq "bcmp") + { + $try .= '$x->bcmp($y);'; + } elsif ($f eq "bround") { + $try .= "$round_mode; \$x->bround(\$y);"; + } elsif ($f eq "broot") { + $try .= "\$x->broot(\$y);"; + } elsif ($f eq "bacmp"){ + $try .= '$x->bacmp($y);'; + } elsif ($f eq "badd"){ + $try .= '$x + $y;'; + } elsif ($f eq "bsub"){ + $try .= '$x - $y;'; + } elsif ($f eq "bmul"){ + $try .= '$x * $y;'; + } elsif ($f eq "bdiv"){ + $try .= '$x / $y;'; + } elsif ($f eq "bdiv-list"){ + $try .= 'join (",",$x->bdiv($y));'; + # overload via x= + } elsif ($f =~ /^.=$/){ + $try .= "\$x $f \$y;"; + # overload via x + } elsif ($f =~ /^.$/){ + $try .= "\$x $f \$y;"; + } elsif ($f eq "bmod"){ + $try .= '$x % $y;'; + } elsif ($f eq "bgcd") + { + if (defined $args[2]) + { + $try .= " \$z = $class->new('$args[2]'); "; + } + $try .= "$class\::bgcd(\$x, \$y"; + $try .= ", \$z" if (defined $args[2]); + $try .= " );"; + } + elsif ($f eq "blcm") + { + if (defined $args[2]) + { + $try .= " \$z = $class->new('$args[2]'); "; + } + $try .= "$class\::blcm(\$x, \$y"; + $try .= ", \$z" if (defined $args[2]); + $try .= " );"; + }elsif ($f eq "blsft"){ + if (defined $args[2]) + { + $try .= "\$x->blsft(\$y,$args[2]);"; + } + else + { + $try .= "\$x << \$y;"; + } + }elsif ($f eq "brsft"){ + if (defined $args[2]) + { + $try .= "\$x->brsft(\$y,$args[2]);"; + } + else + { + $try .= "\$x >> \$y;"; + } + }elsif ($f eq "band"){ + $try .= "\$x & \$y;"; + }elsif ($f eq "bior"){ + $try .= "\$x | \$y;"; + }elsif ($f eq "bxor"){ + $try .= "\$x ^ \$y;"; + }elsif ($f eq "bpow"){ + $try .= "\$x ** \$y;"; + }elsif ($f eq "digit"){ + $try = "\$x = $class->new('$args[0]'); \$x->digit($args[1]);"; + } else { warn "Unknown op '$f'"; } + } # end else all other ops + + $ans1 = eval $try; + # convert hex/binary targets to decimal + if ($ans =~ /^(0x0x|0b0b)/) + { + $ans =~ s/^0[xb]//; $ans = Math::BigInt->new($ans)->bstr(); + } + if ($ans eq "") + { + is ($ans1, undef); + } + else + { + # print "try: $try ans: $ans1 $ans\n"; + print "# Tried: '$try'\n" if !is ($ans1, $ans); + if ($expected_class ne $class) + { + is (ref($ans1),$expected_class); # also checks that it really is ref! + is ($Math::BigInt::upgrade,'Math::BigFloat'); # still okay? + } + } + # check internal state of number objects + is_valid($ans1,$f) if ref $ans1; + } # endwhile data tests +close DATA; + +my $warn = ''; $SIG{__WARN__} = sub { $warn = shift; }; + +# these should not warn +$warn = ''; eval "\$z = 3.17 <= \$y"; is ($z, 1); is ($warn, ''); +$warn = ''; eval "\$z = \$y >= 3.17"; is ($z, 1); is ($warn, ''); + +# all tests done + +1; + +############################################################################### +# sub to check validity of a BigInt internally, to ensure that no op leaves a +# number object in an invalid state (f.i. "-0") + +sub is_valid + { + my ($x,$f,$c) = @_; + + # The checks here are loosened a bit to allow BigInt or BigFloats to pass + + my $e = 0; # error? + # ok as reference? + # $e = "Not a reference to $c" if (ref($x) || '') ne $c; + + # has ok sign? + $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" + if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; + + $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; + #$e = $CALC->_check($x->{value}) if $e eq '0'; + + # test done, see if error did crop up + is (1,1), return if ($e eq '0'); + + is (1,$e." after op '$f'"); + } + +__DATA__ +&.= +1234:-345:1234-345 +&+= +1:2:3 +-1:-2:-3 +&-= +1:2:-1 +-1:-2:1 +&*= +2:3:6 +-1:5:-5 +&%= +100:3:1 +8:9:8 +&/= +100:3:33.33333333333333333333333333333333333333 +-8:2:-4 +&|= +2:1:3 +&&= +5:7:5 +&^= +5:7:2 +&is_negative +0:0 +-1:1 +1:0 ++inf:0 +-inf:1 +NaNneg:0 +&is_positive +0:0 +-1:0 +1:1 ++inf:1 +-inf:0 +NaNneg:0 +&is_odd +abc:0 +0:0 +1:1 +3:1 +-1:1 +-3:1 +10000001:1 +10000002:0 +2:0 +120:0 +121:1 +&is_int +NaN:0 +inf:0 +-inf:0 +1:1 +12:1 +123e12:1 +&is_even +abc:0 +0:1 +1:0 +3:0 +-1:0 +-3:0 +10000001:0 +10000002:1 +2:1 +120:1 +121:0 +&bacmp ++0:-0:0 ++0:+1:-1 +-1:+1:0 ++1:-1:0 +-1:+2:-1 ++2:-1:1 +-123456789:+987654321:-1 ++123456789:-987654321:-1 ++987654321:+123456789:1 +-987654321:+123456789:1 +-123:+4567889:-1 +# NaNs +acmpNaN:123: +123:acmpNaN: +acmpNaN:acmpNaN: +# infinity ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:0 +-inf:+inf:0 ++inf:123:1 +-inf:123:1 ++inf:-123:1 +-inf:-123:1 +# return undef ++inf:NaN: +NaN:inf: +-inf:NaN: +NaN:-inf: +&bnorm +123:123 +12.3:12.3^ +# binary input +0babc:NaN +0b123:NaN +0b0:0 +-0b0:0 +-0b1:-1 +0b0001:1 +0b001:1 +0b011:3 +0b101:5 +0b1001:9 +0b10001:17 +0b100001:33 +0b1000001:65 +0b10000001:129 +0b100000001:257 +0b1000000001:513 +0b10000000001:1025 +0b100000000001:2049 +0b1000000000001:4097 +0b10000000000001:8193 +0b100000000000001:16385 +0b1000000000000001:32769 +0b10000000000000001:65537 +0b100000000000000001:131073 +0b1000000000000000001:262145 +0b10000000000000000001:524289 +0b100000000000000000001:1048577 +0b1000000000000000000001:2097153 +0b10000000000000000000001:4194305 +0b100000000000000000000001:8388609 +0b1000000000000000000000001:16777217 +0b10000000000000000000000001:33554433 +0b100000000000000000000000001:67108865 +0b1000000000000000000000000001:134217729 +0b10000000000000000000000000001:268435457 +0b100000000000000000000000000001:536870913 +0b1000000000000000000000000000001:1073741825 +0b10000000000000000000000000000001:2147483649 +0b100000000000000000000000000000001:4294967297 +0b1000000000000000000000000000000001:8589934593 +0b10000000000000000000000000000000001:17179869185 +0b__101:NaN +0b1_0_1:5 +0b0_0_0_1:1 +# hex input +-0x0:0 +0xabcdefgh:NaN +0x1234:4660 +0xabcdef:11259375 +-0xABCDEF:-11259375 +-0x1234:-4660 +0x12345678:305419896 +0x1_2_3_4_56_78:305419896 +0xa_b_c_d_e_f:11259375 +0x__123:NaN +0x9:9 +0x11:17 +0x21:33 +0x41:65 +0x81:129 +0x101:257 +0x201:513 +0x401:1025 +0x801:2049 +0x1001:4097 +0x2001:8193 +0x4001:16385 +0x8001:32769 +0x10001:65537 +0x20001:131073 +0x40001:262145 +0x80001:524289 +0x100001:1048577 +0x200001:2097153 +0x400001:4194305 +0x800001:8388609 +0x1000001:16777217 +0x2000001:33554433 +0x4000001:67108865 +0x8000001:134217729 +0x10000001:268435457 +0x20000001:536870913 +0x40000001:1073741825 +0x80000001:2147483649 +0x100000001:4294967297 +0x200000001:8589934593 +0x400000001:17179869185 +0x800000001:34359738369 +# inf input +inf:inf ++inf:inf +-inf:-inf +0inf:NaN +# abnormal input +:NaN +abc:NaN + 1 a:NaN +1bcd2:NaN +11111b:NaN ++1z:NaN +-1z:NaN +# only one underscore between two digits +_123:NaN +_123_:NaN +123_:NaN +1__23:NaN +1E1__2:NaN +1_E12:NaN +1E_12:NaN +1_E_12:NaN ++_1E12:NaN ++0_1E2:100 ++0_0_1E2:100 +-0_0_1E2:-100 +-0_0_1E+0_0_2:-100 +E1:NaN +E23:NaN +1.23E1:12.3^ +1.23E-1:0.123^ +# bug with two E's in number being valid +1e2e3:NaN +1e2r:NaN +1e2.0:NaN +# leading zeros +012:12 +0123:123 +01234:1234 +012345:12345 +0123456:123456 +01234567:1234567 +012345678:12345678 +0123456789:123456789 +01234567891:1234567891 +012345678912:12345678912 +0123456789123:123456789123 +01234567891234:1234567891234 +# normal input +0:0 ++0:0 ++00:0 ++000:0 +000000000000000000:0 +-0:0 +-0000:0 ++1:1 ++01:1 ++001:1 ++00000100000:100000 +123456789:123456789 +-1:-1 +-01:-1 +-001:-1 +-123456789:-123456789 +-00000100000:-100000 +1_2_3:123 +10000000000E-1_0:1 +1E2:100 +1E1:10 +1E0:1 +1.23E2:123 +100E-1:10 +# floating point input +# .2e2:20 +1.E3:1000 +1.01E2:101 +1010E-1:101 +-1010E0:-1010 +-1010E1:-10100 +1234.00:1234 +# non-integer numbers +-1010E-2:-10.1^ +-1.01E+1:-10.1^ +-1.01E-1:-0.101^ +&bnan +1:NaN +2:NaN +abc:NaN +&bone +2:+:1 +2:-:-1 +boneNaN:-:-1 +boneNaN:+:1 +2:abc:1 +3::1 +&binf +1:+:inf +2:-:-inf +3:abc:inf +&is_nan +123:0 +abc:1 +NaN:1 +-123:0 +&is_inf ++inf::1 +-inf::1 +abc::0 +1::0 +NaN::0 +-1::0 ++inf:-:0 ++inf:+:1 +-inf:-:1 +-inf:+:0 +# it must be exactly /^[+-]inf$/ ++infinity::0 +-infinity::0 +&blsft +abc:abc:NaN ++2:+2:8 ++1:+32:4294967296 ++1:+48:281474976710656 ++8:-2:NaN +# exercise base 10 ++12345:4:10:123450000 +-1234:0:10:-1234 ++1234:0:10:1234 ++2:2:10:200 ++12:2:10:1200 ++1234:-3:10:NaN +1234567890123:12:10:1234567890123000000000000 +&brsft +abc:abc:NaN ++8:+2:2 ++4294967296:+32:1 ++281474976710656:+48:1 ++2:-2:NaN +# exercise base 10 +-1234:0:10:-1234 ++1234:0:10:1234 ++200:2:10:2 ++1234:3:10:1 ++1234:2:10:12 ++1234:-3:10:NaN +310000:4:10:31 +12300000:5:10:123 +1230000000000:10:10:123 +09876123456789067890:12:10:9876123 +1234561234567890123:13:10:123456 +&bsstr +1e+34:1e+34 +123.456E3:123456e+0 +100:1e+2 +abc:NaN +&bneg +bnegNaN:NaN ++inf:-inf +-inf:inf +abd:NaN +0:0 +1:-1 +-1:1 ++123456789:-123456789 +-123456789:123456789 +&babs +babsNaN:NaN ++inf:inf +-inf:inf +0:0 +1:1 +-1:1 ++123456789:123456789 +-123456789:123456789 +&bcmp +bcmpNaN:bcmpNaN: +bcmpNaN:0: +0:bcmpNaN: +0:0:0 +-1:0:-1 +0:-1:1 +1:0:1 +0:1:-1 +-1:1:-1 +1:-1:1 +-1:-1:0 +1:1:0 +123:123:0 +123:12:1 +12:123:-1 +-123:-123:0 +-123:-12:-1 +-12:-123:1 +123:124:-1 +124:123:1 +-123:-124:1 +-124:-123:-1 +100:5:1 +-123456789:987654321:-1 ++123456789:-987654321:1 +-987654321:123456789:-1 +-inf:5432112345:-1 ++inf:5432112345:1 +-inf:-5432112345:-1 ++inf:-5432112345:1 ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:1 +-inf:+inf:-1 +5:inf:-1 +5:inf:-1 +-5:-inf:1 +-5:-inf:1 +# return undef ++inf:NaN: +NaN:inf: +-inf:NaN: +NaN:-inf: +&binc +abc:NaN ++inf:inf +-inf:-inf ++0:1 ++1:2 +-1:0 +&bdec +abc:NaN ++inf:inf +-inf:-inf ++0:-1 ++1:0 +-1:-2 +&badd +abc:abc:NaN +abc:0:NaN ++0:abc:NaN ++inf:-inf:NaN +-inf:+inf:NaN ++inf:+inf:inf +-inf:-inf:-inf +baddNaN:+inf:NaN +baddNaN:+inf:NaN ++inf:baddNaN:NaN +-inf:baddNaN:NaN +0:0:0 +1:0:1 +0:1:1 +1:1:2 +-1:0:-1 +0:-1:-1 +-1:-1:-2 +-1:+1:0 ++1:-1:0 ++9:+1:10 ++99:+1:100 ++999:+1:1000 ++9999:+1:10000 ++99999:+1:100000 ++999999:+1:1000000 ++9999999:+1:10000000 ++99999999:+1:100000000 ++999999999:+1:1000000000 ++9999999999:+1:10000000000 ++99999999999:+1:100000000000 ++10:-1:9 ++100:-1:99 ++1000:-1:999 ++10000:-1:9999 ++100000:-1:99999 ++1000000:-1:999999 ++10000000:-1:9999999 ++100000000:-1:99999999 ++1000000000:-1:999999999 ++10000000000:-1:9999999999 ++123456789:987654321:1111111110 +-123456789:987654321:864197532 +-123456789:-987654321:-1111111110 ++123456789:-987654321:-864197532 +2:2.5:4.5^ +-123:-1.5:-124.5^ +-1.2:1:-0.2^ +&bsub +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:-inf:inf +-inf:+inf:-inf ++inf:+inf:NaN +-inf:-inf:NaN ++0:+0:0 ++1:+0:1 ++0:+1:-1 ++1:+1:0 +-1:+0:-1 ++0:-1:1 +-1:-1:0 +-1:+1:-2 ++1:-1:2 ++9:+1:8 ++99:+1:98 ++999:+1:998 ++9999:+1:9998 ++99999:+1:99998 ++999999:+1:999998 ++9999999:+1:9999998 ++99999999:+1:99999998 ++999999999:+1:999999998 ++9999999999:+1:9999999998 ++99999999999:+1:99999999998 ++10:-1:11 ++100:-1:101 ++1000:-1:1001 ++10000:-1:10001 ++100000:-1:100001 ++1000000:-1:1000001 ++10000000:-1:10000001 ++100000000:-1:100000001 ++1000000000:-1:1000000001 ++10000000000:-1:10000000001 ++123456789:+987654321:-864197532 +-123456789:+987654321:-1111111110 +-123456789:-987654321:864197532 ++123456789:-987654321:1111111110 +&bmul +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN +NaNmul:+inf:NaN +NaNmul:-inf:NaN +-inf:NaNmul:NaN ++inf:NaNmul:NaN ++inf:+inf:inf ++inf:-inf:-inf +-inf:+inf:-inf +-inf:-inf:inf ++0:+0:0 ++0:+1:0 ++1:+0:0 ++0:-1:0 +-1:+0:0 +123456789123456789:0:0 +0:123456789123456789:0 +-1:-1:1 +-1:+1:-1 ++1:-1:-1 ++1:+1:1 ++2:+3:6 +-2:+3:-6 ++2:-3:-6 +-2:-3:6 +111:111:12321 +10101:10101:102030201 +1001001:1001001:1002003002001 +100010001:100010001:10002000300020001 +10000100001:10000100001:100002000030000200001 +11111111111:9:99999999999 +22222222222:9:199999999998 +33333333333:9:299999999997 +44444444444:9:399999999996 +55555555555:9:499999999995 +66666666666:9:599999999994 +77777777777:9:699999999993 +88888888888:9:799999999992 +99999999999:9:899999999991 ++25:+25:625 ++12345:+12345:152399025 ++99999:+11111:1111088889 +9999:10000:99990000 +99999:100000:9999900000 +999999:1000000:999999000000 +9999999:10000000:99999990000000 +99999999:100000000:9999999900000000 +999999999:1000000000:999999999000000000 +9999999999:10000000000:99999999990000000000 +99999999999:100000000000:9999999999900000000000 +999999999999:1000000000000:999999999999000000000000 +9999999999999:10000000000000:99999999999990000000000000 +99999999999999:100000000000000:9999999999999900000000000000 +999999999999999:1000000000000000:999999999999999000000000000000 +9999999999999999:10000000000000000:99999999999999990000000000000000 +99999999999999999:100000000000000000:9999999999999999900000000000000000 +999999999999999999:1000000000000000000:999999999999999999000000000000000000 +9999999999999999999:10000000000000000000:99999999999999999990000000000000000000 +3:3.5:10.5^ +3.5:3:10.5^ +&bdiv-list +100:20:5,0 +4095:4095:1,0 +-4095:-4095:1,0 +4095:-4095:-1,0 +-4095:4095:-1,0 +123:2:61,1 +9:5:1,4 +9:4:2,1 +# inf handling and general remainder +5:8:0,5 +0:8:0,0 +11:2:5,1 +11:-2:-6,-1 +-11:2:-6,1 +# see table in documentation in MBI +0:inf:0,0 +0:-inf:0,0 +5:inf:0,5 +5:-inf:-1,-inf +-5:inf:-1,inf +-5:-inf:0,-5 +inf:5:inf,NaN +-inf:5:-inf,NaN +inf:-5:-inf,NaN +-inf:-5:inf,NaN +5:5:1,0 +-5:-5:1,0 +inf:inf:NaN,NaN +-inf:-inf:NaN,NaN +-inf:inf:NaN,NaN +inf:-inf:NaN,NaN +8:0:inf,8 +inf:0:inf,inf +# exceptions to remainder rule +-8:0:-inf,-8 +-inf:0:-inf,-inf +0:0:NaN,0 +&bdiv +abc:abc:NaN +abc:1:NaN +1:abc:NaN +0:0:NaN +# inf handling (see table in doc) +0:inf:0 +0:-inf:0 +5:inf:0 +5:-inf:-1 +-5:inf:-1 +-5:-inf:0 +inf:5:inf +-inf:5:-inf +inf:-5:-inf +-inf:-5:inf +5:5:1 +-5:-5:1 +inf:inf:NaN +-inf:-inf:NaN +-inf:inf:NaN +inf:-inf:NaN +8:0:inf +inf:0:inf +-8:0:-inf +-inf:0:-inf +11:2:5.5^ +-11:-2:5.5^ +-11:2:-5.5^ +11:-2:-5.5^ +0:1:0 +0:-1:0 +1:1:1 +-1:-1:1 +1:-1:-1 +-1:1:-1 +1:2:0.5^ +2:1:2 +1000000000:9:111111111.1111111111111111111111111111111^ +2000000000:9:222222222.2222222222222222222222222222222^ +3000000000:9:333333333.3333333333333333333333333333333^ +4000000000:9:444444444.4444444444444444444444444444444^ +5000000000:9:555555555.5555555555555555555555555555556^ +6000000000:9:666666666.6666666666666666666666666666667^ +7000000000:9:777777777.7777777777777777777777777777778^ +8000000000:9:888888888.8888888888888888888888888888889^ +9000000000:9:1000000000 +35500000:113:314159.2920353982300884955752212389380531^ +71000000:226:314159.2920353982300884955752212389380531^ +106500000:339:314159.2920353982300884955752212389380531^ +1000000000:3:333333333.3333333333333333333333333333333^ ++10:+5:2 ++100:+4:25 ++1000:+8:125 ++10000:+16:625 +999999999999:9:111111111111 +999999999999:99:10101010101 +999999999999:999:1001001001 +999999999999:9999:100010001 +999999999999999:99999:10000100001 ++1111088889:99999:11111 +-5:-3:1.666666666666666666666666666666666666667^ +-5:3:-1.666666666666666666666666666666666666667^ +4:3:1.333333333333333333333333333333333333333^ +4:-3:-1.333333333333333333333333333333333333333^ +1:3:0.3333333333333333333333333333333333333333^ +1:-3:-0.3333333333333333333333333333333333333333^ +-2:-3:0.6666666666666666666666666666666666666667^ +-2:3:-0.6666666666666666666666666666666666666667^ +8:5:1.6^ +-8:5:-1.6^ +14:-3:-4.666666666666666666666666666666666666667^ +-14:3:-4.666666666666666666666666666666666666667^ +-14:-3:4.666666666666666666666666666666666666667^ +14:3:4.666666666666666666666666666666666666667^ +# bug in Calc with '99999' vs $BASE-1 +#10000000000000000000000000000000000000000000000000000000000000000000000000000000000:10000000375084540248994272022843165711074:999999962491547381984643365663244474111576 +12:24:0.5^ +&bmod +# inf handling, see table in doc +0:inf:0 +0:-inf:0 +5:inf:5 +5:-inf:-inf +-5:inf:inf +-5:-inf:-5 +inf:5:NaN +-inf:5:NaN +inf:-5:NaN +-inf:-5:NaN +5:5:0 +-5:-5:0 +inf:inf:NaN +-inf:-inf:NaN +-inf:inf:NaN +inf:-inf:NaN +8:0:8 +inf:0:inf +-inf:0:-inf +-8:0:-8 +0:0:0 +abc:abc:NaN +abc:1:abc:NaN +1:abc:NaN +0:1:0 +1:0:1 +0:-1:0 +-1:0:-1 +1:1:0 +-1:-1:0 +1:-1:0 +-1:1:0 +1:2:1 +2:1:0 +1000000000:9:1 +2000000000:9:2 +3000000000:9:3 +4000000000:9:4 +5000000000:9:5 +6000000000:9:6 +7000000000:9:7 +8000000000:9:8 +9000000000:9:0 +35500000:113:33 +71000000:226:66 +106500000:339:99 +1000000000:3:1 +10:5:0 +100:4:0 +1000:8:0 +10000:16:0 +999999999999:9:0 +999999999999:99:0 +999999999999:999:0 +999999999999:9999:0 +999999999999999:99999:0 +-9:+5:1 ++9:-5:-1 +-9:-5:-4 +-5:3:1 +-2:3:1 +4:3:1 +1:3:1 +-5:-3:-2 +-2:-3:-2 +4:-3:-2 +1:-3:-2 +4095:4095:0 +100041000510123:3:0 +152403346:12345:4321 +9:5:4 +&bgcd +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++0:+0:0 ++0:+1:1 ++1:+0:1 ++1:+1:1 ++2:+3:1 ++3:+2:1 +-3:+2:1 +100:625:25 +4096:81:1 +1034:804:2 +27:90:56:1 +27:90:54:9 +&blcm +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++0:+0:NaN ++1:+0:0 ++0:+1:0 ++27:+90:270 ++1034:+804:415668 +&band +abc:abc:NaN +abc:0:NaN +0:abc:NaN +1:2:0 +3:2:2 ++8:+2:0 ++281474976710656:0:0 ++281474976710656:1:0 ++281474976710656:+281474976710656:281474976710656 +-2:-3:-4 +-1:-1:-1 +-6:-6:-6 +-7:-4:-8 +-7:4:0 +-4:7:4 +# equal arguments are treated special, so also do some test with unequal ones +0xFFFF:0xFFFF:0x0xFFFF +0xFFFFFF:0xFFFFFF:0x0xFFFFFF +0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF +0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF +0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF +0xF0F0:0xF0F0:0x0xF0F0 +0x0F0F:0x0F0F:0x0x0F0F +0xF0F0F0:0xF0F0F0:0x0xF0F0F0 +0x0F0F0F:0x0F0F0F:0x0x0F0F0F +0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 +0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F +0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 +0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F +0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 +0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F +0x1F0F0F0F0F0F:0x3F0F0F0F0F0F:0x0x1F0F0F0F0F0F +&bior +abc:abc:NaN +abc:0:NaN +0:abc:NaN +1:2:3 ++8:+2:10 ++281474976710656:0:281474976710656 ++281474976710656:1:281474976710657 ++281474976710656:281474976710656:281474976710656 +-2:-3:-1 +-1:-1:-1 +-6:-6:-6 +-7:4:-3 +-4:7:-1 +# equal arguments are treated special, so also do some test with unequal ones +0xFFFF:0xFFFF:0x0xFFFF +0xFFFFFF:0xFFFFFF:0x0xFFFFFF +0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF +0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF +0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF +0:0xFFFF:0x0xFFFF +0:0xFFFFFF:0x0xFFFFFF +0:0xFFFFFFFF:0x0xFFFFFFFF +0:0xFFFFFFFFFF:0x0xFFFFFFFFFF +0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF +0xFFFF:0:0x0xFFFF +0xFFFFFF:0:0x0xFFFFFF +0xFFFFFFFF:0:0x0xFFFFFFFF +0xFFFFFFFFFF:0:0x0xFFFFFFFFFF +0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF +0xF0F0:0xF0F0:0x0xF0F0 +0x0F0F:0x0F0F:0x0x0F0F +0xF0F0:0x0F0F:0x0xFFFF +0xF0F0F0:0xF0F0F0:0x0xF0F0F0 +0x0F0F0F:0x0F0F0F:0x0x0F0F0F +0x0F0F0F:0xF0F0F0:0x0xFFFFFF +0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 +0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F +0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF +0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 +0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F +0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF +0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 +0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F +0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF +0x1F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF +&bxor +abc:abc:NaN +abc:0:NaN +0:abc:NaN +1:2:3 ++8:+2:10 ++281474976710656:0:281474976710656 ++281474976710656:1:281474976710657 ++281474976710656:281474976710656:0 +-2:-3:3 +-1:-1:0 +-6:-6:0 +-7:4:-3 +-4:7:-5 +4:-7:-3 +-4:-7:5 +# equal arguments are treated special, so also do some test with unequal ones +0xFFFF:0xFFFF:0 +0xFFFFFF:0xFFFFFF:0 +0xFFFFFFFF:0xFFFFFFFF:0 +0xFFFFFFFFFF:0xFFFFFFFFFF:0 +0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0 +0:0xFFFF:0x0xFFFF +0:0xFFFFFF:0x0xFFFFFF +0:0xFFFFFFFF:0x0xFFFFFFFF +0:0xFFFFFFFFFF:0x0xFFFFFFFFFF +0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF +0xFFFF:0:0x0xFFFF +0xFFFFFF:0:0x0xFFFFFF +0xFFFFFFFF:0:0x0xFFFFFFFF +0xFFFFFFFFFF:0:0x0xFFFFFFFFFF +0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF +0xF0F0:0xF0F0:0 +0x0F0F:0x0F0F:0 +0xF0F0:0x0F0F:0x0xFFFF +0xF0F0F0:0xF0F0F0:0 +0x0F0F0F:0x0F0F0F:0 +0x0F0F0F:0xF0F0F0:0x0xFFFFFF +0xF0F0F0F0:0xF0F0F0F0:0 +0x0F0F0F0F:0x0F0F0F0F:0 +0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF +0xF0F0F0F0F0:0xF0F0F0F0F0:0 +0x0F0F0F0F0F:0x0F0F0F0F0F:0 +0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF +0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0 +0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0 +0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF +&bnot +abc:NaN ++0:-1 ++8:-9 ++281474976710656:-281474976710657 +-1:0 +-2:1 +-12:11 +&digit +0:0:0 +12:0:2 +12:1:1 +123:0:3 +123:1:2 +123:2:1 +123:-1:1 +123:-2:2 +123:-3:3 +123456:0:6 +123456:1:5 +123456:2:4 +123456:3:3 +123456:4:2 +123456:5:1 +123456:-1:1 +123456:-2:2 +123456:-3:3 +100000:-3:0 +100000:0:0 +100000:1:0 +&mantissa +abc:NaN +1e4:1 +2e0:2 +123:123 +-1:-1 +-2:-2 ++inf:inf +-inf:-inf +&exponent +abc:NaN +1e4:4 +2e0:0 +123:0 +-1:0 +-2:0 +0:1 ++inf:inf +-inf:inf +&parts +abc:NaN,NaN +1e4:1,4 +2e0:2,0 +123:123,0 +-1:-1,0 +-2:-2,0 +0:0,1 ++inf:inf,inf +-inf:-inf,inf +&bfac +-1:NaN +NaNfac:NaN ++inf:inf +-inf:NaN +0:1 +1:1 +2:2 +3:6 +4:24 +5:120 +6:720 +10:3628800 +11:39916800 +12:479001600 +&bpow +abc:12:NaN +12:abc:NaN +0:0:1 +0:1:0 +0:2:0 +0:-1:inf +0:-2:inf +1:0:1 +1:1:1 +1:2:1 +1:3:1 +1:-1:1 +1:-2:1 +1:-3:1 +2:0:1 +2:1:2 +2:2:4 +2:3:8 +3:3:27 +2:-1:0.5^ +-2:-1:-0.5^ +2:-2:0.25^ +# Y is even => result positive +-2:-2:0.25^ +# Y is odd => result negative +-2:-3:-0.125^ ++inf:1234500012:inf +-inf:1234500012:inf +-inf:1234500013:-inf ++inf:-12345000123:inf +-inf:-12345000123:-inf +# 1 ** -x => 1 / (1 ** x) +-1:0:1 +-2:0:1 +-1:1:-1 +-1:2:1 +-1:3:-1 +-1:4:1 +-1:5:-1 +-1:-1:-1 +-1:-2:1 +-1:-3:-1 +-1:-4:1 +-2:2:4 +-2:3:-8 +-2:4:16 +-2:5:-32 +-3:2:9 +-3:3:-27 +-3:4:81 +-3:5:-243 +10:2:100 +10:3:1000 +10:4:10000 +10:5:100000 +10:6:1000000 +10:7:10000000 +10:8:100000000 +10:9:1000000000 +10:20:100000000000000000000 +123456:2:15241383936 +#2:0.5:1.41^ +&length +100:3 +10:2 +1:1 +0:1 +12345:5 +10000000000000000:17 +-123:3 +215960156869840440586892398248:30 +# broot always upgrades +&broot +144:2:12^ +123:2:11.09053650640941716205160010260993291846^ +# bsqrt always upgrades +&bsqrt +145:12.04159457879229548012824103037860805243^ +144:12^ +143:11.95826074310139802112984075619561661399^ +16:4 +170:13.03840481040529742916594311485836883306^ +169:13 +168:12.96148139681572046193193487217599331541^ +4:2 +3:1.732050807568877293527446341505872366943^ +2:1.41421356237309504880168872420969807857^ +9:3 +12:3.464101615137754587054892683011744733886^ +256:16 +100000000:10000 +4000000000000:2000000 +152399026:12345.00004050222755607815159966235881398^ +152399025:12345 +152399024:12344.99995949777231103967404745303741942^ +1:1 +0:0 +-2:NaN +-123:NaN +Nan:NaN ++inf:inf +-inf:NaN +&bround +$round_mode('trunc') +0:12:0 +NaNbround:12:NaN ++inf:12:inf +-inf:12:-inf +1234:0:1234 +1234:2:1200 +123456:4:123400 +123456:5:123450 +123456:6:123456 ++10123456789:5:10123000000 +-10123456789:5:-10123000000 ++10123456789:9:10123456700 +-10123456789:9:-10123456700 ++101234500:6:101234000 +-101234500:6:-101234000 +#+101234500:-4:101234000 +#-101234500:-4:-101234000 +$round_mode('zero') ++20123456789:5:20123000000 +-20123456789:5:-20123000000 ++20123456789:9:20123456800 +-20123456789:9:-20123456800 ++201234500:6:201234000 +-201234500:6:-201234000 +#+201234500:-4:201234000 +#-201234500:-4:-201234000 ++12345000:4:12340000 +-12345000:4:-12340000 +$round_mode('+inf') ++30123456789:5:30123000000 +-30123456789:5:-30123000000 ++30123456789:9:30123456800 +-30123456789:9:-30123456800 ++301234500:6:301235000 +-301234500:6:-301234000 +#+301234500:-4:301235000 +#-301234500:-4:-301234000 ++12345000:4:12350000 +-12345000:4:-12340000 +$round_mode('-inf') ++40123456789:5:40123000000 +-40123456789:5:-40123000000 ++40123456789:9:40123456800 +-40123456789:9:-40123456800 ++401234500:6:401234000 ++401234500:6:401234000 +#-401234500:-4:-401235000 +#-401234500:-4:-401235000 ++12345000:4:12340000 +-12345000:4:-12350000 +$round_mode('odd') ++50123456789:5:50123000000 +-50123456789:5:-50123000000 ++50123456789:9:50123456800 +-50123456789:9:-50123456800 ++501234500:6:501235000 +-501234500:6:-501235000 +#+501234500:-4:501235000 +#-501234500:-4:-501235000 ++12345000:4:12350000 +-12345000:4:-12350000 +$round_mode('even') ++60123456789:5:60123000000 +-60123456789:5:-60123000000 ++60123456789:9:60123456800 +-60123456789:9:-60123456800 ++601234500:6:601234000 +-601234500:6:-601234000 +#+601234500:-4:601234000 +#-601234500:-4:-601234000 +#-601234500:-9:0 +#-501234500:-9:0 +#-601234500:-8:0 +#-501234500:-8:0 ++1234567:7:1234567 ++1234567:6:1234570 ++12345000:4:12340000 +-12345000:4:-12340000 +&is_zero +0:1 +NaNzero:0 ++inf:0 +-inf:0 +123:0 +-1:0 +1:0 +&is_one +0:0 +NaNone:0 ++inf:0 +-inf:0 +1:1 +2:0 +-1:0 +-2:0 +# floor and ceil are pretty pointless in integer space, but play safe +&bfloor +0:0 +NaNfloor:NaN ++inf:inf +-inf:-inf +-1:-1 +-2:-2 +2:2 +3:3 +abc:NaN +&bceil +NaNceil:NaN ++inf:inf +-inf:-inf +0:0 +-1:-1 +-2:-2 +2:2 +3:3 +abc:NaN +&bint +NaN:NaN ++inf:inf +-inf:-inf +0:0 +-1:-1 +-2:-2 +2:2 +3:3 +&as_hex +128:0x80 +-128:-0x80 +0:0x0 +-0:0x0 +1:0x1 +0x123456789123456789:0x123456789123456789 ++inf:inf +-inf:-inf +NaNas_hex:NaN +&as_bin +128:0b10000000 +-128:-0b10000000 +0:0b0 +-0:0b0 +1:0b1 +0b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101 ++inf:inf +-inf:-inf +NaNas_bin:NaN diff --git a/cpan/Math-BigInt/t/upgrade.t b/cpan/Math-BigInt/t/upgrade.t new file mode 100644 index 0000000000..d209879a89 --- /dev/null +++ b/cpan/Math-BigInt/t/upgrade.t @@ -0,0 +1,19 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 2124 + + 2; # our own tests + +use Math::BigInt upgrade => 'Math::BigFloat'; +use Math::BigFloat; + +use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup + $ECL $CL); +$class = "Math::BigInt"; +$CL = "Math::BigInt::Calc"; +$ECL = "Math::BigFloat"; + +is (Math::BigInt->upgrade(),'Math::BigFloat'); +is (Math::BigInt->downgrade()||'',''); + +require 't/upgrade.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/upgrade2.t b/cpan/Math-BigInt/t/upgrade2.t new file mode 100644 index 0000000000..cdc8d0edf4 --- /dev/null +++ b/cpan/Math-BigInt/t/upgrade2.t @@ -0,0 +1,14 @@ +#!/usr/bin/perl -w + +# Test 2 levels of upgrade classes. This used to cause a segv. + +use Test::More tests => 1; + +use Math::BigInt upgrade => 'Math::BigFloat'; +use Math::BigFloat upgrade => 'Math::BigMouse'; + +no warnings 'once'; +@Math::BigMouse::ISA = 'Math::BigFloat'; + +() = sqrt Math::BigInt->new(2); +pass('sqrt on a big int does not segv if there are 2 upgrade levels'); diff --git a/cpan/Math-BigInt/t/upgradef.t b/cpan/Math-BigInt/t/upgradef.t new file mode 100644 index 0000000000..611d9fad27 --- /dev/null +++ b/cpan/Math-BigInt/t/upgradef.t @@ -0,0 +1,57 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 6; + +############################################################################### +package Math::BigFloat::Test; + +use Math::BigFloat; +require Exporter; +use vars qw/@ISA/; +@ISA = qw/Exporter Math::BigFloat/; + +use overload; + +sub isa + { + my ($self,$class) = @_; + return if $class =~ /^Math::Big(Int|Float)/; # we aren't one of these + UNIVERSAL::isa($self,$class); + } + +sub bmul + { + return __PACKAGE__->new(123); + } + +sub badd + { + return __PACKAGE__->new(321); + } + +############################################################################### +package main; + +# use Math::BigInt upgrade => 'Math::BigFloat'; +use Math::BigFloat upgrade => 'Math::BigFloat::Test'; + +use vars qw ($scale $class $try $x $y $z $f @args $ans $ans1 $ans1_str $setup + $ECL $CL); +$class = "Math::BigFloat"; +$CL = "Math::BigInt::Calc"; +$ECL = "Math::BigFloat::Test"; + +is (Math::BigFloat->upgrade(),$ECL); +is (Math::BigFloat->downgrade()||'',''); + +$x = $class->new(123); $y = $ECL->new(123); $z = $x->bmul($y); +is (ref($z),$ECL); is ($z,123); + +$x = $class->new(123); $y = $ECL->new(123); $z = $x->badd($y); +is (ref($z),$ECL); is ($z,321); + + + +# not yet: +# require 'upgrade.inc'; # all tests here for sharing diff --git a/cpan/Math-BigInt/t/use.t b/cpan/Math-BigInt/t/use.t new file mode 100644 index 0000000000..3d0b9e2cd6 --- /dev/null +++ b/cpan/Math-BigInt/t/use.t @@ -0,0 +1,19 @@ +#!/usr/bin/perl -w + +# use Module(); doesn't call import() - thanx for cpan testers David. M. Town +# and Andreas Marcel Riechert for spotting it. It is fixed by the same code +# that fixes require Math::BigInt, but we make a test to be sure it really +# works. + +use strict; +use Test::More tests => 1; + +my ($try,$ans,$x); + +use Math::BigInt(); $x = Math::BigInt->new(1); ++$x; + +is ($x,2); + +# all tests done + +1; diff --git a/cpan/Math-BigInt/t/use_lib1.t b/cpan/Math-BigInt/t/use_lib1.t new file mode 100644 index 0000000000..2045af1833 --- /dev/null +++ b/cpan/Math-BigInt/t/use_lib1.t @@ -0,0 +1,15 @@ +#!/usr/bin/perl -w + +# see if using Math::BigInt and Math::BigFloat works together nicely. +# all use_lib*.t should be equivalent + +use strict; +use Test::More tests => 2; + +BEGIN { unshift @INC, 't'; } + +use Math::BigFloat lib => 'BareCalc'; + +is (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); + +is (Math::BigFloat->new(123)->badd(123),246); diff --git a/cpan/Math-BigInt/t/use_lib2.t b/cpan/Math-BigInt/t/use_lib2.t new file mode 100644 index 0000000000..23239e10e9 --- /dev/null +++ b/cpan/Math-BigInt/t/use_lib2.t @@ -0,0 +1,16 @@ +#!/usr/bin/perl -w + +# see if using Math::BigInt and Math::BigFloat works together nicely. +# all use_lib*.t should be equivalent + +use strict; +use Test::More tests => 2; + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt; +use Math::BigFloat lib => 'BareCalc'; + +is (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); + +is (Math::BigFloat->new(123)->badd(123),246); diff --git a/cpan/Math-BigInt/t/use_lib3.t b/cpan/Math-BigInt/t/use_lib3.t new file mode 100644 index 0000000000..95263a0dcd --- /dev/null +++ b/cpan/Math-BigInt/t/use_lib3.t @@ -0,0 +1,16 @@ +#!/usr/bin/perl -w + +# see if using Math::BigInt and Math::BigFloat works together nicely. +# all use_lib*.t should be equivalent + +use strict; +use Test::More tests => 2; + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt lib => 'BareCalc'; +use Math::BigFloat; + +is (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); + +is (Math::BigFloat->new(123)->badd(123),246); diff --git a/cpan/Math-BigInt/t/use_lib4.t b/cpan/Math-BigInt/t/use_lib4.t new file mode 100644 index 0000000000..a0d0564a35 --- /dev/null +++ b/cpan/Math-BigInt/t/use_lib4.t @@ -0,0 +1,17 @@ +#!/usr/bin/perl -w + +# see if using Math::BigInt and Math::BigFloat works together nicely. +# all use_lib*.t should be equivalent, except this, since the later overrides +# the former lib statement + +use strict; +use Test::More tests => 2; + +BEGIN { unshift @INC, 't'; } + +use Math::BigInt lib => 'BareCalc'; +use Math::BigFloat lib => 'Calc'; + +is (Math::BigInt->config()->{lib},'Math::BigInt::Calc'); + +is (Math::BigFloat->new(123)->badd(123),246); diff --git a/cpan/Math-BigInt/t/use_mbfw.t b/cpan/Math-BigInt/t/use_mbfw.t new file mode 100644 index 0000000000..afa3733250 --- /dev/null +++ b/cpan/Math-BigInt/t/use_mbfw.t @@ -0,0 +1,26 @@ +#!/usr/bin/perl -w + +# check that using BigFloat with "with" and "lib" at the same time works +# broken in versions up to v1.63 + +use strict; +use Test::More tests => 2; + +BEGIN { unshift @INC, 't'; } + +# the replacement lib can handle the lib statement, but it could also ignore +# it completely, for instance, when it is a 100% replacement for BigInt, but +# doesn't know the concept of alternative libs. But it still needs to cope +# with "lib => ". SubClass does record it, so we test here essential if +# BigFloat hands the lib properly down, any more is outside out testing reach. + +use Math::BigFloat with => 'Math::BigInt::Subclass', lib => 'BareCalc'; + +is (Math::BigFloat->config()->{with}, 'Math::BigInt::BareCalc' ); + +# is ($Math::BigInt::Subclass::lib, 'BareCalc' ); + +# it never arrives here, but that is a design decision in SubClass +is (Math::BigInt->config->{lib}, 'Math::BigInt::BareCalc' ); + +# all tests done diff --git a/cpan/Math-BigInt/t/with_sub.t b/cpan/Math-BigInt/t/with_sub.t new file mode 100644 index 0000000000..d90bbbc7dd --- /dev/null +++ b/cpan/Math-BigInt/t/with_sub.t @@ -0,0 +1,17 @@ +#!/usr/bin/perl -w + +# Test use Math::BigFloat with => 'Math::BigInt::SomeSubclass'; + +use strict; +use Test::More tests => 2340 + 1; + +use Math::BigFloat with => 'Math::BigInt::Subclass', lib => 'Calc'; + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigFloat"; +$CL = "Math::BigInt::Calc"; + +# the with argument is ignored +is (Math::BigFloat->config()->{with}, 'Math::BigInt::Calc'); + +require 't/bigfltpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigRat/lib/Math/BigRat.pm b/cpan/Math-BigRat/lib/Math/BigRat.pm new file mode 100644 index 0000000000..e0c12b31f3 --- /dev/null +++ b/cpan/Math-BigRat/lib/Math/BigRat.pm @@ -0,0 +1,2202 @@ + +# +# "Tax the rat farms." - Lord Vetinari +# + +# The following hash values are used: +# sign : +,-,NaN,+inf,-inf +# _d : denominator +# _n : numerator (value = _n/_d) +# _a : accuracy +# _p : precision +# You should not look at the innards of a BigRat - use the methods for this. + +package Math::BigRat; + +# anything older is untested, and unlikely to work +use 5.006; +use strict; +use Carp (); + +use Math::BigFloat; +use vars qw($VERSION @ISA $upgrade $downgrade + $accuracy $precision $round_mode $div_scale $_trap_nan $_trap_inf); + +@ISA = qw(Math::BigFloat); + +$VERSION = '0.260801'; +$VERSION = eval $VERSION; + +# inherit overload from Math::BigFloat, but disable the bitwise ops that don't +# make much sense for rationals unless they're truncated or something first + +use overload + map { + my $op = $_; + ($op => sub { + Carp::croak("bitwise operation $op not supported in Math::BigRat"); + }); + } qw(& | ^ ~ << >> &= |= ^= <<= >>=); + +BEGIN + { + *objectify = \&Math::BigInt::objectify; # inherit this from BigInt + *AUTOLOAD = \&Math::BigFloat::AUTOLOAD; # can't inherit AUTOLOAD + # we inherit these from BigFloat because currently it is not possible + # that MBF has a different $MBI variable than we, because MBF also uses + # Math::BigInt::config->('lib'); (there is always only one library loaded) + *_e_add = \&Math::BigFloat::_e_add; + *_e_sub = \&Math::BigFloat::_e_sub; + *as_int = \&as_number; + *is_pos = \&is_positive; + *is_neg = \&is_negative; + } + +############################################################################## +# Global constants and flags. Access these only via the accessor methods! + +$accuracy = $precision = undef; +$round_mode = 'even'; +$div_scale = 40; +$upgrade = undef; +$downgrade = undef; + +# These are internally, and not to be used from the outside at all! + +$_trap_nan = 0; # are NaNs ok? set w/ config() +$_trap_inf = 0; # are infs ok? set w/ config() + +# the package we are using for our private parts, defaults to: +# Math::BigInt->config()->{lib} +my $MBI = 'Math::BigInt::Calc'; + +my $nan = 'NaN'; +my $class = 'Math::BigRat'; + +sub isa + { + return 0 if $_[1] =~ /^Math::Big(Int|Float)/; # we aren't + UNIVERSAL::isa(@_); + } + +############################################################################## + +sub _new_from_float + { + # turn a single float input into a rational number (like '0.1') + my ($self,$f) = @_; + + return $self->bnan() if $f->is_nan(); + return $self->binf($f->{sign}) if $f->{sign} =~ /^[+-]inf$/; + + $self->{_n} = $MBI->_copy( $f->{_m} ); # mantissa + $self->{_d} = $MBI->_one(); + $self->{sign} = $f->{sign} || '+'; + if ($f->{_es} eq '-') + { + # something like Math::BigRat->new('0.1'); + # 1 / 1 => 1/10 + $MBI->_lsft ( $self->{_d}, $f->{_e} ,10); + } + else + { + # something like Math::BigRat->new('10'); + # 1 / 1 => 10/1 + $MBI->_lsft ( $self->{_n}, $f->{_e} ,10) unless + $MBI->_is_zero($f->{_e}); + } + $self; + } + +sub new + { + # create a Math::BigRat + my $class = shift; + + my ($n,$d) = @_; + + my $self = { }; bless $self,$class; + + # input like (BigInt) or (BigFloat): + if ((!defined $d) && (ref $n) && (!$n->isa('Math::BigRat'))) + { + if ($n->isa('Math::BigFloat')) + { + $self->_new_from_float($n); + } + if ($n->isa('Math::BigInt')) + { + # TODO: trap NaN, inf + $self->{_n} = $MBI->_copy($n->{value}); # "mantissa" = N + $self->{_d} = $MBI->_one(); # d => 1 + $self->{sign} = $n->{sign}; + } + if ($n->isa('Math::BigInt::Lite')) + { + # TODO: trap NaN, inf + $self->{sign} = '+'; $self->{sign} = '-' if $$n < 0; + $self->{_n} = $MBI->_new(abs($$n)); # "mantissa" = N + $self->{_d} = $MBI->_one(); # d => 1 + } + return $self->bnorm(); # normalize (120/1 => 12/10) + } + + # input like (BigInt,BigInt) or (BigLite,BigLite): + if (ref($d) && ref($n)) + { + # do N first (for $self->{sign}): + if ($n->isa('Math::BigInt')) + { + # TODO: trap NaN, inf + $self->{_n} = $MBI->_copy($n->{value}); # "mantissa" = N + $self->{sign} = $n->{sign}; + } + elsif ($n->isa('Math::BigInt::Lite')) + { + # TODO: trap NaN, inf + $self->{sign} = '+'; $self->{sign} = '-' if $$n < 0; + $self->{_n} = $MBI->_new(abs($$n)); # "mantissa" = $n + } + else + { + require Carp; + Carp::croak(ref($n) . " is not a recognized object format for Math::BigRat->new"); + } + # now D: + if ($d->isa('Math::BigInt')) + { + # TODO: trap NaN, inf + $self->{_d} = $MBI->_copy($d->{value}); # "mantissa" = D + # +/+ or -/- => +, +/- or -/+ => - + $self->{sign} = $d->{sign} ne $self->{sign} ? '-' : '+'; + } + elsif ($d->isa('Math::BigInt::Lite')) + { + # TODO: trap NaN, inf + $self->{_d} = $MBI->_new(abs($$d)); # "mantissa" = D + my $ds = '+'; $ds = '-' if $$d < 0; + # +/+ or -/- => +, +/- or -/+ => - + $self->{sign} = $ds ne $self->{sign} ? '-' : '+'; + } + else + { + require Carp; + Carp::croak(ref($d) . " is not a recognized object format for Math::BigRat->new"); + } + return $self->bnorm(); # normalize (120/1 => 12/10) + } + return $n->copy() if ref $n; # already a BigRat + + if (!defined $n) + { + $self->{_n} = $MBI->_zero(); # undef => 0 + $self->{_d} = $MBI->_one(); + $self->{sign} = '+'; + return $self; + } + + # string input with / delimiter + if ($n =~ /\s*\/\s*/) + { + return $class->bnan() if $n =~ /\/.*\//; # 1/2/3 isn't valid + return $class->bnan() if $n =~ /\/\s*$/; # 1/ isn't valid + ($n,$d) = split (/\//,$n); + # try as BigFloats first + if (($n =~ /[\.eE]/) || ($d =~ /[\.eE]/)) + { + local $Math::BigFloat::accuracy = undef; + local $Math::BigFloat::precision = undef; + + # one of them looks like a float + my $nf = Math::BigFloat->new($n,undef,undef); + $self->{sign} = '+'; + return $self->bnan() if $nf->is_nan(); + + $self->{_n} = $MBI->_copy( $nf->{_m} ); # get mantissa + + # now correct $self->{_n} due to $n + my $f = Math::BigFloat->new($d,undef,undef); + return $self->bnan() if $f->is_nan(); + $self->{_d} = $MBI->_copy( $f->{_m} ); + + # calculate the difference between nE and dE + my $diff_e = $nf->exponent()->bsub( $f->exponent); + if ($diff_e->is_negative()) + { + # < 0: mul d with it + $MBI->_lsft( $self->{_d}, $MBI->_new( $diff_e->babs()), 10); + } + elsif (!$diff_e->is_zero()) + { + # > 0: mul n with it + $MBI->_lsft( $self->{_n}, $MBI->_new( $diff_e), 10); + } + } + else + { + # both d and n look like (big)ints + + $self->{sign} = '+'; # no sign => '+' + $self->{_n} = undef; + $self->{_d} = undef; + if ($n =~ /^([+-]?)0*([0-9]+)\z/) # first part ok? + { + $self->{sign} = $1 || '+'; # no sign => '+' + $self->{_n} = $MBI->_new($2 || 0); + } + + if ($d =~ /^([+-]?)0*([0-9]+)\z/) # second part ok? + { + $self->{sign} =~ tr/+-/-+/ if ($1 || '') eq '-'; # negate if second part neg. + $self->{_d} = $MBI->_new($2 || 0); + } + + if (!defined $self->{_n} || !defined $self->{_d}) + { + $d = Math::BigInt->new($d,undef,undef) unless ref $d; + $n = Math::BigInt->new($n,undef,undef) unless ref $n; + + if ($n->{sign} =~ /^[+-]$/ && $d->{sign} =~ /^[+-]$/) + { + # both parts are ok as integers (weird things like ' 1e0' + $self->{_n} = $MBI->_copy($n->{value}); + $self->{_d} = $MBI->_copy($d->{value}); + $self->{sign} = $n->{sign}; + $self->{sign} =~ tr/+-/-+/ if $d->{sign} eq '-'; # -1/-2 => 1/2 + return $self->bnorm(); + } + + $self->{sign} = '+'; # a default sign + return $self->bnan() if $n->is_nan() || $d->is_nan(); + + # handle inf cases: + if ($n->is_inf() || $d->is_inf()) + { + if ($n->is_inf()) + { + return $self->bnan() if $d->is_inf(); # both are inf => NaN + my $s = '+'; # '+inf/+123' or '-inf/-123' + $s = '-' if substr($n->{sign},0,1) ne $d->{sign}; + # +-inf/123 => +-inf + return $self->binf($s); + } + # 123/inf => 0 + return $self->bzero(); + } + } + } + + return $self->bnorm(); + } + + # simple string input + if (($n =~ /[\.eE]/) && $n !~ /^0x/) + { + # looks like a float, quacks like a float, so probably is a float + $self->{sign} = 'NaN'; + local $Math::BigFloat::accuracy = undef; + local $Math::BigFloat::precision = undef; + $self->_new_from_float(Math::BigFloat->new($n,undef,undef)); + } + else + { + # for simple forms, use $MBI directly + if ($n =~ /^([+-]?)0*([0-9]+)\z/) + { + $self->{sign} = $1 || '+'; + $self->{_n} = $MBI->_new($2 || 0); + $self->{_d} = $MBI->_one(); + } + else + { + my $n = Math::BigInt->new($n,undef,undef); + $self->{_n} = $MBI->_copy($n->{value}); + $self->{_d} = $MBI->_one(); + $self->{sign} = $n->{sign}; + return $self->bnan() if $self->{sign} eq 'NaN'; + return $self->binf($self->{sign}) if $self->{sign} =~ /^[+-]inf$/; + } + } + $self->bnorm(); + } + +sub copy + { + # if two arguments, the first one is the class to "swallow" subclasses + my ($c,$x) = @_; + + if (scalar @_ == 1) + { + $x = $_[0]; + $c = ref($x); + } + return unless ref($x); # only for objects + + my $self = bless {}, $c; + + $self->{sign} = $x->{sign}; + $self->{_d} = $MBI->_copy($x->{_d}); + $self->{_n} = $MBI->_copy($x->{_n}); + $self->{_a} = $x->{_a} if defined $x->{_a}; + $self->{_p} = $x->{_p} if defined $x->{_p}; + $self; + } + +############################################################################## + +sub config + { + # return (later set?) configuration data as hash ref + my $class = shift || 'Math::BigRat'; + + if (@_ == 1 && ref($_[0]) ne 'HASH') + { + my $cfg = $class->SUPER::config(); + return $cfg->{$_[0]}; + } + + my $cfg = $class->SUPER::config(@_); + + # now we need only to override the ones that are different from our parent + $cfg->{class} = $class; + $cfg->{with} = $MBI; + $cfg; + } + +############################################################################## + +sub bstr + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc + { + my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf + return $s; + } + + my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # '+3/2' => '3/2' + + return $s . $MBI->_str($x->{_n}) if $MBI->_is_one($x->{_d}); + $s . $MBI->_str($x->{_n}) . '/' . $MBI->_str($x->{_d}); + } + +sub bsstr + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc + { + my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf + return $s; + } + + my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # +3 vs 3 + $s . $MBI->_str($x->{_n}) . '/' . $MBI->_str($x->{_d}); + } + +sub bnorm + { + # reduce the number to the shortest form + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + # Both parts must be objects of whatever we are using today. + if ( my $c = $MBI->_check($x->{_n}) ) + { + require Carp; Carp::croak( + "n did not pass the self-check ($c) in bnorm()"); + } + if ( my $c = $MBI->_check($x->{_d}) ) + { + require Carp; Carp::croak( + "d did not pass the self-check ($c) in bnorm()"); + } + + # no normalize for NaN, inf etc. + return $x if $x->{sign} !~ /^[+-]$/; + + # normalize zeros to 0/1 + if ($MBI->_is_zero($x->{_n})) + { + $x->{sign} = '+'; # never leave a -0 + $x->{_d} = $MBI->_one() unless $MBI->_is_one($x->{_d}); + return $x; + } + + return $x if $MBI->_is_one($x->{_d}); # no need to reduce + + # reduce other numbers + my $gcd = $MBI->_copy($x->{_n}); + $gcd = $MBI->_gcd($gcd,$x->{_d}); + + if (!$MBI->_is_one($gcd)) + { + $x->{_n} = $MBI->_div($x->{_n},$gcd); + $x->{_d} = $MBI->_div($x->{_d},$gcd); + } + $x; + } + +############################################################################## +# sign manipulation + +sub bneg + { + # (BRAT or num_str) return BRAT + # negate number or make a negated number from string + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x if $x->modify('bneg'); + + # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN' + $x->{sign} =~ tr/+-/-+/ + unless ($x->{sign} eq '+' && $MBI->_is_zero($x->{_n})); + $x; + } + +############################################################################## +# special values + +sub _bnan + { + # used by parent class bnan() to initialize number to NaN + my $self = shift; + + if ($_trap_nan) + { + require Carp; + my $class = ref($self); + # "$self" below will stringify the object, this blows up if $self is a + # partial object (happens under trap_nan), so fix it beforehand + $self->{_d} = $MBI->_zero() unless defined $self->{_d}; + $self->{_n} = $MBI->_zero() unless defined $self->{_n}; + Carp::croak ("Tried to set $self to NaN in $class\::_bnan()"); + } + $self->{_n} = $MBI->_zero(); + $self->{_d} = $MBI->_zero(); + } + +sub _binf + { + # used by parent class bone() to initialize number to +inf/-inf + my $self = shift; + + if ($_trap_inf) + { + require Carp; + my $class = ref($self); + # "$self" below will stringify the object, this blows up if $self is a + # partial object (happens under trap_nan), so fix it beforehand + $self->{_d} = $MBI->_zero() unless defined $self->{_d}; + $self->{_n} = $MBI->_zero() unless defined $self->{_n}; + Carp::croak ("Tried to set $self to inf in $class\::_binf()"); + } + $self->{_n} = $MBI->_zero(); + $self->{_d} = $MBI->_zero(); + } + +sub _bone + { + # used by parent class bone() to initialize number to +1/-1 + my $self = shift; + $self->{_n} = $MBI->_one(); + $self->{_d} = $MBI->_one(); + } + +sub _bzero + { + # used by parent class bzero() to initialize number to 0 + my $self = shift; + $self->{_n} = $MBI->_zero(); + $self->{_d} = $MBI->_one(); + } + +############################################################################## +# mul/add/div etc + +sub badd + { + # add two rational numbers + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + # +inf + +inf => +inf, -inf + -inf => -inf + return $x->binf(substr($x->{sign},0,1)) + if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; + + # +inf + -inf or -inf + +inf => NaN + return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); + + # 1 1 gcd(3,4) = 1 1*3 + 1*4 7 + # - + - = --------- = -- + # 4 3 4*3 12 + + # we do not compute the gcd() here, but simple do: + # 5 7 5*3 + 7*4 43 + # - + - = --------- = -- + # 4 3 4*3 12 + + # and bnorm() will then take care of the rest + + # 5 * 3 + $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_d}); + + # 7 * 4 + my $m = $MBI->_mul( $MBI->_copy( $y->{_n} ), $x->{_d} ); + + # 5 * 3 + 7 * 4 + ($x->{_n}, $x->{sign}) = _e_add( $x->{_n}, $m, $x->{sign}, $y->{sign}); + + # 4 * 3 + $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_d}); + + # normalize result, and possible round + $x->bnorm()->round(@r); + } + +sub bsub + { + # subtract two rational numbers + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + # flip sign of $x, call badd(), then flip sign of result + $x->{sign} =~ tr/+-/-+/ + unless $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); # not -0 + $x->badd($y,@r); # does norm and round + $x->{sign} =~ tr/+-/-+/ + unless $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); # not -0 + $x; + } + +sub bmul + { + # multiply two rational numbers + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x->bnan() if ($x->{sign} eq 'NaN' || $y->{sign} eq 'NaN'); + + # inf handling + if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) + { + return $x->bnan() if $x->is_zero() || $y->is_zero(); + # result will always be +-inf: + # +inf * +/+inf => +inf, -inf * -/-inf => +inf + # +inf * -/-inf => -inf, -inf * +/+inf => -inf + return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); + return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); + return $x->binf('-'); + } + + # x== 0 # also: or y == 1 or y == -1 + return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); + + # XXX TODO: + # According to Knuth, this can be optimized by doing gcd twice (for d and n) + # and reducing in one step. This would save us the bnorm() at the end. + + # 1 2 1 * 2 2 1 + # - * - = ----- = - = - + # 4 3 4 * 3 12 6 + + $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_n}); + $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_d}); + + # compute new sign + $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; + + $x->bnorm()->round(@r); + } + +sub bdiv + { + # (dividend: BRAT or num_str, divisor: BRAT or num_str) return + # (BRAT,BRAT) (quo,rem) or BRAT (only rem) + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bdiv'); + + my $wantarray = wantarray; # call only once + + # At least one argument is NaN. This is handled the same way as in + # Math::BigInt -> bdiv(). See the comments in the code implementing that + # method. + + if ($x -> is_nan() || $y -> is_nan()) { + return $wantarray ? ($x -> bnan(), $self -> bnan()) : $x -> bnan(); + } + + # Divide by zero and modulo zero. This is handled the same way as in + # Math::BigInt -> bdiv(). See the comments in the code implementing that + # method. + + if ($y -> is_zero()) { + my ($quo, $rem); + if ($wantarray) { + $rem = $x -> copy(); + } + if ($x -> is_zero()) { + $quo = $x -> bnan(); + } else { + $quo = $x -> binf($x -> {sign}); + } + return $wantarray ? ($quo, $rem) : $quo; + } + + # Numerator (dividend) is +/-inf. This is handled the same way as in + # Math::BigInt -> bdiv(). See the comments in the code implementing that + # method. + + if ($x -> is_inf()) { + my ($quo, $rem); + $rem = $self -> bnan() if $wantarray; + if ($y -> is_inf()) { + $quo = $x -> bnan(); + } else { + my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-'; + $quo = $x -> binf($sign); + } + return $wantarray ? ($quo, $rem) : $quo; + } + + # Denominator (divisor) is +/-inf. This is handled the same way as in + # Math::BigFloat -> bdiv(). See the comments in the code implementing that + # method. + + if ($y -> is_inf()) { + my ($quo, $rem); + if ($wantarray) { + if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { + $rem = $x -> copy(); + $quo = $x -> bzero(); + } else { + $rem = $self -> binf($y -> {sign}); + $quo = $x -> bone('-'); + } + return ($quo, $rem); + } else { + if ($y -> is_inf()) { + if ($x -> is_nan() || $x -> is_inf()) { + return $x -> bnan(); + } else { + return $x -> bzero(); + } + } + } + } + + # At this point, both the numerator and denominator are finite numbers, and + # the denominator (divisor) is non-zero. + + # x == 0? + return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); + + # XXX TODO: list context, upgrade + # According to Knuth, this can be optimized by doing gcd twice (for d and n) + # and reducing in one step. This would save us the bnorm() at the end. + + # 1 1 1 3 + # - / - == - * - + # 4 3 4 1 + + $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_d}); + $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_n}); + + # compute new sign + $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; + + $x -> bnorm(); + if (wantarray) { + my $rem = $x -> copy(); + $x -> bfloor(); + $x -> round(@r); + $rem -> bsub($x -> copy()) -> bmul($y); + return $x, $rem; + } else { + $x -> round(@r); + return $x; + } + } + +sub bmod + { + # compute "remainder" (in Perl way) of $x / $y + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->modify('bmod'); + + # At least one argument is NaN. This is handled the same way as in + # Math::BigInt -> bmod(). + + if ($x -> is_nan() || $y -> is_nan()) { + return $x -> bnan(); + } + + # Modulo zero. This is handled the same way as in Math::BigInt -> bmod(). + + if ($y -> is_zero()) { + return $x; + } + + # Numerator (dividend) is +/-inf. This is handled the same way as in + # Math::BigInt -> bmod(). + + if ($x -> is_inf()) { + return $x -> bnan(); + } + + # Denominator (divisor) is +/-inf. This is handled the same way as in + # Math::BigInt -> bmod(). + + if ($y -> is_inf()) { + if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { + return $x; + } else { + return $x -> binf($y -> sign()); + } + } + + # At this point, both the numerator and denominator are finite numbers, and + # the denominator (divisor) is non-zero. + + return $x if $x->is_zero(); # 0 / 7 = 0, mod 0 + + # Compute $x - $y * floor($x/$y). This can probably be optimized by working + # on a lower level. + + $x -> bsub($x -> copy() -> bdiv($y) -> bfloor() -> bmul($y)); + return $x -> round(@r); + } + +############################################################################## +# bdec/binc + +sub bdec + { + # decrement value (subtract 1) + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf + + if ($x->{sign} eq '-') + { + $x->{_n} = $MBI->_add( $x->{_n}, $x->{_d}); # -5/2 => -7/2 + } + else + { + if ($MBI->_acmp($x->{_n},$x->{_d}) < 0) # n < d? + { + # 1/3 -- => -2/3 + $x->{_n} = $MBI->_sub( $MBI->_copy($x->{_d}), $x->{_n}); + $x->{sign} = '-'; + } + else + { + $x->{_n} = $MBI->_sub($x->{_n}, $x->{_d}); # 5/2 => 3/2 + } + } + $x->bnorm()->round(@r); + } + +sub binc + { + # increment value (add 1) + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf + + if ($x->{sign} eq '-') + { + if ($MBI->_acmp($x->{_n},$x->{_d}) < 0) + { + # -1/3 ++ => 2/3 (overflow at 0) + $x->{_n} = $MBI->_sub( $MBI->_copy($x->{_d}), $x->{_n}); + $x->{sign} = '+'; + } + else + { + $x->{_n} = $MBI->_sub($x->{_n}, $x->{_d}); # -5/2 => -3/2 + } + } + else + { + $x->{_n} = $MBI->_add($x->{_n},$x->{_d}); # 5/2 => 7/2 + } + $x->bnorm()->round(@r); + } + +############################################################################## +# is_foo methods (the rest is inherited) + +sub is_int + { + # return true if arg (BRAT or num_str) is an integer + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't + $MBI->_is_one($x->{_d}); # x/y && y != 1 => no integer + 0; + } + +sub is_zero + { + # return true if arg (BRAT or num_str) is zero + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 1 if $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); + 0; + } + +sub is_one + { + # return true if arg (BRAT or num_str) is +1 or -1 if signis given + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + my $sign = $_[2] || ''; $sign = '+' if $sign ne '-'; + return 1 + if ($x->{sign} eq $sign && $MBI->_is_one($x->{_n}) && $MBI->_is_one($x->{_d})); + 0; + } + +sub is_odd + { + # return true if arg (BFLOAT or num_str) is odd or false if even + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't + ($MBI->_is_one($x->{_d}) && $MBI->_is_odd($x->{_n})); # x/2 is not, but 3/1 + 0; + } + +sub is_even + { + # return true if arg (BINT or num_str) is even or false if odd + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't + return 1 if ($MBI->_is_one($x->{_d}) # x/3 is never + && $MBI->_is_even($x->{_n})); # but 4/1 is + 0; + } + +############################################################################## +# parts() and friends + +sub numerator + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + # NaN, inf, -inf + return Math::BigInt->new($x->{sign}) if ($x->{sign} !~ /^[+-]$/); + + my $n = Math::BigInt->new($MBI->_str($x->{_n})); $n->{sign} = $x->{sign}; + $n; + } + +sub denominator + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + # NaN + return Math::BigInt->new($x->{sign}) if $x->{sign} eq 'NaN'; + # inf, -inf + return Math::BigInt->bone() if $x->{sign} !~ /^[+-]$/; + + Math::BigInt->new($MBI->_str($x->{_d})); + } + +sub parts + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + my $c = 'Math::BigInt'; + + return ($c->bnan(),$c->bnan()) if $x->{sign} eq 'NaN'; + return ($c->binf(),$c->binf()) if $x->{sign} eq '+inf'; + return ($c->binf('-'),$c->binf()) if $x->{sign} eq '-inf'; + + my $n = $c->new( $MBI->_str($x->{_n})); + $n->{sign} = $x->{sign}; + my $d = $c->new( $MBI->_str($x->{_d})); + ($n,$d); + } + +sub length + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $nan unless $x->is_int(); + $MBI->_len($x->{_n}); # length(-123/1) => length(123) + } + +sub digit + { + my ($self,$x,$n) = ref($_[0]) ? (undef,$_[0],$_[1]) : objectify(1,@_); + + return $nan unless $x->is_int(); + $MBI->_digit($x->{_n},$n || 0); # digit(-123/1,2) => digit(123,2) + } + +############################################################################## +# special calc routines + +sub bceil + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/ || # not for NaN, inf + $MBI->_is_one($x->{_d}); # 22/1 => 22, 0/1 => 0 + + $x->{_n} = $MBI->_div($x->{_n},$x->{_d}); # 22/7 => 3/1 w/ truncate + $x->{_d} = $MBI->_one(); # d => 1 + $x->{_n} = $MBI->_inc($x->{_n}) + if $x->{sign} eq '+'; # +22/7 => 4/1 + $x->{sign} = '+' if $MBI->_is_zero($x->{_n}); # -0 => 0 + $x; + } + +sub bfloor + { + my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); + + return $x if $x->{sign} !~ /^[+-]$/ || # not for NaN, inf + $MBI->_is_one($x->{_d}); # 22/1 => 22, 0/1 => 0 + + $x->{_n} = $MBI->_div($x->{_n},$x->{_d}); # 22/7 => 3/1 w/ truncate + $x->{_d} = $MBI->_one(); # d => 1 + $x->{_n} = $MBI->_inc($x->{_n}) + if $x->{sign} eq '-'; # -22/7 => -4/1 + $x; + } + +sub bfac + { + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + # if $x is not an integer + if (($x->{sign} ne '+') || (!$MBI->_is_one($x->{_d}))) + { + return $x->bnan(); + } + + $x->{_n} = $MBI->_fac($x->{_n}); + # since _d is 1, we don't need to reduce/norm the result + $x->round(@r); + } + +sub bpow + { + # power ($x ** $y) + + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + return $x if $x->{sign} =~ /^[+-]inf$/; # -inf/+inf ** x + return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; + return $x->bone(@r) if $y->is_zero(); + return $x->round(@r) if $x->is_one() || $y->is_one(); + + if ($x->{sign} eq '-' && $MBI->_is_one($x->{_n}) && $MBI->_is_one($x->{_d})) + { + # if $x == -1 and odd/even y => +1/-1 + return $y->is_odd() ? $x->round(@r) : $x->babs()->round(@r); + # my Casio FX-5500L has a bug here: -1 ** 2 is -1, but -1 * -1 is 1; + } + # 1 ** -y => 1 / (1 ** |y|) + # so do test for negative $y after above's clause + + return $x->round(@r) if $x->is_zero(); # 0**y => 0 (if not y <= 0) + + # shortcut if y == 1/N (is then sqrt() respective broot()) + if ($MBI->_is_one($y->{_n})) + { + return $x->bsqrt(@r) if $MBI->_is_two($y->{_d}); # 1/2 => sqrt + return $x->broot($MBI->_str($y->{_d}),@r); # 1/N => root(N) + } + + # shortcut y/1 (and/or x/1) + if ($MBI->_is_one($y->{_d})) + { + # shortcut for x/1 and y/1 + if ($MBI->_is_one($x->{_d})) + { + $x->{_n} = $MBI->_pow($x->{_n},$y->{_n}); # x/1 ** y/1 => (x ** y)/1 + if ($y->{sign} eq '-') + { + # 0.2 ** -3 => 1/(0.2 ** 3) + ($x->{_n},$x->{_d}) = ($x->{_d},$x->{_n}); # swap + } + # correct sign; + ** + => + + if ($x->{sign} eq '-') + { + # - * - => +, - * - * - => - + $x->{sign} = '+' if $MBI->_is_even($y->{_n}); + } + return $x->round(@r); + } + # x/z ** y/1 + $x->{_n} = $MBI->_pow($x->{_n},$y->{_n}); # 5/2 ** y/1 => 5 ** y / 2 ** y + $x->{_d} = $MBI->_pow($x->{_d},$y->{_n}); + if ($y->{sign} eq '-') + { + # 0.2 ** -3 => 1/(0.2 ** 3) + ($x->{_n},$x->{_d}) = ($x->{_d},$x->{_n}); # swap + } + # correct sign; + ** + => + + if ($x->{sign} eq '-') + { + # - * - => +, - * - * - => - + $x->{sign} = '+' if $MBI->_is_even($y->{_n}); + } + return $x->round(@r); + } + +# print STDERR "# $x $y\n"; + + # otherwise: + + # n/d n ______________ + # a/b = -\/ (a/b) ** d + + # (a/b) ** n == (a ** n) / (b ** n) + $MBI->_pow($x->{_n}, $y->{_n} ); + $MBI->_pow($x->{_d}, $y->{_n} ); + + return $x->broot($MBI->_str($y->{_d}),@r); # n/d => root(n) + } + +sub blog + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,$class,@_); + } + + # blog(1,Y) => 0 + return $x->bzero() if $x->is_one() && $y->{sign} eq '+'; + + # $x <= 0 => NaN + return $x->bnan() if $x->is_zero() || $x->{sign} ne '+' || $y->{sign} ne '+'; + + if ($x->is_int() && $y->is_int()) + { + return $self->new($x->as_number()->blog($y->as_number(),@r)); + } + + # do it with floats + $x->_new_from_float( $x->_as_float()->blog(Math::BigFloat->new("$y"),@r) ); + } + +sub bexp + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,$class,@_); + } + + return $x->binf(@r) if $x->{sign} eq '+inf'; + return $x->bzero(@r) if $x->{sign} eq '-inf'; + + # we need to limit the accuracy to protect against overflow + my $fallback = 0; + my ($scale,@params); + ($x,@params) = $x->_find_round_parameters(@r); + + # also takes care of the "error in _find_round_parameters?" case + return $x if $x->{sign} eq 'NaN'; + + # no rounding at all, so must use fallback + if (scalar @params == 0) + { + # simulate old behaviour + $params[0] = $self->div_scale(); # and round to it as accuracy + $params[1] = undef; # P = undef + $scale = $params[0]+4; # at least four more for proper round + $params[2] = $r[2]; # round mode by caller or undef + $fallback = 1; # to clear a/p afterwards + } + else + { + # the 4 below is empirical, and there might be cases where it's not enough... + $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined + } + + return $x->bone(@params) if $x->is_zero(); + + # See the comments in Math::BigFloat on how this algorithm works. + # Basically we calculate A and B (where B is faculty(N)) so that A/B = e + + my $x_org = $x->copy(); + if ($scale <= 75) + { + # set $x directly from a cached string form + $x->{_n} = + $MBI->_new("90933395208605785401971970164779391644753259799242"); + $x->{_d} = + $MBI->_new("33452526613163807108170062053440751665152000000000"); + $x->{sign} = '+'; + } + else + { + # compute A and B so that e = A / B. + + # After some terms we end up with this, so we use it as a starting point: + my $A = $MBI->_new("90933395208605785401971970164779391644753259799242"); + my $F = $MBI->_new(42); my $step = 42; + + # Compute how many steps we need to take to get $A and $B sufficiently big + my $steps = Math::BigFloat::_len_to_steps($scale - 4); +# print STDERR "# Doing $steps steps for ", $scale-4, " digits\n"; + while ($step++ <= $steps) + { + # calculate $a * $f + 1 + $A = $MBI->_mul($A, $F); + $A = $MBI->_inc($A); + # increment f + $F = $MBI->_inc($F); + } + # compute $B as factorial of $steps (this is faster than doing it manually) + my $B = $MBI->_fac($MBI->_new($steps)); + +# print "A ", $MBI->_str($A), "\nB ", $MBI->_str($B), "\n"; + + $x->{_n} = $A; + $x->{_d} = $B; + $x->{sign} = '+'; + } + + # $x contains now an estimate of e, with some surplus digits, so we can round + if (!$x_org->is_one()) + { + # raise $x to the wanted power and round it in one step: + $x->bpow($x_org, @params); + } + else + { + # else just round the already computed result + delete $x->{_a}; delete $x->{_p}; + # shortcut to not run through _find_round_parameters again + if (defined $params[0]) + { + $x->bround($params[0],$params[2]); # then round accordingly + } + else + { + $x->bfround($params[1],$params[2]); # then round accordingly + } + } + if ($fallback) + { + # clear a/p after round, since user did not request it + delete $x->{_a}; delete $x->{_p}; + } + + $x; + } + +sub bnok + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,$class,@_); + } + + # do it with floats + $x->_new_from_float( $x->_as_float()->bnok(Math::BigFloat->new("$y"),@r) ); + } + +sub _float_from_part + { + my $x = shift; + + my $f = Math::BigFloat->bzero(); + $f->{_m} = $MBI->_copy($x); + $f->{_e} = $MBI->_zero(); + + $f; + } + +sub _as_float + { + my $x = shift; + + local $Math::BigFloat::upgrade = undef; + local $Math::BigFloat::accuracy = undef; + local $Math::BigFloat::precision = undef; + # 22/7 => 3.142857143.. + + my $a = $x->accuracy() || 0; + if ($a != 0 || !$MBI->_is_one($x->{_d})) + { + # n/d + return scalar Math::BigFloat->new($x->{sign} . $MBI->_str($x->{_n}))->bdiv( $MBI->_str($x->{_d}), $x->accuracy()); + } + # just n + Math::BigFloat->new($x->{sign} . $MBI->_str($x->{_n})); + } + +sub broot + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + if ($x->is_int() && $y->is_int()) + { + return $self->new($x->as_number()->broot($y->as_number(),@r)); + } + + # do it with floats + $x->_new_from_float( $x->_as_float()->broot($y->_as_float(),@r) )->bnorm()->bround(@r); + } + +sub bmodpow + { + # set up parameters + my ($self,$x,$y,$m,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,$m,@r) = objectify(3,@_); + } + + # $x or $y or $m are NaN or +-inf => NaN + return $x->bnan() + if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/ || + $m->{sign} !~ /^[+-]$/; + + if ($x->is_int() && $y->is_int() && $m->is_int()) + { + return $self->new($x->as_number()->bmodpow($y->as_number(),$m,@r)); + } + + warn ("bmodpow() not fully implemented"); + $x->bnan(); + } + +sub bmodinv + { + # set up parameters + my ($self,$x,$y,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y,@r) = objectify(2,@_); + } + + # $x or $y are NaN or +-inf => NaN + return $x->bnan() + if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/; + + if ($x->is_int() && $y->is_int()) + { + return $self->new($x->as_number()->bmodinv($y->as_number(),@r)); + } + + warn ("bmodinv() not fully implemented"); + $x->bnan(); + } + +sub bsqrt + { + my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); + + return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0 + return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf + return $x->round(@r) if $x->is_zero() || $x->is_one(); + + local $Math::BigFloat::upgrade = undef; + local $Math::BigFloat::downgrade = undef; + local $Math::BigFloat::precision = undef; + local $Math::BigFloat::accuracy = undef; + local $Math::BigInt::upgrade = undef; + local $Math::BigInt::precision = undef; + local $Math::BigInt::accuracy = undef; + + $x->{_n} = _float_from_part( $x->{_n} )->bsqrt(); + $x->{_d} = _float_from_part( $x->{_d} )->bsqrt(); + + # XXX TODO: we probably can optimize this: + + # if sqrt(D) was not integer + if ($x->{_d}->{_es} ne '+') + { + $x->{_n}->blsft($x->{_d}->exponent()->babs(),10); # 7.1/4.51 => 7.1/45.1 + $x->{_d} = $MBI->_copy( $x->{_d}->{_m} ); # 7.1/45.1 => 71/45.1 + } + # if sqrt(N) was not integer + if ($x->{_n}->{_es} ne '+') + { + $x->{_d}->blsft($x->{_n}->exponent()->babs(),10); # 71/45.1 => 710/45.1 + $x->{_n} = $MBI->_copy( $x->{_n}->{_m} ); # 710/45.1 => 710/451 + } + + # convert parts to $MBI again + $x->{_n} = $MBI->_lsft( $MBI->_copy( $x->{_n}->{_m} ), $x->{_n}->{_e}, 10) + if ref($x->{_n}) ne $MBI && ref($x->{_n}) ne 'ARRAY'; + $x->{_d} = $MBI->_lsft( $MBI->_copy( $x->{_d}->{_m} ), $x->{_d}->{_e}, 10) + if ref($x->{_d}) ne $MBI && ref($x->{_d}) ne 'ARRAY'; + + $x->bnorm()->round(@r); + } + +sub blsft + { + my ($self,$x,$y,$b,@r) = objectify(3,@_); + + $b = 2 unless defined $b; + $b = $self->new($b) unless ref ($b); + $x->bmul( $b->copy()->bpow($y), @r); + $x; + } + +sub brsft + { + my ($self,$x,$y,$b,@r) = objectify(3,@_); + + $b = 2 unless defined $b; + $b = $self->new($b) unless ref ($b); + $x->bdiv( $b->copy()->bpow($y), @r); + $x; + } + +############################################################################## +# round + +sub round + { + $_[0]; + } + +sub bround + { + $_[0]; + } + +sub bfround + { + $_[0]; + } + +############################################################################## +# comparing + +sub bcmp + { + # compare two signed numbers + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,@_); + } + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; + return +1 if $x->{sign} eq '+inf'; + return -1 if $x->{sign} eq '-inf'; + return -1 if $y->{sign} eq '+inf'; + return +1; + } + # check sign for speed first + return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y + return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0 + + # shortcut + my $xz = $MBI->_is_zero($x->{_n}); + my $yz = $MBI->_is_zero($y->{_n}); + return 0 if $xz && $yz; # 0 <=> 0 + return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y + return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0 + + my $t = $MBI->_mul( $MBI->_copy($x->{_n}), $y->{_d}); + my $u = $MBI->_mul( $MBI->_copy($y->{_n}), $x->{_d}); + + my $cmp = $MBI->_acmp($t,$u); # signs are equal + $cmp = -$cmp if $x->{sign} eq '-'; # both are '-' => reverse + $cmp; + } + +sub bacmp + { + # compare two numbers (as unsigned) + + # set up parameters + my ($self,$x,$y) = (ref($_[0]),@_); + # objectify is costly, so avoid it + if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) + { + ($self,$x,$y) = objectify(2,$class,@_); + } + + if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) + { + # handle +-inf and NaN + return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); + return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/; + return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/; + return -1; + } + + my $t = $MBI->_mul( $MBI->_copy($x->{_n}), $y->{_d}); + my $u = $MBI->_mul( $MBI->_copy($y->{_n}), $x->{_d}); + $MBI->_acmp($t,$u); # ignore signs + } + +############################################################################## +# output conversation + +sub numify + { + # convert 17/8 => float (aka 2.125) + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, NaN, etc + + # N/1 => N + my $neg = ''; $neg = '-' if $x->{sign} eq '-'; + return $neg . $MBI->_num($x->{_n}) if $MBI->_is_one($x->{_d}); + + $x->_as_float()->numify() + 0.0; + } + +sub as_number + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + # NaN, inf etc + return Math::BigInt->new($x->{sign}) if $x->{sign} !~ /^[+-]$/; + + my $u = Math::BigInt->bzero(); + $u->{value} = $MBI->_div( $MBI->_copy($x->{_n}), $x->{_d}); # 22/7 => 3 + $u->bneg if $x->{sign} eq '-'; # no negative zero + $u; + } + +sub as_float + { + # return N/D as Math::BigFloat + + # set up parameters + my ($self,$x,@r) = (ref($_[0]),@_); + # objectify is costly, so avoid it + ($self,$x,@r) = objectify(1,$class,@_) unless ref $_[0]; + + # NaN, inf etc + return Math::BigFloat->new($x->{sign}) if $x->{sign} !~ /^[+-]$/; + + my $u = Math::BigFloat->bzero(); + $u->{sign} = $x->{sign}; + # n + $u->{_m} = $MBI->_copy($x->{_n}); + $u->{_e} = $MBI->_zero(); + $u->bdiv( $MBI->_str($x->{_d}), @r); + # return $u + $u; + } + +sub as_bin + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x unless $x->is_int(); + + my $s = $x->{sign}; $s = '' if $s eq '+'; + $s . $MBI->_as_bin($x->{_n}); + } + +sub as_hex + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x unless $x->is_int(); + + my $s = $x->{sign}; $s = '' if $s eq '+'; + $s . $MBI->_as_hex($x->{_n}); + } + +sub as_oct + { + my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); + + return $x unless $x->is_int(); + + my $s = $x->{sign}; $s = '' if $s eq '+'; + $s . $MBI->_as_oct($x->{_n}); + } + +############################################################################## + +sub from_hex + { + my $class = shift; + + $class->new(@_); + } + +sub from_bin + { + my $class = shift; + + $class->new(@_); + } + +sub from_oct + { + my $class = shift; + + my @parts; + for my $c (@_) + { + push @parts, Math::BigInt->from_oct($c); + } + $class->new ( @parts ); + } + +############################################################################## +# import + +sub import + { + my $self = shift; + my $l = scalar @_; + my $lib = ''; my @a; + my $try = 'try'; + + for ( my $i = 0; $i < $l ; $i++) + { + if ( $_[$i] eq ':constant' ) + { + # this rest causes overlord er load to step in + overload::constant float => sub { $self->new(shift); }; + } +# elsif ($_[$i] eq 'upgrade') +# { +# # this causes upgrading +# $upgrade = $_[$i+1]; # or undef to disable +# $i++; +# } + elsif ($_[$i] eq 'downgrade') + { + # this causes downgrading + $downgrade = $_[$i+1]; # or undef to disable + $i++; + } + elsif ($_[$i] =~ /^(lib|try|only)\z/) + { + $lib = $_[$i+1] || ''; # default Calc + $try = $1; # lib, try or only + $i++; + } + elsif ($_[$i] eq 'with') + { + # this argument is no longer used + #$MBI = $_[$i+1] || 'Math::BigInt::Calc'; # default Math::BigInt::Calc + $i++; + } + else + { + push @a, $_[$i]; + } + } + require Math::BigInt; + + # let use Math::BigInt lib => 'GMP'; use Math::BigRat; still have GMP + if ($lib ne '') + { + my @c = split /\s*,\s*/, $lib; + foreach (@c) + { + $_ =~ tr/a-zA-Z0-9://cd; # limit to sane characters + } + $lib = join(",", @c); + } + my @import = ('objectify'); + push @import, $try => $lib if $lib ne ''; + + # MBI already loaded, so feed it our lib arguments + Math::BigInt->import( @import ); + + $MBI = Math::BigFloat->config()->{lib}; + + # register us with MBI to get notified of future lib changes + Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } ); + + # any non :constant stuff is handled by our parent, Exporter (loaded + # by Math::BigFloat, even if @_ is empty, to give it a chance + $self->SUPER::import(@a); # for subclasses + $self->export_to_level(1,$self,@a); # need this, too + } + +1; + +__END__ + +=pod + +=head1 NAME + +Math::BigRat - Arbitrary big rational numbers + +=head1 SYNOPSIS + + use Math::BigRat; + + my $x = Math::BigRat->new('3/7'); $x += '5/9'; + + print $x->bstr(),"\n"; + print $x ** 2,"\n"; + + my $y = Math::BigRat->new('inf'); + print "$y ", ($y->is_inf ? 'is' : 'is not') , " infinity\n"; + + my $z = Math::BigRat->new(144); $z->bsqrt(); + +=head1 DESCRIPTION + +Math::BigRat complements Math::BigInt and Math::BigFloat by providing support +for arbitrary big rational numbers. + +=head2 MATH LIBRARY + +You can change the underlying module that does the low-level +math operations by using: + + use Math::BigRat try => 'GMP'; + +Note: This needs Math::BigInt::GMP installed. + +The following would first try to find Math::BigInt::Foo, then +Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: + + use Math::BigRat try => 'Foo,Math::BigInt::Bar'; + +If you want to get warned when the fallback occurs, replace "try" with +"lib": + + use Math::BigRat lib => 'Foo,Math::BigInt::Bar'; + +If you want the code to die instead, replace "try" with +"only": + + use Math::BigRat only => 'Foo,Math::BigInt::Bar'; + +=head1 METHODS + +Any methods not listed here are derived from Math::BigFloat (or +Math::BigInt), so make sure you check these two modules for further +information. + +=head2 new() + + $x = Math::BigRat->new('1/3'); + +Create a new Math::BigRat object. Input can come in various forms: + + $x = Math::BigRat->new(123); # scalars + $x = Math::BigRat->new('inf'); # infinity + $x = Math::BigRat->new('123.3'); # float + $x = Math::BigRat->new('1/3'); # simple string + $x = Math::BigRat->new('1 / 3'); # spaced + $x = Math::BigRat->new('1 / 0.1'); # w/ floats + $x = Math::BigRat->new(Math::BigInt->new(3)); # BigInt + $x = Math::BigRat->new(Math::BigFloat->new('3.1')); # BigFloat + $x = Math::BigRat->new(Math::BigInt::Lite->new('2')); # BigLite + + # You can also give D and N as different objects: + $x = Math::BigRat->new( + Math::BigInt->new(-123), + Math::BigInt->new(7), + ); # => -123/7 + +=head2 numerator() + + $n = $x->numerator(); + +Returns a copy of the numerator (the part above the line) as signed BigInt. + +=head2 denominator() + + $d = $x->denominator(); + +Returns a copy of the denominator (the part under the line) as positive BigInt. + +=head2 parts() + + ($n,$d) = $x->parts(); + +Return a list consisting of (signed) numerator and (unsigned) denominator as +BigInts. + +=head2 numify() + + my $y = $x->numify(); + +Returns the object as a scalar. This will lose some data if the object +cannot be represented by a normal Perl scalar (integer or float), so +use Las_number()> or L instead. + +This routine is automatically used whenever a scalar is required: + + my $x = Math::BigRat->new('3/1'); + @array = (0,1,2,3); + $y = $array[$x]; # set $y to 3 + +=head2 as_int()/as_number() + + $x = Math::BigRat->new('13/7'); + print $x->as_int(),"\n"; # '1' + +Returns a copy of the object as BigInt, truncated to an integer. + +C is an alias for C. + +=head2 as_float() + + $x = Math::BigRat->new('13/7'); + print $x->as_float(),"\n"; # '1' + + $x = Math::BigRat->new('2/3'); + print $x->as_float(5),"\n"; # '0.66667' + +Returns a copy of the object as BigFloat, preserving the +accuracy as wanted, or the default of 40 digits. + +This method was added in v0.22 of Math::BigRat (April 2008). + +=head2 as_hex() + + $x = Math::BigRat->new('13'); + print $x->as_hex(),"\n"; # '0xd' + +Returns the BigRat as hexadecimal string. Works only for integers. + +=head2 as_bin() + + $x = Math::BigRat->new('13'); + print $x->as_bin(),"\n"; # '0x1101' + +Returns the BigRat as binary string. Works only for integers. + +=head2 as_oct() + + $x = Math::BigRat->new('13'); + print $x->as_oct(),"\n"; # '015' + +Returns the BigRat as octal string. Works only for integers. + +=head2 from_hex()/from_bin()/from_oct() + + my $h = Math::BigRat->from_hex('0x10'); + my $b = Math::BigRat->from_bin('0b10000000'); + my $o = Math::BigRat->from_oct('020'); + +Create a BigRat from an hexadecimal, binary or octal number +in string form. + +=head2 length() + + $len = $x->length(); + +Return the length of $x in digits for integer values. + +=head2 digit() + + print Math::BigRat->new('123/1')->digit(1); # 1 + print Math::BigRat->new('123/1')->digit(-1); # 3 + +Return the N'ths digit from X when X is an integer value. + +=head2 bnorm() + + $x->bnorm(); + +Reduce the number to the shortest form. This routine is called +automatically whenever it is needed. + +=head2 bfac() + + $x->bfac(); + +Calculates the factorial of $x. For instance: + + print Math::BigRat->new('3/1')->bfac(),"\n"; # 1*2*3 + print Math::BigRat->new('5/1')->bfac(),"\n"; # 1*2*3*4*5 + +Works currently only for integers. + +=head2 bround()/round()/bfround() + +Are not yet implemented. + +=head2 bmod() + + $x->bmod($y); + +Returns $x modulo $y. When $x is finite, and $y is finite and non-zero, the +result is identical to the remainder after floored division (F-division). If, +in addition, both $x and $y are integers, the result is identical to the result +from Perl's % operator. + +=head2 bneg() + + $x->bneg(); + +Used to negate the object in-place. + +=head2 is_one() + + print "$x is 1\n" if $x->is_one(); + +Return true if $x is exactly one, otherwise false. + +=head2 is_zero() + + print "$x is 0\n" if $x->is_zero(); + +Return true if $x is exactly zero, otherwise false. + +=head2 is_pos()/is_positive() + + print "$x is >= 0\n" if $x->is_positive(); + +Return true if $x is positive (greater than or equal to zero), otherwise +false. Please note that '+inf' is also positive, while 'NaN' and '-inf' aren't. + +C is an alias for C. + +=head2 is_neg()/is_negative() + + print "$x is < 0\n" if $x->is_negative(); + +Return true if $x is negative (smaller than zero), otherwise false. Please +note that '-inf' is also negative, while 'NaN' and '+inf' aren't. + +C is an alias for C. + +=head2 is_int() + + print "$x is an integer\n" if $x->is_int(); + +Return true if $x has a denominator of 1 (e.g. no fraction parts), otherwise +false. Please note that '-inf', 'inf' and 'NaN' aren't integer. + +=head2 is_odd() + + print "$x is odd\n" if $x->is_odd(); + +Return true if $x is odd, otherwise false. + +=head2 is_even() + + print "$x is even\n" if $x->is_even(); + +Return true if $x is even, otherwise false. + +=head2 bceil() + + $x->bceil(); + +Set $x to the next bigger integer value (e.g. truncate the number to integer +and then increment it by one). + +=head2 bfloor() + + $x->bfloor(); + +Truncate $x to an integer value. + +=head2 bsqrt() + + $x->bsqrt(); + +Calculate the square root of $x. + +=head2 broot() + + $x->broot($n); + +Calculate the N'th root of $x. + +=head2 badd() + + $x->badd($y); + +Adds $y to $x and returns the result. + +=head2 bmul() + + $x->bmul($y); + +Multiplies $y to $x and returns the result. + +=head2 bsub() + + $x->bsub($y); + +Subtracts $y from $x and returns the result. + +=head2 bdiv() + + $q = $x->bdiv($y); + ($q, $r) = $x->bdiv($y); + +In scalar context, divides $x by $y and returns the result. In list context, +does floored division (F-division), returning an integer $q and a remainder $r +so that $x = $q * $y + $r. The remainer (modulo) is equal to what is returned +by C<$x->bmod($y)>. + +=head2 bdec() + + $x->bdec(); + +Decrements $x by 1 and returns the result. + +=head2 binc() + + $x->binc(); + +Increments $x by 1 and returns the result. + +=head2 copy() + + my $z = $x->copy(); + +Makes a deep copy of the object. + +Please see the documentation in L for further details. + +=head2 bstr()/bsstr() + + my $x = Math::BigInt->new('8/4'); + print $x->bstr(),"\n"; # prints 1/2 + print $x->bsstr(),"\n"; # prints 1/2 + +Return a string representing this object. + +=head2 bacmp()/bcmp() + +Used to compare numbers. + +Please see the documentation in L for further details. + +=head2 blsft()/brsft() + +Used to shift numbers left/right. + +Please see the documentation in L for further details. + +=head2 bpow() + + $x->bpow($y); + +Compute $x ** $y. + +Please see the documentation in L for further details. + +=head2 bexp() + + $x->bexp($accuracy); # calculate e ** X + +Calculates two integers A and B so that A/B is equal to C, where C is +Euler's number. + +This method was added in v0.20 of Math::BigRat (May 2007). + +See also C. + +=head2 bnok() + + $x->bnok($y); # x over y (binomial coefficient n over k) + +Calculates the binomial coefficient n over k, also called the "choose" +function. The result is equivalent to: + + ( n ) n! + | - | = ------- + ( k ) k!(n-k)! + +This method was added in v0.20 of Math::BigRat (May 2007). + +=head2 config() + + use Data::Dumper; + + print Dumper ( Math::BigRat->config() ); + print Math::BigRat->config()->{lib},"\n"; + +Returns a hash containing the configuration, e.g. the version number, lib +loaded etc. The following hash keys are currently filled in with the +appropriate information. + + key RO/RW Description + Example + ============================================================ + lib RO Name of the Math library + Math::BigInt::Calc + lib_version RO Version of 'lib' + 0.30 + class RO The class of config you just called + Math::BigRat + version RO version number of the class you used + 0.10 + upgrade RW To which class numbers are upgraded + undef + downgrade RW To which class numbers are downgraded + undef + precision RW Global precision + undef + accuracy RW Global accuracy + undef + round_mode RW Global round mode + even + div_scale RW Fallback accuracy for div + 40 + trap_nan RW Trap creation of NaN (undef = no) + undef + trap_inf RW Trap creation of +inf/-inf (undef = no) + undef + +By passing a reference to a hash you may set the configuration values. This +works only for values that a marked with a C above, anything else is +read-only. + +=head2 objectify() + +This is an internal routine that turns scalars into objects. + +=head1 BUGS + +Please report any bugs or feature requests to +C, or through the web interface at +L +(requires login). +We will be notified, and then you'll automatically be notified of progress on +your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc Math::BigRat + +You can also look for information at: + +=over 4 + +=item * RT: CPAN's request tracker + +L + +=item * AnnoCPAN: Annotated CPAN documentation + +L + +=item * CPAN Ratings + +L + +=item * Search CPAN + +L + +=item * CPAN Testers Matrix + +L + +=item * The Bignum mailing list + +=over 4 + +=item * Post to mailing list + +C + +=item * View mailing list + +L + +=item * Subscribe/Unsubscribe + +L + +=back + +=back + +=head1 LICENSE + +This program is free software; you may redistribute it and/or modify it under +the same terms as Perl itself. + +=head1 SEE ALSO + +L, L and L as well as the backends +L, L, and L. + +=head1 AUTHORS + +(C) by Tels L 2001 - 2009. + +Currently maintained by Peter John Acklam . + +=cut diff --git a/cpan/Math-BigRat/t/Math/BigRat/Test.pm b/cpan/Math-BigRat/t/Math/BigRat/Test.pm new file mode 100644 index 0000000000..74f9f9d004 --- /dev/null +++ b/cpan/Math-BigRat/t/Math/BigRat/Test.pm @@ -0,0 +1,122 @@ +package Math::BigRat::Test; + +require 5.005_02; +use strict; + +use Exporter; +use Math::BigRat; +use Math::BigFloat; +use vars qw($VERSION @ISA + $accuracy $precision $round_mode $div_scale); + +@ISA = qw(Math::BigRat Exporter); +$VERSION = 0.04; + +use overload; # inherit overload from BigRat + +# Globals +$accuracy = $precision = undef; +$round_mode = 'even'; +$div_scale = 40; + +my $class = 'Math::BigRat::Test'; + +#ub new +#{ +# my $proto = shift; +# my $class = ref($proto) || $proto; +# +# my $value = shift; +# my $a = $accuracy; $a = $_[0] if defined $_[0]; +# my $p = $precision; $p = $_[1] if defined $_[1]; +# # Store the floating point value +# my $self = Math::BigFloat->new($value,$a,$p,$round_mode); +# bless $self, $class; +# $self->{'_custom'} = 1; # make sure this never goes away +# return $self; +#} + +BEGIN + { + *fstr = \&bstr; + *fsstr = \&bsstr; + *objectify = \&Math::BigInt::objectify; + *AUTOLOAD = \&Math::BigRat::AUTOLOAD; + no strict 'refs'; + foreach my $method ( qw/ div acmp floor ceil root sqrt log fac modpow modinv/) + { + *{'b' . $method} = \&{'Math::BigRat::b' . $method}; + } + } + +sub fround + { + my ($x,$a) = @_; + + #print "$a $accuracy $precision $round_mode\n"; + Math::BigFloat->round_mode($round_mode); + Math::BigFloat->accuracy($a || $accuracy); + Math::BigFloat->precision(undef); + my $y = Math::BigFloat->new($x->bsstr(),undef,undef); + $class->new($y->fround($a)); + } + +sub ffround + { + my ($x,$p) = @_; + + Math::BigFloat->round_mode($round_mode); + Math::BigFloat->accuracy(undef); + Math::BigFloat->precision($p || $precision); + my $y = Math::BigFloat->new($x->bsstr(),undef,undef); + $class->new($y->ffround($p)); + } + +sub bstr + { + # calculate a BigFloat compatible string output + my ($x) = @_; + + $x = $class->new($x) unless ref $x; + + if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc + { + my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf + return $s; + } + + my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # +3 vs 3 + +# print " bstr \$x ", $accuracy || $x->{_a} || 'notset', " ", $precision || $x->{_p} || 'notset', "\n"; + return $s.$x->{_n} if $x->{_d}->is_one(); + my $output = Math::BigFloat->new($x->{_n})->bdiv($x->{_d}); + local $Math::BigFloat::accuracy = $accuracy || $x->{_a}; + local $Math::BigFloat::precision = $precision || $x->{_p}; + $s.$output->bstr(); + } + +sub numify + { + $_[0]->bsstr(); + } + +sub bsstr + { + # calculate a BigFloat compatible string output + my ($x) = @_; + + $x = $class->new($x) unless ref $x; + + if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc + { + my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf + return $s; + } + + my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # +3 vs 3 + + my $output = Math::BigFloat->new($x->{_n})->bdiv($x->{_d}); + return $s.$output->bsstr(); + } + +1; diff --git a/cpan/Math-BigRat/t/big_ap.t b/cpan/Math-BigRat/t/big_ap.t new file mode 100644 index 0000000000..1b45eddfe7 --- /dev/null +++ b/cpan/Math-BigRat/t/big_ap.t @@ -0,0 +1,94 @@ +#!/usr/bin/perl -w + +# Test that accuracy() and precision() in BigInt/BigFloat do not disturb +# the rounding force in BigRat. + +use strict; +use Test::More tests => 17; + +use Math::BigInt; +use Math::BigFloat; +use Math::BigRat; + +my $r = 'Math::BigRat'; +my $proper = $r->new('12345678901234567890/2'); +my $proper_inc = $r->new('12345678901234567890/2')->binc(); +my $proper_dec = $r->new('12345678901234567890/2')->bdec(); +my $proper_int = Math::BigInt->new('12345678901234567890'); +my $proper_float = Math::BigFloat->new('12345678901234567890'); +my $proper2 = $r->new('12345678901234567890'); + +print "# Start\n"; + +Math::BigInt->accuracy(3); +Math::BigFloat->accuracy(5); + +my ($x,$y,$z); + +############################################################################## +# new() + +$z = $r->new('12345678901234567890/2'); +is ($z,$proper); + +$z = $r->new('1234567890123456789E1'); +is ($z,$proper2); + +$z = $r->new('12345678901234567890/1E0'); +is ($z,$proper2); +$z = $r->new('1234567890123456789e1/1'); +is ($z,$proper2); +$z = $r->new('1234567890123456789e1/1E0'); +is ($z,$proper2); + +$z = $r->new($proper_int); +is ($z,$proper2); + +$z = $r->new($proper_float); +is ($z,$proper2); + +############################################################################## +# bdiv + +$x = $r->new('12345678901234567890'); $y = Math::BigRat->new('2'); +$z = $x->copy->bdiv($y); +is ($z,$proper); + +############################################################################## +# bmul + +$x = $r->new("$proper"); $y = Math::BigRat->new('1'); +$z = $x->copy->bmul($y); +is ($z,$proper); +$z = $r->new('12345678901234567890/1E0'); +is ($z,$proper2); + +$z = $r->new($proper_int); +is ($z,$proper2); + +$z = $r->new($proper_float); +is ($z,$proper2); + +############################################################################## +# bdiv + +$x = $r->new('12345678901234567890'); $y = Math::BigRat->new('2'); +$z = $x->copy->bdiv($y); +is ($z,$proper); + +############################################################################## +# bmul + +$x = $r->new("$proper"); $y = Math::BigRat->new('1'); +$z = $x->copy->bmul($y); +is ($z,$proper); + +$x = $r->new("$proper"); $y = Math::BigRat->new('2'); +$z = $x->copy->bmul($y); +is ($z,$proper2); + +############################################################################## +# binc/bdec + +$x = $proper->copy()->binc(); is ($x,$proper_inc); +$x = $proper->copy()->bdec(); is ($x,$proper_dec); diff --git a/cpan/Math-BigRat/t/bigfltpm.inc b/cpan/Math-BigRat/t/bigfltpm.inc new file mode 100644 index 0000000000..9c884b7d9e --- /dev/null +++ b/cpan/Math-BigRat/t/bigfltpm.inc @@ -0,0 +1,1673 @@ +#include this file into another test for subclass testing... + +ok ($class->config()->{lib},$CL); + +use strict; + +my $z; + +while () + { + chomp; + $_ =~ s/#.*$//; # remove comments + $_ =~ s/\s+$//; # trailing spaces + next if /^$/; # skip empty lines & comments + if (s/^&//) + { + $f = $_; + } + elsif (/^\$/) + { + $setup = $_; $setup =~ s/\$/\$${class}::/g; # round_mode, div_scale + #print "\$setup== $setup\n"; + } + else + { + if (m|^(.*?):(/.+)$|) + { + $ans = $2; + @args = split(/:/,$1,99); + } + else + { + @args = split(/:/,$_,99); $ans = pop(@args); + } + $try = "\$x = $class->new(\"$args[0]\");"; + if ($f eq "fnorm") + { + $try .= "\$x;"; + } elsif ($f eq "finf") { + $try .= "\$x->finf('$args[1]');"; + } elsif ($f eq "is_inf") { + $try .= "\$x->is_inf('$args[1]');"; + } elsif ($f eq "fone") { + $try .= "\$x->bone('$args[1]');"; + } elsif ($f eq "fstr") { + $try .= "\$x->accuracy($args[1]); \$x->precision($args[2]);"; + $try .= '$x->fstr();'; + } elsif ($f eq "parts") { + # ->bstr() to see if an object is returned + $try .= '($a,$b) = $x->parts(); $a = $a->bstr(); $b = $b->bstr();'; + $try .= '"$a $b";'; + } elsif ($f eq "exponent") { + # ->bstr() to see if an object is returned + $try .= '$x->exponent()->bstr();'; + } elsif ($f eq "mantissa") { + # ->bstr() to see if an object is returned + $try .= '$x->mantissa()->bstr();'; + } elsif ($f =~ /^(numify|length|as_number|as_hex|as_bin)$/) { + $try .= "\$x->$f();"; + # some unary ops (test the fxxx form, since that is done by AUTOLOAD) + } elsif ($f =~ /^f(nan|sstr|neg|floor|ceil|abs)$/) { + $try .= "\$x->f$1();"; + # some is_xxx test function + } elsif ($f =~ /^is_(zero|one|negative|positive|odd|even|nan|int)$/) { + $try .= "\$x->$f();"; + } elsif ($f eq "finc") { + $try .= '++$x;'; + } elsif ($f eq "fdec") { + $try .= '--$x;'; + }elsif ($f eq "fround") { + $try .= "$setup; \$x->fround($args[1]);"; + } elsif ($f eq "ffround") { + $try .= "$setup; \$x->ffround($args[1]);"; + } elsif ($f eq "fsqrt") { + $try .= "$setup; \$x->fsqrt();"; + } elsif ($f eq "ffac") { + $try .= "$setup; \$x->ffac();"; + } elsif ($f eq "flog") { + if (defined $args[1] && $args[1] ne '') + { + $try .= "\$y = $class->new($args[1]);"; + $try .= "$setup; \$x->flog(\$y);"; + } + else + { + $try .= "$setup; \$x->flog();"; + } + } + else + { + $try .= "\$y = $class->new(\"$args[1]\");"; + + if ($f eq "bgcd") + { + if (defined $args[2]) + { + $try .= " \$z = $class->new(\"$args[2]\"); "; + } + $try .= "$class\::bgcd(\$x, \$y"; + $try .= ", \$z" if (defined $args[2]); + $try .= " );"; + } + elsif ($f eq "blcm") + { + if (defined $args[2]) + { + $try .= " \$z = $class->new(\"$args[2]\"); "; + } + $try .= "$class\::blcm(\$x, \$y"; + $try .= ", \$z" if (defined $args[2]); + $try .= " );"; + } elsif ($f eq "fcmp") { + $try .= '$x->fcmp($y);'; + } elsif ($f eq "facmp") { + $try .= '$x->facmp($y);'; + } elsif ($f eq "fpow") { + $try .= '$x ** $y;'; + } elsif ($f eq "bnok") { + $try .= '$x->bnok($y);'; + } elsif ($f eq "froot") { + $try .= "$setup; \$x->froot(\$y);"; + } elsif ($f eq "fadd") { + $try .= '$x + $y;'; + } elsif ($f eq "fsub") { + $try .= '$x - $y;'; + } elsif ($f eq "fmul") { + $try .= '$x * $y;'; + } elsif ($f eq "fdiv") { + $try .= "$setup; \$x / \$y;"; + } elsif ($f eq "fdiv-list") { + $try .= "$setup; join(',',\$x->fdiv(\$y));"; + } elsif ($f eq "frsft") { + $try .= '$x >> $y;'; + } elsif ($f eq "flsft") { + $try .= '$x << $y;'; + } elsif ($f eq "fmod") { + $try .= '$x % $y;'; + } else { warn "Unknown op '$f'"; } + } + # print "# Trying: '$try'\n"; + $ans1 = eval $try; + print "# Error: $@\n" if $@; + if ($ans =~ m|^/(.*)$|) + { + my $pat = $1; + if ($ans1 =~ /$pat/) + { + ok (1,1); + } + else + { + print "# '$try' expected: /$pat/ got: '$ans1'\n" if !ok(1,0); + } + } + else + { + if ($ans eq "") + { + ok_undef ($ans1); + } + else + { + print "# Tried: '$try'\n" if !ok ($ans1, $ans); + if (ref($ans1) eq "$class") + { + # float numbers are normalized (for now), so mantissa shouldn't have + # trailing zeros + #print $ans1->_trailing_zeros(),"\n"; + print "# Has trailing zeros after '$try'\n" + if !ok ($CL->_zeros( $ans1->{_m}), 0); + } + } + } # end pattern or string + } + } # end while + +# check whether $class->new( Math::BigInt->new()) destroys it +# ($y == 12 in this case) +$x = Math::BigInt->new(1200); $y = $class->new($x); +ok ($y,1200); ok ($x,1200); + +############################################################################### +# Really huge, big, ultra-mega-biggy-monster exponents +# Technically, the exponents should not be limited (they are BigInts), but +# practically there are a few places were they are limited to a Perl scalar. +# This is sometimes for speed, sometimes because otherwise the number wouldn't +# fit into your memory (just think of 1e123456789012345678901234567890 + 1!) +# anyway. We don't test everything here, but let's make sure it just basically +# works. + +my $monster = '1e1234567890123456789012345678901234567890'; + +# new and exponent +ok ($class->new($monster)->bsstr(), + '1e+1234567890123456789012345678901234567890'); +ok ($class->new($monster)->exponent(), + '1234567890123456789012345678901234567890'); +# cmp +ok ($class->new($monster) > 0,1); + +# sub/mul +ok ($class->new($monster)->bsub( $monster),0); +ok ($class->new($monster)->bmul(2)->bsstr(), + '2e+1234567890123456789012345678901234567890'); + +# mantissa +$monster = '1234567890123456789012345678901234567890e2'; +ok ($class->new($monster)->mantissa(), + '123456789012345678901234567890123456789'); + +############################################################################### +# zero,inf,one,nan + +$x = $class->new(2); $x->fzero(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); +$x = $class->new(2); $x->finf(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); +$x = $class->new(2); $x->fone(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); +$x = $class->new(2); $x->fnan(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); + +############################################################################### +# bone/binf etc as plain calls (Lite failed them) + +ok ($class->fzero(),0); +ok ($class->fone(),1); +ok ($class->fone('+'),1); +ok ($class->fone('-'),-1); +ok ($class->fnan(),'NaN'); +ok ($class->finf(),'inf'); +ok ($class->finf('+'),'inf'); +ok ($class->finf('-'),'-inf'); +ok ($class->finf('-inf'),'-inf'); + +$class->accuracy(undef); $class->precision(undef); # reset + +############################################################################### +# bug in bsstr()/numify() showed up in after-rounding in bdiv() + +$x = $class->new('0.008'); $y = $class->new(2); +$x->bdiv(3,$y); +ok ($x,'0.0027'); + +############################################################################### +# fsqrt() with set global A/P or A/P enabled on $x, also a test whether fsqrt() +# correctly modifies $x + + +$x = $class->new(12); $class->precision(-2); $x->fsqrt(); ok ($x,'3.46'); + +$class->precision(undef); +$x = $class->new(12); $class->precision(0); $x->fsqrt(); ok ($x,'3'); + +$class->precision(-3); $x = $class->new(12); $x->fsqrt(); ok ($x,'3.464'); + +{ + no strict 'refs'; + # A and P set => NaN + ${${class}.'::accuracy'} = 4; $x = $class->new(12); + $x->fsqrt(3); ok ($x,'NaN'); + # supplied arg overrides set global + $class->precision(undef); $x = $class->new(12); $x->fsqrt(3); ok ($x,'3.46'); + $class->accuracy(undef); $class->precision(undef); # reset for further tests +} + +############################################################################# +# can we call objectify (broken until v1.52) + +{ + no strict; + $try = + '@args' . " = $class" . "::objectify(2,$class,4,5);".'join(" ",@args);'; + $ans = eval $try; + ok ($ans,"$class 4 5"); +} + +############################################################################# +# is_one('-') (broken until v1.64) + +ok ($class->new(-1)->is_one(),0); +ok ($class->new(-1)->is_one('-'),1); + +############################################################################# +# bug 1/0.5 leaving 2e-0 instead of 2e0 + +ok ($class->new(1)->fdiv('0.5')->bsstr(),'2e+0'); + +############################################################################### +# [perl #30609] bug with $x -= $x not being 0, but 2*$x + +$x = $class->new(3); $x -= $x; ok ($x, 0); +$x = $class->new(-3); $x -= $x; ok ($x, 0); +$x = $class->new(3); $x += $x; ok ($x, 6); +$x = $class->new(-3); $x += $x; ok ($x, -6); + +$x = $class->new('NaN'); $x -= $x; ok ($x->is_nan(), 1); +$x = $class->new('inf'); $x -= $x; ok ($x->is_nan(), 1); +$x = $class->new('-inf'); $x -= $x; ok ($x->is_nan(), 1); + +$x = $class->new('NaN'); $x += $x; ok ($x->is_nan(), 1); +$x = $class->new('inf'); $x += $x; ok ($x->is_inf(), 1); +$x = $class->new('-inf'); $x += $x; ok ($x->is_inf('-'), 1); + +$x = $class->new('3.14'); $x -= $x; ok ($x, 0); +$x = $class->new('-3.14'); $x -= $x; ok ($x, 0); +$x = $class->new('3.14'); $x += $x; ok ($x, '6.28'); +$x = $class->new('-3.14'); $x += $x; ok ($x, '-6.28'); + +$x = $class->new('3.14'); $x *= $x; ok ($x, '9.8596'); +$x = $class->new('-3.14'); $x *= $x; ok ($x, '9.8596'); +$x = $class->new('3.14'); $x /= $x; ok ($x, '1'); +$x = $class->new('-3.14'); $x /= $x; ok ($x, '1'); +$x = $class->new('3.14'); $x %= $x; ok ($x, '0'); +$x = $class->new('-3.14'); $x %= $x; ok ($x, '0'); + +############################################################################### +# the following two were reported by "kenny" via hotmail.com: + +#perl -MMath::BigFloat -wle 'print Math::BigFloat->new(0)->bpow(".1")' +#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. + +$x = $class->new(0); $y = $class->new('0.1'); +ok ($x ** $y, 0, 'no warnings and zero result'); + +#perl -MMath::BigFloat -lwe 'print Math::BigFloat->new(".222222222222222222222222222222222222222222")->bceil()' +#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. + +$x = $class->new(".222222222222222222222222222222222222222222"); +ok ($x->bceil(), 1, 'no warnings and one as result'); + +############################################################################### +# test **=, <<=, >>= + +# ((2^148)-1)/17 +$x = $class->new(2); $x **= 148; $x++; $x->bdiv(17, 60)->bfloor(); $x->accuracy(undef); +ok ($x,"20988936657440586486151264256610222593863921"); +ok ($x->length(),length "20988936657440586486151264256610222593863921"); + +$x = $class->new('2'); +my $y = $class->new('18'); +ok ($x <<= $y, 2 << 18); +ok ($x, 2 << 18); +ok ($x >>= $y, 2); +ok ($x, 2); + +$x = $class->new('2'); +$y = $class->new('18.2'); +$x <<= $y; # 2 * (2 ** 18.2); + +ok ($x->copy()->bfround(-9), '602248.763144685'); +ok ($x >>= $y, 2); # 2 * (2 ** 18.2) / (2 ** 18.2) => 2 +ok ($x, 2); + +1; # all done + +############################################################################### +# Perl 5.005 does not like ok ($x,undef) + +sub ok_undef + { + my $x = shift; + + ok (1,1) and return if !defined $x; + ok ($x,'undef'); + } + +__DATA__ +&bgcd +inf:12:NaN +-inf:12:NaN +12:inf:NaN +12:-inf:NaN +inf:inf:NaN +inf:-inf:NaN +-inf:-inf:NaN +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++0:+0:0 ++0:+1:1 ++1:+0:1 ++1:+1:1 ++2:+3:1 ++3:+2:1 +-3:+2:1 +-3:-2:1 +-144:-60:12 +144:-60:12 +144:60:12 +100:625:25 +4096:81:1 +1034:804:2 +27:90:56:1 +27:90:54:9 +&blcm +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++0:+0:NaN ++1:+0:0 ++0:+1:0 ++27:+90:270 ++1034:+804:415668 +$div_scale = 40; +&bnok ++inf:10:inf +NaN:NaN:NaN +NaN:1:NaN +1:NaN:NaN +1:1:1 +# k > n +1:2:0 +2:3:0 +# k < 0 +1:-2:0 +# 7 over 3 = 35 +7:3:35 +7:6:1 +100:90:17310309456440 +&flog +0::NaN +-1::NaN +-2::NaN +# base > 0, base != 1 +2:-1:NaN +2:0:NaN +2:1:NaN +# log(1) is always 1, regardless of $base +1::0 +1:1:0 +1:2:0 +2::0.6931471805599453094172321214581765680755 +2.718281828::0.9999999998311266953289851340574956564911 +$div_scale = 20; +2.718281828::0.99999999983112669533 +$div_scale = 15; +123::4.81218435537242 +10::2.30258509299405 +1000::6.90775527898214 +100::4.60517018598809 +2::0.693147180559945 +3.1415::1.14470039286086 +12345::9.42100640177928 +0.001::-6.90775527898214 +# bug until v1.71: +10:10:1 +100:100:1 +# reset for further tests +$div_scale = 40; +1::0 +&frsft +NaNfrsft:2:NaN +0:2:0 +1:1:0.5 +2:1:1 +4:1:2 +123:1:61.5 +32:3:4 +&flsft +NaNflsft:0:NaN +2:1:4 +4:3:32 +5:3:40 +1:2:4 +0:5:0 +&fnorm +1:1 +-0:0 +fnormNaN:NaN ++inf:inf +-inf:-inf +123:123 +-123.4567:-123.4567 +# invalid inputs +1__2:NaN +1E1__2:NaN +11__2E2:NaN +.2E-3.:NaN +1e3e4:NaN +# strange, but valid +.2E2:20 +1.E3:1000 +# some inputs that result in zero +0e0:0 ++0e0:0 ++0e+0:0 +-0e+0:0 +0e-0:0 +-0e-0:0 ++0e-0:0 +000:0 +00e2:0 +00e02:0 +000e002:0 +000e1230:0 +00e-3:0 +00e+3:0 +00e-03:0 +00e+03:0 +-000:0 +-00e2:0 +-00e02:0 +-000e002:0 +-000e1230:0 +-00e-3:0 +-00e+3:0 +-00e-03:0 +-00e+03:0 +&as_number +0:0 +1:1 +1.2:1 +2.345:2 +-2:-2 +-123.456:-123 +-200:-200 +# test for bug in brsft() not handling cases that return 0 +0.000641:0 +0.0006412:0 +0.00064123:0 +0.000641234:0 +0.0006412345:0 +0.00064123456:0 +0.000641234567:0 +0.0006412345678:0 +0.00064123456789:0 +0.1:0 +0.01:0 +0.001:0 +0.0001:0 +0.00001:0 +0.000001:0 +0.0000001:0 +0.00000001:0 +0.000000001:0 +0.0000000001:0 +0.00000000001:0 +0.12345:0 +0.123456:0 +0.1234567:0 +0.12345678:0 +0.123456789:0 +&finf +1:+:inf +2:-:-inf +3:abc:inf +&as_hex ++inf:inf +-inf:-inf +hexNaN:NaN +0:0x0 +5:0x5 +-5:-0x5 +&as_bin ++inf:inf +-inf:-inf +hexNaN:NaN +0:0b0 +5:0b101 +-5:-0b101 +&numify +# uses bsstr() so 5 => 5e+0 to be compatible w/ Perls output +0:0e+1 ++1:1e+0 +1234:1234e+0 +NaN:NaN ++inf:inf +-inf:-inf +-5:-5e+0 +100:1e+2 +-100:-1e+2 +&fnan +abc:NaN +2:NaN +-2:NaN +0:NaN +&fone +2:+:1 +-2:-:-1 +-2:+:1 +2:-:-1 +0::1 +-2::1 +abc::1 +2:abc:1 +&fsstr ++inf:inf +-inf:-inf +abcfsstr:NaN +-abcfsstr:NaN +1234.567:1234567e-3 +123:123e+0 +-5:-5e+0 +-100:-1e+2 +&fstr ++inf:::inf +-inf:::-inf +abcfstr:::NaN +1234.567:9::1234.56700 +1234.567::-6:1234.567000 +12345:5::12345 +0.001234:6::0.00123400 +0.001234::-8:0.00123400 +0:4::0 +0::-4:0.0000 +&fnorm +inf:inf ++inf:inf +-inf:-inf ++infinity:NaN ++-inf:NaN +abc:NaN + 1 a:NaN +1bcd2:NaN +11111b:NaN ++1z:NaN +-1z:NaN +0e999:0 +0e-999:0 +-0e999:0 +-0e-999:0 +0:0 ++0:0 ++00:0 ++0_0_0:0 +000000_0000000_00000:0 +-0:0 +-0000:0 ++1:1 ++01:1 ++001:1 ++00000100000:100000 +123456789:123456789 +-1:-1 +-01:-1 +-001:-1 +-123456789:-123456789 +-00000100000:-100000 +123.456a:NaN +123.456:123.456 +0.01:0.01 +.002:0.002 ++.2:0.2 +-0.0003:-0.0003 +-.0000000004:-0.0000000004 +123456E2:12345600 +123456E-2:1234.56 +-123456E2:-12345600 +-123456E-2:-1234.56 +1e1:10 +2e-11:0.00000000002 +# exercise _split + .02e-1:0.002 + 000001:1 + -00001:-1 + -1:-1 + 000.01:0.01 + -000.0023:-0.0023 + 1.1e1:11 +-3e111:-3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +-4e-1111:-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004 +&fpow +NaN:1:NaN +1:NaN:NaN +NaN:-1:NaN +-1:NaN:NaN +NaN:-21:NaN +-21:NaN:NaN +NaN:21:NaN +21:NaN:NaN +0:0:1 +0:1:0 +0:9:0 +0:-2:inf +2:2:4 +1:2:1 +1:3:1 +-1:2:1 +-1:3:-1 +123.456:2:15241.383936 +2:-2:0.25 +2:-3:0.125 +128:-2:0.00006103515625 +abc:123.456:NaN +123.456:abc:NaN ++inf:123.45:inf +-inf:123.45:-inf ++inf:-123.45:inf +-inf:-123.45:-inf +-2:2:4 +-2:3:-8 +-2:4:16 +-2:5:-32 +-3:2:9 +-3:3:-27 +-3:4:81 +-3:5:-243 +# 2 ** 0.5 == sqrt(2) +# 1.41..7 and not 1.4170 since fallback (bsqrt(9) is '3', not 3.0...0) +2:0.5:1.41421356237309504880168872420969807857 +#2:0.2:1.148698354997035006798626946777927589444 +#6:1.5:14.6969384566990685891837044482353483518 +$div_scale = 20; +#62.5:12.5:26447206647554886213592.3959144 +$div_scale = 40; +&fneg +fnegNaN:NaN ++inf:-inf +-inf:inf ++0:0 ++1:-1 +-1:1 ++123456789:-123456789 +-123456789:123456789 ++123.456789:-123.456789 +-123456.789:123456.789 +&fabs +fabsNaN:NaN ++inf:inf +-inf:inf ++0:0 ++1:1 +-1:1 ++123456789:123456789 +-123456789:123456789 ++123.456789:123.456789 +-123456.789:123456.789 +&fround +$round_mode = "trunc" ++inf:5:inf +-inf:5:-inf +0:5:0 +NaNfround:5:NaN ++10123456789:5:10123000000 +-10123456789:5:-10123000000 ++10123456789.123:5:10123000000 +-10123456789.123:5:-10123000000 ++10123456789:9:10123456700 +-10123456789:9:-10123456700 ++101234500:6:101234000 +-101234500:6:-101234000 +$round_mode = "zero" ++20123456789:5:20123000000 +-20123456789:5:-20123000000 ++20123456789.123:5:20123000000 +-20123456789.123:5:-20123000000 ++20123456789:9:20123456800 +-20123456789:9:-20123456800 ++201234500:6:201234000 +-201234500:6:-201234000 +$round_mode = "+inf" ++30123456789:5:30123000000 +-30123456789:5:-30123000000 ++30123456789.123:5:30123000000 +-30123456789.123:5:-30123000000 ++30123456789:9:30123456800 +-30123456789:9:-30123456800 ++301234500:6:301235000 +-301234500:6:-301234000 +$round_mode = "-inf" ++40123456789:5:40123000000 +-40123456789:5:-40123000000 ++40123456789.123:5:40123000000 +-40123456789.123:5:-40123000000 ++40123456789:9:40123456800 +-40123456789:9:-40123456800 ++401234500:6:401234000 +-401234500:6:-401235000 +$round_mode = "odd" ++50123456789:5:50123000000 +-50123456789:5:-50123000000 ++50123456789.123:5:50123000000 +-50123456789.123:5:-50123000000 ++50123456789:9:50123456800 +-50123456789:9:-50123456800 ++501234500:6:501235000 +-501234500:6:-501235000 +$round_mode = "even" ++60123456789:5:60123000000 +-60123456789:5:-60123000000 ++60123456789:9:60123456800 +-60123456789:9:-60123456800 ++601234500:6:601234000 +-601234500:6:-601234000 ++60123456789.0123:5:60123000000 +-60123456789.0123:5:-60123000000 +$round_mode = "common" ++60123456789:5:60123000000 +-60123456789:5:-60123000000 ++60123456789:6:60123500000 +-60123456789:6:-60123500000 ++60123456789:9:60123456800 +-60123456789:9:-60123456800 ++601234500:6:601235000 +-601234500:6:-601235000 ++601234400:6:601234000 +-601234400:6:-601234000 ++601234600:6:601235000 +-601234600:6:-601235000 ++601234300:6:601234000 ++60123456789.0123:5:60123000000 +-60123456789.0123:5:-60123000000 +&ffround +$round_mode = "trunc" ++inf:5:inf +-inf:5:-inf +0:5:0 +NaNffround:5:NaN ++1.23:-1:1.2 ++1.234:-1:1.2 ++1.2345:-1:1.2 ++1.23:-2:1.23 ++1.234:-2:1.23 ++1.2345:-2:1.23 ++1.23:-3:1.230 ++1.234:-3:1.234 ++1.2345:-3:1.234 +-1.23:-1:-1.2 ++1.27:-1:1.2 +-1.27:-1:-1.2 ++1.25:-1:1.2 +-1.25:-1:-1.2 ++1.35:-1:1.3 +-1.35:-1:-1.3 +-0.0061234567890:-1:0.0 +-0.0061:-1:0.0 +-0.00612:-1:0.0 +-0.00612:-2:0.00 +-0.006:-1:0.0 +-0.006:-2:0.00 +-0.0006:-2:0.00 +-0.0006:-3:0.000 +-0.0065:-3:/-0\.006|-6e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:0 +0.51:0:0 +0.41:0:0 +$round_mode = "zero" ++2.23:-1:/2.2(?:0{5}\d+)? +-2.23:-1:/-2.2(?:0{5}\d+)? ++2.27:-1:/2.(?:3|29{5}\d+) +-2.27:-1:/-2.(?:3|29{5}\d+) ++2.25:-1:/2.2(?:0{5}\d+)? +-2.25:-1:/-2.2(?:0{5}\d+)? ++2.35:-1:/2.(?:3|29{5}\d+) +-2.35:-1:/-2.(?:3|29{5}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.006|-6e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:0 +0.51:0:1 +0.41:0:0 +$round_mode = "+inf" ++3.23:-1:/3.2(?:0{5}\d+)? +-3.23:-1:/-3.2(?:0{5}\d+)? ++3.27:-1:/3.(?:3|29{5}\d+) +-3.27:-1:/-3.(?:3|29{5}\d+) ++3.25:-1:/3.(?:3|29{5}\d+) +-3.25:-1:/-3.2(?:0{5}\d+)? ++3.35:-1:/3.(?:4|39{5}\d+) +-3.35:-1:/-3.(?:3|29{5}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.006|-6e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:1 +0.51:0:1 +0.41:0:0 +$round_mode = "-inf" ++4.23:-1:/4.2(?:0{5}\d+)? +-4.23:-1:/-4.2(?:0{5}\d+)? ++4.27:-1:/4.(?:3|29{5}\d+) +-4.27:-1:/-4.(?:3|29{5}\d+) ++4.25:-1:/4.2(?:0{5}\d+)? +-4.25:-1:/-4.(?:3|29{5}\d+) ++4.35:-1:/4.(?:3|29{5}\d+) +-4.35:-1:/-4.(?:4|39{5}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.007|-7e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:0 +0.51:0:1 +0.41:0:0 +$round_mode = "odd" ++5.23:-1:/5.2(?:0{5}\d+)? +-5.23:-1:/-5.2(?:0{5}\d+)? ++5.27:-1:/5.(?:3|29{5}\d+) +-5.27:-1:/-5.(?:3|29{5}\d+) ++5.25:-1:/5.(?:3|29{5}\d+) +-5.25:-1:/-5.(?:3|29{5}\d+) ++5.35:-1:/5.(?:3|29{5}\d+) +-5.35:-1:/-5.(?:3|29{5}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.007|-7e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:1 +0.51:0:1 +0.41:0:0 +$round_mode = "even" ++6.23:-1:/6.2(?:0{5}\d+)? +-6.23:-1:/-6.2(?:0{5}\d+)? ++6.27:-1:/6.(?:3|29{5}\d+) +-6.27:-1:/-6.(?:3|29{5}\d+) ++6.25:-1:/6.(?:2(?:0{5}\d+)?|29{5}\d+) +-6.25:-1:/-6.(?:2(?:0{5}\d+)?|29{5}\d+) ++6.35:-1:/6.(?:4|39{5}\d+|29{8}\d+) +-6.35:-1:/-6.(?:4|39{5}\d+|29{8}\d+) +-0.0065:-1:0.0 +-0.0065:-2:/-0\.01|-1e-02 +-0.0065:-3:/-0\.006|-7e-03 +-0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +-0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 +0.05:0:0 +0.5:0:0 +0.51:0:1 +0.41:0:0 +0.01234567:-3:0.012 +0.01234567:-4:0.0123 +0.01234567:-5:0.01235 +0.01234567:-6:0.012346 +0.01234567:-7:0.0123457 +0.01234567:-8:0.01234567 +0.01234567:-9:0.012345670 +0.01234567:-12:0.012345670000 +&fcmp +fcmpNaN:fcmpNaN: +fcmpNaN:+0: ++0:fcmpNaN: ++0:+0:0 +-1:+0:-1 ++0:-1:1 ++1:+0:1 ++0:+1:-1 +-1:+1:-1 ++1:-1:1 +-1:-1:0 ++1:+1:0 +-1.1:0:-1 ++0:-1.1:1 ++1.1:+0:1 ++0:+1.1:-1 ++123:+123:0 ++123:+12:1 ++12:+123:-1 +-123:-123:0 +-123:-12:-1 +-12:-123:1 ++123:+124:-1 ++124:+123:1 +-123:-124:1 +-124:-123:-1 +0:0.01:-1 +0:0.0001:-1 +0:-0.0001:1 +0:-0.1:1 +0.1:0:1 +0.00001:0:1 +-0.0001:0:-1 +-0.1:0:-1 +0:0.0001234:-1 +0:-0.0001234:1 +0.0001234:0:1 +-0.0001234:0:-1 +0.0001:0.0005:-1 +0.0005:0.0001:1 +0.005:0.0001:1 +0.001:0.0005:1 +0.000001:0.0005:-1 +0.00000123:0.0005:-1 +0.00512:0.0001:1 +0.005:0.000112:1 +0.00123:0.0005:1 +1.5:2:-1 +2:1.5:1 +1.54321:234:-1 +234:1.54321:1 +# infinity +-inf:5432112345:-1 ++inf:5432112345:1 +-inf:-5432112345:-1 ++inf:-5432112345:1 +-inf:54321.12345:-1 ++inf:54321.12345:1 +-inf:-54321.12345:-1 ++inf:-54321.12345:1 ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:1 +-inf:+inf:-1 +# return undef ++inf:NaN: +NaN:inf: +-inf:NaN: +NaN:-inf: +&facmp +fcmpNaN:fcmpNaN: +fcmpNaN:+0: ++0:fcmpNaN: ++0:+0:0 +-1:+0:1 ++0:-1:-1 ++1:+0:1 ++0:+1:-1 +-1:+1:0 ++1:-1:0 +-1:-1:0 ++1:+1:0 +-1.1:0:1 ++0:-1.1:-1 ++1.1:+0:1 ++0:+1.1:-1 ++123:+123:0 ++123:+12:1 ++12:+123:-1 +-123:-123:0 +-123:-12:1 +-12:-123:-1 ++123:+124:-1 ++124:+123:1 +-123:-124:-1 +-124:-123:1 +0:0.01:-1 +0:0.0001:-1 +0:-0.0001:-1 +0:-0.1:-1 +0.1:0:1 +0.00001:0:1 +-0.0001:0:1 +-0.1:0:1 +0:0.0001234:-1 +0:-0.0001234:-1 +0.0001234:0:1 +-0.0001234:0:1 +0.0001:0.0005:-1 +0.0005:0.0001:1 +0.005:0.0001:1 +0.001:0.0005:1 +0.000001:0.0005:-1 +0.00000123:0.0005:-1 +0.00512:0.0001:1 +0.005:0.000112:1 +0.00123:0.0005:1 +1.5:2:-1 +2:1.5:1 +1.54321:234:-1 +234:1.54321:1 +# infinity +-inf:5432112345:1 ++inf:5432112345:1 +-inf:-5432112345:1 ++inf:-5432112345:1 +-inf:54321.12345:1 ++inf:54321.12345:1 +-inf:-54321.12345:1 ++inf:-54321.12345:1 ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:0 +-inf:+inf:0 +5:inf:-1 +-1:inf:-1 +5:-inf:-1 +-1:-inf:-1 +# return undef ++inf:facmpNaN: +facmpNaN:inf: +-inf:facmpNaN: +facmpNaN:-inf: +&fdec +fdecNaN:NaN ++inf:inf +-inf:-inf ++0:-1 ++1:0 +-1:-2 +1.23:0.23 +-1.23:-2.23 +100:99 +101:100 +-100:-101 +-99:-100 +-98:-99 +99:98 +&finc +fincNaN:NaN ++inf:inf +-inf:-inf ++0:1 ++1:2 +-1:0 +1.23:2.23 +-1.23:-0.23 +100:101 +-100:-99 +-99:-98 +-101:-100 +99:100 +&fadd +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:-inf:NaN +-inf:+inf:NaN ++inf:+inf:inf +-inf:-inf:-inf +baddNaN:+inf:NaN +baddNaN:+inf:NaN ++inf:baddNaN:NaN +-inf:baddNaN:NaN ++0:+0:0 ++1:+0:1 ++0:+1:1 ++1:+1:2 +-1:+0:-1 ++0:-1:-1 +-1:-1:-2 +-1:+1:0 ++1:-1:0 ++9:+1:10 ++99:+1:100 ++999:+1:1000 ++9999:+1:10000 ++99999:+1:100000 ++999999:+1:1000000 ++9999999:+1:10000000 ++99999999:+1:100000000 ++999999999:+1:1000000000 ++9999999999:+1:10000000000 ++99999999999:+1:100000000000 ++10:-1:9 ++100:-1:99 ++1000:-1:999 ++10000:-1:9999 ++100000:-1:99999 ++1000000:-1:999999 ++10000000:-1:9999999 ++100000000:-1:99999999 ++1000000000:-1:999999999 ++10000000000:-1:9999999999 ++123456789:+987654321:1111111110 +-123456789:+987654321:864197532 +-123456789:-987654321:-1111111110 ++123456789:-987654321:-864197532 +0.001234:0.0001234:0.0013574 +&fsub +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:-inf:inf +-inf:+inf:-inf ++inf:+inf:NaN +-inf:-inf:NaN +baddNaN:+inf:NaN +baddNaN:+inf:NaN ++inf:baddNaN:NaN +-inf:baddNaN:NaN ++0:+0:0 ++1:+0:1 ++0:+1:-1 ++1:+1:0 +-1:+0:-1 ++0:-1:1 +-1:-1:0 +-1:+1:-2 ++1:-1:2 ++9:+1:8 ++99:+1:98 ++999:+1:998 ++9999:+1:9998 ++99999:+1:99998 ++999999:+1:999998 ++9999999:+1:9999998 ++99999999:+1:99999998 ++999999999:+1:999999998 ++9999999999:+1:9999999998 ++99999999999:+1:99999999998 ++10:-1:11 ++100:-1:101 ++1000:-1:1001 ++10000:-1:10001 ++100000:-1:100001 ++1000000:-1:1000001 ++10000000:-1:10000001 ++100000000:-1:100000001 ++1000000000:-1:1000000001 ++10000000000:-1:10000000001 ++123456789:+987654321:-864197532 +-123456789:+987654321:-1111111110 +-123456789:-987654321:864197532 ++123456789:-987654321:1111111110 +&fmul +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:NaNmul:NaN ++inf:NaNmul:NaN +NaNmul:+inf:NaN +NaNmul:-inf:NaN ++inf:+inf:inf ++inf:-inf:-inf ++inf:-inf:-inf ++inf:+inf:inf ++inf:123.34:inf ++inf:-123.34:-inf +-inf:123.34:-inf +-inf:-123.34:inf +123.34:+inf:inf +-123.34:+inf:-inf +123.34:-inf:-inf +-123.34:-inf:inf ++0:+0:0 ++0:+1:0 ++1:+0:0 ++0:-1:0 +-1:+0:0 ++123456789123456789:+0:0 ++0:+123456789123456789:0 +-1:-1:1 +-1:+1:-1 ++1:-1:-1 ++1:+1:1 ++2:+3:6 +-2:+3:-6 ++2:-3:-6 +-2:-3:6 ++111:+111:12321 ++10101:+10101:102030201 ++1001001:+1001001:1002003002001 ++100010001:+100010001:10002000300020001 ++10000100001:+10000100001:100002000030000200001 ++11111111111:+9:99999999999 ++22222222222:+9:199999999998 ++33333333333:+9:299999999997 ++44444444444:+9:399999999996 ++55555555555:+9:499999999995 ++66666666666:+9:599999999994 ++77777777777:+9:699999999993 ++88888888888:+9:799999999992 ++99999999999:+9:899999999991 +6:120:720 +10:10000:100000 +&fdiv-list +0:0:NaN,NaN +0:1:0,0 +9:4:2.25,1 +9:5:1.8,4 +# bug in v1.74 with bdiv in list context, when $y is 1 or -1 +2.1:-1:-2.1,0 +2.1:1:2.1,0 +-2.1:-1:2.1,0 +-2.1:1:-2.1,0 +&fdiv +$div_scale = 40; $round_mode = 'even' +abc:abc:NaN +abc:+1:abc:NaN ++1:abc:NaN +-1:abc:NaN +0:abc:NaN ++0:+0:NaN ++0:+1:0 ++1:+0:inf ++3214:+0:inf ++0:-1:0 +-1:+0:-inf +-3214:+0:-inf ++1:+1:1 +-1:-1:1 ++1:-1:-1 +-1:+1:-1 ++1:+2:0.5 ++2:+1:2 +123:+inf:0 +123:-inf:0 ++10:+5:2 ++100:+4:25 ++1000:+8:125 ++10000:+16:625 ++10000:-16:-625 ++999999999999:+9:111111111111 ++999999999999:+99:10101010101 ++999999999999:+999:1001001001 ++999999999999:+9999:100010001 ++999999999999999:+99999:10000100001 ++1000000000:+9:111111111.1111111111111111111111111111111 ++2000000000:+9:222222222.2222222222222222222222222222222 ++3000000000:+9:333333333.3333333333333333333333333333333 ++4000000000:+9:444444444.4444444444444444444444444444444 ++5000000000:+9:555555555.5555555555555555555555555555556 ++6000000000:+9:666666666.6666666666666666666666666666667 ++7000000000:+9:777777777.7777777777777777777777777777778 ++8000000000:+9:888888888.8888888888888888888888888888889 ++9000000000:+9:1000000000 ++35500000:+113:314159.2920353982300884955752212389380531 ++71000000:+226:314159.2920353982300884955752212389380531 ++106500000:+339:314159.2920353982300884955752212389380531 ++1000000000:+3:333333333.3333333333333333333333333333333 +2:25.024996000799840031993601279744051189762:0.07992009269196593320152084692285869265447 +123456:1:123456 +$div_scale = 20 ++1000000000:+9:111111111.11111111111 ++2000000000:+9:222222222.22222222222 ++3000000000:+9:333333333.33333333333 ++4000000000:+9:444444444.44444444444 ++5000000000:+9:555555555.55555555556 ++6000000000:+9:666666666.66666666667 ++7000000000:+9:777777777.77777777778 ++8000000000:+9:888888888.88888888889 ++9000000000:+9:1000000000 +1:10:0.1 +1:100:0.01 +1:1000:0.001 +1:10000:0.0001 +1:504:0.001984126984126984127 +2:1.987654321:1.0062111801179738436 +123456789.123456789123456789123456789:1:123456789.12345678912 +# the next two cases are the "old" behaviour, but are now (>v0.01) different +#+35500000:+113:314159.292035398230088 +#+71000000:+226:314159.292035398230088 ++35500000:+113:314159.29203539823009 ++71000000:+226:314159.29203539823009 ++106500000:+339:314159.29203539823009 ++1000000000:+3:333333333.33333333333 +$div_scale = 1 +# round to accuracy 1 after bdiv ++124:+3:40 +123456789.1234:1:100000000 +# reset scale for further tests +$div_scale = 40 +&fmod ++9:4:1 ++9:5:4 ++9000:56:40 ++56:9000:56 +# inf handling, see table in doc +0:inf:0 +0:-inf:0 +5:inf:5 +5:-inf:5 +-5:inf:-5 +-5:-inf:-5 +inf:5:0 +-inf:5:0 +inf:-5:0 +-inf:-5:0 +5:5:0 +-5:-5:0 +inf:inf:NaN +-inf:-inf:NaN +-inf:inf:NaN +inf:-inf:NaN +8:0:8 +inf:0:inf +# exceptions to reminder rule +-inf:0:-inf +-8:0:-8 +0:0:NaN +abc:abc:NaN +abc:1:abc:NaN +1:abc:NaN +0:0:NaN +0:1:0 +1:0:1 +0:-1:0 +-1:0:-1 +1:1:0 +-1:-1:0 +1:-1:0 +-1:1:0 +1:2:1 +2:1:0 +1000000000:9:1 +2000000000:9:2 +3000000000:9:3 +4000000000:9:4 +5000000000:9:5 +6000000000:9:6 +7000000000:9:7 +8000000000:9:8 +9000000000:9:0 +35500000:113:33 +71000000:226:66 +106500000:339:99 +1000000000:3:1 +10:5:0 +100:4:0 +1000:8:0 +10000:16:0 +999999999999:9:0 +999999999999:99:0 +999999999999:999:0 +999999999999:9999:0 +999999999999999:99999:0 +-9:+5:1 ++9:-5:-1 +-9:-5:-4 +-5:3:1 +-2:3:1 +4:3:1 +1:3:1 +-5:-3:-2 +-2:-3:-2 +4:-3:-2 +1:-3:-2 +4095:4095:0 +100041000510123:3:0 +152403346:12345:4321 +87654321:87654321:0 +# now some floating point tests +123:2.5:0.5 +1230:2.5:0 +123.4:2.5:0.9 +123e1:25:5 +-2.1:1:0.9 +2.1:1:0.1 +-2.1:-1:-0.1 +2.1:-1:-0.9 +-3:1:0 +3:1:0 +-3:-1:0 +3:-1:0 +&ffac +Nanfac:NaN +-1:NaN ++inf:inf +-inf:NaN +0:1 +1:1 +2:2 +3:6 +4:24 +5:120 +6:720 +10:3628800 +11:39916800 +12:479001600 +&froot +# sqrt() ++0:2:0 ++1:2:1 +-1:2:NaN +# -$x ** (1/2) => -$y, but not in froot() +-123.456:2:NaN ++inf:2:inf +-inf:2:NaN +2:2:1.41421356237309504880168872420969807857 +-2:2:NaN +4:2:2 +9:2:3 +16:2:4 +100:2:10 +123.456:2:11.11107555549866648462149404118219234119 +15241.38393:2:123.4559999756998444766131352122991626468 +1.44:2:1.2 +12:2:3.464101615137754587054892683011744733886 +0.49:2:0.7 +0.0049:2:0.07 +# invalid ones +1:NaN:NaN +-1:NaN:NaN +0:NaN:NaN +-inf:NaN:NaN ++inf:NaN:NaN +NaN:0:NaN +NaN:2:NaN +NaN:inf:NaN +NaN:inf:NaN +12:-inf:NaN +12:inf:NaN ++0:0:NaN ++1:0:NaN +-1:0:NaN +-2:0:NaN +-123.45:0:NaN ++inf:0:NaN +12:1:12 +-12:1:NaN +8:-1:NaN +-8:-1:NaN +# cubic root +8:3:2 +-8:3:NaN +# fourths root +16:4:2 +81:4:3 +# see t/bigroot() for more tests +&fsqrt ++0:0 +-1:NaN +-2:NaN +-16:NaN +-123.45:NaN +nanfsqrt:NaN ++inf:inf +-inf:NaN +1:1 +2:1.41421356237309504880168872420969807857 +4:2 +9:3 +16:4 +100:10 +123.456:11.11107555549866648462149404118219234119 +15241.38393:123.4559999756998444766131352122991626468 +1.44:1.2 +# sqrt(1.44) = 1.2, sqrt(e10) = e5 => 12e4 +1.44E10:120000 +2e10:141421.356237309504880168872420969807857 +144e20:120000000000 +# proved to be an endless loop under 7-9 +12:3.464101615137754587054892683011744733886 +0.49:0.7 +0.0049:0.07 +&is_nan +123:0 +abc:1 +NaN:1 +-123:0 +&is_inf ++inf::1 +-inf::1 +abc::0 +1::0 +NaN::0 +-1::0 ++inf:-:0 ++inf:+:1 +-inf:-:1 +-inf:+:0 +# it must be exactly /^[+-]inf$/ ++infinity::0 +-infinity::0 +&is_odd +abc:0 +0:0 +-1:1 +-3:1 +1:1 +3:1 +1000001:1 +1000002:0 ++inf:0 +-inf:0 +123.45:0 +-123.45:0 +2:0 +&is_int +NaNis_int:0 +0:1 +1:1 +2:1 +-2:1 +-1:1 +-inf:0 ++inf:0 +123.4567:0 +-0.1:0 +-0.002:0 +&is_even +abc:0 +0:1 +-1:0 +-3:0 +1:0 +3:0 +1000001:0 +1000002:1 +2:1 ++inf:0 +-inf:0 +123.456:0 +-123.456:0 +0.01:0 +-0.01:0 +120:1 +1200:1 +-1200:1 +&is_positive +0:0 +1:1 +-1:0 +-123:0 +NaN:0 +-inf:0 ++inf:1 +&is_negative +0:0 +1:0 +-1:1 +-123:1 +NaN:0 +-inf:1 ++inf:0 +&parts +0:0 1 +1:1 0 +123:123 0 +-123:-123 0 +-1200:-12 2 +NaNparts:NaN NaN ++inf:inf inf +-inf:-inf inf +&exponent +0:1 +1:0 +123:0 +-123:0 +-1200:2 ++inf:inf +-inf:inf +NaNexponent:NaN +&mantissa +0:0 +1:1 +123:123 +-123:-123 +-1200:-12 ++inf:inf +-inf:-inf +NaNmantissa:NaN +&length +123:3 +-123:3 +0:1 +1:1 +12345678901234567890:20 +&is_zero +NaNzero:0 ++inf:0 +-inf:0 +0:1 +-1:0 +1:0 +&is_one +NaNone:0 ++inf:0 +-inf:0 +0:0 +2:0 +1:1 +-1:0 +-2:0 +&ffloor +0:0 +abc:NaN ++inf:inf +-inf:-inf +1:1 +-51:-51 +-51.2:-52 +12.2:12 +0.12345:0 +0.123456:0 +0.1234567:0 +0.12345678:0 +0.123456789:0 +&fceil +0:0 +abc:NaN ++inf:inf +-inf:-inf +1:1 +-51:-51 +-51.2:-51 +12.2:13 diff --git a/cpan/Math-BigRat/t/bigfltrt.t b/cpan/Math-BigRat/t/bigfltrt.t new file mode 100644 index 0000000000..325e6fa77e --- /dev/null +++ b/cpan/Math-BigRat/t/bigfltrt.t @@ -0,0 +1,19 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 1; + +BEGIN { + unshift @INC, 't'; +} + +use Math::BigRat::Test lib => 'Calc'; # test via this Subclass + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigRat::Test"; +$CL = "Math::BigInt::Calc"; + +pass(); + +# fails still too many tests +#require 't/bigfltpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigRat/t/biglog.t b/cpan/Math-BigRat/t/biglog.t new file mode 100644 index 0000000000..42e9ac8d64 --- /dev/null +++ b/cpan/Math-BigRat/t/biglog.t @@ -0,0 +1,72 @@ +#!/usr/bin/perl -w + +# Test blog function (and bpow, since it uses blog), as well as bexp(). + +use strict; +use Test::More tests => 17; + +use Math::BigRat; + +my $cl = "Math::BigRat"; + +############################################################################# +# test log($n) + +# does not work yet +#is ($cl->new(2)->blog(), '0', "blog(2)"); +#is ($cl->new(288)->blog(), '5',"blog(288)"); +#is ($cl->new(2000)->blog(), '7', "blog(2000)"); + +############################################################################# +# test exp($n) + +is ($cl->new(1)->bexp()->as_int(), '2', "bexp(1)"); +is ($cl->new(2)->bexp()->as_int(), '7',"bexp(2)"); +is ($cl->new(3)->bexp()->as_int(), '20', "bexp(3)"); + +# rounding not implemented yet +#is ($cl->new(3)->bexp(10), '20', "bexp(3,10)"); + +# $x < 0 => NaN +ok ($cl->new(-2)->blog(), 'NaN'); +ok ($cl->new(-1)->blog(), 'NaN'); +ok ($cl->new(-10)->blog(), 'NaN'); +ok ($cl->new(-2,2)->blog(), 'NaN'); + +############################################################################# +# test bexp() with cached results + +is ($cl->new(1)->bexp(), + '90933395208605785401971970164779391644753259799242' . '/' . + '33452526613163807108170062053440751665152000000000', + 'bexp(1)'); +is ($cl->new(2)->bexp(1,40), $cl->new(1)->bexp(1,45)->bpow(2,40), 'bexp(2)'); + +is ($cl->new("12.5")->bexp(1,61), $cl->new(1)->bexp(1,65)->bpow(12.5,61), 'bexp(12.5)'); + +############################################################################# +# test bexp() with big values (non-cached) + +is ($cl->new(1)->bexp(1,100)->as_float(100), + '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', + 'bexp(100)'); + +is ($cl->new("12.5")->bexp(1,91), $cl->new(1)->bexp(1,95)->bpow(12.5,91), + 'bexp(12.5) to 91 digits'); + +############################################################################# +# some integer results +is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32"); +is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32"); +is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65"); + +my $x = Math::BigInt->new( '777' ) ** 256; +my $base = Math::BigInt->new( '12345678901234' ); +is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)'); + +$x = Math::BigInt->new( '777' ) ** 777; +$base = Math::BigInt->new( '777' ); +is ($x->copy()->blog($base), 777, 'blog(777**777, 777)'); + +# all done +1; diff --git a/cpan/Math-BigRat/t/bigrat.t b/cpan/Math-BigRat/t/bigrat.t new file mode 100644 index 0000000000..a640e59244 --- /dev/null +++ b/cpan/Math-BigRat/t/bigrat.t @@ -0,0 +1,332 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 202; + +# basic testing of Math::BigRat + +use Math::BigRat; +use Math::BigInt; +use Math::BigFloat; + +# shortcuts +my $cr = 'Math::BigRat'; +my $mbi = 'Math::BigInt'; +my $mbf = 'Math::BigFloat'; + +my ($x,$y,$z); + +$x = Math::BigRat->new(1234); is ($x,1234); +isa_ok ($x, 'Math::BigRat'); +is ($x->isa('Math::BigFloat'), 0); +is ($x->isa('Math::BigInt'), 0); + +############################################################################## +# new and bnorm() + +foreach my $func (qw/new bnorm/) + { + $x = $cr->$func(1234); is ($x,1234); + + $x = $cr->$func('1234/1'); is ($x,1234); + $x = $cr->$func('1234/2'); is ($x,617); + + $x = $cr->$func('100/1.0'); is ($x,100); + $x = $cr->$func('10.0/1.0'); is ($x,10); + $x = $cr->$func('0.1/10'); is ($x,'1/100'); + $x = $cr->$func('0.1/0.1'); is ($x,'1'); + $x = $cr->$func('1e2/10'); is ($x,10); + $x = $cr->$func('5/1e2'); is ($x,'1/20'); + $x = $cr->$func('1e2/1e1'); is ($x,10); + $x = $cr->$func('1 / 3'); is ($x,'1/3'); + $x = $cr->$func('-1 / 3'); is ($x,'-1/3'); + $x = $cr->$func('NaN'); is ($x,'NaN'); + $x = $cr->$func('inf'); is ($x,'inf'); + $x = $cr->$func('-inf'); is ($x,'-inf'); + $x = $cr->$func('1/'); is ($x,'NaN'); + + $x = $cr->$func("0x7e"); is ($x,126); + + # input ala '1+1/3' isn't parsed ok yet + $x = $cr->$func('1+1/3'); is ($x,'NaN'); + + $x = $cr->$func('1/1.2'); is ($x,'5/6'); + $x = $cr->$func('1.3/1.2'); is ($x,'13/12'); + $x = $cr->$func('1.2/1'); is ($x,'6/5'); + + ############################################################################ + # other classes as input + + $x = $cr->$func($mbi->new(1231)); is ($x,'1231'); + $x = $cr->$func($mbf->new(1232)); is ($x,'1232'); + $x = $cr->$func($mbf->new(1232.3)); is ($x,'12323/10'); + } + +my $n = 'numerator'; +my $d = 'denominator'; + +$x = $cr->new('-0'); is ($x,'0'); is ($x->$n(), '0'); is ($x->$d(),'1'); +$x = $cr->new('NaN'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); +$x = $cr->new('-NaN'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); +$x = $cr->new('-1r4'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); + +$x = $cr->new('+inf'); is ($x,'inf'); is ($x->$n(), 'inf'); is ($x->$d(),'1'); +$x = $cr->new('-inf'); is ($x,'-inf'); is ($x->$n(), '-inf'); is ($x->$d(),'1'); +$x = $cr->new('123a4'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); + +# wrong inputs +$x = $cr->new('1e2e2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); +$x = $cr->new('1+2+2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); +# failed due to BigFloat bug +$x = $cr->new('1.2.2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); + +is ($cr->new('123a4'),'NaN'); +is ($cr->new('123e4'),'1230000'); +is ($cr->new('-NaN'),'NaN'); +is ($cr->new('NaN'),'NaN'); +is ($cr->new('+inf'),'inf'); +is ($cr->new('-inf'),'-inf'); + +############################################################################## +# two Bigints + +is ($cr->new($mbi->new(3),$mbi->new(7))->badd(1),'10/7'); +is ($cr->new($mbi->new(-13),$mbi->new(7)),'-13/7'); +is ($cr->new($mbi->new(13),$mbi->new(-7)),'-13/7'); +is ($cr->new($mbi->new(-13),$mbi->new(-7)),'13/7'); + +############################################################################## +# mixed arguments + +is ($cr->new('3/7')->badd(1),'10/7'); +is ($cr->new('3/10')->badd(1.1),'7/5'); +is ($cr->new('3/7')->badd($mbi->new(1)),'10/7'); +is ($cr->new('3/10')->badd($mbf->new('1.1')),'7/5'); + +is ($cr->new('3/7')->bsub(1),'-4/7'); +is ($cr->new('3/10')->bsub(1.1),'-4/5'); +is ($cr->new('3/7')->bsub($mbi->new(1)),'-4/7'); +is ($cr->new('3/10')->bsub($mbf->new('1.1')),'-4/5'); + +is ($cr->new('3/7')->bmul(1),'3/7'); +is ($cr->new('3/10')->bmul(1.1),'33/100'); +is ($cr->new('3/7')->bmul($mbi->new(1)),'3/7'); +is ($cr->new('3/10')->bmul($mbf->new('1.1')),'33/100'); + +is ($cr->new('3/7')->bdiv(1),'3/7'); +is ($cr->new('3/10')->bdiv(1.1),'3/11'); +is ($cr->new('3/7')->bdiv($mbi->new(1)),'3/7'); +is ($cr->new('3/10')->bdiv($mbf->new('1.1')),'3/11'); + +############################################################################## +$x = $cr->new('1/4'); $y = $cr->new('1/3'); + +is ($x + $y, '7/12'); +is ($x * $y, '1/12'); +is ($x / $y, '3/4'); + +$x = $cr->new('7/5'); $x *= '3/2'; +is ($x,'21/10'); +$x -= '0.1'; +is ($x,'2'); # not 21/10 + +$x = $cr->new('2/3'); $y = $cr->new('3/2'); +is ($x > $y,''); +is ($x < $y,1); +is ($x == $y,''); + +$x = $cr->new('-2/3'); $y = $cr->new('3/2'); +is ($x > $y,''); +is ($x < $y,'1'); +is ($x == $y,''); + +$x = $cr->new('-2/3'); $y = $cr->new('-2/3'); +is ($x > $y,''); +is ($x < $y,''); +is ($x == $y,'1'); + +$x = $cr->new('-2/3'); $y = $cr->new('-1/3'); +is ($x > $y,''); +is ($x < $y,'1'); +is ($x == $y,''); + +$x = $cr->new('-124'); $y = $cr->new('-122'); +is ($x->bacmp($y),1); + +$x = $cr->new('-124'); $y = $cr->new('-122'); +is ($x->bcmp($y),-1); + +$x = $cr->new('3/7'); $y = $cr->new('5/7'); +is ($x+$y,'8/7'); + +$x = $cr->new('3/7'); $y = $cr->new('5/7'); +is ($x*$y,'15/49'); + +$x = $cr->new('3/5'); $y = $cr->new('5/7'); +is ($x*$y,'3/7'); + +$x = $cr->new('3/5'); $y = $cr->new('5/7'); +is ($x/$y,'21/25'); + +$x = $cr->new('7/4'); $y = $cr->new('1'); +is ($x % $y,'3/4'); + +$x = $cr->new('7/4'); $y = $cr->new('5/13'); +is ($x % $y,'11/52'); + +$x = $cr->new('7/4'); $y = $cr->new('5/9'); +is ($x % $y,'1/12'); + +$x = $cr->new('-144/9')->bsqrt(); is ($x,'NaN'); +$x = $cr->new('144/9')->bsqrt(); is ($x,'4'); +$x = $cr->new('3/4')->bsqrt(); is ($x, + '1732050807568877293527446341505872366943/' + .'2000000000000000000000000000000000000000'); + +############################################################################## +# bpow + +$x = $cr->new('2/1'); $z = $x->bpow('3/1'); is ($x,'8'); +$x = $cr->new('1/2'); $z = $x->bpow('3/1'); is ($x,'1/8'); +$x = $cr->new('1/3'); $z = $x->bpow('4/1'); is ($x,'1/81'); +$x = $cr->new('2/3'); $z = $x->bpow('4/1'); is ($x,'16/81'); + +$x = $cr->new('2/3'); $z = $x->bpow('5/3'); +is ($x, '31797617848703662994667839220546583581/62500000000000000000000000000000000000'); + +############################################################################## +# bfac + +$x = $cr->new('1'); $x->bfac(); is ($x,'1'); +for (my $i = 0; $i < 8; $i++) + { + $x = $cr->new("$i/1")->bfac(); is ($x,$mbi->new($i)->bfac()); + } + +# test for $self->bnan() vs. $x->bnan(); +$x = $cr->new('-1'); $x->bfac(); is ($x,'NaN'); + +############################################################################## +# binc/bdec + +$x = $cr->new('3/2'); is ($x->binc(),'5/2'); +$x = $cr->new('15/6'); is ($x->bdec(),'3/2'); + +############################################################################## +# bfloor/bceil + +$x = $cr->new('-7/7'); is ($x->$n(), '-1'); is ($x->$d(), '1'); +$x = $cr->new('-7/7')->bfloor(); is ($x->$n(), '-1'); is ($x->$d(), '1'); + +############################################################################## +# bsstr + +$x = $cr->new('7/5')->bsstr(); is ($x,'7/5'); +$x = $cr->new('-7/5')->bsstr(); is ($x,'-7/5'); + +############################################################################## +# numify() + +my @array = qw/1 2 3 4 5 6 7 8 9/; +$x = $cr->new('8/8'); is ($array[$x],2); +$x = $cr->new('16/8'); is ($array[$x],3); +$x = $cr->new('17/8'); is ($array[$x],3); +$x = $cr->new('33/8'); is ($array[$x],5); +$x = $cr->new('-33/8'); is ($array[$x],6); +$x = $cr->new('-8/1'); is ($array[$x],2); # -8 => 2 + +$x = $cr->new('33/8'); is ($x->numify() * 1000, 4125); +$x = $cr->new('-33/8'); is ($x->numify() * 1000, -4125); +$x = $cr->new('inf'); is ($x->numify(), 'inf'); +$x = $cr->new('-inf'); is ($x->numify(), '-inf'); +$x = $cr->new('NaN'); is ($x->numify(), 'NaN'); + +$x = $cr->new('4/3'); is ($x->numify(), 4/3); + +############################################################################## +# as_hex(), as_bin(), as_oct() + +$x = $cr->new('8/8'); +is ($x->as_hex(), '0x1'); is ($x->as_bin(), '0b1'); is ($x->as_oct(), '01'); +$x = $cr->new('80/8'); +is ($x->as_hex(), '0xa'); is ($x->as_bin(), '0b1010'); is ($x->as_oct(), '012'); + +############################################################################## +# broot(), blog(), bmodpow() and bmodinv() + +$x = $cr->new(2) ** 32; +$y = $cr->new(4); +$z = $cr->new(3); + +is ($x->copy()->broot($y), 2 ** 8); +is (ref($x->copy()->broot($y)), $cr); + +is ($x->copy()->bmodpow($y,$z), 1); +is (ref($x->copy()->bmodpow($y,$z)), $cr); + +$x = $cr->new(8); +$y = $cr->new(5033); +$z = $cr->new(4404); + +is ($x->copy()->bmodinv($y), $z); +is (ref($x->copy()->bmodinv($y)), $cr); + +# square root with exact result +$x = $cr->new('1.44'); +is ($x->copy()->broot(2), '6/5'); +is (ref($x->copy()->broot(2)), $cr); + +# log with exact result +$x = $cr->new('256.1'); +is ($x->copy()->blog(2), '8000563442710106079310294693803606983661/1000000000000000000000000000000000000000'); +is (ref($x->copy()->blog(2)), $cr); + +$x = $cr->new(144); +is ($x->copy()->broot('2'), 12, 'v/144 = 12'); + +$x = $cr->new(12*12*12); +is ($x->copy()->broot('3'), 12, '(12*12*12) ** 1/3 = 12'); + +############################################################################## +# from_hex(), from_bin(), from_oct() + +$x = Math::BigRat->from_hex('0x100'); +is ($x, '256', 'from_hex'); +$x = $cr->from_hex('0x100'); +is ($x, '256', 'from_hex'); + +$x = Math::BigRat->from_bin('0b100'); +is ($x, '4', 'from_bin'); +$x = $cr->from_bin('0b100'); +is ($x, '4', 'from_bin'); + +$x = Math::BigRat->from_oct('0100'); +is ($x, '64', 'from_oct'); +$x = $cr->from_oct('0100'); +is ($x, '64', 'from_oct'); + +############################################################################## +# as_float() + +$x = Math::BigRat->new('1/2'); my $f = $x->as_float(); + +is ($x, '1/2', '$x unmodified'); +is ($f, '0.5', 'as_float(0.5)'); + +$x = Math::BigRat->new('2/3'); $f = $x->as_float(5); + +is ($x, '2/3', '$x unmodified'); +is ($f, '0.66667', 'as_float(2/3,5)'); + +############################################################################## +# int() + +$x = Math::BigRat->new('5/2'); +is int($x), '2', '5/2 converted to integer'; +$x = Math::BigRat->new('-1/2'); +is int($x), '0', '-1/2 converted to integer'; + +############################################################################## +# done + +1; diff --git a/cpan/Math-BigRat/t/bigratpm.inc b/cpan/Math-BigRat/t/bigratpm.inc new file mode 100644 index 0000000000..b2f507fee9 --- /dev/null +++ b/cpan/Math-BigRat/t/bigratpm.inc @@ -0,0 +1,916 @@ +#include this file into another test for subclass testing... + +is ($class->config()->{lib},$CL); + +$setup = ''; + +while () + { + chomp; + $_ =~ s/#.*$//; # remove comments + $_ =~ s/\s+$//; # trailing spaces + next if /^$/; # skip empty lines & comments + if (s/^&//) + { + $f = $_; + } + elsif (/^\$/) + { + $setup = $_; $setup =~ s/\$/\$${class}::/g; # round_mode, div_scale + #print "\$setup== $setup\n"; + } + else + { + if (m|^(.*?):(/.+)$|) + { + $ans = $2; + @args = split(/:/,$1,99); + } + else + { + @args = split(/:/,$_,99); $ans = pop(@args); + } + $try = "\$x = new $class \"$args[0]\";"; + if ($f eq "bnorm") + { + $try .= "\$x;"; + } elsif ($f eq "finf") { + my $a = $args[1] || ''; + $try .= "\$x->binf('$a');"; + } elsif ($f eq "is_inf") { + $try .= "\$x->is_inf('$args[1]');"; + } elsif ($f eq "fone") { + $try .= "\$x->bone('$args[1]');"; + } elsif ($f eq "fstr") { + $try .= "\$x->accuracy($args[1]); \$x->precision($args[2]);"; + $try .= '$x->bstr();'; + } elsif ($f eq "parts") { + # ->bstr() to see if an object is returned + $try .= '($a,$b) = $x->parts(); $a = $a->bstr(); $b = $b->bstr();'; + $try .= '"$a $b";'; + } elsif ($f eq "numerator") { + # ->bstr() to see if an object is returned + $try .= '$x->numerator()->bstr();'; + } elsif ($f eq "denominator") { + # ->bstr() to see if an object is returned + $try .= '$x->denominator()->bstr();'; + } elsif ($f =~ /^(length|numify)$/) { + $try .= "\$x->$f();"; + # some unary ops (can't test the fxxx form, since no AUTOLOAD in BigRat) + } elsif ($f =~ /^f(nan|sstr|neg|floor|ceil|abs)$/) { + $try .= "\$x->b$1();"; + # some is_xxx test function + } elsif ($f =~ /^is_(zero|one|pos|neg|negative|positive|odd|even|nan|int)\z/) { + $try .= "\$x->$f();"; + } elsif ($f =~ /^(as_number|as_int)\z/){ + $try .= "\$x->$1();"; + } elsif ($f eq "finc") { + $try .= '++$x;'; + } elsif ($f eq "fdec") { + $try .= '--$x;'; + } elsif ($f eq "digit") { + $try .= "\$x->digit($args[1]);"; + } elsif ($f eq "fround") { + $try .= "$setup; \$x->bround($args[1]);"; + } elsif ($f eq "ffround") { + $try .= "$setup; \$x->bfround($args[1]);"; + } elsif ($f eq "fsqrt") { + $try .= "$setup; \$x->bsqrt();"; + } elsif ($f eq "flog") { + $try .= "$setup; \$x->blog();"; + } elsif ($f eq "ffac") { + $try .= "$setup; \$x->bfac();"; + } + else + { + $try .= "\$y = new $class \"$args[1]\";"; + if ($f eq "bcmp") { + $try .= '$x <=> $y;'; + } elsif ($f eq "bacmp") { + $try .= '$x->bacmp($y);'; + } elsif ($f eq "bpow") { + $try .= '$x ** $y;'; + } elsif ($f eq "fpow") { + $try .= '$x->bpow($y);'; + } elsif ($f eq "badd") { + $try .= '$x + $y;'; + } elsif ($f eq "bsub") { + $try .= '$x - $y;'; + } elsif ($f eq "bmul") { + $try .= '$x * $y;'; + } elsif ($f eq "bdiv") { + $try .= "$setup; \$x / \$y;"; + } elsif ($f eq "bdiv-list") { + $try .= "$setup; join(',',\$x->bdiv(\$y));"; + } elsif ($f eq "brsft") { + $try .= '$x >> $y;'; + } elsif ($f eq "blsft") { + $try .= '$x << $y;'; + } elsif ($f eq "bmod") { + $try .= '$x % $y;'; + } elsif( $f eq "bmodinv") { + $try .= "\$x->bmodinv(\$y);"; + } elsif( $f eq "blog") { + $try .= "\$x->blog(\$y);"; + } else { + $try .= "\$z = $class->new(\"$args[2]\");"; + + # Functions with three arguments + if( $f eq "bmodpow") { + $try .= "\$x->bmodpow(\$y,\$z);"; + } else { warn "Unknown op '$f'"; } + } + } + # print "# Trying: '$try'\n"; + $ans1 = eval $try; + if ($ans =~ m|^/(.*)$|) + { + my $pat = $1; + like ($ans1, qr/$pat/); + } + else + { + if ($ans eq "") + { + is ($ans1, undef); + } + else + { + is ($ans1, $ans) or diag("Tried: '$try'"); +# if (ref($ans1) eq "$class") +# { +# # float numbers are normalized (for now), so mantissa shouldn't have +# # trailing zeros +# #print $ans1->_trailing_zeros(),"\n"; +# print "# Has trailing zeros after '$try'\n" +# if !is ($ans1->{_m}->_trailing_zeros(), 0); +# } + } + } # end pattern or string + } + } # end while + +# check whether $class->new( Math::BigInt->new()) destroys it +# ($y == 12 in this case) +$x = Math::BigInt->new(1200); $y = $class->new($x); +is ($y,1200); is ($x,1200); + +############################################################################### +# zero,inf,one,nan + +$x = $class->new(2); $x->bzero(); is ($x->{_a}, undef); is ($x->{_p}, undef); +$x = $class->new(2); $x->binf(); is ($x->{_a}, undef); is ($x->{_p}, undef); +$x = $class->new(2); $x->bone(); is ($x->{_a}, undef); is ($x->{_p}, undef); +$x = $class->new(2); $x->bnan(); is ($x->{_a}, undef); is ($x->{_p}, undef); + +__DATA__ +&digit +123:2:1 +1234:0:4 +1234:1:3 +1234:2:2 +1234:3:1 +1234:-1:1 +1234:-2:2 +1234:-3:3 +1234:-4:4 +0:0:0 +0:1:0 +&bmodinv +# format: number:modulus:result +# bmodinv Data errors +abc:abc:NaN +abc:5:NaN +5:abc:NaN +# bmodinv Expected Results from normal use +1:5:1 +3:5:2 +3:-5:-3 +-2:5:2 +8:5033:4404 +1234567891:13:6 +-1234567891:13:7 +324958749843759385732954874325984357439658735983745:2348249874968739:1741662881064902 +## bmodinv Error cases / useless use of function +inf:5:NaN +5:inf:NaN +-inf:5:NaN +5:-inf:NaN +&as_number +144/7:20 +12/1:12 +-12/1:-12 +-12/3:-4 +NaN:NaN ++inf:inf +-inf:-inf +&as_int +144/7:20 +12/1:12 +-12/1:-12 +-12/3:-4 +NaN:NaN ++inf:inf +-inf:-inf +&bmodpow +# format: number:exponent:modulus:result +# bmodpow Data errors +abc:abc:abc:NaN +5:abc:abc:NaN +abc:5:abc:NaN +abc:abc:5:NaN +5:5:abc:NaN +5:abc:5:NaN +abc:5:5:NaN +# bmodpow Expected results +0:0:2:1 +1:0:2:1 +0:0:1:0 +8:7:5032:3840 +8:-1:5033:4404 +8:8:-5:-4 +98436739867439843769485798542749827593285729587325:43698764986460981048259837659386739857456983759328457:6943857329857295827698367:3104744730915914415259518 +# bmodpow Error cases +8:-1:16:NaN +inf:5:13:NaN +5:inf:13:NaN +&bmod +NaN:1:NaN +1:NaN:NaN +1:1:0 +2:2:0 +12:6:0 +7/4:4/14:1/28 +7/4:4/16:0 +-7/4:4/16:0 +-7/4:-4/16:0 +7/4:-4/16:0 +7/4:4/32:0 +-7/4:4/32:0 +-7/4:-4/32:0 +7/4:-4/32:0 +7/4:4/28:1/28 +-7/4:4/28:3/28 +7/4:-4/28:-3/28 +-7/4:-4/28:-1/28 +&fsqrt +1:1 +0:0 +NaN:NaN ++inf:inf +-inf:NaN +144:12 +# sqrt(144) / sqrt(4) = 12/2 = 6/1 +144/4:6 +25/16:5/4 +-3:NaN +&flog +NaN:NaN +0:NaN +-2:NaN +&blog +NaN:NaN:NaN +0:NaN:NaN +NaN:0:NaN +NaN:1:NaN +1:NaN:NaN +0:2:NaN +0:-2:NaN +3:-2:NaN +&finf +1:+:inf +2:-:-inf +3:abc:inf +&numify +0:0 ++1:1 +1234:1234 +3/4:0.75 +5/2:2.5 +3/2:1.5 +5/4:1.25 +NaN:NaN ++inf:inf +-inf:-inf +&fnan +abc:NaN +2:NaN +-2:NaN +0:NaN +&fone +2:+:1 +-2:-:-1 +-2:+:1 +2:-:-1 +0::1 +-2::1 +abc::1 +2:abc:1 +&fsstr ++inf:inf +-inf:-inf +abcfsstr:NaN +1:1/1 +3/1:3/1 +0.1:1/10 +&bnorm +1:1 +-0:0 +bnormNaN:NaN ++inf:inf +-inf:-inf +inf/inf:NaN +5/inf:0 +5/-inf:0 +inf/5:inf +-inf/5:-inf +inf/-5:-inf +-inf/-5:inf +123:123 +-123.4567:-1234567/10000 +# invalid inputs +1__2:NaN +1E1__2:NaN +11__2E2:NaN +#1.E3:NaN +.2E-3.:NaN +#1e3e4:NaN +.2E2:20 +inf:inf ++inf:inf +-inf:-inf ++infinity:NaN ++-inf:NaN +abc:NaN + 1 a:NaN +1bcd2:NaN +11111b:NaN ++1z:NaN +-1z:NaN +0:0 ++0:0 ++00:0 ++0_0_0:0 +000000_0000000_00000:0 +-0:0 +-0000:0 ++1:1 ++01:1 ++001:1 ++00000100000:100000 ++00000800/00000010:80 +-00000800/00000010:-80 ++00000800/-00000010:-80 +-00000800/-00000010:80 +123456789:123456789 +-1:-1 +-01:-1 +-001:-1 +-123456789:-123456789 +-00000100000:-100000 +123.456a:NaN +123.456:15432/125 +0.01:1/100 +.002:1/500 ++.2:1/5 +-0.0003:-3/10000 +-.0000000004:-1/2500000000 +123456E2:12345600 +123456E-2:30864/25 +-123456E2:-12345600 +-123456E-2:-30864/25 +1e1:10 +2e-11:1/50000000000 +12/10:6/5 +0.1/0.1:1 +100/0.1:1000 +0.1/10:1/100 +1 / 3:1/3 +1/ 3:1/3 +1 /3:1/3 +&fneg +fnegNaN:NaN ++inf:-inf +-inf:inf ++0:0 ++1:-1 +-1:1 ++123456789:-123456789 +-123456789:123456789 ++123.456789:-123456789/1000000 +-123456.789:123456789/1000 +123/7:-123/7 +-123/7:123/7 +123/-7:123/7 +&fabs +fabsNaN:NaN ++inf:inf +-inf:inf ++0:0 ++1:1 +-1:1 ++123456789:123456789 +-123456789:123456789 ++123.456789:123456789/1000000 +-123456.789:123456789/1000 +&badd +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:-inf:NaN +-inf:+inf:NaN ++inf:+inf:inf +-inf:-inf:-inf +baddNaN:+inf:NaN +baddNaN:+inf:NaN ++inf:baddNaN:NaN +-inf:baddNaN:NaN ++0:+0:0 ++1:+0:1 ++0:+1:1 ++1:+1:2 +-1:+0:-1 ++0:-1:-1 +-1:-1:-2 +-1:+1:0 ++1:-1:0 ++9:+1:10 ++99:+1:100 ++999:+1:1000 ++9999:+1:10000 ++99999:+1:100000 ++999999:+1:1000000 ++9999999:+1:10000000 ++99999999:+1:100000000 ++999999999:+1:1000000000 ++9999999999:+1:10000000000 ++99999999999:+1:100000000000 ++10:-1:9 ++100:-1:99 ++1000:-1:999 ++10000:-1:9999 ++100000:-1:99999 ++1000000:-1:999999 ++10000000:-1:9999999 ++100000000:-1:99999999 ++1000000000:-1:999999999 ++10000000000:-1:9999999999 ++123456789:+987654321:1111111110 +-123456789:+987654321:864197532 +-123456789:-987654321:-1111111110 ++123456789:-987654321:-864197532 +1/3:1/3:2/3 +2/3:-1/3:1/3 +&bsub +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:-inf:inf +-inf:+inf:-inf ++inf:+inf:NaN +-inf:-inf:NaN +baddNaN:+inf:NaN +baddNaN:+inf:NaN ++inf:baddNaN:NaN +-inf:baddNaN:NaN ++0:+0:0 ++1:+0:1 ++0:+1:-1 ++1:+1:0 +-1:+0:-1 ++0:-1:1 +-1:-1:0 +-1:+1:-2 ++1:-1:2 ++9:+1:8 ++99:+1:98 ++999:+1:998 ++9999:+1:9998 ++99999:+1:99998 ++999999:+1:999998 ++9999999:+1:9999998 ++99999999:+1:99999998 ++999999999:+1:999999998 ++9999999999:+1:9999999998 ++99999999999:+1:99999999998 ++10:-1:11 ++100:-1:101 ++1000:-1:1001 ++10000:-1:10001 ++100000:-1:100001 ++1000000:-1:1000001 ++10000000:-1:10000001 ++100000000:-1:100000001 ++1000000000:-1:1000000001 ++10000000000:-1:10000000001 ++123456789:+987654321:-864197532 +-123456789:+987654321:-1111111110 +-123456789:-987654321:864197532 ++123456789:-987654321:1111111110 +2/3:1/3:1/3 +7/27:3/54:11/54 +-2/3:+2/3:-4/3 +-2/3:-2/3:0 +0:-123:123 +0:123:-123 +&bmul +abc:abc:NaN +abc:+0:NaN ++0:abc:NaN ++inf:NaNmul:NaN ++inf:NaNmul:NaN +NaNmul:+inf:NaN +NaNmul:-inf:NaN ++inf:+inf:inf ++inf:-inf:-inf ++inf:-inf:-inf ++inf:+inf:inf ++inf:123.34:inf ++inf:-123.34:-inf +-inf:123.34:-inf +-inf:-123.34:inf +123.34:+inf:inf +-123.34:+inf:-inf +123.34:-inf:-inf +-123.34:-inf:inf ++0:+0:0 ++0:+1:0 ++1:+0:0 ++0:-1:0 +-1:+0:0 ++123456789123456789:+0:0 ++0:+123456789123456789:0 +-1:-1:1 +-1:+1:-1 ++1:-1:-1 ++1:+1:1 ++2:+3:6 +-2:+3:-6 ++2:-3:-6 +-2:-3:6 ++111:+111:12321 ++10101:+10101:102030201 ++1001001:+1001001:1002003002001 ++100010001:+100010001:10002000300020001 ++10000100001:+10000100001:100002000030000200001 ++11111111111:+9:99999999999 ++22222222222:+9:199999999998 ++33333333333:+9:299999999997 ++44444444444:+9:399999999996 ++55555555555:+9:499999999995 ++66666666666:+9:599999999994 ++77777777777:+9:699999999993 ++88888888888:+9:799999999992 ++99999999999:+9:899999999991 +6:120:720 +10:10000:100000 +1/4:1/3:1/12 +&bdiv-list +0:0:NaN,0 +0:1:0,0 +1:0:inf,1 +-1:0:-inf,-1 +9:4:2,1 +-9:4:-3,3 +9:-4:-3,-3 +-9:-4:2,-1 +11/7:2/3:2,5/21 +-11/7:2/3:-3,3/7 +&bdiv +$div_scale = 40; $round_mode = 'even' +abc:abc:NaN +abc:+1:abc:NaN ++1:abc:NaN +-1:abc:NaN +0:abc:NaN ++0:+0:NaN ++0:+1:0 ++1:+0:inf ++3214:+0:inf ++0:-1:0 +-1:+0:-inf +-3214:+0:-inf ++1:+1:1 +-1:-1:1 ++1:-1:-1 +-1:+1:-1 ++1:+2:1/2 ++2:+1:2 +123:+inf:0 +123:-inf:0 ++10:+5:2 ++100:+4:25 ++1000:+8:125 ++10000:+16:625 ++10000:-16:-625 ++999999999999:+9:111111111111 ++999999999999:+99:10101010101 ++999999999999:+999:1001001001 ++999999999999:+9999:100010001 ++999999999999999:+99999:10000100001 ++1000000000:+9:1000000000/9 ++2000000000:+9:2000000000/9 ++3000000000:+9:1000000000/3 ++4000000000:+9:4000000000/9 ++5000000000:+9:5000000000/9 ++6000000000:+9:2000000000/3 ++7000000000:+9:7000000000/9 ++8000000000:+9:8000000000/9 ++9000000000:+9:1000000000 ++35500000:+113:35500000/113 ++71000000:+226:35500000/113 ++106500000:+339:35500000/113 ++1000000000:+3:1000000000/3 +2:25.024996000799840031993601279744051189762:1000000000000000000000000000000000000000/12512498000399920015996800639872025594881 +123456:1:123456 +1/4:1/3:3/4 +# reset scale for further tests +$div_scale = 40 +&is_nan +123:0 +abc:1 +NaN:1 +-123:0 +&is_inf ++inf::1 +-inf::1 +abc::0 +1::0 +NaN::0 +-1::0 ++inf:-:0 ++inf:+:1 +-inf:-:1 +-inf:+:0 +# it must be exactly /^[+-]inf$/ ++infinity::0 +-infinity::0 +&is_odd +abc:0 +0:0 +-1:1 +-3:1 +1:1 +3:1 +1000001:1 +1000002:0 ++inf:0 +-inf:0 +123.45:0 +-123.45:0 +2:0 +&is_int +NaNis_int:0 +0:1 +1:1 +2:1 +-2:1 +-1:1 +-inf:0 ++inf:0 +123.4567:0 +-0.1:0 +-0.002:0 +1/3:0 +3/1:1 +&is_even +abc:0 +0:1 +-1:0 +-3:0 +1:0 +3:0 +1000001:0 +1000002:1 +2:1 ++inf:0 +-inf:0 +123.456:0 +-123.456:0 +0.01:0 +-0.01:0 +120:1 +1200:1 +-1200:1 +&is_pos +0:0 +1:1 +-1:0 +-123:0 +NaN:0 +-inf:0 ++inf:1 +&is_positive +0:0 +1:1 +-1:0 +-123:0 +NaN:0 +-inf:0 ++inf:1 +&is_neg +0:0 +1:0 +-1:1 +-123:1 +NaN:0 +-inf:1 ++inf:0 +&is_negative +0:0 +1:0 +-1:1 +-123:1 +NaN:0 +-inf:1 ++inf:0 +&parts +0:0 1 +1:1 1 +123:123 1 +-123:-123 1 +-1200:-1200 1 +5/7:5 7 +-5/7:-5 7 +NaNparts:NaN NaN ++inf:inf inf +-inf:-inf inf +&length +123:3 +-123:3 +0:1 +1:1 +12345678901234567890:20 +&is_zero +NaNzero:0 ++inf:0 +-inf:0 +0:1 +-1:0 +1:0 +0/3:1 +1/3:0 +-0/3:1 +5/inf:1 +&is_one +NaNone:0 ++inf:0 +-inf:0 +0:0 +2:0 +1:1 +-1:0 +-2:0 +1/3:0 +100/100:1 +0.1/0.1:1 +5/inf:0 +&ffloor +0:0 +abc:NaN ++inf:inf +-inf:-inf +1:1 +-51:-51 +-51.2:-52 +12.2:12 +3/7:0 +6/7:0 +7/7:1 +8/7:1 +13/7:1 +14/7:2 +15/7:2 +-3/7:-1 +-6/7:-1 +-7/1:-7 +-8/7:-2 +-13/7:-2 +-14/7:-2 +-15/7:-3 +&fceil +0:0 +abc:NaN ++inf:inf +-inf:-inf +1:1 +-51:-51 +-51.2:-51 +12.2:13 +3/7:1 +6/7:1 +8/7:2 +13/7:2 +14/7:2 +15/7:3 +-3/7:0 +-6/7:0 +-8/7:-1 +-13/7:-1 +-14/7:-2 +-15/7:-2 +&ffac +NaN:NaN +1:1 +-1:NaN +&bpow +# bpow test for overload of ** +2:2:4 +3:3:27 +&bacmp ++0:-0:0 ++0:+1:-1 +-1:+1:0 ++1:-1:0 +-1:+2:-1 ++2:-1:1 +-123456789:+987654321:-1 ++123456789:-987654321:-1 ++987654321:+123456789:1 +-987654321:+123456789:1 +-123:+4567889:-1 +# NaNs +acmpNaN:123: +123:acmpNaN: +acmpNaN:acmpNaN: +# infinity ++inf:+inf:0 +-inf:-inf:0 ++inf:-inf:0 +-inf:+inf:0 ++inf:123:1 +-inf:123:1 ++inf:-123:1 +-inf:-123:1 ++inf:1/23:1 +-inf:1/23:1 ++inf:-1/23:1 +-inf:-1/23:1 ++inf:12/3:1 +-inf:12/3:1 ++inf:-12/3:1 +-inf:-12/3:1 +123:inf:-1 +-123:inf:-1 +123:-inf:-1 +-123:-inf:-1 +1/23:inf:-1 +-1/23:inf:-1 +1/23:-inf:-1 +-1/23:-inf:-1 +12/3:inf:-1 +-12/3:inf:-1 +12/3:-inf:-1 +-12/3:-inf:-1 +# return undef ++inf:NaN: +NaN:inf: +-inf:NaN: +NaN:-inf: +1/3:2/3:-1 +2/3:1/3:1 +2/3:2/3:0 +&fpow +2/1:3/1:8 +3/1:3/1:27 +5/2:3/1:125/8 +-2/1:3/1:-8 +-3/1:3/1:-27 +-5/2:3/1:-125/8 +-2/1:4/1:16 +-3/1:4/1:81 +-5/2:4/1:625/16 +-5/2:-4/1:16/625 +1/5:-3:125 +-1/5:-3:-125 +&numerator +NaN:NaN +inf:inf +-inf:-inf +3/7:3 +-3/7:-3 +0:0 +1:1 +5/-3:-5 +&denominator +NaN:NaN +inf:1 +-inf:1 +3/7:7 +0:1 +1/1:1 +-1/1:1 +-3/7:7 +4/-5:5 +&finc +3/2:5/2 +-15/6:-3/2 +NaN:NaN +-1/3:2/3 +-2/7:5/7 +&fdec +15/6:3/2 +-3/2:-5/2 +1/3:-2/3 +2/7:-5/7 +NaN:NaN diff --git a/cpan/Math-BigRat/t/bigratpm.t b/cpan/Math-BigRat/t/bigratpm.t new file mode 100644 index 0000000000..b3f550e30c --- /dev/null +++ b/cpan/Math-BigRat/t/bigratpm.t @@ -0,0 +1,12 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More tests => 696; + +use Math::BigRat lib => 'Calc'; + +use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); +$class = "Math::BigRat"; +$CL = "Math::BigInt::Calc"; + +require 't/bigratpm.inc'; # all tests here for sharing diff --git a/cpan/Math-BigRat/t/bigratup.t b/cpan/Math-BigRat/t/bigratup.t new file mode 100644 index 0000000000..a55cbb59ae --- /dev/null +++ b/cpan/Math-BigRat/t/bigratup.t @@ -0,0 +1,31 @@ +#!/usr/bin/perl -w + +# Test whether $Math::BigInt::upgrade breaks our neck + +use strict; +use Test::More tests => 5; + +use Math::BigInt upgrade => 'Math::BigRat'; +use Math::BigRat; + +my $rat = 'Math::BigRat'; +my ($x,$y,$z); + +############################################################################## +# bceil/bfloor + +$x = $rat->new('49/4'); is ($x->bfloor(),'12', 'floor(49/4)'); +$x = $rat->new('49/4'); is ($x->bceil(),'13', 'ceil(49/4)'); + +############################################################################## +# bsqrt + +$x = $rat->new('144'); is ($x->bsqrt(),'12', 'bsqrt(144)'); +$x = $rat->new('144/16'); is ($x->bsqrt(),'3', 'bsqrt(144/16)'); +$x = $rat->new('1/3'); is ($x->bsqrt(), + '1000000000000000000000000000000000000000/1732050807568877293527446341505872366943', + 'bsqrt(1/3)'); + +# all tests successful + +1; diff --git a/cpan/Math-BigRat/t/bigroot.t b/cpan/Math-BigRat/t/bigroot.t new file mode 100644 index 0000000000..24599482e1 --- /dev/null +++ b/cpan/Math-BigRat/t/bigroot.t @@ -0,0 +1,41 @@ +#!/usr/bin/perl -w + +# Test broot function (and bsqrt() function, since it is used by broot()). + +# It is too slow to be simple included in bigfltpm.inc, where it would get +# executed 3 times. + +# But it is better to test the numerical functionality, instead of not testing +# it at all. + +use strict; +use Test::More tests => 8 * 2; + +use Math::BigFloat; +use Math::BigInt; + +my $cl = "Math::BigFloat"; +my $c = "Math::BigInt"; + +# 2 ** 240 = +# 1766847064778384329583297500742918515827483896875618958121606201292619776 + +test_broot ('2','240', 8, undef, '1073741824'); +test_broot ('2','240', 9, undef, '106528681.3099908308759836475139583940127'); +test_broot ('2','120', 9, undef, '10321.27324073880096577298929482324664787'); +test_broot ('2','120', 17, undef, '133.3268493632747279600707813049418888729'); + +test_broot ('2','120', 8, undef, '32768'); +test_broot ('2','60', 8, undef, '181.0193359837561662466161566988413540569'); +test_broot ('2','60', 9, undef, '101.5936673259647663841091609134277286651'); +test_broot ('2','60', 17, undef, '11.54672461623965153271017217302844672562'); + +sub test_broot + { + my ($x,$n,$y,$scale,$result) = @_; + + my $s = $scale || 'undef'; + is ($cl->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $cl $x->bpow($n)->broot($y,$s) == $result"); + $result =~ s/\..*//; + is ($c->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $c $x->bpow($n)->broot($y,$s) == $result"); + } diff --git a/cpan/Math-BigRat/t/bitwise.t b/cpan/Math-BigRat/t/bitwise.t new file mode 100644 index 0000000000..be9aa4ce38 --- /dev/null +++ b/cpan/Math-BigRat/t/bitwise.t @@ -0,0 +1,15 @@ +use strict; +use warnings; +use Test::More tests => 22; + +use Math::BigRat; + +my $x = Math::BigRat->new('3/7'); + +for my $op (qw(& | ^ << >> &= |= ^= <<= >>=)) { + ok !eval "my \$y = \$x $op 42; 1"; + like $@, qr/^bitwise operation \Q$op\E not supported in Math::BigRat/; +} + +ok !eval "my \$y = ~\$x; 1"; +like $@, qr/^bitwise operation ~ not supported in Math::BigRat/; diff --git a/cpan/Math-BigRat/t/hang.t b/cpan/Math-BigRat/t/hang.t new file mode 100644 index 0000000000..b2b94a0011 --- /dev/null +++ b/cpan/Math-BigRat/t/hang.t @@ -0,0 +1,18 @@ +#!/usr/bin/perl -w + +# test for bug #34584: hang in exp(1/2) + +use strict; +use Test::More tests => 1; + +use Math::BigRat; + +my $result = Math::BigRat->new('1/2')->bexp(); + +is ("$result", "9535900335500879457687887524133067574481/5783815921445270815783609372070483523265", + "exp(1/2) worked"); + +############################################################################## +# done + +1; diff --git a/cpan/Math-BigRat/t/requirer.t b/cpan/Math-BigRat/t/requirer.t new file mode 100644 index 0000000000..06ce1f4faa --- /dev/null +++ b/cpan/Math-BigRat/t/requirer.t @@ -0,0 +1,14 @@ +#!/usr/bin/perl -w + +# check that simple requiring BigRat works + +use strict; +use Test::More tests => 1; + +my ($x); + +require Math::BigRat; $x = Math::BigRat->new(1); ++$x; + +is ($x, 2, '$x got successfully modified'); + +# all tests done diff --git a/cpan/Math-BigRat/t/trap.t b/cpan/Math-BigRat/t/trap.t new file mode 100644 index 0000000000..2811524a18 --- /dev/null +++ b/cpan/Math-BigRat/t/trap.t @@ -0,0 +1,74 @@ +#!/usr/bin/perl -w + +# test that config ( trap_nan => 1, trap_inf => 1) really works/dies + +use strict; +use Test::More tests => 29; + +use Math::BigRat; + +my $mbi = 'Math::BigRat'; +my ($cfg,$x); + +foreach my $class ($mbi) + { + # can do and defaults are okay? + can_ok ($class, 'config'); + is ($class->config()->{trap_nan}, 0); + is ($class->config()->{trap_inf}, 0); + + # can set? + $cfg = $class->config( trap_nan => 1 ); is ($cfg->{trap_nan},1); + + # can set via hash ref? + $cfg = $class->config( { trap_nan => 1 } ); is ($cfg->{trap_nan},1); + + # also test that new() still works normally + eval ("\$x = \$class->new('42'); \$x->bnan();"); + like ($@, qr/^Tried to set/); + is ($x,42); # after new() never modified + + # can reset? + $cfg = $class->config( trap_nan => 0 ); is ($cfg->{trap_nan},0); + + # can set? + $cfg = $class->config( trap_inf => 1 ); is ($cfg->{trap_inf},1); + eval ("\$x = \$class->new('4711'); \$x->binf();"); + like ($@, qr/^Tried to set/); + is ($x,4711); # after new() never modified + + # +$x/0 => +inf + eval ("\$x = \$class->new('4711'); \$x->bdiv(0);"); + like ($@, qr/^Tried to set/); + is ($x,4711); # after new() never modified + + # -$x/0 => -inf + eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);"); + like ($@, qr/^Tried to set/); + is ($x,-815); # after new() never modified + + $cfg = $class->config( trap_nan => 1 ); + # 0/0 => NaN + eval ("\$x = \$class->new('0'); \$x->bdiv(0);"); + like ($@, qr/^Tried to set/); + is ($x,0); # after new() never modified + } + +############################################################################## +# BigRat + +$cfg = Math::BigRat->config( trap_nan => 1 ); + +for my $trap (qw/0.1a +inf inf -inf/) + { + my $x = Math::BigRat->new('7/4'); + + eval ("\$x = \$mbi->new('$trap');"); + is ($x,'7/4'); # never modified since it dies + eval ("\$x = \$mbi->new('$trap');"); + is ($x,'7/4'); # never modified since it dies + eval ("\$x = \$mbi->new('$trap/7');"); + is ($x,'7/4'); # never modified since it dies + } + +# all tests done diff --git a/dist/Math-BigInt-FastCalc/FastCalc.xs b/dist/Math-BigInt-FastCalc/FastCalc.xs deleted file mode 100644 index a045c7172e..0000000000 --- a/dist/Math-BigInt-FastCalc/FastCalc.xs +++ /dev/null @@ -1,410 +0,0 @@ -#define PERL_NO_GET_CONTEXT - -#include "EXTERN.h" -#include "perl.h" -#include "XSUB.h" - -/* for Perl prior to v5.7.1 */ -#ifndef SvUOK -# define SvUOK(sv) SvIOK_UV(sv) -#endif - -/* for Perl v5.6 (RT #63859) */ -#ifndef croak_xs_usage -# define croak_xs_usage croak -#endif - -double XS_BASE = 0; -double XS_BASE_LEN = 0; - -MODULE = Math::BigInt::FastCalc PACKAGE = Math::BigInt::FastCalc - -PROTOTYPES: DISABLE - - ############################################################################# - # 2002-08-12 0.03 Tels unreleased - # * is_zero/is_one/is_odd/is_even/len work now (pass v1.61 tests) - # 2002-08-13 0.04 Tels unreleased - # * returns no/yes for is_foo() methods to be faster - # 2002-08-18 0.06alpha - # * added _num(), _inc() and _dec() - # 2002-08-25 0.06 Tels - # * added __strip_zeros(), _copy() - # 2004-08-13 0.07 Tels - # * added _is_two(), _is_ten(), _ten() - # 2007-04-02 0.08 Tels - # * plug leaks by creating mortals - # 2007-05-27 0.09 Tels - # * add _new() - -#define RETURN_MORTAL_INT(value) \ - ST(0) = sv_2mortal(newSViv(value)); \ - XSRETURN(1); - -BOOT: -{ - if (items < 4) - croak("Usage: Math::BigInt::FastCalc::BOOT(package, version, base_len, base)"); - XS_BASE_LEN = SvIV(ST(2)); - XS_BASE = SvNV(ST(3)); -} - -############################################################################## -# _new - -SV * -_new(class, x) - SV* x - INIT: - STRLEN len; - char* cur; - STRLEN part_len; - AV *av = newAV(); - - CODE: - if (SvUOK(x) && SvUV(x) < XS_BASE) - { - /* shortcut for integer arguments */ - av_push (av, newSVuv( SvUV(x) )); - } - else - { - /* split the input (as string) into XS_BASE_LEN long parts */ - /* in perl: - [ reverse(unpack("a" . ($il % $BASE_LEN+1) - . ("a$BASE_LEN" x ($il / $BASE_LEN)), $_[1])) ]; - */ - cur = SvPV(x, len); /* convert to string & store length */ - cur += len; /* doing "cur = SvEND(x)" does not work! */ - # process the string from the back - while (len > 0) - { - /* use either BASE_LEN or the amount of remaining digits */ - part_len = (STRLEN) XS_BASE_LEN; - if (part_len > len) - { - part_len = len; - } - /* processed so many digits */ - cur -= part_len; - len -= part_len; - /* printf ("part '%s' (part_len: %i, len: %i, BASE_LEN: %i)\n", cur, part_len, len, XS_BASE_LEN); */ - if (part_len > 0) - { - av_push (av, newSVpvn(cur, part_len) ); - } - } - } - RETVAL = newRV_noinc((SV *)av); - OUTPUT: - RETVAL - -############################################################################## -# _copy - -void -_copy(class, x) - SV* x - INIT: - AV* a; - AV* a2; - SSize_t elems; - - CODE: - a = (AV*)SvRV(x); /* ref to aray, don't check ref */ - elems = av_len(a); /* number of elems in array */ - a2 = (AV*)sv_2mortal((SV*)newAV()); - av_extend (a2, elems); /* pre-padd */ - while (elems >= 0) - { - /* av_store( a2, elems, newSVsv( (SV*)*av_fetch(a, elems, 0) ) ); */ - - /* looking and trying to preserve IV is actually slower when copying */ - /* temp = (SV*)*av_fetch(a, elems, 0); - if (SvIOK(temp)) - { - av_store( a2, elems, newSViv( SvIV( (SV*)*av_fetch(a, elems, 0) ))); - } - else - { - av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) ))); - } - */ - av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) ))); - elems--; - } - ST(0) = sv_2mortal( newRV_inc((SV*) a2) ); - -############################################################################## -# __strip_zeros (also check for empty arrays from div) - -void -__strip_zeros(x) - SV* x - INIT: - AV* a; - SV* temp; - SSize_t elems; - SSize_t index; - - CODE: - a = (AV*)SvRV(x); /* ref to aray, don't check ref */ - elems = av_len(a); /* number of elems in array */ - ST(0) = x; /* we return x */ - if (elems == -1) - { - av_push (a, newSViv(0)); /* correct empty arrays */ - XSRETURN(1); - } - if (elems == 0) - { - XSRETURN(1); /* nothing to do since only one elem */ - } - index = elems; - while (index > 0) - { - temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ - if (SvNV(temp) != 0) - { - break; - } - index--; - } - if (index < elems) - { - index = elems - index; - while (index-- > 0) - { - av_pop (a); - } - } - XSRETURN(1); - -############################################################################## -# decrement (subtract one) - -void -_dec(class,x) - SV* x - INIT: - AV* a; - SV* temp; - SSize_t elems; - SSize_t index; - NV MAX; - - CODE: - a = (AV*)SvRV(x); /* ref to aray, don't check ref */ - elems = av_len(a); /* number of elems in array */ - ST(0) = x; /* we return x */ - - MAX = XS_BASE - 1; - index = 0; - while (index <= elems) - { - temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ - sv_setnv (temp, SvNV(temp)-1); /* decrement */ - if (SvNV(temp) >= 0) - { - break; /* early out */ - } - sv_setnv (temp, MAX); /* overflow, so set this to $MAX */ - index++; - } - /* do have more than one element? */ - /* (more than one because [0] should be kept as single-element) */ - if (elems > 0) - { - temp = *av_fetch(a, elems, 0); /* fetch last element */ - if (SvIV(temp) == 0) /* did last elem overflow? */ - { - av_pop(a); /* yes, so shrink array */ - /* aka remove leading zeros */ - } - } - XSRETURN(1); /* return x */ - -############################################################################## -# increment (add one) - -void -_inc(class,x) - SV* x - INIT: - AV* a; - SV* temp; - SSize_t elems; - SSize_t index; - NV BASE; - - CODE: - a = (AV*)SvRV(x); /* ref to aray, don't check ref */ - elems = av_len(a); /* number of elems in array */ - ST(0) = x; /* we return x */ - - BASE = XS_BASE; - index = 0; - while (index <= elems) - { - temp = *av_fetch(a, index, 0); /* fetch ptr to current element */ - sv_setnv (temp, SvNV(temp)+1); - if (SvNV(temp) < BASE) - { - XSRETURN(1); /* return (early out) */ - } - sv_setiv (temp, 0); /* overflow, so set this elem to 0 */ - index++; - } - temp = *av_fetch(a, elems, 0); /* fetch last element */ - if (SvIV(temp) == 0) /* did last elem overflow? */ - { - av_push(a, newSViv(1)); /* yes, so extend array by 1 */ - } - XSRETURN(1); /* return x */ - -############################################################################## - -SV * -_zero(class) - ALIAS: - _one = 1 - _two = 2 - _ten = 10 - PREINIT: - AV *av = newAV(); - CODE: - av_push (av, newSViv( ix )); - RETVAL = newRV_noinc((SV *)av); - OUTPUT: - RETVAL - -############################################################################## - -void -_is_even(class, x) - SV* x - ALIAS: - _is_odd = 1 - INIT: - AV* a; - SV* temp; - - CODE: - a = (AV*)SvRV(x); /* ref to aray, don't check ref */ - temp = *av_fetch(a, 0, 0); /* fetch first element */ - ST(0) = sv_2mortal(boolSV((SvIV(temp) & 1) == ix)); - -############################################################################## - -void -_is_zero(class, x) - SV* x - ALIAS: - _is_one = 1 - _is_two = 2 - _is_ten = 10 - INIT: - AV* a; - - CODE: - a = (AV*)SvRV(x); /* ref to aray, don't check ref */ - if ( av_len(a) != 0) - { - ST(0) = &PL_sv_no; /* len != 1, can't be '0' */ - } - else - { - SV *const temp = *av_fetch(a, 0, 0); /* fetch first element */ - ST(0) = boolSV(SvIV(temp) == ix); - } - XSRETURN(1); - -############################################################################## - -void -_len(class,x) - SV* x - INIT: - AV* a; - SV* temp; - IV elems; - STRLEN len; - - CODE: - a = (AV*)SvRV(x); /* ref to aray, don't check ref */ - elems = av_len(a); /* number of elems in array */ - temp = *av_fetch(a, elems, 0); /* fetch last element */ - SvPV(temp, len); /* convert to string & store length */ - len += (IV) XS_BASE_LEN * elems; - ST(0) = sv_2mortal(newSViv(len)); - -############################################################################## - -void -_acmp(class, cx, cy); - SV* cx - SV* cy - INIT: - AV* array_x; - AV* array_y; - SSize_t elemsx, elemsy, diff; - SV* tempx; - SV* tempy; - STRLEN lenx; - STRLEN leny; - NV diff_nv; - SSize_t diff_str; - - CODE: - array_x = (AV*)SvRV(cx); /* ref to aray, don't check ref */ - array_y = (AV*)SvRV(cy); /* ref to aray, don't check ref */ - elemsx = av_len(array_x); - elemsy = av_len(array_y); - diff = elemsx - elemsy; /* difference */ - - if (diff > 0) - { - RETURN_MORTAL_INT(1); /* len differs: X > Y */ - } - else if (diff < 0) - { - RETURN_MORTAL_INT(-1); /* len differs: X < Y */ - } - /* both have same number of elements, so check length of last element - and see if it differs */ - tempx = *av_fetch(array_x, elemsx, 0); /* fetch last element */ - tempy = *av_fetch(array_y, elemsx, 0); /* fetch last element */ - SvPV(tempx, lenx); /* convert to string & store length */ - SvPV(tempy, leny); /* convert to string & store length */ - diff_str = (SSize_t)lenx - (SSize_t)leny; - if (diff_str > 0) - { - RETURN_MORTAL_INT(1); /* same len, but first elems differs in len */ - } - if (diff_str < 0) - { - RETURN_MORTAL_INT(-1); /* same len, but first elems differs in len */ - } - /* same number of digits, so need to make a full compare */ - diff_nv = 0; - while (elemsx >= 0) - { - tempx = *av_fetch(array_x, elemsx, 0); /* fetch curr x element */ - tempy = *av_fetch(array_y, elemsx, 0); /* fetch curr y element */ - diff_nv = SvNV(tempx) - SvNV(tempy); - if (diff_nv != 0) - { - break; - } - elemsx--; - } - if (diff_nv > 0) - { - RETURN_MORTAL_INT(1); - } - if (diff_nv < 0) - { - RETURN_MORTAL_INT(-1); - } - ST(0) = sv_2mortal(newSViv(0)); /* X and Y are equal */ - diff --git a/dist/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm b/dist/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm deleted file mode 100644 index 9bf5a60839..0000000000 --- a/dist/Math-BigInt-FastCalc/lib/Math/BigInt/FastCalc.pm +++ /dev/null @@ -1,112 +0,0 @@ -package Math::BigInt::FastCalc; - -use 5.006; -use strict; -use warnings; - -use Math::BigInt::Calc 1.997; - -use vars '$VERSION'; - -$VERSION = '0.31'; - -############################################################################## -# global constants, flags and accessory - -# announce that we are compatible with MBI v1.83 and up -sub api_version () { 2; } - -# use Calc to override the methods that we do not provide in XS - -for my $method (qw/ - str num - add sub mul div - rsft lsft - mod modpow modinv - gcd - pow root sqrt log_int fac nok - digit check - from_hex from_bin from_oct as_hex as_bin as_oct - zeros base_len - xor or and - alen 1ex - /) - { - no strict 'refs'; - *{'Math::BigInt::FastCalc::_' . $method} = \&{'Math::BigInt::Calc::_' . $method}; - } - -require XSLoader; -XSLoader::load(__PACKAGE__, $VERSION, Math::BigInt::Calc::_base_len()); - -############################################################################## -############################################################################## - -1; -__END__ -=pod - -=head1 NAME - -Math::BigInt::FastCalc - Math::BigInt::Calc with some XS for more speed - -=head1 SYNOPSIS - -Provides support for big integer calculations. Not intended to be used by -other modules. Other modules which sport the same functions can also be used -to support Math::BigInt, like L or L. - -=head1 DESCRIPTION - -In order to allow for multiple big integer libraries, Math::BigInt was -rewritten to use library modules for core math routines. Any module which -follows the same API as this can be used instead by using the following: - - use Math::BigInt lib => 'libname'; - -'libname' is either the long name ('Math::BigInt::Pari'), or only the short -version like 'Pari'. To use this library: - - use Math::BigInt lib => 'FastCalc'; - -Note that from L v1.76 onwards, FastCalc will be loaded -automatically, if possible. - -=head1 STORAGE - -FastCalc works exactly like Calc, in stores the numbers in decimal form, -chopped into parts. - -=head1 METHODS - -The following functions are now implemented in FastCalc.xs: - - _is_odd _is_even _is_one _is_zero - _is_two _is_ten - _zero _one _two _ten - _acmp _len - _inc _dec - __strip_zeros _copy - -=head1 LICENSE - -This program is free software; you may redistribute it and/or modify it under -the same terms as Perl itself. - -=head1 AUTHORS - -Original math code by Mark Biggar, rewritten by Tels L -in late 2000. -Separated from BigInt and shaped API with the help of John Peacock. - -Fixed, sped-up and enhanced by Tels http://bloodgate.com 2001-2003. -Further streamlining (api_version 1 etc.) by Tels 2004-2007. - -Bug-fixing by Peter John Acklam Epjacklam@online.noE 2010-2011. - -=head1 SEE ALSO - -L, L, -L, L and L. - -=cut diff --git a/dist/Math-BigInt-FastCalc/t/bigintfc.t b/dist/Math-BigInt-FastCalc/t/bigintfc.t deleted file mode 100644 index c8751ad81b..0000000000 --- a/dist/Math-BigInt-FastCalc/t/bigintfc.t +++ /dev/null @@ -1,430 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 359; - -use Math::BigInt::FastCalc; - -my ($BASE_LEN, undef, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL) = - Math::BigInt::FastCalc->_base_len(); - -print "# BASE_LEN = $BASE_LEN\n"; -print "# MAX_VAL = $MAX_VAL\n"; -print "# AND_BITS = $AND_BITS\n"; -print "# XOR_BITS = $XOR_BITS\n"; -print "# IOR_BITS = $OR_BITS\n"; - -# testing of Math::BigInt::FastCalc - -my $C = 'Math::BigInt::FastCalc'; # pass classname to sub's - -# _new and _str -my $x = $C->_new("123"); my $y = $C->_new("321"); -is (ref($x),'ARRAY'); is ($C->_str($x),123); is ($C->_str($y),321); - -############################################################################### -# _add, _sub, _mul, _div -is ($C->_str($C->_add($x,$y)),444); -is ($C->_str($C->_sub($x,$y)),123); -is ($C->_str($C->_mul($x,$y)),39483); -is ($C->_str($C->_div($x,$y)),123); - -############################################################################### -# check that mul/div doesn't change $y -# and returns the same reference, not something new -is ($C->_str($C->_mul($x,$y)),39483); -is ($C->_str($x),39483); is ($C->_str($y),321); - -is ($C->_str($C->_div($x,$y)),123); -is ($C->_str($x),123); is ($C->_str($y),321); - -$x = $C->_new("39483"); -my ($x1,$r1) = $C->_div($x,$y); -is ("$x1","$x"); -$C->_inc($x1); -is ("$x1","$x"); -is ($C->_str($r1),'0'); - -$x = $C->_new("39483"); # reset - -############################################################################### -my $z = $C->_new("2"); -is ($C->_str($C->_add($x,$z)),39485); -my ($re,$rr) = $C->_div($x,$y); - -is ($C->_str($re),123); is ($C->_str($rr),2); - -# is_zero, _is_one, _one, _zero -is ($C->_is_zero($x),''); -is ($C->_is_one($x),''); - -is ($C->_str($C->_zero()),"0"); -is ($C->_str($C->_one()),"1"); - -# _two() and _ten() -is ($C->_str($C->_two()),"2"); -is ($C->_str($C->_ten()),"10"); -is ($C->_is_ten($C->_two()),''); -is ($C->_is_two($C->_two()),1); -is ($C->_is_ten($C->_ten()),1); -is ($C->_is_two($C->_ten()),''); - -is ($C->_is_one($C->_one()),1); -is ($C->_is_one($C->_two()), ''); -is ($C->_is_one($C->_ten()), ''); - -is ($C->_is_one($C->_zero()), ''); - -is ($C->_is_zero($C->_zero()),1); - -is ($C->_is_zero($C->_one()), ''); - -# is_odd, is_even -is ($C->_is_odd($C->_one()),1); is ($C->_is_odd($C->_zero()),''); -is ($C->_is_even($C->_one()), ''); is ($C->_is_even($C->_zero()),1); - -# _len -for my $method (qw/_alen _len/) - { - $x = $C->_new("1"); is ($C->$method($x),1); - $x = $C->_new("12"); is ($C->$method($x),2); - $x = $C->_new("123"); is ($C->$method($x),3); - $x = $C->_new("1234"); is ($C->$method($x),4); - $x = $C->_new("12345"); is ($C->$method($x),5); - $x = $C->_new("123456"); is ($C->$method($x),6); - $x = $C->_new("1234567"); is ($C->$method($x),7); - $x = $C->_new("12345678"); is ($C->$method($x),8); - $x = $C->_new("123456789"); is ($C->$method($x),9); - - $x = $C->_new("8"); is ($C->$method($x),1); - $x = $C->_new("21"); is ($C->$method($x),2); - $x = $C->_new("321"); is ($C->$method($x),3); - $x = $C->_new("4321"); is ($C->$method($x),4); - $x = $C->_new("54321"); is ($C->$method($x),5); - $x = $C->_new("654321"); is ($C->$method($x),6); - $x = $C->_new("7654321"); is ($C->$method($x),7); - $x = $C->_new("87654321"); is ($C->$method($x),8); - $x = $C->_new("987654321"); is ($C->$method($x),9); - - $x = $C->_new("0"); is ($C->$method($x),1); - $x = $C->_new("20"); is ($C->$method($x),2); - $x = $C->_new("320"); is ($C->$method($x),3); - $x = $C->_new("4320"); is ($C->$method($x),4); - $x = $C->_new("54320"); is ($C->$method($x),5); - $x = $C->_new("654320"); is ($C->$method($x),6); - $x = $C->_new("7654320"); is ($C->$method($x),7); - $x = $C->_new("87654320"); is ($C->$method($x),8); - $x = $C->_new("987654320"); is ($C->$method($x),9); - - for (my $i = 1; $i < 9; $i++) - { - my $a = "$i" . '0' x ($i-1); - $x = $C->_new($a); - print "# Tried len '$a'\n" unless is ($C->_len($x),$i); - } - } - -# _digit -$x = $C->_new("123456789"); -is ($C->_digit($x,0),9); -is ($C->_digit($x,1),8); -is ($C->_digit($x,2),7); -is ($C->_digit($x,-1),1); -is ($C->_digit($x,-2),2); -is ($C->_digit($x,-3),3); - -# _copy -foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/) - { - $x = $C->_new("$_"); - is ($C->_str($C->_copy($x)),"$_"); - is ($C->_str($x),"$_"); # did _copy destroy original x? - } - -# _zeros -$x = $C->_new("1256000000"); is ($C->_zeros($x),6); -$x = $C->_new("152"); is ($C->_zeros($x),0); -$x = $C->_new("123000"); is ($C->_zeros($x),3); -$x = $C->_new("0"); is ($C->_zeros($x),0); - -# _lsft, _rsft -$x = $C->_new("10"); $y = $C->_new("3"); -is ($C->_str($C->_lsft($x,$y,10)),10000); -$x = $C->_new("20"); $y = $C->_new("3"); -is ($C->_str($C->_lsft($x,$y,10)),20000); - -$x = $C->_new("128"); $y = $C->_new("4"); -is ($C->_str($C->_lsft($x,$y,2)), 128 << 4); - -$x = $C->_new("1000"); $y = $C->_new("3"); -is ($C->_str($C->_rsft($x,$y,10)),1); -$x = $C->_new("20000"); $y = $C->_new("3"); -is ($C->_str($C->_rsft($x,$y,10)),20); -$x = $C->_new("256"); $y = $C->_new("4"); -is ($C->_str($C->_rsft($x,$y,2)),256 >> 4); - -$x = $C->_new("6411906467305339182857313397200584952398"); -$y = $C->_new("45"); -is ($C->_str($C->_rsft($x,$y,10)),0); - -# _acmp -$x = $C->_new("123456789"); -$y = $C->_new("987654321"); -is ($C->_acmp($x,$y),-1); -is ($C->_acmp($y,$x),1); -is ($C->_acmp($x,$x),0); -is ($C->_acmp($y,$y),0); -$x = $C->_new("12"); -$y = $C->_new("12"); -is ($C->_acmp($x,$y),0); -$x = $C->_new("21"); -is ($C->_acmp($x,$y),1); -is ($C->_acmp($y,$x),-1); -$x = $C->_new("123456789"); -$y = $C->_new("1987654321"); -is ($C->_acmp($x,$y),-1); -is ($C->_acmp($y,$x),+1); - -$x = $C->_new("1234567890123456789"); -$y = $C->_new("987654321012345678"); -is ($C->_acmp($x,$y),1); -is ($C->_acmp($y,$x),-1); -is ($C->_acmp($x,$x),0); -is ($C->_acmp($y,$y),0); - -$x = $C->_new("1234"); -$y = $C->_new("987654321012345678"); -is ($C->_acmp($x,$y),-1); -is ($C->_acmp($y,$x),1); -is ($C->_acmp($x,$x),0); -is ($C->_acmp($y,$y),0); - -# _modinv -$x = $C->_new("8"); -$y = $C->_new("5033"); -my ($xmod,$sign) = $C->_modinv($x,$y); -is ($C->_str($xmod),'629'); # -629 % 5033 == 4404 -is ($sign, '-'); - -# _div -$x = $C->_new("3333"); $y = $C->_new("1111"); -is ($C->_str(scalar $C->_div($x,$y)),3); -$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); -is ($C->_str($x),30); is ($C->_str($y),3); -$x = $C->_new("123"); $y = $C->_new("1111"); -($x,$y) = $C->_div($x,$y); is ($C->_str($x),0); is ($C->_str($y),123); - -# _num -foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/) - { - $x = $C->_new("$_"); - is (ref($x),'ARRAY'); is ($C->_str($x),"$_"); - $x = $C->_num($x); is (ref($x),''); is ($x,$_); - } - -# _sqrt -$x = $C->_new("144"); is ($C->_str($C->_sqrt($x)),'12'); -$x = $C->_new("144000000000000"); is ($C->_str($C->_sqrt($x)),'12000000'); - -# _root -$x = $C->_new("81"); my $n = $C->_new("3"); # 4*4*4 = 64, 5*5*5 = 125 -is ($C->_str($C->_root($x,$n)),'4'); # 4.xx => 4.0 -$x = $C->_new("81"); $n = $C->_new("4"); # 3*3*3*3 == 81 -is ($C->_str($C->_root($x,$n)),'3'); - -# _pow (and _root) -$x = $C->_new("0"); $n = $C->_new("3"); # 0 ** y => 0 -is ($C->_str($C->_pow($x,$n)), 0); -$x = $C->_new("3"); $n = $C->_new("0"); # x ** 0 => 1 -is ($C->_str($C->_pow($x,$n)), 1); -$x = $C->_new("1"); $n = $C->_new("3"); # 1 ** y => 1 -is ($C->_str($C->_pow($x,$n)), 1); -$x = $C->_new("5"); $n = $C->_new("1"); # x ** 1 => x -is ($C->_str($C->_pow($x,$n)), 5); - -$x = $C->_new("81"); $n = $C->_new("3"); # 81 ** 3 == 531441 -is ($C->_str($C->_pow($x,$n)),81 ** 3); - -is ($C->_str($C->_root($x,$n)),81); - -$x = $C->_new("81"); -is ($C->_str($C->_pow($x,$n)),81 ** 3); -is ($C->_str($C->_pow($x,$n)),'150094635296999121'); # 531441 ** 3 == - -is ($C->_str($C->_root($x,$n)),'531441'); -is ($C->_str($C->_root($x,$n)),'81'); - -$x = $C->_new("81"); $n = $C->_new("14"); -is ($C->_str($C->_pow($x,$n)),'523347633027360537213511521'); -is ($C->_str($C->_root($x,$n)),'81'); - -$x = $C->_new("523347633027360537213511520"); -is ($C->_str($C->_root($x,$n)),'80'); - -$x = $C->_new("523347633027360537213511522"); -is ($C->_str($C->_root($x,$n)),'81'); - -my $res = [ qw/ 9 31 99 316 999 3162 9999/ ]; - -# 99 ** 2 = 9801, 999 ** 2 = 998001 etc -for my $i (2 .. 9) - { - $x = '9' x $i; $x = $C->_new($x); - $n = $C->_new("2"); - my $rc = '9' x ($i-1). '8' . '0' x ($i-1) . '1'; - print "# _pow( ", '9' x $i, ", 2) \n" unless - is ($C->_str($C->_pow($x,$n)),$rc); - - if ($i <= 7) - { - $x = '9' x $i; $x = $C->_new($x); - $n = '9' x $i; $n = $C->_new($n); - print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless - is ($C->_str($C->_root($x,$n)),'1'); - - $x = '9' x $i; $x = $C->_new($x); - $n = $C->_new("2"); - print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless - is ($C->_str($C->_root($x,$n)), $res->[$i-2]); - } - } - -############################################################################## -# _fac -$x = $C->_new("0"); is ($C->_str($C->_fac($x)),'1'); -$x = $C->_new("1"); is ($C->_str($C->_fac($x)),'1'); -$x = $C->_new("2"); is ($C->_str($C->_fac($x)),'2'); -$x = $C->_new("3"); is ($C->_str($C->_fac($x)),'6'); -$x = $C->_new("4"); is ($C->_str($C->_fac($x)),'24'); -$x = $C->_new("5"); is ($C->_str($C->_fac($x)),'120'); -$x = $C->_new("10"); is ($C->_str($C->_fac($x)),'3628800'); -$x = $C->_new("11"); is ($C->_str($C->_fac($x)),'39916800'); -$x = $C->_new("12"); is ($C->_str($C->_fac($x)),'479001600'); -$x = $C->_new("13"); is ($C->_str($C->_fac($x)),'6227020800'); - -# test that _fac modifies $x in place for small arguments -$x = $C->_new("3"); $C->_fac($x); is ($C->_str($x),'6'); -$x = $C->_new("13"); $C->_fac($x); is ($C->_str($x),'6227020800'); - -############################################################################## -# _inc and _dec -foreach (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless is ($C->_str($x),substr($_,0,length($_)-1) . '2'); - $C->_dec($x); is ($C->_str($x),$_); - } -foreach (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless is ($C->_str($x),substr($_,0,length($_)-2) . '20'); - $C->_dec($x); is ($C->_str($x),$_); - } -foreach (qw/999 9999 99999 9999999 99999999 999999999 9999999999 99999999999/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless is ($C->_str($x), '1' . '0' x (length($_))); - $C->_dec($x); is ($C->_str($x),$_); - } - -$x = $C->_new("1000"); $C->_inc($x); is ($C->_str($x),'1001'); -$C->_dec($x); is ($C->_str($x),'1000'); - -my $BL; -{ - no strict 'refs'; - $BL = &{"$C"."::_base_len"}(); -} - -$x = '1' . '0' x $BL; -$z = '1' . '0' x ($BL-1); $z .= '1'; -$x = $C->_new($x); $C->_inc($x); is ($C->_str($x),$z); - -$x = '1' . '0' x $BL; $z = '9' x $BL; -$x = $C->_new($x); $C->_dec($x); is ($C->_str($x),$z); - -# should not happen: -# $x = $C->_new("-2"); $y = $C->_new("4"); is ($C->_acmp($x,$y),-1); - -############################################################################### -# _mod -$x = $C->_new("1000"); $y = $C->_new("3"); -is ($C->_str(scalar $C->_mod($x,$y)),1); -$x = $C->_new("1000"); $y = $C->_new("2"); -is ($C->_str(scalar $C->_mod($x,$y)),0); - -# _and, _or, _xor -$x = $C->_new("5"); $y = $C->_new("2"); -is ($C->_str(scalar $C->_xor($x,$y)),7); -$x = $C->_new("5"); $y = $C->_new("2"); -is ($C->_str(scalar $C->_or($x,$y)),7); -$x = $C->_new("5"); $y = $C->_new("3"); -is ($C->_str(scalar $C->_and($x,$y)),1); - -# _from_hex, _from_bin, _from_oct -is ($C->_str( $C->_from_hex("0xFf")),255); -is ($C->_str( $C->_from_bin("0b10101011")),160+11); -is ($C->_str( $C->_from_oct("0100")), 8*8); -is ($C->_str( $C->_from_oct("01000")), 8*8*8); -is ($C->_str( $C->_from_oct("010001")), 8*8*8*8+1); -is ($C->_str( $C->_from_oct("010007")), 8*8*8*8+7); - -# _as_hex, _as_bin, as_oct -is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("128")))), 128); -is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("128")))), 128); -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("128")))), 128); - -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456")))), 123456); -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456789")))), "123456789"); -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("1234567890123")))), "1234567890123"); - -# _1ex -is ($C->_str($C->_1ex(0)), "1"); -is ($C->_str($C->_1ex(1)), "10"); -is ($C->_str($C->_1ex(2)), "100"); -is ($C->_str($C->_1ex(12)), "1000000000000"); -is ($C->_str($C->_1ex(16)), "10000000000000000"); - -# _check -$x = $C->_new("123456789"); -is ($C->_check($x),0); -is ($C->_check(123),'123 is not a reference'); - -############################################################################### -# __strip_zeros - -{ - no strict 'refs'; - # correct empty arrays - $x = &{$C."::__strip_zeros"}([]); is (@$x,1); is ($x->[0],0); - # don't strip single elements - $x = &{$C."::__strip_zeros"}([0]); is (@$x,1); is ($x->[0],0); - $x = &{$C."::__strip_zeros"}([1]); is (@$x,1); is ($x->[0],1); - # don't strip non-zero elements - $x = &{$C."::__strip_zeros"}([0,1]); - is (@$x,2); is ($x->[0],0); is ($x->[1],1); - $x = &{$C."::__strip_zeros"}([0,1,2]); - is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); - - # but strip leading zeros - $x = &{$C."::__strip_zeros"}([0,1,2,0]); - is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); - - $x = &{$C."::__strip_zeros"}([0,1,2,0,0]); - is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); - - $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]); - is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); - - # collapse multiple zeros - $x = &{$C."::__strip_zeros"}([0,0,0,0]); - is (@$x,1); is ($x->[0],0); -} - -# done - -1; - diff --git a/dist/Math-BigInt-FastCalc/t/bootstrap.t b/dist/Math-BigInt-FastCalc/t/bootstrap.t deleted file mode 100644 index d73afcb47c..0000000000 --- a/dist/Math-BigInt-FastCalc/t/bootstrap.t +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/perl -w - -use Test::More tests => 1; - -BEGIN { - use_ok('Math::BigInt::FastCalc'); -} - diff --git a/dist/Math-BigInt-FastCalc/t/leak.t b/dist/Math-BigInt-FastCalc/t/leak.t deleted file mode 100644 index 5db38e1dbd..0000000000 --- a/dist/Math-BigInt-FastCalc/t/leak.t +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/perl -w - -# Test for memory leaks. - -# XXX TODO: This test file doesn't actually seem to work! If you remove -# the sv_2mortal() in the XS file, it still happily passes all tests... - -use strict; -use Test::More tests => 22; - -use Math::BigInt::FastCalc; - -############################################################################# -package Math::BigInt::FastCalc::LeakCheck; -use parent qw(Math::BigInt::FastCalc); - -my $destroyed = 0; -sub DESTROY { $destroyed++; } - -############################################################################# -package main; - -for my $method (qw(_zero _one _two _ten)) - { - $destroyed = 0; - { - my $num = Math::BigInt::FastCalc::LeakCheck->$method(); - bless $num, "Math::BigInt::FastCalc::LeakCheck"; - } - is ($destroyed, 1, "$method does not leak memory"); - } - -my $num = Math::BigInt::FastCalc->_zero(); -for my $method (qw(_is_zero _is_one _is_two _is_ten _num)) - { - $destroyed = 0; - { - my $rc = Math::BigInt::FastCalc->$method($num); - bless \$rc, "Math::BigInt::FastCalc::LeakCheck"; - } - is ($destroyed, 1, "$method does not leak memory"); - } - -my $num_10 = Math::BigInt::FastCalc->_ten(); -my $num_2 = Math::BigInt::FastCalc->_two(); - -my $num_long = Math::BigInt::FastCalc->_new("1234567890"); -my $num_long_2 = Math::BigInt::FastCalc->_new("12345678900987654321"); - -is (Math::BigInt::FastCalc->_str($num_long), "1234567890"); -is (Math::BigInt::FastCalc->_str($num_long_2), "12345678900987654321"); - -# to hit all possible code branches -_test_acmp($num, $num); -_test_acmp($num_10, $num_10); -_test_acmp($num, $num_10); -_test_acmp($num_10, $num); -_test_acmp($num, $num_2); -_test_acmp($num_2, $num); -_test_acmp($num_long, $num); -_test_acmp($num, $num_long); -_test_acmp($num_long, $num_long); -_test_acmp($num_long, $num_long_2); -_test_acmp($num_long_2, $num_long); - -sub _test_acmp - { - my ($n1,$n2) = @_; - - $destroyed = 0; - { - my $rc = Math::BigInt::FastCalc->_acmp($n1,$n2); - bless \$rc, "Math::BigInt::FastCalc::LeakCheck"; - } - my $n_1 = Math::BigInt::FastCalc->_str($n1); - my $n_2 = Math::BigInt::FastCalc->_str($n2); - is ($destroyed, 1, "_acmp($n_1,$n_2) does not leak memory"); - } - diff --git a/dist/Math-BigInt-FastCalc/t/mbi_rand.t b/dist/Math-BigInt-FastCalc/t/mbi_rand.t deleted file mode 100644 index 4ad473d882..0000000000 --- a/dist/Math-BigInt-FastCalc/t/mbi_rand.t +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More; - -my $count = 128; - -plan(($^O eq 'os390') - ? (skip_all => 'takes too long on os390') : (tests => $count*2)); - -use Math::BigInt lib => 'FastCalc'; -my $c = 'Math::BigInt'; - -my $length = 128; - -# If you get a failure here, please re-run the test with the printed seed -# value as input: perl t/mbi_rand.t seed - -my $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(65537)); -print "# seed: $seed\n"; srand($seed); - -my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb); -my $two = Math::BigInt->new(2); -for (my $i = 0; $i < $count; $i++) - { - # length of A and B - $la = int(rand($length)+1); $lb = int(rand($length)+1); - $As = ''; $Bs = ''; - # we create the numbers from "patterns", e.g. get a random number and a - # random count and string them together. This means things like - # "100000999999999999911122222222" are much more likely. If we just strung - # together digits, we would end up with "1272398823211223" etc. - while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); } - while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); } - $As =~ s/^0+//; $Bs =~ s/^0+//; - $As = $As || '0'; $Bs = $Bs || '0'; - # print "# As $As\n# Bs $Bs\n"; - $A = $c->new($As); $B = $c->new($Bs); - # print "# A $A\n# B $B\n"; - if ($A->is_zero() || $B->is_zero()) - { - is (1,1); is (1,1); next; - } - # check that int(A/B)*B + A % B == A holds for all inputs - # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B); - ($ADB,$AMB) = $A->copy()->bdiv($B); - print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n" - unless is ($ADB*$B+$two*$AMB-$AMB,$As); - # swap 'em and try this, too - # $X = ($B/$A)*$A + $B % $A; - ($ADB,$AMB) = $B->copy()->bdiv($A); - print "# ". join(' ',Math::BigInt::Calc->_base_len()),"\n" - unless is ($ADB*$A+$two*$AMB-$AMB,$Bs); - } - diff --git a/dist/Math-BigInt/lib/Math/BigFloat.pm b/dist/Math-BigInt/lib/Math/BigFloat.pm deleted file mode 100644 index a423b35f02..0000000000 --- a/dist/Math-BigInt/lib/Math/BigFloat.pm +++ /dev/null @@ -1,4692 +0,0 @@ -package Math::BigFloat; - -# -# Mike grinned. 'Two down, infinity to go' - Mike Nostrus in 'Before and After' -# - -# The following hash values are internally used: -# _e : exponent (ref to $CALC object) -# _m : mantissa (ref to $CALC object) -# _es : sign of _e -# sign : +,-,+inf,-inf, or "NaN" if not a number -# _a : accuracy -# _p : precision - -$VERSION = '1.999701'; -require 5.006002; - -require Exporter; -@ISA = qw/Math::BigInt/; -@EXPORT_OK = qw/bpi/; - -use strict; -# $_trap_inf/$_trap_nan are internal and should never be accessed from outside -use vars qw/$AUTOLOAD $accuracy $precision $div_scale $round_mode $rnd_mode - $upgrade $downgrade $_trap_nan $_trap_inf/; -my $class = "Math::BigFloat"; - -use overload -'<=>' => sub { my $rc = $_[2] ? - ref($_[0])->bcmp($_[1],$_[0]) : - ref($_[0])->bcmp($_[0],$_[1]); - $rc = 1 unless defined $rc; - $rc <=> 0; - }, -# we need '>=' to get things like "1 >= NaN" right: -'>=' => sub { my $rc = $_[2] ? - ref($_[0])->bcmp($_[1],$_[0]) : - ref($_[0])->bcmp($_[0],$_[1]); - # if there was a NaN involved, return false - return '' unless defined $rc; - $rc >= 0; - }, -'int' => sub { $_[0]->as_number() }, # 'trunc' to bigint -; - -############################################################################## -# global constants, flags and assorted stuff - -# the following are public, but their usage is not recommended. Use the -# accessor methods instead. - -# class constants, use Class->constant_name() to access -# one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common' -$round_mode = 'even'; -$accuracy = undef; -$precision = undef; -$div_scale = 40; - -$upgrade = undef; -$downgrade = undef; -# the package we are using for our private parts, defaults to: -# Math::BigInt->config()->{lib} -my $MBI = 'Math::BigInt::Calc'; - -# are NaNs ok? (otherwise it dies when encountering an NaN) set w/ config() -$_trap_nan = 0; -# the same for infinity -$_trap_inf = 0; - -# constant for easier life -my $nan = 'NaN'; - -my $IMPORT = 0; # was import() called yet? used to make require work - -# some digits of accuracy for blog(undef,10); which we use in blog() for speed -my $LOG_10 = - '2.3025850929940456840179914546843642076011014886287729760333279009675726097'; -my $LOG_10_A = length($LOG_10)-1; -# ditto for log(2) -my $LOG_2 = - '0.6931471805599453094172321214581765680755001343602552541206800094933936220'; -my $LOG_2_A = length($LOG_2)-1; -my $HALF = '0.5'; # made into an object if nec. - -############################################################################## -# the old code had $rnd_mode, so we need to support it, too - -sub TIESCALAR { my ($class) = @_; bless \$round_mode, $class; } -sub FETCH { return $round_mode; } -sub STORE { $rnd_mode = $_[0]->round_mode($_[1]); } - -BEGIN - { - # when someone sets $rnd_mode, we catch this and check the value to see - # whether it is valid or not. - $rnd_mode = 'even'; tie $rnd_mode, 'Math::BigFloat'; - - # we need both of them in this package: - *as_int = \&as_number; - } - -############################################################################## - -{ - # valid method aliases for AUTOLOAD - my %methods = map { $_ => 1 } - qw / fadd fsub fmul fdiv fround ffround fsqrt fmod fstr fsstr fpow fnorm - fint facmp fcmp fzero fnan finf finc fdec ffac fneg - fceil ffloor frsft flsft fone flog froot fexp - /; - # valid methods that can be handed up (for AUTOLOAD) - my %hand_ups = map { $_ => 1 } - qw / is_nan is_inf is_negative is_positive is_pos is_neg - accuracy precision div_scale round_mode fabs fnot - objectify upgrade downgrade - bone binf bnan bzero - bsub - /; - - sub _method_alias { exists $methods{$_[0]||''}; } - sub _method_hand_up { exists $hand_ups{$_[0]||''}; } -} - -############################################################################## -# constructors - -sub new - { - # create a new BigFloat object from a string or another bigfloat object. - # _e: exponent - # _m: mantissa - # sign => sign (+/-), or "NaN" - - my ($class,$wanted,@r) = @_; - - # avoid numify-calls by not using || on $wanted! - return $class->bzero() if !defined $wanted; # default to 0 - return $wanted->copy() if UNIVERSAL::isa($wanted,'Math::BigFloat'); - - $class->import() if $IMPORT == 0; # make require work - - my $self = {}; bless $self, $class; - # shortcut for bigints and its subclasses - if ((ref($wanted)) && UNIVERSAL::can( $wanted, "as_number")) - { - $self->{_m} = $wanted->as_number()->{value}; # get us a bigint copy - $self->{_e} = $MBI->_zero(); - $self->{_es} = '+'; - $self->{sign} = $wanted->sign(); - return $self->bnorm(); - } - # else: got a string or something masquerading as number (with overload) - - # handle '+inf', '-inf' first - if ($wanted =~ /^[+-]?inf\z/) - { - return $downgrade->new($wanted) if $downgrade; - - $self->{sign} = $wanted; # set a default sign for bstr() - return $self->binf($wanted); - } - - # shortcut for simple forms like '12' that neither have trailing nor leading - # zeros - if ($wanted =~ /^([+-]?)([1-9][0-9]*[1-9])$/) - { - $self->{_e} = $MBI->_zero(); - $self->{_es} = '+'; - $self->{sign} = $1 || '+'; - $self->{_m} = $MBI->_new($2); - return $self->round(@r) if !$downgrade; - } - - my ($mis,$miv,$mfv,$es,$ev) = Math::BigInt::_split($wanted); - if (!ref $mis) - { - if ($_trap_nan) - { - require Carp; - Carp::croak ("$wanted is not a number initialized to $class"); - } - - return $downgrade->bnan() if $downgrade; - - $self->{_e} = $MBI->_zero(); - $self->{_es} = '+'; - $self->{_m} = $MBI->_zero(); - $self->{sign} = $nan; - } - else - { - # make integer from mantissa by adjusting exp, then convert to int - $self->{_e} = $MBI->_new($$ev); # exponent - $self->{_es} = $$es || '+'; - my $mantissa = "$$miv$$mfv"; # create mant. - $mantissa =~ s/^0+(\d)/$1/; # strip leading zeros - $self->{_m} = $MBI->_new($mantissa); # create mant. - - # 3.123E0 = 3123E-3, and 3.123E-2 => 3123E-5 - if (CORE::length($$mfv) != 0) - { - my $len = $MBI->_new( CORE::length($$mfv)); - ($self->{_e}, $self->{_es}) = - _e_sub ($self->{_e}, $len, $self->{_es}, '+'); - } - # we can only have trailing zeros on the mantissa if $$mfv eq '' - else - { - # Use a regexp to count the trailing zeros in $$miv instead of _zeros() - # because that is faster, especially when _m is not stored in base 10. - my $zeros = 0; $zeros = CORE::length($1) if $$miv =~ /[1-9](0*)$/; - if ($zeros != 0) - { - my $z = $MBI->_new($zeros); - # turn '120e2' into '12e3' - $MBI->_rsft ( $self->{_m}, $z, 10); - ($self->{_e}, $self->{_es}) = - _e_add ( $self->{_e}, $z, $self->{_es}, '+'); - } - } - $self->{sign} = $$mis; - - # for something like 0Ey, set y to 1, and -0 => +0 - # Check $$miv for being '0' and $$mfv eq '', because otherwise _m could not - # have become 0. That's faster than to call $MBI->_is_zero(). - $self->{sign} = '+', $self->{_e} = $MBI->_one() - if $$miv eq '0' and $$mfv eq ''; - - return $self->round(@r) if !$downgrade; - } - # if downgrade, inf, NaN or integers go down - - if ($downgrade && $self->{_es} eq '+') - { - if ($MBI->_is_zero( $self->{_e} )) - { - return $downgrade->new($$mis . $MBI->_str( $self->{_m} )); - } - return $downgrade->new($self->bsstr()); - } - $self->bnorm()->round(@r); # first normalize, then round - } - -sub copy - { - # if two arguments, the first one is the class to "swallow" subclasses - if (@_ > 1) - { - my $self = bless { - sign => $_[1]->{sign}, - _es => $_[1]->{_es}, - _m => $MBI->_copy($_[1]->{_m}), - _e => $MBI->_copy($_[1]->{_e}), - }, $_[0] if @_ > 1; - - $self->{_a} = $_[1]->{_a} if defined $_[1]->{_a}; - $self->{_p} = $_[1]->{_p} if defined $_[1]->{_p}; - return $self; - } - - my $self = bless { - sign => $_[0]->{sign}, - _es => $_[0]->{_es}, - _m => $MBI->_copy($_[0]->{_m}), - _e => $MBI->_copy($_[0]->{_e}), - }, ref($_[0]); - - $self->{_a} = $_[0]->{_a} if defined $_[0]->{_a}; - $self->{_p} = $_[0]->{_p} if defined $_[0]->{_p}; - $self; - } - -sub _bnan - { - # used by parent class bone() to initialize number to NaN - my $self = shift; - - if ($_trap_nan) - { - require Carp; - my $class = ref($self); - Carp::croak ("Tried to set $self to NaN in $class\::_bnan()"); - } - - $IMPORT=1; # call our import only once - $self->{_m} = $MBI->_zero(); - $self->{_e} = $MBI->_zero(); - $self->{_es} = '+'; - } - -sub _binf - { - # used by parent class bone() to initialize number to +-inf - my $self = shift; - - if ($_trap_inf) - { - require Carp; - my $class = ref($self); - Carp::croak ("Tried to set $self to +-inf in $class\::_binf()"); - } - - $IMPORT=1; # call our import only once - $self->{_m} = $MBI->_zero(); - $self->{_e} = $MBI->_zero(); - $self->{_es} = '+'; - } - -sub _bone - { - # used by parent class bone() to initialize number to 1 - my $self = shift; - $IMPORT=1; # call our import only once - $self->{_m} = $MBI->_one(); - $self->{_e} = $MBI->_zero(); - $self->{_es} = '+'; - } - -sub _bzero - { - # used by parent class bone() to initialize number to 0 - my $self = shift; - $IMPORT=1; # call our import only once - $self->{_m} = $MBI->_zero(); - $self->{_e} = $MBI->_one(); - $self->{_es} = '+'; - } - -sub isa - { - my ($self,$class) = @_; - return if $class =~ /^Math::BigInt/; # we aren't one of these - UNIVERSAL::isa($self,$class); - } - -sub config - { - # return (later set?) configuration data as hash ref - my $class = shift || 'Math::BigFloat'; - - if (@_ == 1 && ref($_[0]) ne 'HASH') - { - my $cfg = $class->SUPER::config(); - return $cfg->{$_[0]}; - } - - my $cfg = $class->SUPER::config(@_); - - # now we need only to override the ones that are different from our parent - $cfg->{class} = $class; - $cfg->{with} = $MBI; - $cfg; - } - -############################################################################## -# string conversion - -sub bstr - { - # (ref to BFLOAT or num_str ) return num_str - # Convert number from internal format to (non-scientific) string format. - # internal format is always normalized (no leading zeros, "-0" => "+0") - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN - return 'inf'; # +inf - } - - my $es = '0'; my $len = 1; my $cad = 0; my $dot = '.'; - - # $x is zero? - my $not_zero = !($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})); - if ($not_zero) - { - $es = $MBI->_str($x->{_m}); - $len = CORE::length($es); - my $e = $MBI->_num($x->{_e}); - $e = -$e if $x->{_es} eq '-'; - if ($e < 0) - { - $dot = ''; - # if _e is bigger than a scalar, the following will blow your memory - if ($e <= -$len) - { - my $r = abs($e) - $len; - $es = '0.'. ('0' x $r) . $es; $cad = -($len+$r); - } - else - { - substr($es,$e,0) = '.'; $cad = $MBI->_num($x->{_e}); - $cad = -$cad if $x->{_es} eq '-'; - } - } - elsif ($e > 0) - { - # expand with zeros - $es .= '0' x $e; $len += $e; $cad = 0; - } - } # if not zero - - $es = '-'.$es if $x->{sign} eq '-'; - # if set accuracy or precision, pad with zeros on the right side - if ((defined $x->{_a}) && ($not_zero)) - { - # 123400 => 6, 0.1234 => 4, 0.001234 => 4 - my $zeros = $x->{_a} - $cad; # cad == 0 => 12340 - $zeros = $x->{_a} - $len if $cad != $len; - $es .= $dot.'0' x $zeros if $zeros > 0; - } - elsif ((($x->{_p} || 0) < 0)) - { - # 123400 => 6, 0.1234 => 4, 0.001234 => 6 - my $zeros = -$x->{_p} + $cad; - $es .= $dot.'0' x $zeros if $zeros > 0; - } - $es; - } - -sub bsstr - { - # (ref to BFLOAT or num_str ) return num_str - # Convert number from internal format to scientific string format. - # internal format is always normalized (no leading zeros, "-0E0" => "+0E0") - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN - return 'inf'; # +inf - } - my $sep = 'e'.$x->{_es}; - my $sign = $x->{sign}; $sign = '' if $sign eq '+'; - $sign . $MBI->_str($x->{_m}) . $sep . $MBI->_str($x->{_e}); - } - -sub numify - { - # Convert a Perl scalar number from a BigFloat object. - # Create a string and let Perl's atoi()/atof() handle the rest. - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - return 0 + $x->bsstr(); - } - -############################################################################## -# public stuff (usually prefixed with "b") - -sub bneg - { - # (BINT or num_str) return BINT - # negate number or make a negated number from string - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x if $x->modify('bneg'); - - # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN' - $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})); - $x; - } - -# tels 2001-08-04 -# XXX TODO this must be overwritten and return NaN for non-integer values -# band(), bior(), bxor(), too -#sub bnot -# { -# $class->SUPER::bnot($class,@_); -# } - -sub bcmp - { - # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort) - - # set up parameters - my ($self,$x,$y) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y) = objectify(2,@_); - } - - return $upgrade->bcmp($x,$y) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - # Handle all 'nan' cases. - - return undef if ($x->{sign} eq $nan) || ($y->{sign} eq $nan); - - # Handle all '+inf' and '-inf' cases. - - return 0 if ($x->{sign} eq '+inf' && $y->{sign} eq '+inf' || - $x->{sign} eq '-inf' && $y->{sign} eq '-inf'); - return +1 if $x->{sign} eq '+inf'; # x = +inf and y < +inf - return -1 if $x->{sign} eq '-inf'; # x = -inf and y > -inf - return -1 if $y->{sign} eq '+inf'; # x < +inf and y = +inf - return +1 if $y->{sign} eq '-inf'; # x > -inf and y = -inf - - # Handle all cases with opposite signs. - - return +1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # also does 0 <=> -y - return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # also does -x <=> 0 - - # Handle all remaining zero cases. - - my $xz = $x->is_zero(); - my $yz = $y->is_zero(); - return 0 if $xz && $yz; # 0 <=> 0 - return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y - return +1 if $yz && $x->{sign} eq '+'; # +x <=> 0 - - # Both arguments are now finite, non-zero numbers with the same sign. - - my $cmp; - - # The next step is to compare the exponents, but since each mantissa is an - # integer of arbitrary value, the exponents must be normalized by the length - # of the mantissas before we can compare them. - - my $mxl = $MBI->_len($x->{_m}); - my $myl = $MBI->_len($y->{_m}); - - # If the mantissas have the same length, there is no point in normalizing the - # exponents by the length of the mantissas, so treat that as a special case. - - if ($mxl == $myl) { - - # First handle the two cases where the exponents have different signs. - - if ($x->{_es} eq '+' && $y->{_es} eq '-') { - $cmp = +1; - } - - elsif ($x->{_es} eq '-' && $y->{_es} eq '+') { - $cmp = -1; - } - - # Then handle the case where the exponents have the same sign. - - else { - $cmp = $MBI->_acmp($x->{_e}, $y->{_e}); - $cmp = -$cmp if $x->{_es} eq '-'; - } - - # Adjust for the sign, which is the same for x and y, and bail out if - # we're done. - - $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123 - return $cmp if $cmp; - - } - - # We must normalize each exponent by the length of the corresponding - # mantissa. Life is a lot easier if we first make both exponents - # non-negative. We do this by adding the same positive value to both - # exponent. This is safe, because when comparing the exponents, only the - # relative difference is important. - - my $ex; - my $ey; - - if ($x->{_es} eq '+') { - - # If the exponent of x is >= 0 and the exponent of y is >= 0, there is no - # need to do anything special. - - if ($y->{_es} eq '+') { - $ex = $MBI->_copy($x->{_e}); - $ey = $MBI->_copy($y->{_e}); - } - - # If the exponent of x is >= 0 and the exponent of y is < 0, add the - # absolute value of the exponent of y to both. - - else { - $ex = $MBI->_copy($x->{_e}); - $ex = $MBI->_add($ex, $y->{_e}); # ex + |ey| - $ey = $MBI->_zero(); # -ex + |ey| = 0 - } - - } else { - - # If the exponent of x is < 0 and the exponent of y is >= 0, add the - # absolute value of the exponent of x to both. - - if ($y->{_es} eq '+') { - $ex = $MBI->_zero(); # -ex + |ex| = 0 - $ey = $MBI->_copy($y->{_e}); - $ey = $MBI->_add($ey, $x->{_e}); # ey + |ex| - } - - # If the exponent of x is < 0 and the exponent of y is < 0, add the - # absolute values of both exponents to both exponents. - - else { - $ex = $MBI->_copy($y->{_e}); # -ex + |ey| + |ex| = |ey| - $ey = $MBI->_copy($x->{_e}); # -ey + |ex| + |ey| = |ex| - } - - } - - # Now we can normalize the exponents by adding lengths of the mantissas. - - $MBI->_add($ex, $MBI->_new($mxl)); - $MBI->_add($ey, $MBI->_new($myl)); - - # We're done if the exponents are different. - - $cmp = $MBI->_acmp($ex, $ey); - $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123 - return $cmp if $cmp; - - # Compare the mantissas, but first normalize them by padding the shorter - # mantissa with zeros (shift left) until it has the same length as the longer - # mantissa. - - my $mx = $x->{_m}; - my $my = $y->{_m}; - - if ($mxl > $myl) { - $my = $MBI->_lsft($MBI->_copy($my), $MBI->_new($mxl - $myl), 10); - } elsif ($mxl < $myl) { - $mx = $MBI->_lsft($MBI->_copy($mx), $MBI->_new($myl - $mxl), 10); - } - - $cmp = $MBI->_acmp($mx, $my); - $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123 - return $cmp; - - } - -sub bacmp - { - # Compares 2 values, ignoring their signs. - # Returns one of undef, <0, =0, >0. (suitable for sort) - - # set up parameters - my ($self,$x,$y) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y) = objectify(2,@_); - } - - return $upgrade->bacmp($x,$y) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - # handle +-inf and NaN's - if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) - { - return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - return 0 if ($x->is_inf() && $y->is_inf()); - return 1 if ($x->is_inf() && !$y->is_inf()); - return -1; - } - - # shortcut - my $xz = $x->is_zero(); - my $yz = $y->is_zero(); - return 0 if $xz && $yz; # 0 <=> 0 - return -1 if $xz && !$yz; # 0 <=> +y - return 1 if $yz && !$xz; # +x <=> 0 - - # adjust so that exponents are equal - my $lxm = $MBI->_len($x->{_m}); - my $lym = $MBI->_len($y->{_m}); - my ($xes,$yes) = (1,1); - $xes = -1 if $x->{_es} ne '+'; - $yes = -1 if $y->{_es} ne '+'; - # the numify somewhat limits our length, but makes it much faster - my $lx = $lxm + $xes * $MBI->_num($x->{_e}); - my $ly = $lym + $yes * $MBI->_num($y->{_e}); - my $l = $lx - $ly; - return $l <=> 0 if $l != 0; - - # lengths (corrected by exponent) are equal - # so make mantissa equal-length by padding with zero (shift left) - my $diff = $lxm - $lym; - my $xm = $x->{_m}; # not yet copy it - my $ym = $y->{_m}; - if ($diff > 0) - { - $ym = $MBI->_copy($y->{_m}); - $ym = $MBI->_lsft($ym, $MBI->_new($diff), 10); - } - elsif ($diff < 0) - { - $xm = $MBI->_copy($x->{_m}); - $xm = $MBI->_lsft($xm, $MBI->_new(-$diff), 10); - } - $MBI->_acmp($xm,$ym); - } - -sub badd - { - # add second arg (BFLOAT or string) to first (BFLOAT) (modifies first) - # return result as BFLOAT - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('badd'); - - # inf and NaN handling - if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) - { - # NaN first - return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - # inf handling - if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) - { - # +inf++inf or -inf+-inf => same, rest is NaN - return $x if $x->{sign} eq $y->{sign}; - return $x->bnan(); - } - # +-inf + something => +inf; something +-inf => +-inf - $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/; - return $x; - } - - return $upgrade->badd($x,$y,@r) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - $r[3] = $y; # no push! - - # speed: no add for 0+y or x+0 - return $x->bround(@r) if $y->is_zero(); # x+0 - if ($x->is_zero()) # 0+y - { - # make copy, clobbering up x (modify in place!) - $x->{_e} = $MBI->_copy($y->{_e}); - $x->{_es} = $y->{_es}; - $x->{_m} = $MBI->_copy($y->{_m}); - $x->{sign} = $y->{sign} || $nan; - return $x->round(@r); - } - - # take lower of the two e's and adapt m1 to it to match m2 - my $e = $y->{_e}; - $e = $MBI->_zero() if !defined $e; # if no BFLOAT? - $e = $MBI->_copy($e); # make copy (didn't do it yet) - - my $es; - - ($e,$es) = _e_sub($e, $x->{_e}, $y->{_es} || '+', $x->{_es}); - - my $add = $MBI->_copy($y->{_m}); - - if ($es eq '-') # < 0 - { - $MBI->_lsft( $x->{_m}, $e, 10); - ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es); - } - elsif (!$MBI->_is_zero($e)) # > 0 - { - $MBI->_lsft($add, $e, 10); - } - # else: both e are the same, so just leave them - - if ($x->{sign} eq $y->{sign}) - { - # add - $x->{_m} = $MBI->_add($x->{_m}, $add); - } - else - { - ($x->{_m}, $x->{sign}) = - _e_add($x->{_m}, $add, $x->{sign}, $y->{sign}); - } - - # delete trailing zeros, then round - $x->bnorm()->round(@r); - } - -# sub bsub is inherited from Math::BigInt! - -sub binc - { - # increment arg by one - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->modify('binc'); - - if ($x->{_es} eq '-') - { - return $x->badd($self->bone(),@r); # digits after dot - } - - if (!$MBI->_is_zero($x->{_e})) # _e == 0 for NaN, inf, -inf - { - # 1e2 => 100, so after the shift below _m has a '0' as last digit - $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100 - $x->{_e} = $MBI->_zero(); # normalize - $x->{_es} = '+'; - # we know that the last digit of $x will be '1' or '9', depending on the - # sign - } - # now $x->{_e} == 0 - if ($x->{sign} eq '+') - { - $MBI->_inc($x->{_m}); - return $x->bnorm()->bround(@r); - } - elsif ($x->{sign} eq '-') - { - $MBI->_dec($x->{_m}); - $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0 - return $x->bnorm()->bround(@r); - } - # inf, nan handling etc - $x->badd($self->bone(),@r); # badd() does round - } - -sub bdec - { - # decrement arg by one - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->modify('bdec'); - - if ($x->{_es} eq '-') - { - return $x->badd($self->bone('-'),@r); # digits after dot - } - - if (!$MBI->_is_zero($x->{_e})) - { - $x->{_m} = $MBI->_lsft($x->{_m}, $x->{_e},10); # 1e2 => 100 - $x->{_e} = $MBI->_zero(); # normalize - $x->{_es} = '+'; - } - # now $x->{_e} == 0 - my $zero = $x->is_zero(); - # <= 0 - if (($x->{sign} eq '-') || $zero) - { - $MBI->_inc($x->{_m}); - $x->{sign} = '-' if $zero; # 0 => 1 => -1 - $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # -1 +1 => -0 => +0 - return $x->bnorm()->round(@r); - } - # > 0 - elsif ($x->{sign} eq '+') - { - $MBI->_dec($x->{_m}); - return $x->bnorm()->round(@r); - } - # inf, nan handling etc - $x->badd($self->bone('-'),@r); # does round - } - -sub DEBUG () { 0; } - -sub blog - { - my ($self,$x,$base,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->modify('blog'); - - # $base > 0, $base != 1; if $base == undef default to $base == e - # $x >= 0 - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my ($scale,@params); - ($x,@params) = $x->_find_round_parameters($a,$p,$r); - - # also takes care of the "error in _find_round_parameters?" case - return $x->bnan() if $x->{sign} ne '+' || $x->is_zero(); - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $params[1] = undef; # P = undef - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - return $x->bzero(@params) if $x->is_one(); - # base not defined => base == Euler's number e - if (defined $base) - { - # make object, since we don't feed it through objectify() to still get the - # case of $base == undef - $base = $self->new($base) unless ref($base); - # $base > 0; $base != 1 - return $x->bnan() if $base->is_zero() || $base->is_one() || - $base->{sign} ne '+'; - # if $x == $base, we know the result must be 1.0 - if ($x->bcmp($base) == 0) - { - $x->bone('+',@params); - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - return $x; - } - } - - # when user set globals, they would interfere with our calculation, so - # disable them and later re-enable them - no strict 'refs'; - my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; - my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; - # we also need to disable any set A or P on $x (_find_round_parameters took - # them already into account), since these would interfere, too - delete $x->{_a}; delete $x->{_p}; - # need to disable $upgrade in BigInt, to avoid deep recursion - local $Math::BigInt::upgrade = undef; - local $Math::BigFloat::downgrade = undef; - - # upgrade $x if $x is not a BigFloat (handle BigInt input) - # XXX TODO: rebless! - if (!$x->isa('Math::BigFloat')) - { - $x = Math::BigFloat->new($x); - $self = ref($x); - } - - my $done = 0; - - # If the base is defined and an integer, try to calculate integer result - # first. This is very fast, and in case the real result was found, we can - # stop right here. - if (defined $base && $base->is_int() && $x->is_int()) - { - my $i = $MBI->_copy( $x->{_m} ); - $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); - my $int = Math::BigInt->bzero(); - $int->{value} = $i; - $int->blog($base->as_number()); - # if ($exact) - if ($base->as_number()->bpow($int) == $x) - { - # found result, return it - $x->{_m} = $int->{value}; - $x->{_e} = $MBI->_zero(); - $x->{_es} = '+'; - $x->bnorm(); - $done = 1; - } - } - - if ($done == 0) - { - # base is undef, so base should be e (Euler's number), so first calculate the - # log to base e (using reduction by 10 (and probably 2)): - $self->_log_10($x,$scale); - - # and if a different base was requested, convert it - if (defined $base) - { - $base = Math::BigFloat->new($base) unless $base->isa('Math::BigFloat'); - # not ln, but some other base (don't modify $base) - $x->bdiv( $base->copy()->blog(undef,$scale), $scale ); - } - } - - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # restore globals - $$abr = $ab; $$pbr = $pb; - - $x; - } - -sub _len_to_steps - { - # Given D (digits in decimal), compute N so that N! (N factorial) is - # at least D digits long. D should be at least 50. - my $d = shift; - - # two constants for the Ramanujan estimate of ln(N!) - my $lg2 = log(2 * 3.14159265) / 2; - my $lg10 = log(10); - - # D = 50 => N => 42, so L = 40 and R = 50 - my $l = 40; my $r = $d; - - # Otherwise this does not work under -Mbignum and we do not yet have "no bignum;" :( - $l = $l->numify if ref($l); - $r = $r->numify if ref($r); - $lg2 = $lg2->numify if ref($lg2); - $lg10 = $lg10->numify if ref($lg10); - - # binary search for the right value (could this be written as the reverse of lg(n!)?) - while ($r - $l > 1) - { - my $n = int(($r - $l) / 2) + $l; - my $ramanujan = - int(($n * log($n) - $n + log( $n * (1 + 4*$n*(1+2*$n)) ) / 6 + $lg2) / $lg10); - $ramanujan > $d ? $r = $n : $l = $n; - } - $l; - } - -sub bnok - { - # Calculate n over k (binomial coefficient or "choose" function) as integer. - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bnok'); - - return $x->bnan() if $x->is_nan() || $y->is_nan(); - return $x->binf() if $x->is_inf(); - - my $u = $x->as_int(); - $u->bnok($y->as_int()); - - $x->{_m} = $u->{value}; - $x->{_e} = $MBI->_zero(); - $x->{_es} = '+'; - $x->{sign} = '+'; - $x->bnorm(@r); - } - -sub bexp - { - # Calculate e ** X (Euler's number to the power of X) - my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->modify('bexp'); - - return $x->binf() if $x->{sign} eq '+inf'; - return $x->bzero() if $x->{sign} eq '-inf'; - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my ($scale,@params); - ($x,@params) = $x->_find_round_parameters($a,$p,$r); - - # also takes care of the "error in _find_round_parameters?" case - return $x if $x->{sign} eq 'NaN'; - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $params[1] = undef; # P = undef - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it's not enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - return $x->bone(@params) if $x->is_zero(); - - if (!$x->isa('Math::BigFloat')) - { - $x = Math::BigFloat->new($x); - $self = ref($x); - } - - # when user set globals, they would interfere with our calculation, so - # disable them and later re-enable them - no strict 'refs'; - my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; - my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; - # we also need to disable any set A or P on $x (_find_round_parameters took - # them already into account), since these would interfere, too - delete $x->{_a}; delete $x->{_p}; - # need to disable $upgrade in BigInt, to avoid deep recursion - local $Math::BigInt::upgrade = undef; - local $Math::BigFloat::downgrade = undef; - - my $x_org = $x->copy(); - - # We use the following Taylor series: - - # x x^2 x^3 x^4 - # e = 1 + --- + --- + --- + --- ... - # 1! 2! 3! 4! - - # The difference for each term is X and N, which would result in: - # 2 copy, 2 mul, 2 add, 1 inc, 1 div operations per term - - # But it is faster to compute exp(1) and then raising it to the - # given power, esp. if $x is really big and an integer because: - - # * The numerator is always 1, making the computation faster - # * the series converges faster in the case of x == 1 - # * We can also easily check when we have reached our limit: when the - # term to be added is smaller than "1E$scale", we can stop - f.i. - # scale == 5, and we have 1/40320, then we stop since 1/40320 < 1E-5. - # * we can compute the *exact* result by simulating bigrat math: - - # 1 1 gcd(3,4) = 1 1*24 + 1*6 5 - # - + - = ---------- = -- - # 6 24 6*24 24 - - # We do not compute the gcd() here, but simple do: - # 1 1 1*24 + 1*6 30 - # - + - = --------- = -- - # 6 24 6*24 144 - - # In general: - # a c a*d + c*b and note that c is always 1 and d = (b*f) - # - + - = --------- - # b d b*d - - # This leads to: which can be reduced by b to: - # a 1 a*b*f + b a*f + 1 - # - + - = --------- = ------- - # b b*f b*b*f b*f - - # The first terms in the series are: - - # 1 1 1 1 1 1 1 1 13700 - # -- + -- + -- + -- + -- + --- + --- + ---- = ----- - # 1 1 2 6 24 120 720 5040 5040 - - # Note that we cannot simple reduce 13700/5040 to 685/252, but must keep A and B! - - if ($scale <= 75) - { - # set $x directly from a cached string form - $x->{_m} = $MBI->_new( - "27182818284590452353602874713526624977572470936999595749669676277240766303535476"); - $x->{sign} = '+'; - $x->{_es} = '-'; - $x->{_e} = $MBI->_new(79); - } - else - { - # compute A and B so that e = A / B. - - # After some terms we end up with this, so we use it as a starting point: - my $A = $MBI->_new("90933395208605785401971970164779391644753259799242"); - my $F = $MBI->_new(42); my $step = 42; - - # Compute how many steps we need to take to get $A and $B sufficiently big - my $steps = _len_to_steps($scale - 4); -# print STDERR "# Doing $steps steps for ", $scale-4, " digits\n"; - while ($step++ <= $steps) - { - # calculate $a * $f + 1 - $A = $MBI->_mul($A, $F); - $A = $MBI->_inc($A); - # increment f - $F = $MBI->_inc($F); - } - # compute $B as factorial of $steps (this is faster than doing it manually) - my $B = $MBI->_fac($MBI->_new($steps)); - -# print "A ", $MBI->_str($A), "\nB ", $MBI->_str($B), "\n"; - - # compute A/B with $scale digits in the result (truncate, not round) - $A = $MBI->_lsft( $A, $MBI->_new($scale), 10); - $A = $MBI->_div( $A, $B ); - - $x->{_m} = $A; - $x->{sign} = '+'; - $x->{_es} = '-'; - $x->{_e} = $MBI->_new($scale); - } - - # $x contains now an estimate of e, with some surplus digits, so we can round - if (!$x_org->is_one()) - { - # raise $x to the wanted power and round it in one step: - $x->bpow($x_org, @params); - } - else - { - # else just round the already computed result - delete $x->{_a}; delete $x->{_p}; - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # restore globals - $$abr = $ab; $$pbr = $pb; - - $x; # return modified $x - } - -sub _log - { - # internal log function to calculate ln() based on Taylor series. - # Modifies $x in place. - my ($self,$x,$scale) = @_; - - # in case of $x == 1, result is 0 - return $x->bzero() if $x->is_one(); - - # XXX TODO: rewrite this in a similar manner to bexp() - - # http://www.efunda.com/math/taylor_series/logarithmic.cfm?search_string=log - - # u = x-1, v = x+1 - # _ _ - # Taylor: | u 1 u^3 1 u^5 | - # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 0 - # |_ v 3 v^3 5 v^5 _| - - # This takes much more steps to calculate the result and is thus not used - # u = x-1 - # _ _ - # Taylor: | u 1 u^2 1 u^3 | - # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 1/2 - # |_ x 2 x^2 3 x^3 _| - - my ($limit,$v,$u,$below,$factor,$two,$next,$over,$f); - - $v = $x->copy(); $v->binc(); # v = x+1 - $x->bdec(); $u = $x->copy(); # u = x-1; x = x-1 - $x->bdiv($v,$scale); # first term: u/v - $below = $v->copy(); - $over = $u->copy(); - $u *= $u; $v *= $v; # u^2, v^2 - $below->bmul($v); # u^3, v^3 - $over->bmul($u); - $factor = $self->new(3); $f = $self->new(2); - - my $steps = 0 if DEBUG; - $limit = $self->new("1E-". ($scale-1)); - while (3 < 5) - { - # we calculate the next term, and add it to the last - # when the next term is below our limit, it won't affect the outcome - # anymore, so we stop - - # calculating the next term simple from over/below will result in quite - # a time hog if the input has many digits, since over and below will - # accumulate more and more digits, and the result will also have many - # digits, but in the end it is rounded to $scale digits anyway. So if we - # round $over and $below first, we save a lot of time for the division - # (not with log(1.2345), but try log (123**123) to see what I mean. This - # can introduce a rounding error if the division result would be f.i. - # 0.1234500000001 and we round it to 5 digits it would become 0.12346, but - # if we truncated $over and $below we might get 0.12345. Does this matter - # for the end result? So we give $over and $below 4 more digits to be - # on the safe side (unscientific error handling as usual... :+D - - $next = $over->copy->bround($scale+4)->bdiv( - $below->copy->bmul($factor)->bround($scale+4), - $scale); - -## old version: -## $next = $over->copy()->bdiv($below->copy()->bmul($factor),$scale); - - last if $next->bacmp($limit) <= 0; - - delete $next->{_a}; delete $next->{_p}; - $x->badd($next); - # calculate things for the next term - $over *= $u; $below *= $v; $factor->badd($f); - if (DEBUG) - { - $steps++; print "step $steps = $x\n" if $steps % 10 == 0; - } - } - print "took $steps steps\n" if DEBUG; - $x->bmul($f); # $x *= 2 - } - -sub _log_10 - { - # Internal log function based on reducing input to the range of 0.1 .. 9.99 - # and then "correcting" the result to the proper one. Modifies $x in place. - my ($self,$x,$scale) = @_; - - # Taking blog() from numbers greater than 10 takes a *very long* time, so we - # break the computation down into parts based on the observation that: - # blog(X*Y) = blog(X) + blog(Y) - # We set Y here to multiples of 10 so that $x becomes below 1 - the smaller - # $x is the faster it gets. Since 2*$x takes about 10 times as - # long, we make it faster by about a factor of 100 by dividing $x by 10. - - # The same observation is valid for numbers smaller than 0.1, e.g. computing - # log(1) is fastest, and the further away we get from 1, the longer it takes. - # So we also 'break' this down by multiplying $x with 10 and subtract the - # log(10) afterwards to get the correct result. - - # To get $x even closer to 1, we also divide by 2 and then use log(2) to - # correct for this. For instance if $x is 2.4, we use the formula: - # blog(2.4 * 2) == blog (1.2) + blog(2) - # and thus calculate only blog(1.2) and blog(2), which is faster in total - # than calculating blog(2.4). - - # In addition, the values for blog(2) and blog(10) are cached. - - # Calculate nr of digits before dot: - my $dbd = $MBI->_num($x->{_e}); - $dbd = -$dbd if $x->{_es} eq '-'; - $dbd += $MBI->_len($x->{_m}); - - # more than one digit (e.g. at least 10), but *not* exactly 10 to avoid - # infinite recursion - - my $calc = 1; # do some calculation? - - # disable the shortcut for 10, since we need log(10) and this would recurse - # infinitely deep - if ($x->{_es} eq '+' && $MBI->_is_one($x->{_e}) && $MBI->_is_one($x->{_m})) - { - $dbd = 0; # disable shortcut - # we can use the cached value in these cases - if ($scale <= $LOG_10_A) - { - $x->bzero(); $x->badd($LOG_10); # modify $x in place - $calc = 0; # no need to calc, but round - } - # if we can't use the shortcut, we continue normally - } - else - { - # disable the shortcut for 2, since we maybe have it cached - if (($MBI->_is_zero($x->{_e}) && $MBI->_is_two($x->{_m}))) - { - $dbd = 0; # disable shortcut - # we can use the cached value in these cases - if ($scale <= $LOG_2_A) - { - $x->bzero(); $x->badd($LOG_2); # modify $x in place - $calc = 0; # no need to calc, but round - } - # if we can't use the shortcut, we continue normally - } - } - - # if $x = 0.1, we know the result must be 0-log(10) - if ($calc != 0 && $x->{_es} eq '-' && $MBI->_is_one($x->{_e}) && - $MBI->_is_one($x->{_m})) - { - $dbd = 0; # disable shortcut - # we can use the cached value in these cases - if ($scale <= $LOG_10_A) - { - $x->bzero(); $x->bsub($LOG_10); - $calc = 0; # no need to calc, but round - } - } - - return if $calc == 0; # already have the result - - # default: these correction factors are undef and thus not used - my $l_10; # value of ln(10) to A of $scale - my $l_2; # value of ln(2) to A of $scale - - my $two = $self->new(2); - - # $x == 2 => 1, $x == 13 => 2, $x == 0.1 => 0, $x == 0.01 => -1 - # so don't do this shortcut for 1 or 0 - if (($dbd > 1) || ($dbd < 0)) - { - # convert our cached value to an object if not already (avoid doing this - # at import() time, since not everybody needs this) - $LOG_10 = $self->new($LOG_10,undef,undef) unless ref $LOG_10; - - #print "x = $x, dbd = $dbd, calc = $calc\n"; - # got more than one digit before the dot, or more than one zero after the - # dot, so do: - # log(123) == log(1.23) + log(10) * 2 - # log(0.0123) == log(1.23) - log(10) * 2 - - if ($scale <= $LOG_10_A) - { - # use cached value - $l_10 = $LOG_10->copy(); # copy for mul - } - else - { - # else: slower, compute and cache result - # also disable downgrade for this code path - local $Math::BigFloat::downgrade = undef; - - # shorten the time to calculate log(10) based on the following: - # log(1.25 * 8) = log(1.25) + log(8) - # = log(1.25) + log(2) + log(2) + log(2) - - # first get $l_2 (and possible compute and cache log(2)) - $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2; - if ($scale <= $LOG_2_A) - { - # use cached value - $l_2 = $LOG_2->copy(); # copy() for the mul below - } - else - { - # else: slower, compute and cache result - $l_2 = $two->copy(); $self->_log($l_2, $scale); # scale+4, actually - $LOG_2 = $l_2->copy(); # cache the result for later - # the copy() is for mul below - $LOG_2_A = $scale; - } - - # now calculate log(1.25): - $l_10 = $self->new('1.25'); $self->_log($l_10, $scale); # scale+4, actually - - # log(1.25) + log(2) + log(2) + log(2): - $l_10->badd($l_2); - $l_10->badd($l_2); - $l_10->badd($l_2); - $LOG_10 = $l_10->copy(); # cache the result for later - # the copy() is for mul below - $LOG_10_A = $scale; - } - $dbd-- if ($dbd > 1); # 20 => dbd=2, so make it dbd=1 - $l_10->bmul( $self->new($dbd)); # log(10) * (digits_before_dot-1) - my $dbd_sign = '+'; - if ($dbd < 0) - { - $dbd = -$dbd; - $dbd_sign = '-'; - } - ($x->{_e}, $x->{_es}) = - _e_sub( $x->{_e}, $MBI->_new($dbd), $x->{_es}, $dbd_sign); # 123 => 1.23 - - } - - # Now: 0.1 <= $x < 10 (and possible correction in l_10) - - ### Since $x in the range 0.5 .. 1.5 is MUCH faster, we do a repeated div - ### or mul by 2 (maximum times 3, since x < 10 and x > 0.1) - - $HALF = $self->new($HALF) unless ref($HALF); - - my $twos = 0; # default: none (0 times) - while ($x->bacmp($HALF) <= 0) # X <= 0.5 - { - $twos--; $x->bmul($two); - } - while ($x->bacmp($two) >= 0) # X >= 2 - { - $twos++; $x->bdiv($two,$scale+4); # keep all digits - } - $x->bround($scale+4); - # $twos > 0 => did mul 2, < 0 => did div 2 (but we never did both) - # So calculate correction factor based on ln(2): - if ($twos != 0) - { - $LOG_2 = $self->new($LOG_2,undef,undef) unless ref $LOG_2; - if ($scale <= $LOG_2_A) - { - # use cached value - $l_2 = $LOG_2->copy(); # copy() for the mul below - } - else - { - # else: slower, compute and cache result - # also disable downgrade for this code path - local $Math::BigFloat::downgrade = undef; - $l_2 = $two->copy(); $self->_log($l_2, $scale); # scale+4, actually - $LOG_2 = $l_2->copy(); # cache the result for later - # the copy() is for mul below - $LOG_2_A = $scale; - } - $l_2->bmul($twos); # * -2 => subtract, * 2 => add - } - else - { - undef $l_2; - } - - $self->_log($x,$scale); # need to do the "normal" way - $x->badd($l_10) if defined $l_10; # correct it by ln(10) - $x->badd($l_2) if defined $l_2; # and maybe by ln(2) - - # all done, $x contains now the result - $x; - } - -sub blcm - { - # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT - # does not modify arguments, but returns new object - # Lowest Common Multiplicator - - my ($self,@arg) = objectify(0,@_); - my $x = $self->new(shift @arg); - while (@arg) { $x = Math::BigInt::__lcm($x,shift @arg); } - $x; - } - -sub bgcd - { - # (BINT or num_str, BINT or num_str) return BINT - # does not modify arguments, but returns new object - - my $y = shift; - $y = __PACKAGE__->new($y) if !ref($y); - my $self = ref($y); - my $x = $y->copy()->babs(); # keep arguments - - return $x->bnan() if $x->{sign} !~ /^[+-]$/ # x NaN? - || !$x->is_int(); # only for integers now - - while (@_) - { - my $t = shift; $t = $self->new($t) if !ref($t); - $y = $t->copy()->babs(); - - return $x->bnan() if $y->{sign} !~ /^[+-]$/ # y NaN? - || !$y->is_int(); # only for integers now - - # greatest common divisor - while (! $y->is_zero()) - { - ($x,$y) = ($y->copy(), $x->copy()->bmod($y)); - } - - last if $x->is_one(); - } - $x; - } - -############################################################################## - -sub _e_add - { - # Internal helper sub to take two positive integers and their signs and - # then add them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')), - # output ($CALC,('+'|'-')) - my ($x,$y,$xs,$ys) = @_; - - # if the signs are equal we can add them (-5 + -3 => -(5 + 3) => -8) - if ($xs eq $ys) - { - $x = $MBI->_add ($x, $y ); # a+b - # the sign follows $xs - return ($x, $xs); - } - - my $a = $MBI->_acmp($x,$y); - if ($a > 0) - { - $x = $MBI->_sub ($x , $y); # abs sub - } - elsif ($a == 0) - { - $x = $MBI->_zero(); # result is 0 - $xs = '+'; - } - else # a < 0 - { - $x = $MBI->_sub ( $y, $x, 1 ); # abs sub - $xs = $ys; - } - ($x,$xs); - } - -sub _e_sub - { - # Internal helper sub to take two positive integers and their signs and - # then subtract them. Input ($CALC,$CALC,('+'|'-'),('+'|'-')), - # output ($CALC,('+'|'-')) - my ($x,$y,$xs,$ys) = @_; - - # flip sign - $ys =~ tr/+-/-+/; - _e_add($x,$y,$xs,$ys); # call add (does subtract now) - } - -############################################################################### -# is_foo methods (is_negative, is_positive are inherited from BigInt) - -sub is_int - { - # return true if arg (BFLOAT or num_str) is an integer - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - (($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't - ($x->{_es} eq '+')) ? 1 : 0; # 1e-1 => no integer - } - -sub is_zero - { - # return true if arg (BFLOAT or num_str) is zero - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - ($x->{sign} eq '+' && $MBI->_is_zero($x->{_m})) ? 1 : 0; - } - -sub is_one - { - # return true if arg (BFLOAT or num_str) is +1 or -1 if signis given - my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - $sign = '+' if !defined $sign || $sign ne '-'; - - ($x->{sign} eq $sign && - $MBI->_is_zero($x->{_e}) && - $MBI->_is_one($x->{_m}) ) ? 1 : 0; - } - -sub is_odd - { - # return true if arg (BFLOAT or num_str) is odd or false if even - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't - ($MBI->_is_zero($x->{_e})) && - ($MBI->_is_odd($x->{_m}))) ? 1 : 0; - } - -sub is_even - { - # return true if arg (BINT or num_str) is even or false if odd - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't - ($x->{_es} eq '+') && # 123.45 isn't - ($MBI->_is_even($x->{_m}))) ? 1 : 0; # but 1200 is - } - -sub bmul - { - # multiply two numbers - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bmul'); - - return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - - # inf handling - if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) - { - return $x->bnan() if $x->is_zero() || $y->is_zero(); - # result will always be +-inf: - # +inf * +/+inf => +inf, -inf * -/-inf => +inf - # +inf * -/-inf => -inf, -inf * +/+inf => -inf - return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); - return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); - return $x->binf('-'); - } - - return $upgrade->bmul($x,$y,@r) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - # aEb * cEd = (a*c)E(b+d) - $MBI->_mul($x->{_m},$y->{_m}); - ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); - - $r[3] = $y; # no push! - - # adjust sign: - $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+'; - $x->bnorm->round(@r); - } - -sub bmuladd - { - # multiply two numbers and add the third to the result - - # set up parameters - my ($self,$x,$y,$z,@r) = objectify(3,@_); - - return $x if $x->modify('bmuladd'); - - return $x->bnan() if (($x->{sign} eq $nan) || - ($y->{sign} eq $nan) || - ($z->{sign} eq $nan)); - - # inf handling - if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) - { - return $x->bnan() if $x->is_zero() || $y->is_zero(); - # result will always be +-inf: - # +inf * +/+inf => +inf, -inf * -/-inf => +inf - # +inf * -/-inf => -inf, -inf * +/+inf => -inf - return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); - return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); - return $x->binf('-'); - } - - return $upgrade->bmul($x,$y,@r) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - # aEb * cEd = (a*c)E(b+d) - $MBI->_mul($x->{_m},$y->{_m}); - ($x->{_e}, $x->{_es}) = _e_add($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); - - $r[3] = $y; # no push! - - # adjust sign: - $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+'; - - # z=inf handling (z=NaN handled above) - $x->{sign} = $z->{sign}, return $x if $z->{sign} =~ /^[+-]inf$/; - - # take lower of the two e's and adapt m1 to it to match m2 - my $e = $z->{_e}; - $e = $MBI->_zero() if !defined $e; # if no BFLOAT? - $e = $MBI->_copy($e); # make copy (didn't do it yet) - - my $es; - - ($e,$es) = _e_sub($e, $x->{_e}, $z->{_es} || '+', $x->{_es}); - - my $add = $MBI->_copy($z->{_m}); - - if ($es eq '-') # < 0 - { - $MBI->_lsft( $x->{_m}, $e, 10); - ($x->{_e},$x->{_es}) = _e_add($x->{_e}, $e, $x->{_es}, $es); - } - elsif (!$MBI->_is_zero($e)) # > 0 - { - $MBI->_lsft($add, $e, 10); - } - # else: both e are the same, so just leave them - - if ($x->{sign} eq $z->{sign}) - { - # add - $x->{_m} = $MBI->_add($x->{_m}, $add); - } - else - { - ($x->{_m}, $x->{sign}) = - _e_add($x->{_m}, $add, $x->{sign}, $z->{sign}); - } - - # delete trailing zeros, then round - $x->bnorm()->round(@r); - } - -sub bdiv - { - # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return - # (BFLOAT, BFLOAT) (quo, rem) or BFLOAT (only quo) - - # set up parameters - my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$a,$p,$r) = objectify(2,@_); - } - - return $x if $x->modify('bdiv'); - - my $wantarray = wantarray; # call only once - - # At least one argument is NaN. This is handled the same way as in - # Math::BigInt -> bdiv(). - - if ($x -> is_nan() || $y -> is_nan()) { - return $wantarray ? ($x -> bnan(), $self -> bnan()) : $x -> bnan(); - } - - # Divide by zero and modulo zero. This is handled the same way as in - # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt -> - # bdiv() for further details. - - if ($y -> is_zero()) { - my ($quo, $rem); - if ($wantarray) { - $rem = $x -> copy(); - } - if ($x -> is_zero()) { - $quo = $x -> bnan(); - } else { - $quo = $x -> binf($x -> {sign}); - } - return $wantarray ? ($quo, $rem) : $quo; - } - - # Numerator (dividend) is +/-inf. This is handled the same way as in - # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt -> - # bdiv() for further details. - - if ($x -> is_inf()) { - my ($quo, $rem); - $rem = $self -> bnan() if $wantarray; - if ($y -> is_inf()) { - $quo = $x -> bnan(); - } else { - my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-'; - $quo = $x -> binf($sign); - } - return $wantarray ? ($quo, $rem) : $quo; - } - - # Denominator (divisor) is +/-inf. This is handled the same way as in - # Math::BigInt -> bdiv(), with one exception: In scalar context, - # Math::BigFloat does true division (although rounded), not floored division - # (F-division), so a finite number divided by +/-inf is always zero. See the - # comment in the code for Math::BigInt -> bdiv() for further details. - - if ($y -> is_inf()) { - my ($quo, $rem); - if ($wantarray) { - if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { - $rem = $x -> copy(); - $quo = $x -> bzero(); - } else { - $rem = $self -> binf($y -> {sign}); - $quo = $x -> bone('-'); - } - return ($quo, $rem); - } else { - if ($y -> is_inf()) { - if ($x -> is_nan() || $x -> is_inf()) { - return $x -> bnan(); - } else { - return $x -> bzero(); - } - } - } - } - - # At this point, both the numerator and denominator are finite numbers, and - # the denominator (divisor) is non-zero. - - # x == 0? - return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); - - # upgrade ? - return $upgrade->bdiv($upgrade->new($x),$y,$a,$p,$r) if defined $upgrade; - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my (@params,$scale); - ($x,@params) = $x->_find_round_parameters($a,$p,$r,$y); - - return $x if $x->is_nan(); # error in _find_round_parameters? - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } else { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - my $rem; - $rem = $self -> bzero() if wantarray; - - $y = $self->new($y) unless $y->isa('Math::BigFloat'); - - my $lx = $MBI -> _len($x->{_m}); my $ly = $MBI -> _len($y->{_m}); - $scale = $lx if $lx > $scale; - $scale = $ly if $ly > $scale; - my $diff = $ly - $lx; - $scale += $diff if $diff > 0; # if lx << ly, but not if ly << lx! - - # check that $y is not 1 nor -1 and cache the result: - my $y_not_one = !($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m})); - - # flipping the sign of $y will also flip the sign of $x for the special - # case of $x->bsub($x); so we can catch it below: - my $xsign = $x->{sign}; - $y->{sign} =~ tr/+-/-+/; - - if ($xsign ne $x->{sign}) - { - # special case of $x /= $x results in 1 - $x->bone(); # "fixes" also sign of $y, since $x is $y - } - else - { - # correct $y's sign again - $y->{sign} =~ tr/+-/-+/; - # continue with normal div code: - - # make copy of $x in case of list context for later remainder calculation - if (wantarray && $y_not_one) - { - $rem = $x->copy(); - } - - $x->{sign} = $x->{sign} ne $y->sign() ? '-' : '+'; - - # check for / +-1 ( +/- 1E0) - if ($y_not_one) - { - # promote BigInts and it's subclasses (except when already a BigFloat) - $y = $self->new($y) unless $y->isa('Math::BigFloat'); - - # calculate the result to $scale digits and then round it - # a * 10 ** b / c * 10 ** d => a/c * 10 ** (b-d) - $MBI->_lsft($x->{_m},$MBI->_new($scale),10); - $MBI->_div ($x->{_m},$y->{_m}); # a/c - - # correct exponent of $x - ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $y->{_e}, $x->{_es}, $y->{_es}); - # correct for 10**scale - ($x->{_e},$x->{_es}) = _e_sub($x->{_e}, $MBI->_new($scale), $x->{_es}, '+'); - $x->bnorm(); # remove trailing 0's - } - } # end else $x != $y - - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - delete $x->{_a}; # clear before round - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - delete $x->{_p}; # clear before round - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - - if (wantarray) - { - if ($y_not_one) - { - $x -> bfloor(); - $rem->bmod($y,@params); # copy already done - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $rem->{_a}; delete $rem->{_p}; - } - return ($x,$rem); - } - $x; - } - -sub bmod - { - # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return remainder - - # set up parameters - my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$a,$p,$r) = objectify(2,@_); - } - - return $x if $x->modify('bmod'); - - # At least one argument is NaN. This is handled the same way as in - # Math::BigInt -> bmod(). - - if ($x -> is_nan() || $y -> is_nan()) { - return $x -> bnan(); - } - - # Modulo zero. This is handled the same way as in Math::BigInt -> bmod(). - - if ($y -> is_zero()) { - return $x; - } - - # Numerator (dividend) is +/-inf. This is handled the same way as in - # Math::BigInt -> bmod(). - - if ($x -> is_inf()) { - return $x -> bnan(); - } - - # Denominator (divisor) is +/-inf. This is handled the same way as in - # Math::BigInt -> bmod(). - - if ($y -> is_inf()) { - if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { - return $x; - } else { - return $x -> binf($y -> sign()); - } - } - - return $x->bzero() if $x->is_zero() - || ($x->is_int() && - # check that $y == +1 or $y == -1: - ($MBI->_is_zero($y->{_e}) && $MBI->_is_one($y->{_m}))); - - my $cmp = $x->bacmp($y); # equal or $x < $y? - if ($cmp == 0) { # $x == $y => result 0 - return $x -> bzero($a, $p); - } - - # only $y of the operands negative? - my $neg = $x->{sign} ne $y->{sign} ? 1 : 0; - - $x->{sign} = $y->{sign}; # calc sign first - if ($cmp < 0 && $neg == 0) { # $x < $y => result $x - return $x -> round($a, $p, $r); - } - - my $ym = $MBI->_copy($y->{_m}); - - # 2e1 => 20 - $MBI->_lsft( $ym, $y->{_e}, 10) - if $y->{_es} eq '+' && !$MBI->_is_zero($y->{_e}); - - # if $y has digits after dot - my $shifty = 0; # correct _e of $x by this - if ($y->{_es} eq '-') # has digits after dot - { - # 123 % 2.5 => 1230 % 25 => 5 => 0.5 - $shifty = $MBI->_num($y->{_e}); # no more digits after dot - $MBI->_lsft($x->{_m}, $y->{_e}, 10);# 123 => 1230, $y->{_m} is already 25 - } - # $ym is now mantissa of $y based on exponent 0 - - my $shiftx = 0; # correct _e of $x by this - if ($x->{_es} eq '-') # has digits after dot - { - # 123.4 % 20 => 1234 % 200 - $shiftx = $MBI->_num($x->{_e}); # no more digits after dot - $MBI->_lsft($ym, $x->{_e}, 10); # 123 => 1230 - } - # 123e1 % 20 => 1230 % 20 - if ($x->{_es} eq '+' && !$MBI->_is_zero($x->{_e})) - { - $MBI->_lsft( $x->{_m}, $x->{_e},10); # es => '+' here - } - - $x->{_e} = $MBI->_new($shiftx); - $x->{_es} = '+'; - $x->{_es} = '-' if $shiftx != 0 || $shifty != 0; - $MBI->_add( $x->{_e}, $MBI->_new($shifty)) if $shifty != 0; - - # now mantissas are equalized, exponent of $x is adjusted, so calc result - - $x->{_m} = $MBI->_mod( $x->{_m}, $ym); - - $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0 - $x->bnorm(); - - if ($neg != 0 && ! $x -> is_zero()) # one of them negative => correct in place - { - my $r = $y - $x; - $x->{_m} = $r->{_m}; - $x->{_e} = $r->{_e}; - $x->{_es} = $r->{_es}; - $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # fix sign for -0 - $x->bnorm(); - } - - $x->round($a,$p,$r,$y); # round and return - } - -sub broot - { - # calculate $y'th root of $x - - # set up parameters - my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$a,$p,$r) = objectify(2,@_); - } - - return $x if $x->modify('broot'); - - # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0 - return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() || - $y->{sign} !~ /^\+$/; - - return $x if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one(); - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my (@params,$scale); - ($x,@params) = $x->_find_round_parameters($a,$p,$r); - - return $x if $x->is_nan(); # error in _find_round_parameters? - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - # when user set globals, they would interfere with our calculation, so - # disable them and later re-enable them - no strict 'refs'; - my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; - my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; - # we also need to disable any set A or P on $x (_find_round_parameters took - # them already into account), since these would interfere, too - delete $x->{_a}; delete $x->{_p}; - # need to disable $upgrade in BigInt, to avoid deep recursion - local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI - - # remember sign and make $x positive, since -4 ** (1/2) => -2 - my $sign = 0; $sign = 1 if $x->{sign} eq '-'; $x->{sign} = '+'; - - my $is_two = 0; - if ($y->isa('Math::BigFloat')) - { - $is_two = ($y->{sign} eq '+' && $MBI->_is_two($y->{_m}) && $MBI->_is_zero($y->{_e})); - } - else - { - $is_two = ($y == 2); - } - - # normal square root if $y == 2: - if ($is_two) - { - $x->bsqrt($scale+4); - } - elsif ($y->is_one('-')) - { - # $x ** -1 => 1/$x - my $u = $self->bone()->bdiv($x,$scale); - # copy private parts over - $x->{_m} = $u->{_m}; - $x->{_e} = $u->{_e}; - $x->{_es} = $u->{_es}; - } - else - { - # calculate the broot() as integer result first, and if it fits, return - # it rightaway (but only if $x and $y are integer): - - my $done = 0; # not yet - if ($y->is_int() && $x->is_int()) - { - my $i = $MBI->_copy( $x->{_m} ); - $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); - my $int = Math::BigInt->bzero(); - $int->{value} = $i; - $int->broot($y->as_number()); - # if ($exact) - if ($int->copy()->bpow($y) == $x) - { - # found result, return it - $x->{_m} = $int->{value}; - $x->{_e} = $MBI->_zero(); - $x->{_es} = '+'; - $x->bnorm(); - $done = 1; - } - } - if ($done == 0) - { - my $u = $self->bone()->bdiv($y,$scale+4); - delete $u->{_a}; delete $u->{_p}; # otherwise it conflicts - $x->bpow($u,$scale+4); # el cheapo - } - } - $x->bneg() if $sign == 1; - - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # restore globals - $$abr = $ab; $$pbr = $pb; - $x; - } - -sub bsqrt - { - # calculate square root - my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->modify('bsqrt'); - - return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0 - return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf - return $x->round($a,$p,$r) if $x->is_zero() || $x->is_one(); - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my (@params,$scale); - ($x,@params) = $x->_find_round_parameters($a,$p,$r); - - return $x if $x->is_nan(); # error in _find_round_parameters? - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - # when user set globals, they would interfere with our calculation, so - # disable them and later re-enable them - no strict 'refs'; - my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; - my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; - # we also need to disable any set A or P on $x (_find_round_parameters took - # them already into account), since these would interfere, too - delete $x->{_a}; delete $x->{_p}; - # need to disable $upgrade in BigInt, to avoid deep recursion - local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI - - my $i = $MBI->_copy( $x->{_m} ); - $MBI->_lsft( $i, $x->{_e}, 10 ) unless $MBI->_is_zero($x->{_e}); - my $xas = Math::BigInt->bzero(); - $xas->{value} = $i; - - my $gs = $xas->copy()->bsqrt(); # some guess - - if (($x->{_es} ne '-') # guess can't be accurate if there are - # digits after the dot - && ($xas->bacmp($gs * $gs) == 0)) # guess hit the nail on the head? - { - # exact result, copy result over to keep $x - $x->{_m} = $gs->{value}; $x->{_e} = $MBI->_zero(); $x->{_es} = '+'; - $x->bnorm(); - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # re-enable A and P, upgrade is taken care of by "local" - ${"$self\::accuracy"} = $ab; ${"$self\::precision"} = $pb; - return $x; - } - - # sqrt(2) = 1.4 because sqrt(2*100) = 1.4*10; so we can increase the accuracy - # of the result by multiplying the input by 100 and then divide the integer - # result of sqrt(input) by 10. Rounding afterwards returns the real result. - - # The following steps will transform 123.456 (in $x) into 123456 (in $y1) - my $y1 = $MBI->_copy($x->{_m}); - - my $length = $MBI->_len($y1); - - # Now calculate how many digits the result of sqrt(y1) would have - my $digits = int($length / 2); - - # But we need at least $scale digits, so calculate how many are missing - my $shift = $scale - $digits; - - # This happens if the input had enough digits - # (we take care of integer guesses above) - $shift = 0 if $shift < 0; - - # Multiply in steps of 100, by shifting left two times the "missing" digits - my $s2 = $shift * 2; - - # We now make sure that $y1 has the same odd or even number of digits than - # $x had. So when _e of $x is odd, we must shift $y1 by one digit left, - # because we always must multiply by steps of 100 (sqrt(100) is 10) and not - # steps of 10. The length of $x does not count, since an even or odd number - # of digits before the dot is not changed by adding an even number of digits - # after the dot (the result is still odd or even digits long). - $s2++ if $MBI->_is_odd($x->{_e}); - - $MBI->_lsft( $y1, $MBI->_new($s2), 10); - - # now take the square root and truncate to integer - $y1 = $MBI->_sqrt($y1); - - # By "shifting" $y1 right (by creating a negative _e) we calculate the final - # result, which is than later rounded to the desired scale. - - # calculate how many zeros $x had after the '.' (or before it, depending - # on sign of $dat, the result should have half as many: - my $dat = $MBI->_num($x->{_e}); - $dat = -$dat if $x->{_es} eq '-'; - $dat += $length; - - if ($dat > 0) - { - # no zeros after the dot (e.g. 1.23, 0.49 etc) - # preserve half as many digits before the dot than the input had - # (but round this "up") - $dat = int(($dat+1)/2); - } - else - { - $dat = int(($dat)/2); - } - $dat -= $MBI->_len($y1); - if ($dat < 0) - { - $dat = abs($dat); - $x->{_e} = $MBI->_new( $dat ); - $x->{_es} = '-'; - } - else - { - $x->{_e} = $MBI->_new( $dat ); - $x->{_es} = '+'; - } - $x->{_m} = $y1; - $x->bnorm(); - - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # restore globals - $$abr = $ab; $$pbr = $pb; - $x; - } - -sub bfac - { - # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT - # compute factorial number, modifies first argument - - # set up parameters - my ($self,$x,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - ($self,$x,@r) = objectify(1,@_) if !ref($x); - - # inf => inf - return $x if $x->modify('bfac') || $x->{sign} eq '+inf'; - - return $x->bnan() - if (($x->{sign} ne '+') || # inf, NaN, <0 etc => NaN - ($x->{_es} ne '+')); # digits after dot? - - # use BigInt's bfac() for faster calc - if (! $MBI->_is_zero($x->{_e})) - { - $MBI->_lsft($x->{_m}, $x->{_e},10); # change 12e1 to 120e0 - $x->{_e} = $MBI->_zero(); # normalize - $x->{_es} = '+'; - } - $MBI->_fac($x->{_m}); # calculate factorial - $x->bnorm()->round(@r); # norm again and round result - } - -sub _pow - { - # Calculate a power where $y is a non-integer, like 2 ** 0.3 - my ($x,$y,@r) = @_; - my $self = ref($x); - - # if $y == 0.5, it is sqrt($x) - $HALF = $self->new($HALF) unless ref($HALF); - return $x->bsqrt(@r,$y) if $y->bcmp($HALF) == 0; - - # Using: - # a ** x == e ** (x * ln a) - - # u = y * ln x - # _ _ - # Taylor: | u u^2 u^3 | - # x ** y = 1 + | --- + --- + ----- + ... | - # |_ 1 1*2 1*2*3 _| - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my ($scale,@params); - ($x,@params) = $x->_find_round_parameters(@r); - - return $x if $x->is_nan(); # error in _find_round_parameters? - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $params[1] = undef; # disable P - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r[2]; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - # when user set globals, they would interfere with our calculation, so - # disable them and later re-enable them - no strict 'refs'; - my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; - my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; - # we also need to disable any set A or P on $x (_find_round_parameters took - # them already into account), since these would interfere, too - delete $x->{_a}; delete $x->{_p}; - # need to disable $upgrade in BigInt, to avoid deep recursion - local $Math::BigInt::upgrade = undef; - - my ($limit,$v,$u,$below,$factor,$next,$over); - - $u = $x->copy()->blog(undef,$scale)->bmul($y); - $v = $self->bone(); # 1 - $factor = $self->new(2); # 2 - $x->bone(); # first term: 1 - - $below = $v->copy(); - $over = $u->copy(); - - $limit = $self->new("1E-". ($scale-1)); - #my $steps = 0; - while (3 < 5) - { - # we calculate the next term, and add it to the last - # when the next term is below our limit, it won't affect the outcome - # anymore, so we stop: - $next = $over->copy()->bdiv($below,$scale); - last if $next->bacmp($limit) <= 0; - $x->badd($next); - # calculate things for the next term - $over *= $u; $below *= $factor; $factor->binc(); - - last if $x->{sign} !~ /^[-+]$/; - - #$steps++; - } - - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # restore globals - $$abr = $ab; $$pbr = $pb; - $x; - } - -sub bpow - { - # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT - # compute power of two numbers, second arg is used as integer - # modifies first argument - - # set up parameters - my ($self,$x,$y,$a,$p,$r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$a,$p,$r) = objectify(2,@_); - } - - return $x if $x->modify('bpow'); - - return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; - return $x if $x->{sign} =~ /^[+-]inf$/; - - # cache the result of is_zero - my $y_is_zero = $y->is_zero(); - return $x->bone() if $y_is_zero; - return $x if $x->is_one() || $y->is_one(); - - my $x_is_zero = $x->is_zero(); - return $x->_pow($y,$a,$p,$r) if !$x_is_zero && !$y->is_int(); # non-integer power - - my $y1 = $y->as_number()->{value}; # make MBI part - - # if ($x == -1) - if ($x->{sign} eq '-' && $MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) - { - # if $x == -1 and odd/even y => +1/-1 because +-1 ^ (+-1) => +-1 - return $MBI->_is_odd($y1) ? $x : $x->babs(1); - } - if ($x_is_zero) - { - return $x if $y->{sign} eq '+'; # 0**y => 0 (if not y <= 0) - # 0 ** -y => 1 / (0 ** y) => 1 / 0! (1 / 0 => +inf) - return $x->binf(); - } - - my $new_sign = '+'; - $new_sign = $MBI->_is_odd($y1) ? '-' : '+' if $x->{sign} ne '+'; - - # calculate $x->{_m} ** $y and $x->{_e} * $y separately (faster) - $x->{_m} = $MBI->_pow( $x->{_m}, $y1); - $x->{_e} = $MBI->_mul ($x->{_e}, $y1); - - $x->{sign} = $new_sign; - $x->bnorm(); - if ($y->{sign} eq '-') - { - # modify $x in place! - my $z = $x->copy(); $x->bone(); - return scalar $x->bdiv($z,$a,$p,$r); # round in one go (might ignore y's A!) - } - $x->round($a,$p,$r,$y); - } - -sub bmodpow - { - # takes a very large number to a very large exponent in a given very - # large modulus, quickly, thanks to binary exponentiation. Supports - # negative exponents. - my ($self,$num,$exp,$mod,@r) = objectify(3,@_); - - return $num if $num->modify('bmodpow'); - - # check modulus for valid values - return $num->bnan() if ($mod->{sign} ne '+' # NaN, - , -inf, +inf - || $mod->is_zero()); - - # check exponent for valid values - if ($exp->{sign} =~ /\w/) - { - # i.e., if it's NaN, +inf, or -inf... - return $num->bnan(); - } - - $num->bmodinv ($mod) if ($exp->{sign} eq '-'); - - # check num for valid values (also NaN if there was no inverse but $exp < 0) - return $num->bnan() if $num->{sign} !~ /^[+-]$/; - - # $mod is positive, sign on $exp is ignored, result also positive - - # XXX TODO: speed it up when all three numbers are integers - $num->bpow($exp)->bmod($mod); - } - -############################################################################### -# trigonometric functions - -# helper function for bpi() and batan2(), calculates arcus tanges (1/x) - -sub _atan_inv - { - # return a/b so that a/b approximates atan(1/x) to at least limit digits - my ($self, $x, $limit) = @_; - - # Taylor: x^3 x^5 x^7 x^9 - # atan = x - --- + --- - --- + --- - ... - # 3 5 7 9 - - # 1 1 1 1 - # atan 1/x = - - ------- + ------- - ------- + ... - # x x^3 * 3 x^5 * 5 x^7 * 7 - - # 1 1 1 1 - # atan 1/x = - - --------- + ---------- - ----------- + ... - # 5 3 * 125 5 * 3125 7 * 78125 - - # Subtraction/addition of a rational: - - # 5 7 5*3 +- 7*4 - # - +- - = ---------- - # 4 3 4*3 - - # Term: N N+1 - # - # a 1 a * d * c +- b - # ----- +- ------------------ = ---------------- - # b d * c b * d * c - - # since b1 = b0 * (d-2) * c - - # a 1 a * d +- b / c - # ----- +- ------------------ = ---------------- - # b d * c b * d - - # and d = d + 2 - # and c = c * x * x - - # u = d * c - # stop if length($u) > limit - # a = a * u +- b - # b = b * u - # d = d + 2 - # c = c * x * x - # sign = 1 - sign - - my $a = $MBI->_one(); - my $b = $MBI->_copy($x); - - my $x2 = $MBI->_mul( $MBI->_copy($x), $b); # x2 = x * x - my $d = $MBI->_new( 3 ); # d = 3 - my $c = $MBI->_mul( $MBI->_copy($x), $x2); # c = x ^ 3 - my $two = $MBI->_new( 2 ); - - # run the first step unconditionally - my $u = $MBI->_mul( $MBI->_copy($d), $c); - $a = $MBI->_mul($a, $u); - $a = $MBI->_sub($a, $b); - $b = $MBI->_mul($b, $u); - $d = $MBI->_add($d, $two); - $c = $MBI->_mul($c, $x2); - - # a is now a * (d-3) * c - # b is now b * (d-2) * c - - # run the second step unconditionally - $u = $MBI->_mul( $MBI->_copy($d), $c); - $a = $MBI->_mul($a, $u); - $a = $MBI->_add($a, $b); - $b = $MBI->_mul($b, $u); - $d = $MBI->_add($d, $two); - $c = $MBI->_mul($c, $x2); - - # a is now a * (d-3) * (d-5) * c * c - # b is now b * (d-2) * (d-4) * c * c - - # so we can remove c * c from both a and b to shorten the numbers involved: - $a = $MBI->_div($a, $x2); - $b = $MBI->_div($b, $x2); - $a = $MBI->_div($a, $x2); - $b = $MBI->_div($b, $x2); - -# my $step = 0; - my $sign = 0; # 0 => -, 1 => + - while (3 < 5) - { -# $step++; -# if (($i++ % 100) == 0) -# { -# print "a=",$MBI->_str($a),"\n"; -# print "b=",$MBI->_str($b),"\n"; -# } -# print "d=",$MBI->_str($d),"\n"; -# print "x2=",$MBI->_str($x2),"\n"; -# print "c=",$MBI->_str($c),"\n"; - - my $u = $MBI->_mul( $MBI->_copy($d), $c); - # use _alen() for libs like GMP where _len() would be O(N^2) - last if $MBI->_alen($u) > $limit; - my ($bc,$r) = $MBI->_div( $MBI->_copy($b), $c); - if ($MBI->_is_zero($r)) - { - # b / c is an integer, so we can remove c from all terms - # this happens almost every time: - $a = $MBI->_mul($a, $d); - $a = $MBI->_sub($a, $bc) if $sign == 0; - $a = $MBI->_add($a, $bc) if $sign == 1; - $b = $MBI->_mul($b, $d); - } - else - { - # b / c is not an integer, so we keep c in the terms - # this happens very rarely, for instance for x = 5, this happens only - # at the following steps: - # 1, 5, 14, 32, 72, 157, 340, ... - $a = $MBI->_mul($a, $u); - $a = $MBI->_sub($a, $b) if $sign == 0; - $a = $MBI->_add($a, $b) if $sign == 1; - $b = $MBI->_mul($b, $u); - } - $d = $MBI->_add($d, $two); - $c = $MBI->_mul($c, $x2); - $sign = 1 - $sign; - - } - -# print "Took $step steps for ", $MBI->_str($x),"\n"; -# print "a=",$MBI->_str($a),"\n"; print "b=",$MBI->_str($b),"\n"; - # return a/b so that a/b approximates atan(1/x) - ($a,$b); - } - -sub bpi - { - my ($self,$n) = @_; - if (@_ == 0) - { - $self = $class; - } - if (@_ == 1) - { - # called like Math::BigFloat::bpi(10); - $n = $self; $self = $class; - # called like Math::BigFloat->bpi(); - $n = undef if $n eq 'Math::BigFloat'; - } - $self = ref($self) if ref($self); - my $fallback = defined $n ? 0 : 1; - $n = 40 if !defined $n || $n < 1; - - # after 黃見利 (Hwang Chien-Lih) (1997) - # pi/4 = 183 * atan(1/239) + 32 * atan(1/1023) – 68 * atan(1/5832) - # + 12 * atan(1/110443) - 12 * atan(1/4841182) - 100 * atan(1/6826318) - - # a few more to prevent rounding errors - $n += 4; - - my ($a,$b) = $self->_atan_inv( $MBI->_new(239),$n); - my ($c,$d) = $self->_atan_inv( $MBI->_new(1023),$n); - my ($e,$f) = $self->_atan_inv( $MBI->_new(5832),$n); - my ($g,$h) = $self->_atan_inv( $MBI->_new(110443),$n); - my ($i,$j) = $self->_atan_inv( $MBI->_new(4841182),$n); - my ($k,$l) = $self->_atan_inv( $MBI->_new(6826318),$n); - - $MBI->_mul($a, $MBI->_new(732)); - $MBI->_mul($c, $MBI->_new(128)); - $MBI->_mul($e, $MBI->_new(272)); - $MBI->_mul($g, $MBI->_new(48)); - $MBI->_mul($i, $MBI->_new(48)); - $MBI->_mul($k, $MBI->_new(400)); - - my $x = $self->bone(); $x->{_m} = $a; my $x_d = $self->bone(); $x_d->{_m} = $b; - my $y = $self->bone(); $y->{_m} = $c; my $y_d = $self->bone(); $y_d->{_m} = $d; - my $z = $self->bone(); $z->{_m} = $e; my $z_d = $self->bone(); $z_d->{_m} = $f; - my $u = $self->bone(); $u->{_m} = $g; my $u_d = $self->bone(); $u_d->{_m} = $h; - my $v = $self->bone(); $v->{_m} = $i; my $v_d = $self->bone(); $v_d->{_m} = $j; - my $w = $self->bone(); $w->{_m} = $k; my $w_d = $self->bone(); $w_d->{_m} = $l; - $x->bdiv($x_d, $n); - $y->bdiv($y_d, $n); - $z->bdiv($z_d, $n); - $u->bdiv($u_d, $n); - $v->bdiv($v_d, $n); - $w->bdiv($w_d, $n); - - delete $x->{_a}; delete $y->{_a}; delete $z->{_a}; - delete $u->{_a}; delete $v->{_a}; delete $w->{_a}; - $x->badd($y)->bsub($z)->badd($u)->bsub($v)->bsub($w); - - $x->bround($n-4); - delete $x->{_a} if $fallback == 1; - $x; - } - -sub bcos - { - # Calculate a cosinus of x. - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - # Taylor: x^2 x^4 x^6 x^8 - # cos = 1 - --- + --- - --- + --- ... - # 2! 4! 6! 8! - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my ($scale,@params); - ($x,@params) = $x->_find_round_parameters(@r); - - # constant object or error in _find_round_parameters? - return $x if $x->modify('bcos') || $x->is_nan(); - - return $x->bone(@r) if $x->is_zero(); - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $params[1] = undef; # disable P - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r[2]; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - # when user set globals, they would interfere with our calculation, so - # disable them and later re-enable them - no strict 'refs'; - my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; - my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; - # we also need to disable any set A or P on $x (_find_round_parameters took - # them already into account), since these would interfere, too - delete $x->{_a}; delete $x->{_p}; - # need to disable $upgrade in BigInt, to avoid deep recursion - local $Math::BigInt::upgrade = undef; - - my $last = 0; - my $over = $x * $x; # X ^ 2 - my $x2 = $over->copy(); # X ^ 2; difference between terms - my $sign = 1; # start with -= - my $below = $self->new(2); my $factorial = $self->new(3); - $x->bone(); delete $x->{_a}; delete $x->{_p}; - - my $limit = $self->new("1E-". ($scale-1)); - #my $steps = 0; - while (3 < 5) - { - # we calculate the next term, and add it to the last - # when the next term is below our limit, it won't affect the outcome - # anymore, so we stop: - my $next = $over->copy()->bdiv($below,$scale); - last if $next->bacmp($limit) <= 0; - - if ($sign == 0) - { - $x->badd($next); - } - else - { - $x->bsub($next); - } - $sign = 1-$sign; # alternate - # calculate things for the next term - $over->bmul($x2); # $x*$x - $below->bmul($factorial); $factorial->binc(); # n*(n+1) - $below->bmul($factorial); $factorial->binc(); # n*(n+1) - } - - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # restore globals - $$abr = $ab; $$pbr = $pb; - $x; - } - -sub bsin - { - # Calculate a sinus of x. - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - # taylor: x^3 x^5 x^7 x^9 - # sin = x - --- + --- - --- + --- ... - # 3! 5! 7! 9! - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my ($scale,@params); - ($x,@params) = $x->_find_round_parameters(@r); - - # constant object or error in _find_round_parameters? - return $x if $x->modify('bsin') || $x->is_nan(); - - return $x->bzero(@r) if $x->is_zero(); - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $params[1] = undef; # disable P - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r[2]; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - # when user set globals, they would interfere with our calculation, so - # disable them and later re-enable them - no strict 'refs'; - my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; - my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; - # we also need to disable any set A or P on $x (_find_round_parameters took - # them already into account), since these would interfere, too - delete $x->{_a}; delete $x->{_p}; - # need to disable $upgrade in BigInt, to avoid deep recursion - local $Math::BigInt::upgrade = undef; - - my $last = 0; - my $over = $x * $x; # X ^ 2 - my $x2 = $over->copy(); # X ^ 2; difference between terms - $over->bmul($x); # X ^ 3 as starting value - my $sign = 1; # start with -= - my $below = $self->new(6); my $factorial = $self->new(4); - delete $x->{_a}; delete $x->{_p}; - - my $limit = $self->new("1E-". ($scale-1)); - #my $steps = 0; - while (3 < 5) - { - # we calculate the next term, and add it to the last - # when the next term is below our limit, it won't affect the outcome - # anymore, so we stop: - my $next = $over->copy()->bdiv($below,$scale); - last if $next->bacmp($limit) <= 0; - - if ($sign == 0) - { - $x->badd($next); - } - else - { - $x->bsub($next); - } - $sign = 1-$sign; # alternate - # calculate things for the next term - $over->bmul($x2); # $x*$x - $below->bmul($factorial); $factorial->binc(); # n*(n+1) - $below->bmul($factorial); $factorial->binc(); # n*(n+1) - } - - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # restore globals - $$abr = $ab; $$pbr = $pb; - $x; - } - -sub batan2 - { - # calculate arcus tangens of ($y/$x) - - # set up parameters - my ($self,$y,$x,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$y,$x,@r) = objectify(2,@_); - } - - return $y if $y->modify('batan2'); - - return $y->bnan() if ($y->{sign} eq $nan) || ($x->{sign} eq $nan); - - # Y X - # 0 0 result is 0 - # 0 +x result is 0 - # ? inf result is 0 - return $y->bzero(@r) if ($x->is_inf('+') && !$y->is_inf()) || ($y->is_zero() && $x->{sign} eq '+'); - - # Y X - # != 0 -inf result is +- pi - if ($x->is_inf() || $y->is_inf()) - { - # calculate PI - my $pi = $self->bpi(@r); - if ($y->is_inf()) - { - # upgrade to BigRat etc. - return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; - if ($x->{sign} eq '-inf') - { - # calculate 3 pi/4 - $MBI->_mul($pi->{_m}, $MBI->_new(3)); - $MBI->_div($pi->{_m}, $MBI->_new(4)); - } - elsif ($x->{sign} eq '+inf') - { - # calculate pi/4 - $MBI->_div($pi->{_m}, $MBI->_new(4)); - } - else - { - # calculate pi/2 - $MBI->_div($pi->{_m}, $MBI->_new(2)); - } - $y->{sign} = substr($y->{sign},0,1); # keep +/- - } - # modify $y in place - $y->{_m} = $pi->{_m}; - $y->{_e} = $pi->{_e}; - $y->{_es} = $pi->{_es}; - # keep the sign of $y - return $y; - } - - return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; - - # Y X - # 0 -x result is PI - if ($y->is_zero()) - { - # calculate PI - my $pi = $self->bpi(@r); - # modify $y in place - $y->{_m} = $pi->{_m}; - $y->{_e} = $pi->{_e}; - $y->{_es} = $pi->{_es}; - $y->{sign} = '+'; - return $y; - } - - # Y X - # +y 0 result is PI/2 - # -y 0 result is -PI/2 - if ($x->is_zero()) - { - # calculate PI/2 - my $pi = $self->bpi(@r); - # modify $y in place - $y->{_m} = $pi->{_m}; - $y->{_e} = $pi->{_e}; - $y->{_es} = $pi->{_es}; - # -y => -PI/2, +y => PI/2 - $MBI->_div($y->{_m}, $MBI->_new(2)); - return $y; - } - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my ($scale,@params); - ($y,@params) = $y->_find_round_parameters(@r); - - # error in _find_round_parameters? - return $y if $y->is_nan(); - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $params[1] = undef; # disable P - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r[2]; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - # inlined is_one() && is_one('-') - if ($MBI->_is_one($y->{_m}) && $MBI->_is_zero($y->{_e})) - { - # shortcut: 1 1 result is PI/4 - # inlined is_one() && is_one('-') - if ($MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) - { - # 1,1 => PI/4 - my $pi_4 = $self->bpi( $scale - 3); - # modify $y in place - $y->{_m} = $pi_4->{_m}; - $y->{_e} = $pi_4->{_e}; - $y->{_es} = $pi_4->{_es}; - # 1 1 => + - # -1 1 => - - # 1 -1 => - - # -1 -1 => + - $y->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; - $MBI->_div($y->{_m}, $MBI->_new(4)); - return $y; - } - # shortcut: 1 int(X) result is _atan_inv(X) - - # is integer - if ($x->{_es} eq '+') - { - my $x1 = $MBI->_copy($x->{_m}); - $MBI->_lsft($x1, $x->{_e},10) unless $MBI->_is_zero($x->{_e}); - - my ($a,$b) = $self->_atan_inv($x1, $scale); - my $y_sign = $y->{sign}; - # calculate A/B - $y->bone(); $y->{_m} = $a; my $y_d = $self->bone(); $y_d->{_m} = $b; - $y->bdiv($y_d, @r); - $y->{sign} = $y_sign; - return $y; - } - } - - # handle all other cases - # X Y - # +x +y 0 to PI/2 - # -x +y PI/2 to PI - # +x -y 0 to -PI/2 - # -x -y -PI/2 to -PI - - my $y_sign = $y->{sign}; - - # divide $x by $y - $y->bdiv($x, $scale) unless $x->is_one(); - $y->batan(@r); - - # restore sign - $y->{sign} = $y_sign; - - $y; - } - -sub batan - { - # Calculate a arcus tangens of x. - my ($x,@r) = @_; - my $self = ref($x); - - # taylor: x^3 x^5 x^7 x^9 - # atan = x - --- + --- - --- + --- ... - # 3 5 7 9 - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my ($scale,@params); - ($x,@params) = $x->_find_round_parameters(@r); - - # constant object or error in _find_round_parameters? - return $x if $x->modify('batan') || $x->is_nan(); - - if ($x->{sign} =~ /^[+-]inf\z/) - { - # +inf result is PI/2 - # -inf result is -PI/2 - # calculate PI/2 - my $pi = $self->bpi(@r); - # modify $x in place - $x->{_m} = $pi->{_m}; - $x->{_e} = $pi->{_e}; - $x->{_es} = $pi->{_es}; - # -y => -PI/2, +y => PI/2 - $x->{sign} = substr($x->{sign},0,1); # +inf => + - $MBI->_div($x->{_m}, $MBI->_new(2)); - return $x; - } - - return $x->bzero(@r) if $x->is_zero(); - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $params[1] = undef; # disable P - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r[2]; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it is not - # enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - # 1 or -1 => PI/4 - # inlined is_one() && is_one('-') - if ($MBI->_is_one($x->{_m}) && $MBI->_is_zero($x->{_e})) - { - my $pi = $self->bpi($scale - 3); - # modify $x in place - $x->{_m} = $pi->{_m}; - $x->{_e} = $pi->{_e}; - $x->{_es} = $pi->{_es}; - # leave the sign of $x alone (+1 => +PI/4, -1 => -PI/4) - $MBI->_div($x->{_m}, $MBI->_new(4)); - return $x; - } - - # This series is only valid if -1 < x < 1, so for other x we need to - # to calculate PI/2 - atan(1/x): - my $one = $MBI->_new(1); - my $pi = undef; - if ($x->{_es} eq '+' && ($MBI->_acmp($x->{_m},$one) >= 0)) - { - # calculate PI/2 - $pi = $self->bpi($scale - 3); - $MBI->_div($pi->{_m}, $MBI->_new(2)); - # calculate 1/$x: - my $x_copy = $x->copy(); - # modify $x in place - $x->bone(); $x->bdiv($x_copy,$scale); - } - - # when user set globals, they would interfere with our calculation, so - # disable them and later re-enable them - no strict 'refs'; - my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef; - my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef; - # we also need to disable any set A or P on $x (_find_round_parameters took - # them already into account), since these would interfere, too - delete $x->{_a}; delete $x->{_p}; - # need to disable $upgrade in BigInt, to avoid deep recursion - local $Math::BigInt::upgrade = undef; - - my $last = 0; - my $over = $x * $x; # X ^ 2 - my $x2 = $over->copy(); # X ^ 2; difference between terms - $over->bmul($x); # X ^ 3 as starting value - my $sign = 1; # start with -= - my $below = $self->new(3); - my $two = $self->new(2); - delete $x->{_a}; delete $x->{_p}; - - my $limit = $self->new("1E-". ($scale-1)); - #my $steps = 0; - while (3 < 5) - { - # we calculate the next term, and add it to the last - # when the next term is below our limit, it won't affect the outcome - # anymore, so we stop: - my $next = $over->copy()->bdiv($below,$scale); - last if $next->bacmp($limit) <= 0; - - if ($sign == 0) - { - $x->badd($next); - } - else - { - $x->bsub($next); - } - $sign = 1-$sign; # alternate - # calculate things for the next term - $over->bmul($x2); # $x*$x - $below->badd($two); # n += 2 - } - - if (defined $pi) - { - my $x_copy = $x->copy(); - # modify $x in place - $x->{_m} = $pi->{_m}; - $x->{_e} = $pi->{_e}; - $x->{_es} = $pi->{_es}; - # PI/2 - $x - $x->bsub($x_copy); - } - - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - # restore globals - $$abr = $ab; $$pbr = $pb; - $x; - } - -############################################################################### -# rounding functions - -sub bfround - { - # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.' - # $n == 0 means round to integer - # expects and returns normalized numbers! - my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x); - - my ($scale,$mode) = $x->_scale_p(@_); - return $x if !defined $scale || $x->modify('bfround'); # no-op - - # never round a 0, +-inf, NaN - if ($x->is_zero()) - { - $x->{_p} = $scale if !defined $x->{_p} || $x->{_p} < $scale; # -3 < -2 - return $x; - } - return $x if $x->{sign} !~ /^[+-]$/; - - # don't round if x already has lower precision - return $x if (defined $x->{_p} && $x->{_p} < 0 && $scale < $x->{_p}); - - $x->{_p} = $scale; # remember round in any case - delete $x->{_a}; # and clear A - if ($scale < 0) - { - # round right from the '.' - - return $x if $x->{_es} eq '+'; # e >= 0 => nothing to round - - $scale = -$scale; # positive for simplicity - my $len = $MBI->_len($x->{_m}); # length of mantissa - - # the following poses a restriction on _e, but if _e is bigger than a - # scalar, you got other problems (memory etc) anyway - my $dad = -(0+ ($x->{_es}.$MBI->_num($x->{_e}))); # digits after dot - my $zad = 0; # zeros after dot - $zad = $dad - $len if (-$dad < -$len); # for 0.00..00xxx style - - # print "scale $scale dad $dad zad $zad len $len\n"; - # number bsstr len zad dad - # 0.123 123e-3 3 0 3 - # 0.0123 123e-4 3 1 4 - # 0.001 1e-3 1 2 3 - # 1.23 123e-2 3 0 2 - # 1.2345 12345e-4 5 0 4 - - # do not round after/right of the $dad - return $x if $scale > $dad; # 0.123, scale >= 3 => exit - - # round to zero if rounding inside the $zad, but not for last zero like: - # 0.0065, scale -2, round last '0' with following '65' (scale == zad case) - return $x->bzero() if $scale < $zad; - if ($scale == $zad) # for 0.006, scale -3 and trunc - { - $scale = -$len; - } - else - { - # adjust round-point to be inside mantissa - if ($zad != 0) - { - $scale = $scale-$zad; - } - else - { - my $dbd = $len - $dad; $dbd = 0 if $dbd < 0; # digits before dot - $scale = $dbd+$scale; - } - } - } - else - { - # round left from the '.' - - # 123 => 100 means length(123) = 3 - $scale (2) => 1 - - my $dbt = $MBI->_len($x->{_m}); - # digits before dot - my $dbd = $dbt + ($x->{_es} . $MBI->_num($x->{_e})); - # should be the same, so treat it as this - $scale = 1 if $scale == 0; - # shortcut if already integer - return $x if $scale == 1 && $dbt <= $dbd; - # maximum digits before dot - ++$dbd; - - if ($scale > $dbd) - { - # not enough digits before dot, so round to zero - return $x->bzero; - } - elsif ( $scale == $dbd ) - { - # maximum - $scale = -$dbt; - } - else - { - $scale = $dbd - $scale; - } - } - # pass sign to bround for rounding modes '+inf' and '-inf' - my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt'; - $m->bround($scale,$mode); - $x->{_m} = $m->{value}; # get our mantissa back - $x->bnorm(); - } - -sub bround - { - # accuracy: preserve $N digits, and overwrite the rest with 0's - my $x = shift; my $self = ref($x) || $x; $x = $self->new(shift) if !ref($x); - - if (($_[0] || 0) < 0) - { - require Carp; Carp::croak ('bround() needs positive accuracy'); - } - - my ($scale,$mode) = $x->_scale_a(@_); - return $x if !defined $scale || $x->modify('bround'); # no-op - - # scale is now either $x->{_a}, $accuracy, or the user parameter - # test whether $x already has lower accuracy, do nothing in this case - # but do round if the accuracy is the same, since a math operation might - # want to round a number with A=5 to 5 digits afterwards again - return $x if defined $x->{_a} && $x->{_a} < $scale; - - # scale < 0 makes no sense - # scale == 0 => keep all digits - # never round a +-inf, NaN - return $x if ($scale <= 0) || $x->{sign} !~ /^[+-]$/; - - # 1: never round a 0 - # 2: if we should keep more digits than the mantissa has, do nothing - if ($x->is_zero() || $MBI->_len($x->{_m}) <= $scale) - { - $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; - return $x; - } - - # pass sign to bround for '+inf' and '-inf' rounding modes - my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt'; - - $m->bround($scale,$mode); # round mantissa - $x->{_m} = $m->{value}; # get our mantissa back - $x->{_a} = $scale; # remember rounding - delete $x->{_p}; # and clear P - $x->bnorm(); # del trailing zeros gen. by bround() - } - -sub bfloor - { - # round towards minus infinity - my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->modify('bfloor'); - - return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf - - # if $x has digits after dot - if ($x->{_es} eq '-') - { - $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot - $x->{_e} = $MBI->_zero(); # trunc/norm - $x->{_es} = '+'; # abs e - $MBI->_inc($x->{_m}) if $x->{sign} eq '-'; # increment if negative - } - $x->round($a,$p,$r); - } - -sub bceil - { - # round towards plus infinity - my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->modify('bceil'); - return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf - - # if $x has digits after dot - if ($x->{_es} eq '-') - { - $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot - $x->{_e} = $MBI->_zero(); # trunc/norm - $x->{_es} = '+'; # abs e - if ($x->{sign} eq '+') { - $MBI->_inc($x->{_m}); # increment if positive - } else { - $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # avoid -0 - } - } - $x->round($a,$p,$r); - } - -sub bint - { - # round towards zero - my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->modify('bint'); - return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf - - # if $x has digits after the decimal point - if ($x->{_es} eq '-') - { - $x->{_m} = $MBI->_rsft($x->{_m},$x->{_e},10); # cut off digits after dot - $x->{_e} = $MBI->_zero(); # truncate/normalize - $x->{_es} = '+'; # abs e - $x->{sign} = '+' if $MBI->_is_zero($x->{_m}); # avoid -0 - } - $x->round($a,$p,$r); - } - -sub brsft - { - # shift right by $y (divide by power of $n) - - # set up parameters - my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_); - } - - return $x if $x->modify('brsft'); - return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf - - $n = 2 if !defined $n; $n = $self->new($n); - - # negative amount? - return $x->blsft($y->copy()->babs(),$n) if $y->{sign} =~ /^-/; - - # the following call to bdiv() will return either quo or (quo,remainder): - $x->bdiv($n->bpow($y),$a,$p,$r,$y); - } - -sub blsft - { - # shift left by $y (multiply by power of $n) - - # set up parameters - my ($self,$x,$y,$n,$a,$p,$r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$n,$a,$p,$r) = objectify(2,@_); - } - - return $x if $x->modify('blsft'); - return $x if $x->{sign} !~ /^[+-]$/; # nan, +inf, -inf - - $n = 2 if !defined $n; $n = $self->new($n); - - # negative amount? - return $x->brsft($y->copy()->babs(),$n) if $y->{sign} =~ /^-/; - - $x->bmul($n->bpow($y),$a,$p,$r,$y); - } - -############################################################################### - -sub DESTROY - { - # going through AUTOLOAD for every DESTROY is costly, avoid it by empty sub - } - -sub AUTOLOAD - { - # make fxxx and bxxx both work by selectively mapping fxxx() to MBF::bxxx() - # or falling back to MBI::bxxx() - my $name = $AUTOLOAD; - - $name =~ s/(.*):://; # split package - my $c = $1 || $class; - no strict 'refs'; - $c->import() if $IMPORT == 0; - if (!_method_alias($name)) - { - if (!defined $name) - { - # delayed load of Carp and avoid recursion - require Carp; - Carp::croak ("$c: Can't call a method without name"); - } - if (!_method_hand_up($name)) - { - # delayed load of Carp and avoid recursion - require Carp; - Carp::croak ("Can't call $c\-\>$name, not a valid method"); - } - # try one level up, but subst. bxxx() for fxxx() since MBI only got bxxx() - $name =~ s/^f/b/; - return &{"Math::BigInt"."::$name"}(@_); - } - my $bname = $name; $bname =~ s/^f/b/; - $c .= "::$name"; - *{$c} = \&{$bname}; - &{$c}; # uses @_ - } - -sub exponent - { - # return a copy of the exponent - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - my $s = $x->{sign}; $s =~ s/^[+-]//; - return Math::BigInt->new($s); # -inf, +inf => +inf - } - Math::BigInt->new( $x->{_es} . $MBI->_str($x->{_e})); - } - -sub mantissa - { - # return a copy of the mantissa - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - my $s = $x->{sign}; $s =~ s/^[+]//; - return Math::BigInt->new($s); # -inf, +inf => +inf - } - my $m = Math::BigInt->new( $MBI->_str($x->{_m})); - $m->bneg() if $x->{sign} eq '-'; - - $m; - } - -sub parts - { - # return a copy of both the exponent and the mantissa - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - my $s = $x->{sign}; $s =~ s/^[+]//; my $se = $s; $se =~ s/^[-]//; - return ($self->new($s),$self->new($se)); # +inf => inf and -inf,+inf => inf - } - my $m = Math::BigInt->bzero(); - $m->{value} = $MBI->_copy($x->{_m}); - $m->bneg() if $x->{sign} eq '-'; - ($m, Math::BigInt->new( $x->{_es} . $MBI->_num($x->{_e}) )); - } - -############################################################################## -# private stuff (internal use only) - -sub import - { - my $self = shift; - my $l = scalar @_; - my $lib = ''; my @a; - my $lib_kind = 'try'; - $IMPORT=1; - for ( my $i = 0; $i < $l ; $i++) - { - if ( $_[$i] eq ':constant' ) - { - # This causes overlord er load to step in. 'binary' and 'integer' - # are handled by BigInt. - overload::constant float => sub { $self->new(shift); }; - } - elsif ($_[$i] eq 'upgrade') - { - # this causes upgrading - $upgrade = $_[$i+1]; # or undef to disable - $i++; - } - elsif ($_[$i] eq 'downgrade') - { - # this causes downgrading - $downgrade = $_[$i+1]; # or undef to disable - $i++; - } - elsif ($_[$i] =~ /^(lib|try|only)\z/) - { - # alternative library - $lib = $_[$i+1] || ''; # default Calc - $lib_kind = $1; # lib, try or only - $i++; - } - elsif ($_[$i] eq 'with') - { - # alternative class for our private parts() - # XXX: no longer supported - # $MBI = $_[$i+1] || 'Math::BigInt'; - $i++; - } - else - { - push @a, $_[$i]; - } - } - - $lib =~ tr/a-zA-Z0-9,://cd; # restrict to sane characters - # let use Math::BigInt lib => 'GMP'; use Math::BigFloat; still work - my $mbilib = eval { Math::BigInt->config()->{lib} }; - if ((defined $mbilib) && ($MBI eq 'Math::BigInt::Calc')) - { - # MBI already loaded - Math::BigInt->import( $lib_kind, "$lib,$mbilib", 'objectify'); - } - else - { - # MBI not loaded, or with ne "Math::BigInt::Calc" - $lib .= ",$mbilib" if defined $mbilib; - $lib =~ s/^,//; # don't leave empty - - # replacement library can handle lib statement, but also could ignore it - - # Perl < 5.6.0 dies with "out of memory!" when eval() and ':constant' is - # used in the same script, or eval inside import(). So we require MBI: - require Math::BigInt; - Math::BigInt->import( $lib_kind => $lib, 'objectify' ); - } - if ($@) - { - require Carp; Carp::croak ("Couldn't load $lib: $! $@"); - } - # find out which one was actually loaded - $MBI = Math::BigInt->config()->{lib}; - - # register us with MBI to get notified of future lib changes - Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } ); - - $self->export_to_level(1,$self,@a); # export wanted functions - } - -sub bnorm - { - # adjust m and e so that m is smallest possible - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x if $x->{sign} !~ /^[+-]$/; # inf, nan etc - - my $zeros = $MBI->_zeros($x->{_m}); # correct for trailing zeros - if ($zeros != 0) - { - my $z = $MBI->_new($zeros); - $x->{_m} = $MBI->_rsft ($x->{_m}, $z, 10); - if ($x->{_es} eq '-') - { - if ($MBI->_acmp($x->{_e},$z) >= 0) - { - $x->{_e} = $MBI->_sub ($x->{_e}, $z); - $x->{_es} = '+' if $MBI->_is_zero($x->{_e}); - } - else - { - $x->{_e} = $MBI->_sub ( $MBI->_copy($z), $x->{_e}); - $x->{_es} = '+'; - } - } - else - { - $x->{_e} = $MBI->_add ($x->{_e}, $z); - } - } - else - { - # $x can only be 0Ey if there are no trailing zeros ('0' has 0 trailing - # zeros). So, for something like 0Ey, set y to 1, and -0 => +0 - $x->{sign} = '+', $x->{_es} = '+', $x->{_e} = $MBI->_one() - if $MBI->_is_zero($x->{_m}); - } - - $x; # MBI bnorm is no-op, so do not call it - } - -############################################################################## - -sub as_hex - { - # return number as hexadecimal string (only for integers defined) - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc - return '0x0' if $x->is_zero(); - - return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? - - my $z = $MBI->_copy($x->{_m}); - if (! $MBI->_is_zero($x->{_e})) # > 0 - { - $MBI->_lsft( $z, $x->{_e},10); - } - $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); - $z->as_hex(); - } - -sub as_bin - { - # return number as binary digit string (only for integers defined) - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc - return '0b0' if $x->is_zero(); - - return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? - - my $z = $MBI->_copy($x->{_m}); - if (! $MBI->_is_zero($x->{_e})) # > 0 - { - $MBI->_lsft( $z, $x->{_e},10); - } - $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); - $z->as_bin(); - } - -sub as_oct - { - # return number as octal digit string (only for integers defined) - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc - return '0' if $x->is_zero(); - - return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex!? - - my $z = $MBI->_copy($x->{_m}); - if (! $MBI->_is_zero($x->{_e})) # > 0 - { - $MBI->_lsft( $z, $x->{_e},10); - } - $z = Math::BigInt->new( $x->{sign} . $MBI->_num($z)); - $z->as_oct(); - } - -sub as_number - { - # return copy as a bigint representation of this BigFloat number - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - return $x if $x->modify('as_number'); - - if (!$x->isa('Math::BigFloat')) - { - # if the object can as_number(), use it - return $x->as_number() if $x->can('as_number'); - # otherwise, get us a float and then a number - $x = $x->can('as_float') ? $x->as_float() : $self->new(0+"$x"); - } - - return Math::BigInt->binf($x->sign()) if $x->is_inf(); - return Math::BigInt->bnan() if $x->is_nan(); - - my $z = $MBI->_copy($x->{_m}); - if ($x->{_es} eq '-') # < 0 - { - $MBI->_rsft( $z, $x->{_e},10); - } - elsif (! $MBI->_is_zero($x->{_e})) # > 0 - { - $MBI->_lsft( $z, $x->{_e},10); - } - $z = Math::BigInt->new( $x->{sign} . $MBI->_str($z)); - $z; - } - -sub length - { - my $x = shift; - my $class = ref($x) || $x; - $x = $class->new(shift) unless ref($x); - - return 1 if $MBI->_is_zero($x->{_m}); - - my $len = $MBI->_len($x->{_m}); - $len += $MBI->_num($x->{_e}) if $x->{_es} eq '+'; - if (wantarray()) - { - my $t = 0; - $t = $MBI->_num($x->{_e}) if $x->{_es} eq '-'; - return ($len, $t); - } - $len; - } - -1; - -__END__ - -=pod - -=head1 NAME - -Math::BigFloat - Arbitrary size floating point math package - -=head1 SYNOPSIS - - use Math::BigFloat; - - # Number creation - my $x = Math::BigFloat->new($str); # defaults to 0 - my $y = $x->copy(); # make a true copy - my $nan = Math::BigFloat->bnan(); # create a NotANumber - my $zero = Math::BigFloat->bzero(); # create a +0 - my $inf = Math::BigFloat->binf(); # create a +inf - my $inf = Math::BigFloat->binf('-'); # create a -inf - my $one = Math::BigFloat->bone(); # create a +1 - my $mone = Math::BigFloat->bone('-'); # create a -1 - - my $pi = Math::BigFloat->bpi(100); # PI to 100 digits - - # the following examples compute their result to 100 digits accuracy: - my $cos = Math::BigFloat->new(1)->bcos(100); # cosinus(1) - my $sin = Math::BigFloat->new(1)->bsin(100); # sinus(1) - my $atan = Math::BigFloat->new(1)->batan(100); # arcus tangens(1) - - my $atan2 = Math::BigFloat->new( 1 )->batan2( 1 ,100); # batan(1) - my $atan2 = Math::BigFloat->new( 1 )->batan2( 8 ,100); # batan(1/8) - my $atan2 = Math::BigFloat->new( -2 )->batan2( 1 ,100); # batan(-2) - - # Testing - $x->is_zero(); # true if arg is +0 - $x->is_nan(); # true if arg is NaN - $x->is_one(); # true if arg is +1 - $x->is_one('-'); # true if arg is -1 - $x->is_odd(); # true if odd, false for even - $x->is_even(); # true if even, false for odd - $x->is_pos(); # true if >= 0 - $x->is_neg(); # true if < 0 - $x->is_inf(sign); # true if +inf, or -inf (default is '+') - - $x->bcmp($y); # compare numbers (undef,<0,=0,>0) - $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) - $x->sign(); # return the sign, either +,- or NaN - $x->digit($n); # return the nth digit, counting from right - $x->digit(-$n); # return the nth digit, counting from left - - # The following all modify their first argument. If you want to pre- - # serve $x, use $z = $x->copy()->bXXX($y); See under L for - # necessary when mixing $a = $b assignments with non-overloaded math. - - # set - $x->bzero(); # set $i to 0 - $x->bnan(); # set $i to NaN - $x->bone(); # set $x to +1 - $x->bone('-'); # set $x to -1 - $x->binf(); # set $x to inf - $x->binf('-'); # set $x to -inf - - $x->bneg(); # negation - $x->babs(); # absolute value - $x->bnorm(); # normalize (no-op) - $x->bnot(); # two's complement (bit wise not) - $x->binc(); # increment x by 1 - $x->bdec(); # decrement x by 1 - - $x->badd($y); # addition (add $y to $x) - $x->bsub($y); # subtraction (subtract $y from $x) - $x->bmul($y); # multiplication (multiply $x by $y) - $x->bdiv($y); # divide, set $x to quotient - # return (quo,rem) or quo if scalar - - $x->bmod($y); # modulus ($x % $y) - $x->bpow($y); # power of arguments ($x ** $y) - $x->bmodpow($exp,$mod); # modular exponentiation (($num**$exp) % $mod)) - $x->blsft($y, $n); # left shift by $y places in base $n - $x->brsft($y, $n); # right shift by $y places in base $n - # returns (quo,rem) or quo if in scalar context - - $x->blog(); # logarithm of $x to base e (Euler's number) - $x->blog($base); # logarithm of $x to base $base (f.i. 2) - $x->bexp(); # calculate e ** $x where e is Euler's number - - $x->band($y); # bit-wise and - $x->bior($y); # bit-wise inclusive or - $x->bxor($y); # bit-wise exclusive or - $x->bnot(); # bit-wise not (two's complement) - - $x->bsqrt(); # calculate square-root - $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root) - $x->bfac(); # factorial of $x (1*2*3*4*..$x) - - $x->bround($N); # accuracy: preserve $N digits - $x->bfround($N); # precision: round to the $Nth digit - - $x->bfloor(); # return integer less or equal than $x - $x->bceil(); # return integer greater or equal than $x - $x->bint(); # round towards zero - - # The following do not modify their arguments: - - bgcd(@values); # greatest common divisor - blcm(@values); # lowest common multiplicator - - $x->bstr(); # return string - $x->bsstr(); # return string in scientific notation - - $x->as_int(); # return $x as BigInt - $x->exponent(); # return exponent as BigInt - $x->mantissa(); # return mantissa as BigInt - $x->parts(); # return (mantissa,exponent) as BigInt - - $x->length(); # number of digits (w/o sign and '.') - ($l,$f) = $x->length(); # number of digits, and length of fraction - - $x->precision(); # return P of $x (or global, if P of $x undef) - $x->precision($n); # set P of $x to $n - $x->accuracy(); # return A of $x (or global, if A of $x undef) - $x->accuracy($n); # set A $x to $n - - # these get/set the appropriate global value for all BigFloat objects - Math::BigFloat->precision(); # Precision - Math::BigFloat->accuracy(); # Accuracy - Math::BigFloat->round_mode(); # rounding mode - -=head1 DESCRIPTION - -All operators (including basic math operations) are overloaded if you -declare your big floating point numbers as - - $i = new Math::BigFloat '12_3.456_789_123_456_789E-2'; - -Operations with overloaded operators preserve the arguments, which is -exactly what you expect. - -=head2 Input - -Input to these routines are either BigFloat objects, or strings of the -following four forms: - -=over - -=item * - -C - -=item * - -C - -=item * - -C - -=item * - -C - -=back - -all with optional leading and trailing zeros and/or spaces. Additionally, -numbers are allowed to have an underscore between any two digits. - -Empty strings as well as other illegal numbers results in 'NaN'. - -bnorm() on a BigFloat object is now effectively a no-op, since the numbers -are always stored in normalized form. On a string, it creates a BigFloat -object. - -=head2 Output - -Output values are BigFloat objects (normalized), except for bstr() and bsstr(). - -The string output will always have leading and trailing zeros stripped and drop -a plus sign. C will give you always the form with a decimal point, -while C (s for scientific) gives you the scientific notation. - - Input bstr() bsstr() - '-0' '0' '0E1' - ' -123 123 123' '-123123123' '-123123123E0' - '00.0123' '0.0123' '123E-4' - '123.45E-2' '1.2345' '12345E-4' - '10E+3' '10000' '1E4' - -Some routines (C, C, C, C, -C) return true or false, while others (C, C) -return either undef, <0, 0 or >0 and are suited for sort. - -Actual math is done by using the class defined with C<< with => Class; >> -(which defaults to BigInts) to represent the mantissa and exponent. - -The sign C is stored separately. The string 'NaN' is used to -represent the result when input arguments are not numbers, and 'inf' and -'-inf' are used to represent positive and negative infinity, respectively. - -=head2 mantissa(), exponent() and parts() - -mantissa() and exponent() return the said parts of the BigFloat -as BigInts such that: - - $m = $x->mantissa(); - $e = $x->exponent(); - $y = $m * ( 10 ** $e ); - print "ok\n" if $x == $y; - -C<< ($m,$e) = $x->parts(); >> is just a shortcut giving you both of them. - -A zero is represented and returned as C<0E1>, B C<0E0> (after Knuth). - -Currently the mantissa is reduced as much as possible, favouring higher -exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0). -This might change in the future, so do not depend on it. - -=head2 Accuracy vs. Precision - -See also: L. - -Math::BigFloat supports both precision (rounding to a certain place before or -after the dot) and accuracy (rounding to a certain number of digits). For a -full documentation, examples and tips on these topics please see the large -section about rounding in L. - -Since things like C or C<1 / 3> must presented with a limited -accuracy lest a operation consumes all resources, each operation produces -no more than the requested number of digits. - -If there is no global precision or accuracy set, B the operation in -question was not called with a requested precision or accuracy, B the -input $x has no accuracy or precision set, then a fallback parameter will -be used. For historical reasons, it is called C and can be accessed -via: - - $d = Math::BigFloat->div_scale(); # query - Math::BigFloat->div_scale($n); # set to $n digits - -The default value for C is 40. - -In case the result of one operation has more digits than specified, -it is rounded. The rounding mode taken is either the default mode, or the one -supplied to the operation after the I: - - $x = Math::BigFloat->new(2); - Math::BigFloat->accuracy(5); # 5 digits max - $y = $x->copy()->bdiv(3); # will give 0.66667 - $y = $x->copy()->bdiv(3,6); # will give 0.666667 - $y = $x->copy()->bdiv(3,6,undef,'odd'); # will give 0.666667 - Math::BigFloat->round_mode('zero'); - $y = $x->copy()->bdiv(3,6); # will also give 0.666667 - -Note that C<< Math::BigFloat->accuracy() >> and C<< Math::BigFloat->precision() >> -set the global variables, and thus B newly created number will be subject -to the global rounding B. This means that in the examples above, the -C<3> as argument to C will also get an accuracy of B<5>. - -It is less confusing to either calculate the result fully, and afterwards -round it explicitly, or use the additional parameters to the math -functions like so: - - use Math::BigFloat; - $x = Math::BigFloat->new(2); - $y = $x->copy()->bdiv(3); - print $y->bround(5),"\n"; # will give 0.66667 - - or - - use Math::BigFloat; - $x = Math::BigFloat->new(2); - $y = $x->copy()->bdiv(3,5); # will give 0.66667 - print "$y\n"; - -=head2 Rounding - -=over - -=item ffround ( +$scale ) - -Rounds to the $scale'th place left from the '.', counting from the dot. -The first digit is numbered 1. - -=item ffround ( -$scale ) - -Rounds to the $scale'th place right from the '.', counting from the dot. - -=item ffround ( 0 ) - -Rounds to an integer. - -=item fround ( +$scale ) - -Preserves accuracy to $scale digits from the left (aka significant digits) -and pads the rest with zeros. If the number is between 1 and -1, the -significant digits count from the first non-zero after the '.' - -=item fround ( -$scale ) and fround ( 0 ) - -These are effectively no-ops. - -=back - -All rounding functions take as a second parameter a rounding mode from one of -the following: 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'. - -The default rounding mode is 'even'. By using -C<< Math::BigFloat->round_mode($round_mode); >> you can get and set the default -mode for subsequent rounding. The usage of C<$Math::BigFloat::$round_mode> is -no longer supported. -The second parameter to the round functions then overrides the default -temporarily. - -The C function returns a BigInt from a Math::BigFloat. It uses -'trunc' as rounding mode to make it equivalent to: - - $x = 2.5; - $y = int($x) + 2; - -You can override this by passing the desired rounding mode as parameter to -C: - - $x = Math::BigFloat->new(2.5); - $y = $x->as_number('odd'); # $y = 3 - -=head1 METHODS - -Math::BigFloat supports all methods that Math::BigInt supports, except it -calculates non-integer results when possible. Please see L -for a full description of each method. Below are just the most important -differences: - -=over - -=item accuracy() - - $x->accuracy(5); # local for $x - CLASS->accuracy(5); # global for all members of CLASS - # Note: This also applies to new()! - - $A = $x->accuracy(); # read out accuracy that affects $x - $A = CLASS->accuracy(); # read out global accuracy - -Set or get the global or local accuracy, aka how many significant digits the -results have. If you set a global accuracy, then this also applies to new()! - -Warning! The accuracy I, e.g. once you created a number under the -influence of C<< CLASS->accuracy($A) >>, all results from math operations with -that number will also be rounded. - -In most cases, you should probably round the results explicitly using one of -L, L or L or by passing the desired accuracy -to the math operation as additional parameter: - - my $x = Math::BigInt->new(30000); - my $y = Math::BigInt->new(7); - print scalar $x->copy()->bdiv($y, 2); # print 4300 - print scalar $x->copy()->bdiv($y)->bround(2); # print 4300 - -=item precision() - - $x->precision(-2); # local for $x, round at the second - # digit right of the dot - $x->precision(2); # ditto, round at the second digit - # left of the dot - - CLASS->precision(5); # Global for all members of CLASS - # This also applies to new()! - CLASS->precision(-5); # ditto - - $P = CLASS->precision(); # read out global precision - $P = $x->precision(); # read out precision that affects $x - -Note: You probably want to use L instead. With L you -set the number of digits each result should have, with L you -set the place where to round! - -=item bdiv() - - $q = $x->bdiv($y); - ($q, $r) = $x->bdiv($y); - -In scalar context, divides $x by $y and returns the result to the given or -default accuracy/precision. In list context, does floored division -(F-division), returning an integer $q and a remainder $r so that $x = $q * $y + -$r. The remainer (modulo) is equal to what is returned by C<$x->bmod($y)>. - -=item bmod() - - $x->bmod($y); - -Returns $x modulo $y. When $x is finite, and $y is finite and non-zero, the -result is identical to the remainder after floored division (F-division). If, -in addition, both $x and $y are integers, the result is identical to the result -from Perl's % operator. - -=item bexp() - - $x->bexp($accuracy); # calculate e ** X - -Calculates the expression C where C is Euler's number. - -This method was added in v1.82 of Math::BigInt (April 2007). - -=item bnok() - - $x->bnok($y); # x over y (binomial coefficient n over k) - -Calculates the binomial coefficient n over k, also called the "choose" -function. The result is equivalent to: - - ( n ) n! - | - | = ------- - ( k ) k!(n-k)! - -This method was added in v1.84 of Math::BigInt (April 2007). - -=item bpi() - - print Math::BigFloat->bpi(100), "\n"; - -Calculate PI to N digits (including the 3 before the dot). The result is -rounded according to the current rounding mode, which defaults to "even". - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item bcos() - - my $x = Math::BigFloat->new(1); - print $x->bcos(100), "\n"; - -Calculate the cosinus of $x, modifying $x in place. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item bsin() - - my $x = Math::BigFloat->new(1); - print $x->bsin(100), "\n"; - -Calculate the sinus of $x, modifying $x in place. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item batan2() - - my $y = Math::BigFloat->new(2); - my $x = Math::BigFloat->new(3); - print $y->batan2($x), "\n"; - -Calculate the arcus tanges of C<$y> divided by C<$x>, modifying $y in place. -See also L. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item batan() - - my $x = Math::BigFloat->new(1); - print $x->batan(100), "\n"; - -Calculate the arcus tanges of $x, modifying $x in place. See also L. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item bmuladd() - - $x->bmuladd($y,$z); - -Multiply $x by $y, and then add $z to the result. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=back - -=head1 Autocreating constants - -After C all the floating point constants -in the given scope are converted to C. This conversion -happens at compile time. - -In particular - - perl -MMath::BigFloat=:constant -e 'print 2E-100,"\n"' - -prints the value of C<2E-100>. Note that without conversion of -constants the expression 2E-100 will be calculated as normal floating point -number. - -Please note that ':constant' does not affect integer constants, nor binary -nor hexadecimal constants. Use L or L to get this to -work. - -=head2 Math library - -Math with the numbers is done (by default) by a module called -Math::BigInt::Calc. This is equivalent to saying: - - use Math::BigFloat lib => 'Calc'; - -You can change this by using: - - use Math::BigFloat lib => 'GMP'; - -B: General purpose packages should not be explicit about the library -to use; let the script author decide which is best. - -Note: The keyword 'lib' will warn when the requested library could not be -loaded. To suppress the warning use 'try' instead: - - use Math::BigFloat try => 'GMP'; - -If your script works with huge numbers and Calc is too slow for them, -you can also for the loading of one of these libraries and if none -of them can be used, the code will die: - - use Math::BigFloat only => 'GMP,Pari'; - -The following would first try to find Math::BigInt::Foo, then -Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: - - use Math::BigFloat lib => 'Foo,Math::BigInt::Bar'; - -See the respective low-level library documentation for further details. - -Please note that Math::BigFloat does B use the denoted library itself, -but it merely passes the lib argument to Math::BigInt. So, instead of the need -to do: - - use Math::BigInt lib => 'GMP'; - use Math::BigFloat; - -you can roll it all into one line: - - use Math::BigFloat lib => 'GMP'; - -It is also possible to just require Math::BigFloat: - - require Math::BigFloat; - -This will load the necessary things (like BigInt) when they are needed, and -automatically. - -See L for more details than you ever wanted to know about using -a different low-level library. - -=head2 Using Math::BigInt::Lite - -For backwards compatibility reasons it is still possible to -request a different storage class for use with Math::BigFloat: - - use Math::BigFloat with => 'Math::BigInt::Lite'; - -However, this request is ignored, as the current code now uses the low-level -math library for directly storing the number parts. - -=head1 EXPORTS - -C exports nothing by default, but can export the C method: - - use Math::BigFloat qw/bpi/; - - print bpi(10), "\n"; - -=head1 CAVEATS - -Do not try to be clever to insert some operations in between switching -libraries: - - require Math::BigFloat; - my $matter = Math::BigFloat->bone() + 4; # load BigInt and Calc - Math::BigFloat->import( lib => 'Pari' ); # load Pari, too - my $anti_matter = Math::BigFloat->bone()+4; # now use Pari - -This will create objects with numbers stored in two different backend libraries, -and B will happen when you use these together: - - my $flash_and_bang = $matter + $anti_matter; # Don't do this! - -=over - -=item stringify, bstr() - -Both stringify and bstr() now drop the leading '+'. The old code would return -'+1.23', the new returns '1.23'. See the documentation in L for -reasoning and details. - -=item bdiv() - -The following will probably not print what you expect: - - print $c->bdiv(123.456),"\n"; - -It prints both quotient and remainder since print works in list context. Also, -bdiv() will modify $c, so be careful. You probably want to use - - print $c / 123.456,"\n"; - # or if you want to modify $c: - print scalar $c->bdiv(123.456),"\n"; - -instead. - -=item brsft() - -The following will probably not print what you expect: - - my $c = Math::BigFloat->new('3.14159'); - print $c->brsft(3,10),"\n"; # prints 0.00314153.1415 - -It prints both quotient and remainder, since print calls C in list -context. Also, C<< $c->brsft() >> will modify $c, so be careful. -You probably want to use - - print scalar $c->copy()->brsft(3,10),"\n"; - # or if you really want to modify $c - print scalar $c->brsft(3,10),"\n"; - -instead. - -=item Modifying and = - -Beware of: - - $x = Math::BigFloat->new(5); - $y = $x; - -It will not do what you think, e.g. making a copy of $x. Instead it just makes -a second reference to the B object and stores it in $y. Thus anything -that modifies $x will modify $y (except overloaded math operators), and vice -versa. See L for details and how to avoid that. - -=item bpow() - -C now modifies the first argument, unlike the old code which left -it alone and only returned the result. This is to be consistent with -C etc. The first will modify $x, the second one won't: - - print bpow($x,$i),"\n"; # modify $x - print $x->bpow($i),"\n"; # ditto - print $x ** $i,"\n"; # leave $x alone - -=item precision() vs. accuracy() - -A common pitfall is to use L when you want to round a result to -a certain number of digits: - - use Math::BigFloat; - - Math::BigFloat->precision(4); # does not do what you - # think it does - my $x = Math::BigFloat->new(12345); # rounds $x to "12000"! - print "$x\n"; # print "12000" - my $y = Math::BigFloat->new(3); # rounds $y to "0"! - print "$y\n"; # print "0" - $z = $x / $y; # 12000 / 0 => NaN! - print "$z\n"; - print $z->precision(),"\n"; # 4 - -Replacing L with L is probably not what you want, either: - - use Math::BigFloat; - - Math::BigFloat->accuracy(4); # enables global rounding: - my $x = Math::BigFloat->new(123456); # rounded immediately - # to "12350" - print "$x\n"; # print "123500" - my $y = Math::BigFloat->new(3); # rounded to "3 - print "$y\n"; # print "3" - print $z = $x->copy()->bdiv($y),"\n"; # 41170 - print $z->accuracy(),"\n"; # 4 - -What you want to use instead is: - - use Math::BigFloat; - - my $x = Math::BigFloat->new(123456); # no rounding - print "$x\n"; # print "123456" - my $y = Math::BigFloat->new(3); # no rounding - print "$y\n"; # print "3" - print $z = $x->copy()->bdiv($y,4),"\n"; # 41150 - print $z->accuracy(),"\n"; # undef - -In addition to computing what you expected, the last example also does B -"taint" the result with an accuracy or precision setting, which would -influence any further operation. - -=back - -=head1 BUGS - -Please report any bugs or feature requests to -C, or through the web interface at -L -(requires login). -We will be notified, and then you'll automatically be notified of progress on -your bug as I make changes. - -=head1 SUPPORT - -You can find documentation for this module with the perldoc command. - - perldoc Math::BigFloat - -You can also look for information at: - -=over 4 - -=item * RT: CPAN's request tracker - -L - -=item * AnnoCPAN: Annotated CPAN documentation - -L - -=item * CPAN Ratings - -L - -=item * Search CPAN - -L - -=item * CPAN Testers Matrix - -L - -=item * The Bignum mailing list - -=over 4 - -=item * Post to mailing list - -C - -=item * View mailing list - -L - -=item * Subscribe/Unsubscribe - -L - -=back - -=back - -=head1 LICENSE - -This program is free software; you may redistribute it and/or modify it under -the same terms as Perl itself. - -=head1 SEE ALSO - -L and L as well as the backends -L, L, and L. - -The pragmas L, L and L also might be of interest -because they solve the autoupgrading/downgrading issue, at least partly. - -=head1 AUTHORS - -Mark Biggar, overloaded interface by Ilya Zakharevich. -Completely rewritten by Tels L in 2001 - 2006, and still -at it in 2007. - -=cut diff --git a/dist/Math-BigInt/lib/Math/BigInt.pm b/dist/Math-BigInt/lib/Math/BigInt.pm deleted file mode 100644 index d990272d39..0000000000 --- a/dist/Math-BigInt/lib/Math/BigInt.pm +++ /dev/null @@ -1,5556 +0,0 @@ -package Math::BigInt; - -# -# "Mike had an infinite amount to do and a negative amount of time in which -# to do it." - Before and After -# - -# The following hash values are used: -# value: unsigned int with actual value (as a Math::BigInt::Calc or similar) -# sign : +,-,NaN,+inf,-inf -# _a : accuracy -# _p : precision -# _f : flags, used by MBF to flag parts of a float as untouchable - -# Remember not to take shortcuts ala $xs = $x->{value}; $CALC->foo($xs); since -# underlying lib might change the reference! - -my $class = "Math::BigInt"; -use 5.006002; - -$VERSION = '1.999701'; - -@ISA = qw(Exporter); -@EXPORT_OK = qw(objectify bgcd blcm); - -# _trap_inf and _trap_nan are internal and should never be accessed from the -# outside -use vars qw/$round_mode $accuracy $precision $div_scale $rnd_mode - $upgrade $downgrade $_trap_nan $_trap_inf/; -use strict; - -# Inside overload, the first arg is always an object. If the original code had -# it reversed (like $x = 2 * $y), then the third parameter is true. -# In some cases (like add, $x = $x + 2 is the same as $x = 2 + $x) this makes -# no difference, but in some cases it does. - -# For overloaded ops with only one argument we simple use $_[0]->copy() to -# preserve the argument. - -# Thus inheritance of overload operators becomes possible and transparent for -# our subclasses without the need to repeat the entire overload section there. - -# We register ops that are not registerable yet, so suppress warnings -{ no warnings; -use overload -'=' => sub { $_[0]->copy(); }, - -# some shortcuts for speed (assumes that reversed order of arguments is routed -# to normal '+' and we thus can always modify first arg. If this is changed, -# this breaks and must be adjusted.) -'+=' => sub { $_[0]->badd($_[1]); }, -'-=' => sub { $_[0]->bsub($_[1]); }, -'*=' => sub { $_[0]->bmul($_[1]); }, -'/=' => sub { scalar $_[0]->bdiv($_[1]); }, -'%=' => sub { $_[0]->bmod($_[1]); }, -'^=' => sub { $_[0]->bxor($_[1]); }, -'&=' => sub { $_[0]->band($_[1]); }, -'|=' => sub { $_[0]->bior($_[1]); }, - -'**=' => sub { $_[0]->bpow($_[1]); }, -'<<=' => sub { $_[0]->blsft($_[1]); }, -'>>=' => sub { $_[0]->brsft($_[1]); }, - -# not supported by Perl yet -'..' => \&_pointpoint, - -'<=>' => sub { my $rc = $_[2] ? - ref($_[0])->bcmp($_[1],$_[0]) : - $_[0]->bcmp($_[1]); - $rc = 1 unless defined $rc; - $rc <=> 0; - }, -# we need '>=' to get things like "1 >= NaN" right: -'>=' => sub { my $rc = $_[2] ? - ref($_[0])->bcmp($_[1],$_[0]) : - $_[0]->bcmp($_[1]); - # if there was a NaN involved, return false - return '' unless defined $rc; - $rc >= 0; - }, -'cmp' => sub { - $_[2] ? - "$_[1]" cmp $_[0]->bstr() : - $_[0]->bstr() cmp "$_[1]" }, - -'cos' => sub { $_[0]->copy->bcos(); }, -'sin' => sub { $_[0]->copy->bsin(); }, -'atan2' => sub { $_[2] ? - ref($_[0])->new($_[1])->batan2($_[0]) : - $_[0]->copy()->batan2($_[1]) }, - -# are not yet overloadable -#'hex' => sub { print "hex"; $_[0]; }, -#'oct' => sub { print "oct"; $_[0]; }, - -# log(N) is log(N, e), where e is Euler's number -'log' => sub { $_[0]->copy()->blog($_[1], undef); }, -'exp' => sub { $_[0]->copy()->bexp($_[1]); }, -'int' => sub { $_[0]->copy(); }, -'neg' => sub { $_[0]->copy()->bneg(); }, -'abs' => sub { $_[0]->copy()->babs(); }, -'sqrt' => sub { $_[0]->copy()->bsqrt(); }, -'~' => sub { $_[0]->copy()->bnot(); }, - -# for subtract it's a bit tricky to not modify b: b-a => -a+b -'-' => sub { my $c = $_[0]->copy; $_[2] ? - $c->bneg()->badd( $_[1]) : - $c->bsub( $_[1]) }, -'+' => sub { $_[0]->copy()->badd($_[1]); }, -'*' => sub { $_[0]->copy()->bmul($_[1]); }, - -'/' => sub { - $_[2] ? ref($_[0])->new($_[1])->bdiv($_[0]) : $_[0]->copy->bdiv($_[1]); - }, -'%' => sub { - $_[2] ? ref($_[0])->new($_[1])->bmod($_[0]) : $_[0]->copy->bmod($_[1]); - }, -'**' => sub { - $_[2] ? ref($_[0])->new($_[1])->bpow($_[0]) : $_[0]->copy->bpow($_[1]); - }, -'<<' => sub { - $_[2] ? ref($_[0])->new($_[1])->blsft($_[0]) : $_[0]->copy->blsft($_[1]); - }, -'>>' => sub { - $_[2] ? ref($_[0])->new($_[1])->brsft($_[0]) : $_[0]->copy->brsft($_[1]); - }, -'&' => sub { - $_[2] ? ref($_[0])->new($_[1])->band($_[0]) : $_[0]->copy->band($_[1]); - }, -'|' => sub { - $_[2] ? ref($_[0])->new($_[1])->bior($_[0]) : $_[0]->copy->bior($_[1]); - }, -'^' => sub { - $_[2] ? ref($_[0])->new($_[1])->bxor($_[0]) : $_[0]->copy->bxor($_[1]); - }, - -# can modify arg of ++ and --, so avoid a copy() for speed, but don't -# use $_[0]->bone(), it would modify $_[0] to be 1! -'++' => sub { $_[0]->binc() }, -'--' => sub { $_[0]->bdec() }, - -# if overloaded, O(1) instead of O(N) and twice as fast for small numbers -'bool' => sub { - # this kludge is needed for perl prior 5.6.0 since returning 0 here fails :-/ - # v5.6.1 dumps on this: return !$_[0]->is_zero() || undef; :-( - my $t = undef; - $t = 1 if !$_[0]->is_zero(); - $t; - }, - -# the original qw() does not work with the TIESCALAR below, why? -# Order of arguments insignificant -'""' => sub { $_[0]->bstr(); }, -'0+' => sub { $_[0]->numify(); } -; -} # no warnings scope - -############################################################################## -# global constants, flags and accessory - -# These vars are public, but their direct usage is not recommended, use the -# accessor methods instead - -$round_mode = 'even'; # one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common' -$accuracy = undef; -$precision = undef; -$div_scale = 40; - -$upgrade = undef; # default is no upgrade -$downgrade = undef; # default is no downgrade - -# These are internally, and not to be used from the outside at all - -$_trap_nan = 0; # are NaNs ok? set w/ config() -$_trap_inf = 0; # are infs ok? set w/ config() -my $nan = 'NaN'; # constants for easier life - -my $CALC = 'Math::BigInt::Calc'; # module to do the low level math - # default is Calc.pm -my $IMPORT = 0; # was import() called yet? - # used to make require work -my %WARN; # warn only once for low-level libs -my %CAN; # cache for $CALC->can(...) -my %CALLBACKS; # callbacks to notify on lib loads -my $EMU_LIB = 'Math/BigInt/CalcEmu.pm'; # emulate low-level math - -############################################################################## -# the old code had $rnd_mode, so we need to support it, too - -$rnd_mode = 'even'; -sub TIESCALAR { my ($class) = @_; bless \$round_mode, $class; } -sub FETCH { return $round_mode; } -sub STORE { $rnd_mode = $_[0]->round_mode($_[1]); } - -BEGIN - { - # tie to enable $rnd_mode to work transparently - tie $rnd_mode, 'Math::BigInt'; - - # set up some handy alias names - *as_int = \&as_number; - *is_pos = \&is_positive; - *is_neg = \&is_negative; - } - -############################################################################## - -sub round_mode - { - no strict 'refs'; - # make Class->round_mode() work - my $self = shift; - my $class = ref($self) || $self || __PACKAGE__; - if (defined $_[0]) - { - my $m = shift; - if ($m !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) - { - require Carp; Carp::croak ("Unknown round mode '$m'"); - } - return ${"${class}::round_mode"} = $m; - } - ${"${class}::round_mode"}; - } - -sub upgrade - { - no strict 'refs'; - # make Class->upgrade() work - my $self = shift; - my $class = ref($self) || $self || __PACKAGE__; - # need to set new value? - if (@_ > 0) - { - return ${"${class}::upgrade"} = $_[0]; - } - ${"${class}::upgrade"}; - } - -sub downgrade - { - no strict 'refs'; - # make Class->downgrade() work - my $self = shift; - my $class = ref($self) || $self || __PACKAGE__; - # need to set new value? - if (@_ > 0) - { - return ${"${class}::downgrade"} = $_[0]; - } - ${"${class}::downgrade"}; - } - -sub div_scale - { - no strict 'refs'; - # make Class->div_scale() work - my $self = shift; - my $class = ref($self) || $self || __PACKAGE__; - if (defined $_[0]) - { - if ($_[0] < 0) - { - require Carp; Carp::croak ('div_scale must be greater than zero'); - } - ${"${class}::div_scale"} = $_[0]; - } - ${"${class}::div_scale"}; - } - -sub accuracy - { - # $x->accuracy($a); ref($x) $a - # $x->accuracy(); ref($x) - # Class->accuracy(); class - # Class->accuracy($a); class $a - - my $x = shift; - my $class = ref($x) || $x || __PACKAGE__; - - no strict 'refs'; - # need to set new value? - if (@_ > 0) - { - my $a = shift; - # convert objects to scalars to avoid deep recursion. If object doesn't - # have numify(), then hopefully it will have overloading for int() and - # boolean test without wandering into a deep recursion path... - $a = $a->numify() if ref($a) && $a->can('numify'); - - if (defined $a) - { - # also croak on non-numerical - if (!$a || $a <= 0) - { - require Carp; - Carp::croak ('Argument to accuracy must be greater than zero'); - } - if (int($a) != $a) - { - require Carp; - Carp::croak ('Argument to accuracy must be an integer'); - } - } - if (ref($x)) - { - # $object->accuracy() or fallback to global - $x->bround($a) if $a; # not for undef, 0 - $x->{_a} = $a; # set/overwrite, even if not rounded - delete $x->{_p}; # clear P - $a = ${"${class}::accuracy"} unless defined $a; # proper return value - } - else - { - ${"${class}::accuracy"} = $a; # set global A - ${"${class}::precision"} = undef; # clear global P - } - return $a; # shortcut - } - - my $a; - # $object->accuracy() or fallback to global - $a = $x->{_a} if ref($x); - # but don't return global undef, when $x's accuracy is 0! - $a = ${"${class}::accuracy"} if !defined $a; - $a; - } - -sub precision - { - # $x->precision($p); ref($x) $p - # $x->precision(); ref($x) - # Class->precision(); class - # Class->precision($p); class $p - - my $x = shift; - my $class = ref($x) || $x || __PACKAGE__; - - no strict 'refs'; - if (@_ > 0) - { - my $p = shift; - # convert objects to scalars to avoid deep recursion. If object doesn't - # have numify(), then hopefully it will have overloading for int() and - # boolean test without wandering into a deep recursion path... - $p = $p->numify() if ref($p) && $p->can('numify'); - if ((defined $p) && (int($p) != $p)) - { - require Carp; Carp::croak ('Argument to precision must be an integer'); - } - if (ref($x)) - { - # $object->precision() or fallback to global - $x->bfround($p) if $p; # not for undef, 0 - $x->{_p} = $p; # set/overwrite, even if not rounded - delete $x->{_a}; # clear A - $p = ${"${class}::precision"} unless defined $p; # proper return value - } - else - { - ${"${class}::precision"} = $p; # set global P - ${"${class}::accuracy"} = undef; # clear global A - } - return $p; # shortcut - } - - my $p; - # $object->precision() or fallback to global - $p = $x->{_p} if ref($x); - # but don't return global undef, when $x's precision is 0! - $p = ${"${class}::precision"} if !defined $p; - $p; - } - -sub config - { - # return (or set) configuration data as hash ref - my $class = shift || 'Math::BigInt'; - - no strict 'refs'; - if (@_ > 1 || (@_ == 1 && (ref($_[0]) eq 'HASH'))) - { - # try to set given options as arguments from hash - - my $args = $_[0]; - if (ref($args) ne 'HASH') - { - $args = { @_ }; - } - # these values can be "set" - my $set_args = {}; - foreach my $key ( - qw/trap_inf trap_nan - upgrade downgrade precision accuracy round_mode div_scale/ - ) - { - $set_args->{$key} = $args->{$key} if exists $args->{$key}; - delete $args->{$key}; - } - if (keys %$args > 0) - { - require Carp; - Carp::croak ("Illegal key(s) '", - join("','",keys %$args),"' passed to $class\->config()"); - } - foreach my $key (keys %$set_args) - { - if ($key =~ /^trap_(inf|nan)\z/) - { - ${"${class}::_trap_$1"} = ($set_args->{"trap_$1"} ? 1 : 0); - next; - } - # use a call instead of just setting the $variable to check argument - $class->$key($set_args->{$key}); - } - } - - # now return actual configuration - - my $cfg = { - lib => $CALC, - lib_version => ${"${CALC}::VERSION"}, - class => $class, - trap_nan => ${"${class}::_trap_nan"}, - trap_inf => ${"${class}::_trap_inf"}, - version => ${"${class}::VERSION"}, - }; - foreach my $key (qw/ - upgrade downgrade precision accuracy round_mode div_scale - /) - { - $cfg->{$key} = ${"${class}::$key"}; - }; - if (@_ == 1 && (ref($_[0]) ne 'HASH')) - { - # calls of the style config('lib') return just this value - return $cfg->{$_[0]}; - } - $cfg; - } - -sub _scale_a - { - # select accuracy parameter based on precedence, - # used by bround() and bfround(), may return undef for scale (means no op) - my ($x,$scale,$mode) = @_; - - $scale = $x->{_a} unless defined $scale; - - no strict 'refs'; - my $class = ref($x); - - $scale = ${ $class . '::accuracy' } unless defined $scale; - $mode = ${ $class . '::round_mode' } unless defined $mode; - - if (defined $scale) - { - $scale = $scale->can('numify') ? $scale->numify() - : "$scale" if ref($scale); - $scale = int($scale); - } - - ($scale,$mode); - } - -sub _scale_p - { - # select precision parameter based on precedence, - # used by bround() and bfround(), may return undef for scale (means no op) - my ($x,$scale,$mode) = @_; - - $scale = $x->{_p} unless defined $scale; - - no strict 'refs'; - my $class = ref($x); - - $scale = ${ $class . '::precision' } unless defined $scale; - $mode = ${ $class . '::round_mode' } unless defined $mode; - - if (defined $scale) - { - $scale = $scale->can('numify') ? $scale->numify() - : "$scale" if ref($scale); - $scale = int($scale); - } - - ($scale,$mode); - } - -############################################################################## -# constructors - -sub copy - { - # if two arguments, the first one is the class to "swallow" subclasses - if (@_ > 1) - { - my $self = bless { - sign => $_[1]->{sign}, - value => $CALC->_copy($_[1]->{value}), - }, $_[0] if @_ > 1; - - $self->{_a} = $_[1]->{_a} if defined $_[1]->{_a}; - $self->{_p} = $_[1]->{_p} if defined $_[1]->{_p}; - return $self; - } - - my $self = bless { - sign => $_[0]->{sign}, - value => $CALC->_copy($_[0]->{value}), - }, ref($_[0]); - - $self->{_a} = $_[0]->{_a} if defined $_[0]->{_a}; - $self->{_p} = $_[0]->{_p} if defined $_[0]->{_p}; - $self; - } - -sub new - { - # create a new BigInt object from a string or another BigInt object. - # see hash keys documented at top - - # the argument could be an object, so avoid ||, && etc on it, this would - # cause costly overloaded code to be called. The only allowed ops are - # ref() and defined. - - my ($class,$wanted,$a,$p,$r) = @_; - - # avoid numify-calls by not using || on $wanted! - return $class->bzero($a,$p) if !defined $wanted; # default to 0 - return $class->copy($wanted,$a,$p,$r) - if ref($wanted) && $wanted->isa($class); # MBI or subclass - - $class->import() if $IMPORT == 0; # make require work - - my $self = bless {}, $class; - - # shortcut for "normal" numbers - if ((!ref $wanted) && ($wanted =~ /^([+-]?)[1-9][0-9]*\z/)) - { - $self->{sign} = $1 || '+'; - - if ($wanted =~ /^[+-]/) - { - # remove sign without touching wanted to make it work with constants - my $t = $wanted; $t =~ s/^[+-]//; - $self->{value} = $CALC->_new($t); - } - else - { - $self->{value} = $CALC->_new($wanted); - } - no strict 'refs'; - if ( (defined $a) || (defined $p) - || (defined ${"${class}::precision"}) - || (defined ${"${class}::accuracy"}) - ) - { - $self->round($a,$p,$r) unless (@_ == 4 && !defined $a && !defined $p); - } - return $self; - } - - # handle '+inf', '-inf' first - if ($wanted =~ /^[+-]?inf\z/) - { - $self->{sign} = $wanted; # set a default sign for bstr() - return $self->binf($wanted); - } - # split str in m mantissa, e exponent, i integer, f fraction, v value, s sign - my ($mis,$miv,$mfv,$es,$ev) = _split($wanted); - if (!ref $mis) - { - if ($_trap_nan) - { - require Carp; Carp::croak("$wanted is not a number in $class"); - } - $self->{value} = $CALC->_zero(); - $self->{sign} = $nan; - return $self; - } - if (!ref $miv) - { - # _from_hex or _from_bin - $self->{value} = $mis->{value}; - $self->{sign} = $mis->{sign}; - return $self; # throw away $mis - } - # make integer from mantissa by adjusting exp, then convert to bigint - $self->{sign} = $$mis; # store sign - $self->{value} = $CALC->_zero(); # for all the NaN cases - my $e = int("$$es$$ev"); # exponent (avoid recursion) - if ($e > 0) - { - my $diff = $e - CORE::length($$mfv); - if ($diff < 0) # Not integer - { - if ($_trap_nan) - { - require Carp; Carp::croak("$wanted not an integer in $class"); - } - #print "NOI 1\n"; - return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; - $self->{sign} = $nan; - } - else # diff >= 0 - { - # adjust fraction and add it to value - #print "diff > 0 $$miv\n"; - $$miv = $$miv . ($$mfv . '0' x $diff); - } - } - else - { - if ($$mfv ne '') # e <= 0 - { - # fraction and negative/zero E => NOI - if ($_trap_nan) - { - require Carp; Carp::croak("$wanted not an integer in $class"); - } - #print "NOI 2 \$\$mfv '$$mfv'\n"; - return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; - $self->{sign} = $nan; - } - elsif ($e < 0) - { - # xE-y, and empty mfv - # Split the mantissa at the decimal point. E.g., if - # $$miv = 12345 and $e = -2, then $frac = 45 and $$miv = 123. - - my $frac = substr($$miv, $e); # $frac is fraction part - substr($$miv, $e) = ""; # $$miv is now integer part - - if ($frac =~ /[^0]/) - { - if ($_trap_nan) - { - require Carp; Carp::croak("$wanted not an integer in $class"); - } - #print "NOI 3\n"; - return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; - $self->{sign} = $nan; - } - } - } - unless ($self->{sign} eq $nan) { - $self->{sign} = '+' if $$miv eq '0'; # normalize -0 => +0 - $self->{value} = $CALC->_new($$miv) if $self->{sign} =~ /^[+-]$/; - } - # if any of the globals is set, use them to round and store them inside $self - # do not round for new($x,undef,undef) since that is used by MBF to signal - # no rounding - $self->round($a,$p,$r) unless @_ == 4 && !defined $a && !defined $p; - $self; - } - -sub bnan - { - # create a bigint 'NaN', if given a BigInt, set it to 'NaN' - my $self = shift; - $self = $class if !defined $self; - if (!ref($self)) - { - my $c = $self; $self = {}; bless $self, $c; - } - no strict 'refs'; - if (${"${class}::_trap_nan"}) - { - require Carp; - Carp::croak ("Tried to set $self to NaN in $class\::bnan()"); - } - $self->import() if $IMPORT == 0; # make require work - return if $self->modify('bnan'); - if ($self->can('_bnan')) - { - # use subclass to initialize - $self->_bnan(); - } - else - { - # otherwise do our own thing - $self->{value} = $CALC->_zero(); - } - $self->{sign} = $nan; - delete $self->{_a}; delete $self->{_p}; # rounding NaN is silly - $self; - } - -sub binf - { - # create a bigint '+-inf', if given a BigInt, set it to '+-inf' - # the sign is either '+', or if given, used from there - my $self = shift; - my $sign = shift; $sign = '+' if !defined $sign || $sign !~ /^-(inf)?$/; - $self = $class if !defined $self; - if (!ref($self)) - { - my $c = $self; $self = {}; bless $self, $c; - } - no strict 'refs'; - if (${"${class}::_trap_inf"}) - { - require Carp; - Carp::croak ("Tried to set $self to +-inf in $class\::binf()"); - } - $self->import() if $IMPORT == 0; # make require work - return if $self->modify('binf'); - if ($self->can('_binf')) - { - # use subclass to initialize - $self->_binf(); - } - else - { - # otherwise do our own thing - $self->{value} = $CALC->_zero(); - } - $sign = $sign . 'inf' if $sign !~ /inf$/; # - => -inf - $self->{sign} = $sign; - ($self->{_a},$self->{_p}) = @_; # take over requested rounding - $self; - } - -sub bzero - { - # create a bigint '+0', if given a BigInt, set it to 0 - my $self = shift; - $self = __PACKAGE__ if !defined $self; - - if (!ref($self)) - { - my $c = $self; $self = {}; bless $self, $c; - } - $self->import() if $IMPORT == 0; # make require work - return if $self->modify('bzero'); - - if ($self->can('_bzero')) - { - # use subclass to initialize - $self->_bzero(); - } - else - { - # otherwise do our own thing - $self->{value} = $CALC->_zero(); - } - $self->{sign} = '+'; - if (@_ > 0) - { - if (@_ > 3) - { - # call like: $x->bzero($a,$p,$r,$y); - ($self,$self->{_a},$self->{_p}) = $self->_find_round_parameters(@_); - } - else - { - $self->{_a} = $_[0] - if ( (!defined $self->{_a}) || (defined $_[0] && $_[0] > $self->{_a})); - $self->{_p} = $_[1] - if ( (!defined $self->{_p}) || (defined $_[1] && $_[1] > $self->{_p})); - } - } - $self; - } - -sub bone - { - # create a bigint '+1' (or -1 if given sign '-'), - # if given a BigInt, set it to +1 or -1, respectively - my $self = shift; - my $sign = shift; $sign = '+' if !defined $sign || $sign ne '-'; - $self = $class if !defined $self; - - if (!ref($self)) - { - my $c = $self; $self = {}; bless $self, $c; - } - $self->import() if $IMPORT == 0; # make require work - return if $self->modify('bone'); - - if ($self->can('_bone')) - { - # use subclass to initialize - $self->_bone(); - } - else - { - # otherwise do our own thing - $self->{value} = $CALC->_one(); - } - $self->{sign} = $sign; - if (@_ > 0) - { - if (@_ > 3) - { - # call like: $x->bone($sign,$a,$p,$r,$y); - ($self,$self->{_a},$self->{_p}) = $self->_find_round_parameters(@_); - } - else - { - # call like: $x->bone($sign,$a,$p,$r); - $self->{_a} = $_[0] - if ( (!defined $self->{_a}) || (defined $_[0] && $_[0] > $self->{_a})); - $self->{_p} = $_[1] - if ( (!defined $self->{_p}) || (defined $_[1] && $_[1] > $self->{_p})); - } - } - $self; - } - -############################################################################## -# string conversion - -sub bsstr - { - # (ref to BFLOAT or num_str ) return num_str - # Convert number from internal format to scientific string format. - # internal format is always normalized (no leading zeros, "-0E0" => "+0E0") - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN - return 'inf'; # +inf - } - my ($m,$e) = $x->parts(); - #$m->bstr() . 'e+' . $e->bstr(); # e can only be positive in BigInt - # 'e+' because E can only be positive in BigInt - $m->bstr() . 'e+' . $CALC->_str($e->{value}); - } - -sub bstr - { - # make a string from bigint object - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN - return 'inf'; # +inf - } - my $es = ''; $es = $x->{sign} if $x->{sign} eq '-'; - $es.$CALC->_str($x->{value}); - } - -sub numify - { - # Make a "normal" scalar from a BigInt object - my $x = shift; $x = $class->new($x) unless ref $x; - - return $x->bstr() if $x->{sign} !~ /^[+-]$/; - my $num = $CALC->_num($x->{value}); - return -$num if $x->{sign} eq '-'; - $num; - } - -############################################################################## -# public stuff (usually prefixed with "b") - -sub sign - { - # return the sign of the number: +/-/-inf/+inf/NaN - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - $x->{sign}; - } - -sub _find_round_parameters - { - # After any operation or when calling round(), the result is rounded by - # regarding the A & P from arguments, local parameters, or globals. - - # !!!!!!! If you change this, remember to change round(), too! !!!!!!!!!! - - # This procedure finds the round parameters, but it is for speed reasons - # duplicated in round. Otherwise, it is tested by the testsuite and used - # by fdiv(). - - # returns ($self) or ($self,$a,$p,$r) - sets $self to NaN of both A and P - # were requested/defined (locally or globally or both) - - my ($self,$a,$p,$r,@args) = @_; - # $a accuracy, if given by caller - # $p precision, if given by caller - # $r round_mode, if given by caller - # @args all 'other' arguments (0 for unary, 1 for binary ops) - - my $c = ref($self); # find out class of argument(s) - no strict 'refs'; - - # convert to normal scalar for speed and correctness in inner parts - $a = $a->can('numify') ? $a->numify() : "$a" if defined $a && ref($a); - $p = $p->can('numify') ? $p->numify() : "$p" if defined $p && ref($p); - - # now pick $a or $p, but only if we have got "arguments" - if (!defined $a) - { - foreach ($self,@args) - { - # take the defined one, or if both defined, the one that is smaller - $a = $_->{_a} if (defined $_->{_a}) && (!defined $a || $_->{_a} < $a); - } - } - if (!defined $p) - { - # even if $a is defined, take $p, to signal error for both defined - foreach ($self,@args) - { - # take the defined one, or if both defined, the one that is bigger - # -2 > -3, and 3 > 2 - $p = $_->{_p} if (defined $_->{_p}) && (!defined $p || $_->{_p} > $p); - } - } - # if still none defined, use globals (#2) - $a = ${"$c\::accuracy"} unless defined $a; - $p = ${"$c\::precision"} unless defined $p; - - # A == 0 is useless, so undef it to signal no rounding - $a = undef if defined $a && $a == 0; - - # no rounding today? - return ($self) unless defined $a || defined $p; # early out - - # set A and set P is an fatal error - return ($self->bnan()) if defined $a && defined $p; # error - - $r = ${"$c\::round_mode"} unless defined $r; - if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) - { - require Carp; Carp::croak ("Unknown round mode '$r'"); - } - - $a = int($a) if defined $a; - $p = int($p) if defined $p; - - ($self,$a,$p,$r); - } - -sub round - { - # Round $self according to given parameters, or given second argument's - # parameters or global defaults - - # for speed reasons, _find_round_parameters is embedded here: - - my ($self,$a,$p,$r,@args) = @_; - # $a accuracy, if given by caller - # $p precision, if given by caller - # $r round_mode, if given by caller - # @args all 'other' arguments (0 for unary, 1 for binary ops) - - my $c = ref($self); # find out class of argument(s) - no strict 'refs'; - - # now pick $a or $p, but only if we have got "arguments" - if (!defined $a) - { - foreach ($self,@args) - { - # take the defined one, or if both defined, the one that is smaller - $a = $_->{_a} if (defined $_->{_a}) && (!defined $a || $_->{_a} < $a); - } - } - if (!defined $p) - { - # even if $a is defined, take $p, to signal error for both defined - foreach ($self,@args) - { - # take the defined one, or if both defined, the one that is bigger - # -2 > -3, and 3 > 2 - $p = $_->{_p} if (defined $_->{_p}) && (!defined $p || $_->{_p} > $p); - } - } - # if still none defined, use globals (#2) - $a = ${"$c\::accuracy"} unless defined $a; - $p = ${"$c\::precision"} unless defined $p; - - # A == 0 is useless, so undef it to signal no rounding - $a = undef if defined $a && $a == 0; - - # no rounding today? - return $self unless defined $a || defined $p; # early out - - # set A and set P is an fatal error - return $self->bnan() if defined $a && defined $p; - - $r = ${"$c\::round_mode"} unless defined $r; - if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/) - { - require Carp; Carp::croak ("Unknown round mode '$r'"); - } - - # now round, by calling either fround or ffround: - if (defined $a) - { - $self->bround(int($a),$r) if !defined $self->{_a} || $self->{_a} >= $a; - } - else # both can't be undefined due to early out - { - $self->bfround(int($p),$r) if !defined $self->{_p} || $self->{_p} <= $p; - } - # bround() or bfround() already called bnorm() if nec. - $self; - } - -sub bnorm - { - # (numstr or BINT) return BINT - # Normalize number -- no-op here - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - $x; - } - -sub babs - { - # (BINT or num_str) return BINT - # make number absolute, or return absolute BINT from string - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x if $x->modify('babs'); - # post-normalized abs for internal use (does nothing for NaN) - $x->{sign} =~ s/^-/+/; - $x; - } - -sub bsgn { - # Signum function. - - my $self = shift; - - return $self if $self->modify('bsgn'); - - return $self -> bone("+") if $self -> is_pos(); - return $self -> bone("-") if $self -> is_neg(); - return $self; # zero or NaN -} - -sub bneg - { - # (BINT or num_str) return BINT - # negate number or make a negated number from string - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x if $x->modify('bneg'); - - # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN' - $x->{sign} =~ tr/+-/-+/ unless ($x->{sign} eq '+' && $CALC->_is_zero($x->{value})); - $x; - } - -sub bcmp - { - # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort) - # (BINT or num_str, BINT or num_str) return cond_code - - # set up parameters - my ($self,$x,$y) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y) = objectify(2,@_); - } - - return $upgrade->bcmp($x,$y) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) - { - # handle +-inf and NaN - return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; - return +1 if $x->{sign} eq '+inf'; - return -1 if $x->{sign} eq '-inf'; - return -1 if $y->{sign} eq '+inf'; - return +1; - } - # check sign for speed first - return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y - return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0 - - # have same sign, so compare absolute values. Don't make tests for zero - # here because it's actually slower than testing in Calc (especially w/ Pari - # et al) - - # post-normalized compare for internal use (honors signs) - if ($x->{sign} eq '+') - { - # $x and $y both > 0 - return $CALC->_acmp($x->{value},$y->{value}); - } - - # $x && $y both < 0 - $CALC->_acmp($y->{value},$x->{value}); # swapped acmp (lib returns 0,1,-1) - } - -sub bacmp - { - # Compares 2 values, ignoring their signs. - # Returns one of undef, <0, =0, >0. (suitable for sort) - # (BINT, BINT) return cond_code - - # set up parameters - my ($self,$x,$y) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y) = objectify(2,@_); - } - - return $upgrade->bacmp($x,$y) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) - { - # handle +-inf and NaN - return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/; - return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/; - return -1; - } - $CALC->_acmp($x->{value},$y->{value}); # lib does only 0,1,-1 - } - -sub badd - { - # add second arg (BINT or string) to first (BINT) (modifies first) - # return result as BINT - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('badd'); - return $upgrade->badd($upgrade->new($x),$upgrade->new($y),@r) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - $r[3] = $y; # no push! - # inf and NaN handling - if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) - { - # NaN first - return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - # inf handling - if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) - { - # +inf++inf or -inf+-inf => same, rest is NaN - return $x if $x->{sign} eq $y->{sign}; - return $x->bnan(); - } - # +-inf + something => +inf - # something +-inf => +-inf - $x->{sign} = $y->{sign}, return $x if $y->{sign} =~ /^[+-]inf$/; - return $x; - } - - my ($sx, $sy) = ( $x->{sign}, $y->{sign} ); # get signs - - if ($sx eq $sy) - { - $x->{value} = $CALC->_add($x->{value},$y->{value}); # same sign, abs add - } - else - { - my $a = $CALC->_acmp ($y->{value},$x->{value}); # absolute compare - if ($a > 0) - { - $x->{value} = $CALC->_sub($y->{value},$x->{value},1); # abs sub w/ swap - $x->{sign} = $sy; - } - elsif ($a == 0) - { - # speedup, if equal, set result to 0 - $x->{value} = $CALC->_zero(); - $x->{sign} = '+'; - } - else # a < 0 - { - $x->{value} = $CALC->_sub($x->{value}, $y->{value}); # abs sub - } - } - $x->round(@r); - } - -sub bsub - { - # (BINT or num_str, BINT or num_str) return BINT - # subtract second arg from first, modify first - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bsub'); - - return $upgrade->new($x)->bsub($upgrade->new($y),@r) if defined $upgrade && - ((!$x->isa($self)) || (!$y->isa($self))); - - return $x->round(@r) if $y->is_zero(); - - # To correctly handle the lone special case $x->bsub($x), we note the sign - # of $x, then flip the sign from $y, and if the sign of $x did change, too, - # then we caught the special case: - my $xsign = $x->{sign}; - $y->{sign} =~ tr/+\-/-+/; # does nothing for NaN - if ($xsign ne $x->{sign}) - { - # special case of $x->bsub($x) results in 0 - return $x->bzero(@r) if $xsign =~ /^[+-]$/; - return $x->bnan(); # NaN, -inf, +inf - } - $x->badd($y,@r); # badd does not leave internal zeros - $y->{sign} =~ tr/+\-/-+/; # refix $y (does nothing for NaN) - $x; # already rounded by badd() or no round nec. - } - -sub binc - { - # increment arg by one - my ($self,$x,$a,$p,$r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - return $x if $x->modify('binc'); - - if ($x->{sign} eq '+') - { - $x->{value} = $CALC->_inc($x->{value}); - return $x->round($a,$p,$r); - } - elsif ($x->{sign} eq '-') - { - $x->{value} = $CALC->_dec($x->{value}); - $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # -1 +1 => -0 => +0 - return $x->round($a,$p,$r); - } - # inf, nan handling etc - $x->badd($self->bone(),$a,$p,$r); # badd does round - } - -sub bdec - { - # decrement arg by one - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - return $x if $x->modify('bdec'); - - if ($x->{sign} eq '-') - { - # x already < 0 - $x->{value} = $CALC->_inc($x->{value}); - } - else - { - return $x->badd($self->bone('-'),@r) - unless $x->{sign} eq '+'; # inf or NaN - # >= 0 - if ($CALC->_is_zero($x->{value})) - { - # == 0 - $x->{value} = $CALC->_one(); $x->{sign} = '-'; # 0 => -1 - } - else - { - # > 0 - $x->{value} = $CALC->_dec($x->{value}); - } - } - $x->round(@r); - } - -sub blog - { - # calculate $x = $a ** $base + $b and return $a (e.g. the log() to base - # $base of $x) - - # set up parameters - my ($self,$x,$base,@r) = (undef,@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$base,@r) = objectify(2,@_); - } - - return $x if $x->modify('blog'); - - $base = $self->new($base) if defined $base && !ref $base; - - # inf, -inf, NaN, <0 => NaN - return $x->bnan() - if $x->{sign} ne '+' || (defined $base && $base->{sign} ne '+'); - - return $upgrade->blog($upgrade->new($x),$base,@r) if - defined $upgrade; - - # fix for bug #24969: - # the default base is e (Euler's number) which is not an integer - if (!defined $base) - { - require Math::BigFloat; - my $u = Math::BigFloat->blog(Math::BigFloat->new($x))->as_int(); - # modify $x in place - $x->{value} = $u->{value}; - $x->{sign} = $u->{sign}; - return $x; - } - - my ($rc,$exact) = $CALC->_log_int($x->{value},$base->{value}); - return $x->bnan() unless defined $rc; # not possible to take log? - $x->{value} = $rc; - $x->round(@r); - } - -sub bnok - { - # Calculate n over k (binomial coefficient or "choose" function) as integer. - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bnok'); - return $x->bnan() if $x->{sign} eq 'NaN' || $y->{sign} eq 'NaN'; - return $x->binf() if $x->{sign} eq '+inf'; - - # k > n or k < 0 => 0 - my $cmp = $x->bacmp($y); - return $x->bzero() if $cmp < 0 || $y->{sign} =~ /^-/; - # k == n => 1 - return $x->bone(@r) if $cmp == 0; - - if ($CALC->can('_nok')) - { - $x->{value} = $CALC->_nok($x->{value},$y->{value}); - } - else - { - # ( 7 ) 7! 1*2*3*4 * 5*6*7 5 * 6 * 7 6 7 - # ( - ) = --------- = --------------- = --------- = 5 * - * - - # ( 3 ) (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3 2 3 - - if (!$y->is_zero()) - { - my $z = $x - $y; - $z->binc(); - my $r = $z->copy(); $z->binc(); - my $d = $self->new(2); - while ($z->bacmp($x) <= 0) # f <= x ? - { - $r->bmul($z); $r->bdiv($d); - $z->binc(); $d->binc(); - } - $x->{value} = $r->{value}; $x->{sign} = '+'; - } - else { $x->bone(); } - } - $x->round(@r); - } - -sub bexp - { - # Calculate e ** $x (Euler's number to the power of X), truncated to - # an integer value. - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - return $x if $x->modify('bexp'); - - # inf, -inf, NaN, <0 => NaN - return $x->bnan() if $x->{sign} eq 'NaN'; - return $x->bone() if $x->is_zero(); - return $x if $x->{sign} eq '+inf'; - return $x->bzero() if $x->{sign} eq '-inf'; - - my $u; - { - # run through Math::BigFloat unless told otherwise - require Math::BigFloat unless defined $upgrade; - local $upgrade = 'Math::BigFloat' unless defined $upgrade; - # calculate result, truncate it to integer - $u = $upgrade->bexp($upgrade->new($x),@r); - } - - if (!defined $upgrade) - { - $u = $u->as_int(); - # modify $x in place - $x->{value} = $u->{value}; - $x->round(@r); - } - else { $x = $u; } - } - -sub blcm - { - # (BINT or num_str, BINT or num_str) return BINT - # does not modify arguments, but returns new object - # Lowest Common Multiple - - my $y = shift; my ($x); - if (ref($y)) - { - $x = $y->copy(); - } - else - { - $x = $class->new($y); - } - my $self = ref($x); - while (@_) - { - my $y = shift; $y = $self->new($y) if !ref ($y); - $x = __lcm($x,$y); - } - $x; - } - -sub bgcd - { - # (BINT or num_str, BINT or num_str) return BINT - # does not modify arguments, but returns new object - # GCD -- Euclid's algorithm, variant C (Knuth Vol 3, pg 341 ff) - - my $y = shift; - $y = $class->new($y) if !ref($y); - my $self = ref($y); - my $x = $y->copy()->babs(); # keep arguments - return $x->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN? - - while (@_) - { - $y = shift; $y = $self->new($y) if !ref($y); - return $x->bnan() if $y->{sign} !~ /^[+-]$/; # y NaN? - $x->{value} = $CALC->_gcd($x->{value},$y->{value}); - last if $CALC->_is_one($x->{value}); - } - $x; - } - -sub bnot - { - # (num_str or BINT) return BINT - # represent ~x as twos-complement number - # we don't need $self, so undef instead of ref($_[0]) make it slightly faster - my ($self,$x,$a,$p,$r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - return $x if $x->modify('bnot'); - $x->binc()->bneg(); # binc already does round - } - -############################################################################## -# is_foo test routines -# we don't need $self, so undef instead of ref($_[0]) make it slightly faster - -sub is_zero - { - # return true if arg (BINT or num_str) is zero (array '+', '0') - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return 0 if $x->{sign} !~ /^\+$/; # -, NaN & +-inf aren't - $CALC->_is_zero($x->{value}); - } - -sub is_nan - { - # return true if arg (BINT or num_str) is NaN - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - $x->{sign} eq $nan ? 1 : 0; - } - -sub is_inf - { - # return true if arg (BINT or num_str) is +-inf - my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - if (defined $sign) - { - $sign = '[+-]inf' if $sign eq ''; # +- doesn't matter, only that's inf - $sign = "[$1]inf" if $sign =~ /^([+-])(inf)?$/; # extract '+' or '-' - return $x->{sign} =~ /^$sign$/ ? 1 : 0; - } - $x->{sign} =~ /^[+-]inf$/ ? 1 : 0; # only +-inf is infinity - } - -sub is_one - { - # return true if arg (BINT or num_str) is +1, or -1 if sign is given - my ($self,$x,$sign) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - $sign = '+' if !defined $sign || $sign ne '-'; - - return 0 if $x->{sign} ne $sign; # -1 != +1, NaN, +-inf aren't either - $CALC->_is_one($x->{value}); - } - -sub is_odd - { - # return true when arg (BINT or num_str) is odd, false for even - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't - $CALC->_is_odd($x->{value}); - } - -sub is_even - { - # return true when arg (BINT or num_str) is even, false for odd - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't - $CALC->_is_even($x->{value}); - } - -sub is_positive - { - # return true when arg (BINT or num_str) is positive (> 0) - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return 1 if $x->{sign} eq '+inf'; # +inf is positive - - # 0+ is neither positive nor negative - ($x->{sign} eq '+' && !$x->is_zero()) ? 1 : 0; - } - -sub is_negative - { - # return true when arg (BINT or num_str) is negative (< 0) - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - $x->{sign} =~ /^-/ ? 1 : 0; # -inf is negative, but NaN is not - } - -sub is_int - { - # return true when arg (BINT or num_str) is an integer - # always true for BigInt, but different for BigFloats - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - $x->{sign} =~ /^[+-]$/ ? 1 : 0; # inf/-inf/NaN aren't - } - -############################################################################### - -sub bmul - { - # multiply the first number by the second number - # (BINT or num_str, BINT or num_str) return BINT - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bmul'); - - return $x->bnan() if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - - # inf handling - if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) - { - return $x->bnan() if $x->is_zero() || $y->is_zero(); - # result will always be +-inf: - # +inf * +/+inf => +inf, -inf * -/-inf => +inf - # +inf * -/-inf => -inf, -inf * +/+inf => -inf - return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); - return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); - return $x->binf('-'); - } - - return $upgrade->bmul($x,$upgrade->new($y),@r) - if defined $upgrade && !$y->isa($self); - - $r[3] = $y; # no push here - - $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => + - - $x->{value} = $CALC->_mul($x->{value},$y->{value}); # do actual math - $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # no -0 - - $x->round(@r); - } - -sub bmuladd - { - # multiply two numbers and then add the third to the result - # (BINT or num_str, BINT or num_str, BINT or num_str) return BINT - - # set up parameters - my ($self,$x,$y,$z,@r) = objectify(3,@_); - - return $x if $x->modify('bmuladd'); - - return $x->bnan() if ($x->{sign} eq $nan) || - ($y->{sign} eq $nan) || - ($z->{sign} eq $nan); - - # inf handling of x and y - if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) - { - return $x->bnan() if $x->is_zero() || $y->is_zero(); - # result will always be +-inf: - # +inf * +/+inf => +inf, -inf * -/-inf => +inf - # +inf * -/-inf => -inf, -inf * +/+inf => -inf - return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); - return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); - return $x->binf('-'); - } - # inf handling x*y and z - if (($z->{sign} =~ /^[+-]inf$/)) - { - # something +-inf => +-inf - $x->{sign} = $z->{sign}, return $x if $z->{sign} =~ /^[+-]inf$/; - } - - return $upgrade->bmuladd($x,$upgrade->new($y),$upgrade->new($z),@r) - if defined $upgrade && (!$y->isa($self) || !$z->isa($self) || !$x->isa($self)); - - # TODO: what if $y and $z have A or P set? - $r[3] = $z; # no push here - - $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => + - - $x->{value} = $CALC->_mul($x->{value},$y->{value}); # do actual math - $x->{sign} = '+' if $CALC->_is_zero($x->{value}); # no -0 - - my ($sx, $sz) = ( $x->{sign}, $z->{sign} ); # get signs - - if ($sx eq $sz) - { - $x->{value} = $CALC->_add($x->{value},$z->{value}); # same sign, abs add - } - else - { - my $a = $CALC->_acmp ($z->{value},$x->{value}); # absolute compare - if ($a > 0) - { - $x->{value} = $CALC->_sub($z->{value},$x->{value},1); # abs sub w/ swap - $x->{sign} = $sz; - } - elsif ($a == 0) - { - # speedup, if equal, set result to 0 - $x->{value} = $CALC->_zero(); - $x->{sign} = '+'; - } - else # a < 0 - { - $x->{value} = $CALC->_sub($x->{value}, $z->{value}); # abs sub - } - } - $x->round(@r); - } - -sub bdiv - { - - # This does floored division, where the quotient is floored toward negative - # infinity and the remainder has the same sign as the divisor. - - # Set up parameters. - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - # objectify() is costly, so avoid it if we can. - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bdiv'); - - my $wantarray = wantarray; # call only once - - # At least one argument is NaN. Return NaN for both quotient and the - # modulo/remainder. - - if ($x -> is_nan() || $y -> is_nan()) { - return $wantarray ? ($x -> bnan(), $self -> bnan()) : $x -> bnan(); - } - - # Divide by zero and modulo zero. - # - # Division: Use the common convention that x / 0 is inf with the same sign - # as x, except when x = 0, where we return NaN. This is also what earlier - # versions did. - # - # Modulo: In modular arithmetic, the congruence relation z = x (mod y) - # means that there is some integer k such that z - x = k y. If y = 0, we - # get z - x = 0 or z = x. This is also what earlier versions did, except - # that 0 % 0 returned NaN. - # - # inf / 0 = inf inf % 0 = inf - # 5 / 0 = inf 5 % 0 = 5 - # 0 / 0 = NaN 0 % 0 = 0 (before: NaN) - # -5 / 0 = -inf -5 % 0 = -5 - # -inf / 0 = -inf -inf % 0 = -inf - - if ($y -> is_zero()) { - my ($quo, $rem); - if ($wantarray) { - $rem = $x -> copy(); - } - if ($x -> is_zero()) { - $quo = $x -> bnan(); - } else { - $quo = $x -> binf($x -> {sign}); - } - return $wantarray ? ($quo, $rem) : $quo; - } - - # Numerator (dividend) is +/-inf, and denominator is finite and non-zero. - # The divide by zero cases are covered above. In all of the cases listed - # below we return the same as core Perl. - # - # inf / -inf = NaN inf % -inf = NaN - # inf / -5 = -inf inf % -5 = NaN (before: 0) - # inf / 5 = inf inf % 5 = NaN (before: 0) - # inf / inf = NaN inf % inf = NaN - # - # -inf / -inf = NaN -inf % -inf = NaN - # -inf / -5 = inf -inf % -5 = NaN (before: 0) - # -inf / 5 = -inf -inf % 5 = NaN (before: 0) - # -inf / inf = NaN -inf % inf = NaN - - if ($x -> is_inf()) { - my ($quo, $rem); - $rem = $self -> bnan() if $wantarray; - if ($y -> is_inf()) { - $quo = $x -> bnan(); - } else { - my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-'; - $quo = $x -> binf($sign); - } - return $wantarray ? ($quo, $rem) : $quo; - } - - # Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf - # are covered above. In the modulo cases (in the right column) we return - # the same as core Perl, which does floored division, so for consistency we - # also do floored division in the division cases (in the left column). - # - # -5 / inf = -1 (before: 0) -5 % inf = inf (before: -5) - # 0 / inf = 0 0 % inf = 0 - # 5 / inf = 0 5 % inf = 5 - # - # -5 / -inf = 0 -5 % -inf = -5 - # 0 / -inf = 0 0 % -inf = 0 - # 5 / -inf = -1 (before: 0) 5 % -inf = -inf (before: 5) - - if ($y -> is_inf()) { - my ($quo, $rem); - if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { - $rem = $x -> copy() if $wantarray; - $quo = $x -> bzero(); - } else { - $rem = $self -> binf($y -> {sign}) if $wantarray; - $quo = $x -> bone('-'); - } - return $wantarray ? ($quo, $rem) : $quo; - } - - # At this point, both the numerator and denominator are finite numbers, and - # the denominator (divisor) is non-zero. - - return $upgrade->bdiv($upgrade->new($x),$upgrade->new($y),@r) - if defined $upgrade; - - $r[3] = $y; # no push! - - # Inialize remainder. - - my $rem = $self->bzero(); - - # Are both operands the same object, i.e., like $x -> bdiv($x)? - # If so, flipping the sign of $y also flips the sign of $x. - - my $xsign = $x->{sign}; - my $ysign = $y->{sign}; - - $y->{sign} =~ tr/+-/-+/; # Flip the sign of $y, and see ... - my $same = $xsign ne $x->{sign}; # ... if that changed the sign of $x. - $y->{sign} = $ysign; # Re-insert the original sign. - - if ($same) { - $x -> bone(); - } else { - ($x->{value},$rem->{value}) = $CALC->_div($x->{value},$y->{value}); - - if ($CALC -> _is_zero($rem->{value})) { - if ($xsign eq $ysign || $CALC -> _is_zero($x->{value})) { - $x->{sign} = '+'; - } else { - $x->{sign} = '-'; - } - } else { - if ($xsign eq $ysign) { - $x->{sign} = '+'; - } else { - if ($xsign eq '+') { - $x -> badd(1); - } else { - $x -> bsub(1); - } - $x->{sign} = '-'; - } - } - } - - $x->round(@r); - - if ($wantarray) { - unless ($CALC -> _is_zero($rem->{value})) { - if ($xsign ne $ysign) { - $rem = $y -> copy() -> babs() -> bsub($rem); - } - $rem->{sign} = $ysign; - } - $rem->{_a} = $x->{_a}; - $rem->{_p} = $x->{_p}; - $rem->round(@r); - return ($x,$rem); - } - - return $x; - } - -############################################################################### -# modulus functions - -sub bmod - { - - # This is the remainder after floored division, where the quotient is - # floored toward negative infinity and the remainder has the same sign as - # the divisor. - - # Set up parameters. - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bmod'); - $r[3] = $y; # no push! - - # At least one argument is NaN. - - if ($x -> is_nan() || $y -> is_nan()) { - return $x -> bnan(); - } - - # Modulo zero. See documentation for bdiv(). - - if ($y -> is_zero()) { - return $x; - } - - # Numerator (dividend) is +/-inf. - - if ($x -> is_inf()) { - return $x -> bnan(); - } - - # Denominator (divisor) is +/-inf. - - if ($y -> is_inf()) { - if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { - return $x; - } else { - return $x -> binf($y -> sign()); - } - } - - # Calc new sign and in case $y == +/- 1, return $x. - - $x->{value} = $CALC->_mod($x->{value},$y->{value}); - if ($CALC -> _is_zero($x->{value})) - { - $x->{sign} = '+'; # do not leave -0 - } - else - { - $x->{value} = $CALC->_sub($y->{value},$x->{value},1) # $y-$x - if ($x->{sign} ne $y->{sign}); - $x->{sign} = $y->{sign}; - } - - $x->round(@r); - } - -sub bmodinv - { - # Return modular multiplicative inverse: - # - # z is the modular inverse of x (mod y) if and only if - # - # x*z ≡ 1 (mod y) - # - # If the modulus y is larger than one, x and z are relative primes (i.e., - # their greatest common divisor is one). - # - # If no modular multiplicative inverse exists, NaN is returned. - - # set up parameters - my ($self,$x,$y,@r) = (undef,@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bmodinv'); - - # Return NaN if one or both arguments is +inf, -inf, or nan. - - return $x->bnan() if ($y->{sign} !~ /^[+-]$/ || - $x->{sign} !~ /^[+-]$/); - - # Return NaN if $y is zero; 1 % 0 makes no sense. - - return $x->bnan() if $y->is_zero(); - - # Return 0 in the trivial case. $x % 1 or $x % -1 is zero for all finite - # integers $x. - - return $x->bzero() if ($y->is_one() || - $y->is_one('-')); - - # Return NaN if $x = 0, or $x modulo $y is zero. The only valid case when - # $x = 0 is when $y = 1 or $y = -1, but that was covered above. - # - # Note that computing $x modulo $y here affects the value we'll feed to - # $CALC->_modinv() below when $x and $y have opposite signs. E.g., if $x = - # 5 and $y = 7, those two values are fed to _modinv(), but if $x = -5 and - # $y = 7, the values fed to _modinv() are $x = 2 (= -5 % 7) and $y = 7. - # The value if $x is affected only when $x and $y have opposite signs. - - $x->bmod($y); - return $x->bnan() if $x->is_zero(); - - # Compute the modular multiplicative inverse of the absolute values. We'll - # correct for the signs of $x and $y later. Return NaN if no GCD is found. - - ($x->{value}, $x->{sign}) = $CALC->_modinv($x->{value}, $y->{value}); - return $x->bnan() if !defined $x->{value}; - - # Library inconsistency workaround: _modinv() in Math::BigInt::GMP versions - # <= 1.32 return undef rather than a "+" for the sign. - - $x->{sign} = '+' unless defined $x->{sign}; - - # When one or both arguments are negative, we have the following - # relations. If x and y are positive: - # - # modinv(-x, -y) = -modinv(x, y) - # modinv(-x, y) = y - modinv(x, y) = -modinv(x, y) (mod y) - # modinv( x, -y) = modinv(x, y) - y = modinv(x, y) (mod -y) - - # We must swap the sign of the result if the original $x is negative. - # However, we must compensate for ignoring the signs when computing the - # inverse modulo. The net effect is that we must swap the sign of the - # result if $y is negative. - - $x -> bneg() if $y->{sign} eq '-'; - - # Compute $x modulo $y again after correcting the sign. - - $x -> bmod($y) if $x->{sign} ne $y->{sign}; - - return $x; - } - -sub bmodpow - { - # Modular exponentiation. Raises a very large number to a very large exponent - # in a given very large modulus quickly, thanks to binary exponentiation. - # Supports negative exponents. - my ($self,$num,$exp,$mod,@r) = objectify(3,@_); - - return $num if $num->modify('bmodpow'); - - # When the exponent 'e' is negative, use the following relation, which is - # based on finding the multiplicative inverse 'd' of 'b' modulo 'm': - # - # b^(-e) (mod m) = d^e (mod m) where b*d = 1 (mod m) - - $num->bmodinv($mod) if ($exp->{sign} eq '-'); - - # Check for valid input. All operands must be finite, and the modulus must be - # non-zero. - - return $num->bnan() if ($num->{sign} =~ /NaN|inf/ || # NaN, -inf, +inf - $exp->{sign} =~ /NaN|inf/ || # NaN, -inf, +inf - $mod->{sign} =~ /NaN|inf/); # NaN, -inf, +inf - - # Modulo zero. See documentation for Math::BigInt's bmod() method. - - if ($mod -> is_zero()) { - if ($num -> is_zero()) { - return $self -> bnan(); - } else { - return $num -> copy(); - } - } - - # Compute 'a (mod m)', ignoring the signs on 'a' and 'm'. If the resulting - # value is zero, the output is also zero, regardless of the signs on 'a' and - # 'm'. - - my $value = $CALC->_modpow($num->{value}, $exp->{value}, $mod->{value}); - my $sign = '+'; - - # If the resulting value is non-zero, we have four special cases, depending - # on the signs on 'a' and 'm'. - - unless ($CALC->_is_zero($value)) { - - # There is a negative sign on 'a' (= $num**$exp) only if the number we - # are exponentiating ($num) is negative and the exponent ($exp) is odd. - - if ($num->{sign} eq '-' && $exp->is_odd()) { - - # When both the number 'a' and the modulus 'm' have a negative sign, - # use this relation: - # - # -a (mod -m) = -(a (mod m)) - - if ($mod->{sign} eq '-') { - $sign = '-'; - } - - # When only the number 'a' has a negative sign, use this relation: - # - # -a (mod m) = m - (a (mod m)) - - else { - # Use copy of $mod since _sub() modifies the first argument. - my $mod = $CALC->_copy($mod->{value}); - $value = $CALC->_sub($mod, $value); - $sign = '+'; - } - - } else { - - # When only the modulus 'm' has a negative sign, use this relation: - # - # a (mod -m) = (a (mod m)) - m - # = -(m - (a (mod m))) - - if ($mod->{sign} eq '-') { - # Use copy of $mod since _sub() modifies the first argument. - my $mod = $CALC->_copy($mod->{value}); - $value = $CALC->_sub($mod, $value); - $sign = '-'; - } - - # When neither the number 'a' nor the modulus 'm' have a negative - # sign, directly return the already computed value. - # - # (a (mod m)) - - } - - } - - $num->{value} = $value; - $num->{sign} = $sign; - - return $num; - } - -############################################################################### - -sub bfac - { - # (BINT or num_str, BINT or num_str) return BINT - # compute factorial number from $x, modify $x in place - my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - return $x if $x->modify('bfac') || $x->{sign} eq '+inf'; # inf => inf - return $x->bnan() if $x->{sign} ne '+'; # NaN, <0 etc => NaN - - $x->{value} = $CALC->_fac($x->{value}); - $x->round(@r); - } - -sub bpow - { - # (BINT or num_str, BINT or num_str) return BINT - # compute power of two numbers -- stolen from Knuth Vol 2 pg 233 - # modifies first argument - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bpow'); - - return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; - - # inf handling - if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) - { - if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) - { - # +-inf ** +-inf - return $x->bnan(); - } - # +-inf ** Y - if ($x->{sign} =~ /^[+-]inf/) - { - # +inf ** 0 => NaN - return $x->bnan() if $y->is_zero(); - # -inf ** -1 => 1/inf => 0 - return $x->bzero() if $y->is_one('-') && $x->is_negative(); - - # +inf ** Y => inf - return $x if $x->{sign} eq '+inf'; - - # -inf ** Y => -inf if Y is odd - return $x if $y->is_odd(); - return $x->babs(); - } - # X ** +-inf - - # 1 ** +inf => 1 - return $x if $x->is_one(); - - # 0 ** inf => 0 - return $x if $x->is_zero() && $y->{sign} =~ /^[+]/; - - # 0 ** -inf => inf - return $x->binf() if $x->is_zero(); - - # -1 ** -inf => NaN - return $x->bnan() if $x->is_one('-') && $y->{sign} =~ /^[-]/; - - # -X ** -inf => 0 - return $x->bzero() if $x->{sign} eq '-' && $y->{sign} =~ /^[-]/; - - # -1 ** inf => NaN - return $x->bnan() if $x->{sign} eq '-'; - - # X ** inf => inf - return $x->binf() if $y->{sign} =~ /^[+]/; - # X ** -inf => 0 - return $x->bzero(); - } - - return $upgrade->bpow($upgrade->new($x),$y,@r) - if defined $upgrade && (!$y->isa($self) || $y->{sign} eq '-'); - - $r[3] = $y; # no push! - - # cases 0 ** Y, X ** 0, X ** 1, 1 ** Y are handled by Calc or Emu - - my $new_sign = '+'; - $new_sign = $y->is_odd() ? '-' : '+' if ($x->{sign} ne '+'); - - # 0 ** -7 => ( 1 / (0 ** 7)) => 1 / 0 => +inf - return $x->binf() - if $y->{sign} eq '-' && $x->{sign} eq '+' && $CALC->_is_zero($x->{value}); - # 1 ** -y => 1 / (1 ** |y|) - # so do test for negative $y after above's clause - return $x->bnan() if $y->{sign} eq '-' && !$CALC->_is_one($x->{value}); - - $x->{value} = $CALC->_pow($x->{value},$y->{value}); - $x->{sign} = $new_sign; - $x->{sign} = '+' if $CALC->_is_zero($y->{value}); - $x->round(@r); - } - -sub blsft - { - # (BINT or num_str, BINT or num_str) return BINT - # compute x << y, base n, y >= 0 - - # set up parameters - my ($self,$x,$y,$n,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$n,@r) = objectify(2,@_); - } - - return $x if $x->modify('blsft'); - return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); - return $x->round(@r) if $y->is_zero(); - - $n = 2 if !defined $n; return $x->bnan() if $n <= 0 || $y->{sign} eq '-'; - - $x->{value} = $CALC->_lsft($x->{value},$y->{value},$n); - $x->round(@r); - } - -sub brsft - { - # (BINT or num_str, BINT or num_str) return BINT - # compute x >> y, base n, y >= 0 - - # set up parameters - my ($self,$x,$y,$n,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$n,@r) = objectify(2,@_); - } - - return $x if $x->modify('brsft'); - return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); - return $x->round(@r) if $y->is_zero(); - return $x->bzero(@r) if $x->is_zero(); # 0 => 0 - - $n = 2 if !defined $n; return $x->bnan() if $n <= 0 || $y->{sign} eq '-'; - - # this only works for negative numbers when shifting in base 2 - if (($x->{sign} eq '-') && ($n == 2)) - { - return $x->round(@r) if $x->is_one('-'); # -1 => -1 - if (!$y->is_one()) - { - # although this is O(N*N) in calc (as_bin!) it is O(N) in Pari et al - # but perhaps there is a better emulation for two's complement shift... - # if $y != 1, we must simulate it by doing: - # convert to bin, flip all bits, shift, and be done - $x->binc(); # -3 => -2 - my $bin = $x->as_bin(); - $bin =~ s/^-0b//; # strip '-0b' prefix - $bin =~ tr/10/01/; # flip bits - # now shift - if ($y >= CORE::length($bin)) - { - $bin = '0'; # shifting to far right creates -1 - # 0, because later increment makes - # that 1, attached '-' makes it '-1' - # because -1 >> x == -1 ! - } - else - { - $bin =~ s/.{$y}$//; # cut off at the right side - $bin = '1' . $bin; # extend left side by one dummy '1' - $bin =~ tr/10/01/; # flip bits back - } - my $res = $self->new('0b'.$bin); # add prefix and convert back - $res->binc(); # remember to increment - $x->{value} = $res->{value}; # take over value - return $x->round(@r); # we are done now, magic, isn't? - } - # x < 0, n == 2, y == 1 - $x->bdec(); # n == 2, but $y == 1: this fixes it - } - - $x->{value} = $CALC->_rsft($x->{value},$y->{value},$n); - $x->round(@r); - } - -sub band - { - #(BINT or num_str, BINT or num_str) return BINT - # compute x & y - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('band'); - - $r[3] = $y; # no push! - - return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); - - my $sx = $x->{sign} eq '+' ? 1 : -1; - my $sy = $y->{sign} eq '+' ? 1 : -1; - - if ($sx == 1 && $sy == 1) - { - $x->{value} = $CALC->_and($x->{value},$y->{value}); - return $x->round(@r); - } - - if ($CAN{signed_and}) - { - $x->{value} = $CALC->_signed_and($x->{value},$y->{value},$sx,$sy); - return $x->round(@r); - } - - require $EMU_LIB; - __emu_band($self,$x,$y,$sx,$sy,@r); - } - -sub bior - { - #(BINT or num_str, BINT or num_str) return BINT - # compute x | y - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bior'); - $r[3] = $y; # no push! - - return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); - - my $sx = $x->{sign} eq '+' ? 1 : -1; - my $sy = $y->{sign} eq '+' ? 1 : -1; - - # the sign of X follows the sign of X, e.g. sign of Y irrelevant for bior() - - # don't use lib for negative values - if ($sx == 1 && $sy == 1) - { - $x->{value} = $CALC->_or($x->{value},$y->{value}); - return $x->round(@r); - } - - # if lib can do negative values, let it handle this - if ($CAN{signed_or}) - { - $x->{value} = $CALC->_signed_or($x->{value},$y->{value},$sx,$sy); - return $x->round(@r); - } - - require $EMU_LIB; - __emu_bior($self,$x,$y,$sx,$sy,@r); - } - -sub bxor - { - #(BINT or num_str, BINT or num_str) return BINT - # compute x ^ y - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bxor'); - $r[3] = $y; # no push! - - return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); - - my $sx = $x->{sign} eq '+' ? 1 : -1; - my $sy = $y->{sign} eq '+' ? 1 : -1; - - # don't use lib for negative values - if ($sx == 1 && $sy == 1) - { - $x->{value} = $CALC->_xor($x->{value},$y->{value}); - return $x->round(@r); - } - - # if lib can do negative values, let it handle this - if ($CAN{signed_xor}) - { - $x->{value} = $CALC->_signed_xor($x->{value},$y->{value},$sx,$sy); - return $x->round(@r); - } - - require $EMU_LIB; - __emu_bxor($self,$x,$y,$sx,$sy,@r); - } - -sub length - { - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - my $e = $CALC->_len($x->{value}); - wantarray ? ($e,0) : $e; - } - -sub digit - { - # return the nth decimal digit, negative values count backward, 0 is right - my ($self,$x,$n) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - $n = $n->numify() if ref($n); - $CALC->_digit($x->{value},$n||0); - } - -sub _trailing_zeros - { - # return the amount of trailing zeros in $x (as scalar) - my $x = shift; - $x = $class->new($x) unless ref $x; - - return 0 if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf etc - - $CALC->_zeros($x->{value}); # must handle odd values, 0 etc - } - -sub bsqrt - { - # calculate square root of $x - my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - return $x if $x->modify('bsqrt'); - - return $x->bnan() if $x->{sign} !~ /^\+/; # -x or -inf or NaN => NaN - return $x if $x->{sign} eq '+inf'; # sqrt(+inf) == inf - - return $upgrade->bsqrt($x,@r) if defined $upgrade; - - $x->{value} = $CALC->_sqrt($x->{value}); - $x->round(@r); - } - -sub broot - { - # calculate $y'th root of $x - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - $y = $self->new(2) unless defined $y; - - # objectify is costly, so avoid it - if ((!ref($x)) || (ref($x) ne ref($y))) - { - ($self,$x,$y,@r) = objectify(2,$self || $class,@_); - } - - return $x if $x->modify('broot'); - - # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0 - return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() || - $y->{sign} !~ /^\+$/; - - return $x->round(@r) - if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one(); - - return $upgrade->new($x)->broot($upgrade->new($y),@r) if defined $upgrade; - - $x->{value} = $CALC->_root($x->{value},$y->{value}); - $x->round(@r); - } - -sub exponent - { - # return a copy of the exponent (here always 0, NaN or 1 for $m == 0) - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - my $s = $x->{sign}; $s =~ s/^[+-]//; # NaN, -inf,+inf => NaN or inf - return $self->new($s); - } - return $self->bone() if $x->is_zero(); - - # 12300 => 2 trailing zeros => exponent is 2 - $self->new( $CALC->_zeros($x->{value}) ); - } - -sub mantissa - { - # return the mantissa (compatible to Math::BigFloat, e.g. reduced) - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) - { - # for NaN, +inf, -inf: keep the sign - return $self->new($x->{sign}); - } - my $m = $x->copy(); delete $m->{_p}; delete $m->{_a}; - - # that's a bit inefficient: - my $zeros = $CALC->_zeros($m->{value}); - $m->brsft($zeros,10) if $zeros != 0; - $m; - } - -sub parts - { - # return a copy of both the exponent and the mantissa - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - ($x->mantissa(),$x->exponent()); - } - -############################################################################## -# rounding functions - -sub bfround - { - # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.' - # $n == 0 || $n == 1 => round to integer - my $x = shift; my $self = ref($x) || $x; $x = $self->new($x) unless ref $x; - - my ($scale,$mode) = $x->_scale_p(@_); - - return $x if !defined $scale || $x->modify('bfround'); # no-op - - # no-op for BigInts if $n <= 0 - $x->bround( $x->length()-$scale, $mode) if $scale > 0; - - delete $x->{_a}; # delete to save memory - $x->{_p} = $scale; # store new _p - $x; - } - -sub _scan_for_nonzero - { - # internal, used by bround() to scan for non-zeros after a '5' - my ($x,$pad,$xs,$len) = @_; - - return 0 if $len == 1; # "5" is trailed by invisible zeros - my $follow = $pad - 1; - return 0 if $follow > $len || $follow < 1; - - # use the string form to check whether only '0's follow or not - substr ($xs,-$follow) =~ /[^0]/ ? 1 : 0; - } - -sub fround - { - # Exists to make life easier for switch between MBF and MBI (should we - # autoload fxxx() like MBF does for bxxx()?) - my $x = shift; $x = $class->new($x) unless ref $x; - $x->bround(@_); - } - -sub bround - { - # accuracy: +$n preserve $n digits from left, - # -$n preserve $n digits from right (f.i. for 0.1234 style in MBF) - # no-op for $n == 0 - # and overwrite the rest with 0's, return normalized number - # do not return $x->bnorm(), but $x - - my $x = shift; $x = $class->new($x) unless ref $x; - my ($scale,$mode) = $x->_scale_a(@_); - return $x if !defined $scale || $x->modify('bround'); # no-op - - if ($x->is_zero() || $scale == 0) - { - $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; # 3 > 2 - return $x; - } - return $x if $x->{sign} !~ /^[+-]$/; # inf, NaN - - # we have fewer digits than we want to scale to - my $len = $x->length(); - # convert $scale to a scalar in case it is an object (put's a limit on the - # number length, but this would already limited by memory constraints), makes - # it faster - $scale = $scale->numify() if ref ($scale); - - # scale < 0, but > -len (not >=!) - if (($scale < 0 && $scale < -$len-1) || ($scale >= $len)) - { - $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; # 3 > 2 - return $x; - } - - # count of 0's to pad, from left (+) or right (-): 9 - +6 => 3, or |-6| => 6 - my ($pad,$digit_round,$digit_after); - $pad = $len - $scale; - $pad = abs($scale-1) if $scale < 0; - - # do not use digit(), it is very costly for binary => decimal - # getting the entire string is also costly, but we need to do it only once - my $xs = $CALC->_str($x->{value}); - my $pl = -$pad-1; - - # pad: 123: 0 => -1, at 1 => -2, at 2 => -3, at 3 => -4 - # pad+1: 123: 0 => 0, at 1 => -1, at 2 => -2, at 3 => -3 - $digit_round = '0'; $digit_round = substr($xs,$pl,1) if $pad <= $len; - $pl++; $pl ++ if $pad >= $len; - $digit_after = '0'; $digit_after = substr($xs,$pl,1) if $pad > 0; - - # in case of 01234 we round down, for 6789 up, and only in case 5 we look - # closer at the remaining digits of the original $x, remember decision - my $round_up = 1; # default round up - $round_up -- if - ($mode eq 'trunc') || # trunc by round down - ($digit_after =~ /[01234]/) || # round down anyway, - # 6789 => round up - ($digit_after eq '5') && # not 5000...0000 - ($x->_scan_for_nonzero($pad,$xs,$len) == 0) && - ( - ($mode eq 'even') && ($digit_round =~ /[24680]/) || - ($mode eq 'odd') && ($digit_round =~ /[13579]/) || - ($mode eq '+inf') && ($x->{sign} eq '-') || - ($mode eq '-inf') && ($x->{sign} eq '+') || - ($mode eq 'zero') # round down if zero, sign adjusted below - ); - my $put_back = 0; # not yet modified - - if (($pad > 0) && ($pad <= $len)) - { - substr($xs,-$pad,$pad) = '0' x $pad; # replace with '00...' - $put_back = 1; # need to put back - } - elsif ($pad > $len) - { - $x->bzero(); # round to '0' - } - - if ($round_up) # what gave test above? - { - $put_back = 1; # need to put back - $pad = $len, $xs = '0' x $pad if $scale < 0; # tlr: whack 0.51=>1.0 - - # we modify directly the string variant instead of creating a number and - # adding it, since that is faster (we already have the string) - my $c = 0; $pad ++; # for $pad == $len case - while ($pad <= $len) - { - $c = substr($xs,-$pad,1) + 1; $c = '0' if $c eq '10'; - substr($xs,-$pad,1) = $c; $pad++; - last if $c != 0; # no overflow => early out - } - $xs = '1'.$xs if $c == 0; - - } - $x->{value} = $CALC->_new($xs) if $put_back == 1; # put back, if needed - - $x->{_a} = $scale if $scale >= 0; - if ($scale < 0) - { - $x->{_a} = $len+$scale; - $x->{_a} = 0 if $scale < -$len; - } - $x; - } - -sub bfloor - { - # round towards minus infinity; no-op since it's already integer - my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - $x->round(@r); - } - -sub bceil - { - # round towards plus infinity; no-op since it's already int - my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - $x->round(@r); - } - -sub bint { - # round towards zero; no-op since it's already integer - my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - $x->round(@r); -} - -sub as_number - { - # An object might be asked to return itself as bigint on certain overloaded - # operations. This does exactly this, so that sub classes can simple inherit - # it or override with their own integer conversion routine. - $_[0]->copy(); - } - -sub as_hex - { - # return as hex string, with prefixed 0x - my $x = shift; $x = $class->new($x) if !ref($x); - - return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc - - my $s = ''; - $s = $x->{sign} if $x->{sign} eq '-'; - $s . $CALC->_as_hex($x->{value}); - } - -sub as_bin - { - # return as binary string, with prefixed 0b - my $x = shift; $x = $class->new($x) if !ref($x); - - return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc - - my $s = ''; $s = $x->{sign} if $x->{sign} eq '-'; - return $s . $CALC->_as_bin($x->{value}); - } - -sub as_oct - { - # return as octal string, with prefixed 0 - my $x = shift; $x = $class->new($x) if !ref($x); - - return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc - - my $s = ''; $s = $x->{sign} if $x->{sign} eq '-'; - return $s . $CALC->_as_oct($x->{value}); - } - -############################################################################## -# private stuff (internal use only) - -sub objectify { - # Convert strings and "foreign objects" to the objects we want. - - # The first argument, $count, is the number of following arguments that - # objectify() looks at and converts to objects. The first is a classname. - # If the given count is 0, all arguments will be used. - - # After the count is read, objectify obtains the name of the class to which - # the following arguments are converted. If the second argument is a - # reference, use the reference type as the class name. Otherwise, if it is - # a string that looks like a class name, use that. Otherwise, use $class. - - # Caller: Gives us: - # - # $x->badd(1); => ref x, scalar y - # Class->badd(1,2); => classname x (scalar), scalar x, scalar y - # Class->badd(Class->(1),2); => classname x (scalar), ref x, scalar y - # Math::BigInt::badd(1,2); => scalar x, scalar y - - # A shortcut for the common case $x->unary_op(): - - return (ref($_[1]), $_[1]) if (@_ == 2) && ($_[0]||0 == 1) && ref($_[1]); - - # Check the context. - - unless (wantarray) { - require Carp; - Carp::croak ("${class}::objectify() needs list context"); - } - - # Get the number of arguments to objectify. - - my $count = shift; - $count ||= @_; - - # Initialize the output array. - - my @a = @_; - - # If the first argument is a reference, use that reference type as our - # class name. Otherwise, if the first argument looks like a class name, - # then use that as our class name. Otherwise, use the default class name. - - { - if (ref($a[0])) { # reference? - unshift @a, ref($a[0]); - last; - } - if ($a[0] =~ /^[A-Z].*::/) { # string with class name? - last; - } - unshift @a, $class; # default class name - } - - no strict 'refs'; - - # What we upgrade to, if anything. - - my $up = ${"$a[0]::upgrade"}; - - # Disable downgrading, because Math::BigFloat -> foo('1.0','2.0') needs - # floats. - - my $down; - if (defined ${"$a[0]::downgrade"}) { - $down = ${"$a[0]::downgrade"}; - ${"$a[0]::downgrade"} = undef; - } - - for my $i (1 .. $count) { - my $ref = ref $a[$i]; - - # Perl scalars are fed to the appropriate constructor. - - unless ($ref) { - $a[$i] = $a[0] -> new($a[$i]); - next; - } - - # If it is an object of the right class, all is fine. - - if ($ref -> isa($a[0])) { - next; - } - - # Upgrading is OK, so skip further tests if the argument is upgraded. - - if (defined $up && $ref -> isa($up)) { - next; - } - - # If we want a Math::BigInt, see if the object can become one. - # Support the old misnomer as_number(). - - if ($a[0] eq 'Math::BigInt') { - if ($a[$i] -> can('as_int')) { - $a[$i] = $a[$i] -> as_int(); - next; - } - if ($a[$i] -> can('as_number')) { - $a[$i] = $a[$i] -> as_number(); - next; - } - } - - # If we want a Math::BigFloat, see if the object can become one. - - if ($a[0] eq 'Math::BigFloat') { - if ($a[$i] -> can('as_float')) { - $a[$i] = $a[$i] -> as_float(); - next; - } - } - - # Last resort. - - $a[$i] = $a[0] -> new($a[$i]); - } - - # Reset the downgrading. - - ${"$a[0]::downgrade"} = $down; - - return @a; -} - -sub _register_callback - { - my ($class,$callback) = @_; - - if (ref($callback) ne 'CODE') - { - require Carp; - Carp::croak ("$callback is not a coderef"); - } - $CALLBACKS{$class} = $callback; - } - -sub import - { - my $self = shift; - - $IMPORT++; # remember we did import() - my @a; my $l = scalar @_; - my $warn_or_die = 0; # 0 - no warn, 1 - warn, 2 - die - for ( my $i = 0; $i < $l ; $i++ ) - { - if ($_[$i] eq ':constant') - { - # this causes overlord er load to step in - overload::constant - integer => sub { $self->new(shift) }, - binary => sub { $self->new(shift) }; - } - elsif ($_[$i] eq 'upgrade') - { - # this causes upgrading - $upgrade = $_[$i+1]; # or undef to disable - $i++; - } - elsif ($_[$i] =~ /^(lib|try|only)\z/) - { - # this causes a different low lib to take care... - $CALC = $_[$i+1] || ''; - # lib => 1 (warn on fallback), try => 0 (no warn), only => 2 (die on fallback) - $warn_or_die = 1 if $_[$i] eq 'lib'; - $warn_or_die = 2 if $_[$i] eq 'only'; - $i++; - } - else - { - push @a, $_[$i]; - } - } - # any non :constant stuff is handled by our parent, Exporter - if (@a > 0) - { - require Exporter; - - $self->SUPER::import(@a); # need it for subclasses - $self->export_to_level(1,$self,@a); # need it for MBF - } - - # try to load core math lib - my @c = split /\s*,\s*/,$CALC; - foreach (@c) - { - $_ =~ tr/a-zA-Z0-9://cd; # limit to sane characters - } - push @c, \'Calc' # if all fail, try these - if $warn_or_die < 2; # but not for "only" - $CALC = ''; # signal error - foreach my $l (@c) - { - # fallback libraries are "marked" as \'string', extract string if nec. - my $lib = $l; $lib = $$l if ref($l); - - next if ($lib || '') eq ''; - $lib = 'Math::BigInt::'.$lib if $lib !~ /^Math::BigInt/i; - $lib =~ s/\.pm$//; - if ($] < 5.006) - { - # Perl < 5.6.0 dies with "out of memory!" when eval("") and ':constant' is - # used in the same script, or eval("") inside import(). - my @parts = split /::/, $lib; # Math::BigInt => Math BigInt - my $file = pop @parts; $file .= '.pm'; # BigInt => BigInt.pm - require File::Spec; - $file = File::Spec->catfile (@parts, $file); - eval { require "$file"; $lib->import( @c ); } - } - else - { - eval "use $lib qw/@c/;"; - } - if ($@ eq '') - { - my $ok = 1; - # loaded it ok, see if the api_version() is high enough - if ($lib->can('api_version') && $lib->api_version() >= 1.0) - { - $ok = 0; - # api_version matches, check if it really provides anything we need - for my $method (qw/ - one two ten - str num - add mul div sub dec inc - acmp len digit is_one is_zero is_even is_odd - is_two is_ten - zeros new copy check - from_hex from_oct from_bin as_hex as_bin as_oct - rsft lsft xor and or - mod sqrt root fac pow modinv modpow log_int gcd - /) - { - if (!$lib->can("_$method")) - { - if (($WARN{$lib}||0) < 2) - { - require Carp; - Carp::carp ("$lib is missing method '_$method'"); - $WARN{$lib} = 1; # still warn about the lib - } - $ok++; last; - } - } - } - if ($ok == 0) - { - $CALC = $lib; - if ($warn_or_die > 0 && ref($l)) - { - require Carp; - my $msg = - "Math::BigInt: couldn't load specified math lib(s), fallback to $lib"; - Carp::carp ($msg) if $warn_or_die == 1; - Carp::croak ($msg) if $warn_or_die == 2; - } - last; # found a usable one, break - } - else - { - if (($WARN{$lib}||0) < 2) - { - my $ver = eval "\$$lib\::VERSION" || 'unknown'; - require Carp; - Carp::carp ("Cannot load outdated $lib v$ver, please upgrade"); - $WARN{$lib} = 2; # never warn again - } - } - } - } - if ($CALC eq '') - { - require Carp; - if ($warn_or_die == 2) - { - Carp::croak( - "Couldn't load specified math lib(s) and fallback disallowed"); - } - else - { - Carp::croak( - "Couldn't load any math lib(s), not even fallback to Calc.pm"); - } - } - - # notify callbacks - foreach my $class (keys %CALLBACKS) - { - &{$CALLBACKS{$class}}($CALC); - } - - # Fill $CAN with the results of $CALC->can(...) for emulating lower math lib - # functions - - %CAN = (); - for my $method (qw/ signed_and signed_or signed_xor /) - { - $CAN{$method} = $CALC->can("_$method") ? 1 : 0; - } - - # import done - } - -sub from_hex { - # Create a bigint from a hexadecimal string. - - my ($self, $str) = @_; - - if ($str =~ s/ - ^ - ( [+-]? ) - (0?x)? - ( - [0-9a-fA-F]* - ( _ [0-9a-fA-F]+ )* - ) - $ - //x) - { - # Get a "clean" version of the string, i.e., non-emtpy and with no - # underscores or invalid characters. - - my $sign = $1; - my $chrs = $3; - $chrs =~ tr/_//d; - $chrs = '0' unless CORE::length $chrs; - - # Initialize output. - - my $x = Math::BigInt->bzero(); - - # The library method requires a prefix. - - $x->{value} = $CALC->_from_hex('0x' . $chrs); - - # Place the sign. - - if ($sign eq '-' && ! $CALC->_is_zero($x->{value})) { - $x->{sign} = '-'; - } - - return $x; - } - - # CORE::hex() parses as much as it can, and ignores any trailing garbage. - # For backwards compatibility, we return NaN. - - return $self->bnan(); -} - -sub from_oct { - # Create a bigint from an octal string. - - my ($self, $str) = @_; - - if ($str =~ s/ - ^ - ( [+-]? ) - ( - [0-7]* - ( _ [0-7]+ )* - ) - $ - //x) - { - # Get a "clean" version of the string, i.e., non-emtpy and with no - # underscores or invalid characters. - - my $sign = $1; - my $chrs = $2; - $chrs =~ tr/_//d; - $chrs = '0' unless CORE::length $chrs; - - # Initialize output. - - my $x = Math::BigInt->bzero(); - - # The library method requires a prefix. - - $x->{value} = $CALC->_from_oct('0' . $chrs); - - # Place the sign. - - if ($sign eq '-' && ! $CALC->_is_zero($x->{value})) { - $x->{sign} = '-'; - } - - return $x; - } - - # CORE::oct() parses as much as it can, and ignores any trailing garbage. - # For backwards compatibility, we return NaN. - - return $self->bnan(); -} - -sub from_bin { - # Create a bigint from a binary string. - - my ($self, $str) = @_; - - if ($str =~ s/ - ^ - ( [+-]? ) - (0?b)? - ( - [01]* - ( _ [01]+ )* - ) - $ - //x) - { - # Get a "clean" version of the string, i.e., non-emtpy and with no - # underscores or invalid characters. - - my $sign = $1; - my $chrs = $3; - $chrs =~ tr/_//d; - $chrs = '0' unless CORE::length $chrs; - - # Initialize output. - - my $x = Math::BigInt->bzero(); - - # The library method requires a prefix. - - $x->{value} = $CALC->_from_bin('0b' . $chrs); - - # Place the sign. - - if ($sign eq '-' && ! $CALC->_is_zero($x->{value})) { - $x->{sign} = '-'; - } - - return $x; - } - - # For consistency with from_hex() and from_oct(), we return NaN when the - # input is invalid. - - return $self->bnan(); -} - -sub _split - { - # input: num_str; output: undef for invalid or - # (\$mantissa_sign,\$mantissa_value,\$mantissa_fraction, - # \$exp_sign,\$exp_value) - # Internal, take apart a string and return the pieces. - # Strip leading/trailing whitespace, leading zeros, underscore and reject - # invalid input. - my $x = shift; - - # strip white space at front, also extraneous leading zeros - $x =~ s/^\s*([-]?)0*([0-9])/$1$2/g; # will not strip ' .2' - $x =~ s/^\s+//; # but this will - $x =~ s/\s+$//g; # strip white space at end - - # shortcut, if nothing to split, return early - if ($x =~ /^[+-]?[0-9]+\z/) - { - $x =~ s/^([+-])0*([0-9])/$2/; my $sign = $1 || '+'; - return (\$sign, \$x, \'', \'', \0); - } - - # invalid starting char? - return if $x !~ /^[+-]?(\.?[0-9]|0b[0-1]|0x[0-9a-fA-F])/; - - return Math::BigInt->from_hex($x) if $x =~ /^[+-]?0x/; # hex string - return Math::BigInt->from_bin($x) if $x =~ /^[+-]?0b/; # binary string - - # strip underscores between digits - $x =~ s/([0-9])_([0-9])/$1$2/g; - $x =~ s/([0-9])_([0-9])/$1$2/g; # do twice for 1_2_3 - - # some possible inputs: - # 2.1234 # 0.12 # 1 # 1E1 # 2.134E1 # 434E-10 # 1.02009E-2 - # .2 # 1_2_3.4_5_6 # 1.4E1_2_3 # 1e3 # +.2 # 0e999 - - my ($m,$e,$last) = split /[Ee]/,$x; - return if defined $last; # last defined => 1e2E3 or others - $e = '0' if !defined $e || $e eq ""; - - # sign,value for exponent,mantint,mantfrac - my ($es,$ev,$mis,$miv,$mfv); - # valid exponent? - if ($e =~ /^([+-]?)0*([0-9]+)$/) # strip leading zeros - { - $es = $1; $ev = $2; - # valid mantissa? - return if $m eq '.' || $m eq ''; - my ($mi,$mf,$lastf) = split /\./,$m; - return if defined $lastf; # lastf defined => 1.2.3 or others - $mi = '0' if !defined $mi; - $mi .= '0' if $mi =~ /^[\-\+]?$/; - $mf = '0' if !defined $mf || $mf eq ''; - if ($mi =~ /^([+-]?)0*([0-9]+)$/) # strip leading zeros - { - $mis = $1||'+'; $miv = $2; - return unless ($mf =~ /^([0-9]*?)0*$/); # strip trailing zeros - $mfv = $1; - # handle the 0e999 case here - $ev = 0 if $miv eq '0' && $mfv eq ''; - return (\$mis,\$miv,\$mfv,\$es,\$ev); - } - } - return; # NaN, not a number - } - -############################################################################## -# internal calculation routines (others are in Math::BigInt::Calc etc) - -sub __lcm - { - # (BINT or num_str, BINT or num_str) return BINT - # does modify first argument - # LCM - - my ($x,$ty) = @_; - return $x->bnan() if ($x->{sign} eq $nan) || ($ty->{sign} eq $nan); - my $method = ref($x) . '::bgcd'; - no strict 'refs'; - $x * $ty / &$method($x,$ty); - } - -############################################################################### -# trigonometric functions - -sub bpi - { - # Calculate PI to N digits. Unless upgrading is in effect, returns the - # result truncated to an integer, that is, always returns '3'. - my ($self,$n) = @_; - if (@_ == 1) - { - # called like Math::BigInt::bpi(10); - $n = $self; $self = $class; - } - $self = ref($self) if ref($self); - - return $upgrade->new($n) if defined $upgrade; - - # hard-wired to "3" - $self->new(3); - } - -sub bcos - { - # Calculate cosinus(x) to N digits. Unless upgrading is in effect, returns the - # result truncated to an integer. - my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - return $x if $x->modify('bcos'); - - return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN - - return $upgrade->new($x)->bcos(@r) if defined $upgrade; - - require Math::BigFloat; - # calculate the result and truncate it to integer - my $t = Math::BigFloat->new($x)->bcos(@r)->as_int(); - - $x->bone() if $t->is_one(); - $x->bzero() if $t->is_zero(); - $x->round(@r); - } - -sub bsin - { - # Calculate sinus(x) to N digits. Unless upgrading is in effect, returns the - # result truncated to an integer. - my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - return $x if $x->modify('bsin'); - - return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN - - return $upgrade->new($x)->bsin(@r) if defined $upgrade; - - require Math::BigFloat; - # calculate the result and truncate it to integer - my $t = Math::BigFloat->new($x)->bsin(@r)->as_int(); - - $x->bone() if $t->is_one(); - $x->bzero() if $t->is_zero(); - $x->round(@r); - } - -sub batan2 - { - # calculate arcus tangens of ($y/$x) - - # set up parameters - my ($self,$y,$x,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$y,$x,@r) = objectify(2,@_); - } - - return $y if $y->modify('batan2'); - - return $y->bnan() if ($y->{sign} eq $nan) || ($x->{sign} eq $nan); - - # Y X - # != 0 -inf result is +- pi - if ($x->is_inf() || $y->is_inf()) - { - # upgrade to BigFloat etc. - return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; - if ($y->is_inf()) - { - if ($x->{sign} eq '-inf') - { - # calculate 3 pi/4 => 2.3.. => 2 - $y->bone( substr($y->{sign},0,1) ); - $y->bmul($self->new(2)); - } - elsif ($x->{sign} eq '+inf') - { - # calculate pi/4 => 0.7 => 0 - $y->bzero(); - } - else - { - # calculate pi/2 => 1.5 => 1 - $y->bone( substr($y->{sign},0,1) ); - } - } - else - { - if ($x->{sign} eq '+inf') - { - # calculate pi/4 => 0.7 => 0 - $y->bzero(); - } - else - { - # PI => 3.1415.. => 3 - $y->bone( substr($y->{sign},0,1) ); - $y->bmul($self->new(3)); - } - } - return $y; - } - - return $upgrade->new($y)->batan2($upgrade->new($x),@r) if defined $upgrade; - - require Math::BigFloat; - my $r = Math::BigFloat->new($y) - ->batan2(Math::BigFloat->new($x),@r) - ->as_int(); - - $x->{value} = $r->{value}; - $x->{sign} = $r->{sign}; - - $x; - } - -sub batan - { - # Calculate arcus tangens of x to N digits. Unless upgrading is in effect, returns the - # result truncated to an integer. - my ($self,$x,@r) = ref($_[0]) ? (undef,@_) : objectify(1,@_); - - return $x if $x->modify('batan'); - - return $x->bnan() if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN - - return $upgrade->new($x)->batan(@r) if defined $upgrade; - - # calculate the result and truncate it to integer - my $t = Math::BigFloat->new($x)->batan(@r); - - $x->{value} = $CALC->_new( $x->as_int()->bstr() ); - $x->round(@r); - } - -############################################################################### -# this method returns 0 if the object can be modified, or 1 if not. -# We use a fast constant sub() here, to avoid costly calls. Subclasses -# may override it with special code (f.i. Math::BigInt::Constant does so) - -sub modify () { 0; } - -1; -__END__ - -=pod - -=head1 NAME - -Math::BigInt - Arbitrary size integer/float math package - -=head1 SYNOPSIS - - use Math::BigInt; - - # or make it faster with huge numbers: install (optional) - # Math::BigInt::GMP and always use (it will fall back to - # pure Perl if the GMP library is not installed): - # (See also the L section!) - - # will warn if Math::BigInt::GMP cannot be found - use Math::BigInt lib => 'GMP'; - - # to suppress the warning use this: - # use Math::BigInt try => 'GMP'; - - # dies if GMP cannot be loaded: - # use Math::BigInt only => 'GMP'; - - my $str = '1234567890'; - my @values = (64,74,18); - my $n = 1; my $sign = '-'; - - # Number creation - my $x = Math::BigInt->new($str); # defaults to 0 - my $y = $x->copy(); # make a true copy - my $nan = Math::BigInt->bnan(); # create a NotANumber - my $zero = Math::BigInt->bzero(); # create a +0 - my $inf = Math::BigInt->binf(); # create a +inf - my $inf = Math::BigInt->binf('-'); # create a -inf - my $one = Math::BigInt->bone(); # create a +1 - my $mone = Math::BigInt->bone('-'); # create a -1 - - my $pi = Math::BigInt->bpi(); # returns '3' - # see Math::BigFloat::bpi() - - $h = Math::BigInt->new('0x123'); # from hexadecimal - $b = Math::BigInt->new('0b101'); # from binary - $o = Math::BigInt->from_oct('0101'); # from octal - - # Testing (don't modify their arguments) - # (return true if the condition is met, otherwise false) - - $x->is_zero(); # if $x is +0 - $x->is_nan(); # if $x is NaN - $x->is_one(); # if $x is +1 - $x->is_one('-'); # if $x is -1 - $x->is_odd(); # if $x is odd - $x->is_even(); # if $x is even - $x->is_pos(); # if $x > 0 - $x->is_neg(); # if $x < 0 - $x->is_inf($sign); # if $x is +inf, or -inf (sign is default '+') - $x->is_int(); # if $x is an integer (not a float) - - # comparing and digit/sign extraction - $x->bcmp($y); # compare numbers (undef,<0,=0,>0) - $x->bacmp($y); # compare absolutely (undef,<0,=0,>0) - $x->sign(); # return the sign, either +,- or NaN - $x->digit($n); # return the nth digit, counting from right - $x->digit(-$n); # return the nth digit, counting from left - - # The following all modify their first argument. If you want to pre- - # serve $x, use $z = $x->copy()->bXXX($y); See under L for - # why this is necessary when mixing $a = $b assignments with non-over- - # loaded math. - - $x->bzero(); # set $x to 0 - $x->bnan(); # set $x to NaN - $x->bone(); # set $x to +1 - $x->bone('-'); # set $x to -1 - $x->binf(); # set $x to inf - $x->binf('-'); # set $x to -inf - - $x->bneg(); # negation - $x->babs(); # absolute value - $x->bsgn(); # sign function (-1, 0, 1, or NaN) - $x->bnorm(); # normalize (no-op in BigInt) - $x->bnot(); # two's complement (bit wise not) - $x->binc(); # increment $x by 1 - $x->bdec(); # decrement $x by 1 - - $x->badd($y); # addition (add $y to $x) - $x->bsub($y); # subtraction (subtract $y from $x) - $x->bmul($y); # multiplication (multiply $x by $y) - $x->bdiv($y); # divide, set $x to quotient - # return (quo,rem) or quo if scalar - - $x->bmuladd($y,$z); # $x = $x * $y + $z - - $x->bmod($y); # modulus (x % y) - $x->bmodpow($y,$mod); # modular exponentiation (($x ** $y) % $mod) - $x->bmodinv($mod); # modular multiplicative inverse - $x->bpow($y); # power of arguments (x ** y) - $x->blsft($y); # left shift in base 2 - $x->brsft($y); # right shift in base 2 - # returns (quo,rem) or quo if in sca- - # lar context - $x->blsft($y,$n); # left shift by $y places in base $n - $x->brsft($y,$n); # right shift by $y places in base $n - # returns (quo,rem) or quo if in sca- - # lar context - - $x->band($y); # bitwise and - $x->bior($y); # bitwise inclusive or - $x->bxor($y); # bitwise exclusive or - $x->bnot(); # bitwise not (two's complement) - - $x->bsqrt(); # calculate square-root - $x->broot($y); # $y'th root of $x (e.g. $y == 3 => cubic root) - $x->bfac(); # factorial of $x (1*2*3*4*..$x) - - $x->bnok($y); # x over y (binomial coefficient n over k) - - $x->blog(); # logarithm of $x to base e (Euler's number) - $x->blog($base); # logarithm of $x to base $base (f.i. 2) - $x->bexp(); # calculate e ** $x where e is Euler's number - - $x->round($A,$P,$mode); # round to accuracy or precision using - # mode $mode - $x->bround($n); # accuracy: preserve $n digits - $x->bfround($n); # $n > 0: round $nth digits, - # $n < 0: round to the $nth digit after the - # dot, no-op for BigInts - - # The following do not modify their arguments in BigInt (are no-ops), - # but do so in BigFloat: - - $x->bfloor(); # round towards minus infinity - $x->bceil(); # round towards plus infinity - $x->bint(); # round towards zero - - # The following do not modify their arguments: - - # greatest common divisor (no OO style) - my $gcd = Math::BigInt::bgcd(@values); - # lowest common multiple (no OO style) - my $lcm = Math::BigInt::blcm(@values); - - $x->length(); # return number of digits in number - ($xl,$f) = $x->length(); # length of number and length of fraction - # part, latter is always 0 digits long - # for BigInts - - $x->exponent(); # return exponent as BigInt - $x->mantissa(); # return (signed) mantissa as BigInt - $x->parts(); # return (mantissa,exponent) as BigInt - $x->copy(); # make a true copy of $x (unlike $y = $x;) - $x->as_int(); # return as BigInt (in BigInt: same as copy()) - $x->numify(); # return as scalar (might overflow!) - - # conversion to string (do not modify their argument) - $x->bstr(); # normalized string (e.g. '3') - $x->bsstr(); # norm. string in scientific notation (e.g. '3E0') - $x->as_hex(); # as signed hexadecimal string with prefixed 0x - $x->as_bin(); # as signed binary string with prefixed 0b - $x->as_oct(); # as signed octal string with prefixed 0 - - - # precision and accuracy (see section about rounding for more) - $x->precision(); # return P of $x (or global, if P of $x undef) - $x->precision($n); # set P of $x to $n - $x->accuracy(); # return A of $x (or global, if A of $x undef) - $x->accuracy($n); # set A $x to $n - - # Global methods - Math::BigInt->precision(); # get/set global P for all BigInt objects - Math::BigInt->accuracy(); # get/set global A for all BigInt objects - Math::BigInt->round_mode(); # get/set global round mode, one of - # 'even', 'odd', '+inf', '-inf', 'zero', - # 'trunc' or 'common' - Math::BigInt->config(); # return hash containing configuration - -=head1 DESCRIPTION - -All operators (including basic math operations) are overloaded if you -declare your big integers as - - $i = new Math::BigInt '123_456_789_123_456_789'; - -Operations with overloaded operators preserve the arguments which is -exactly what you expect. - -=head2 Input - -Input values to these routines may be any string, that looks like a number -and results in an integer, including hexadecimal and binary numbers. - -Scalars holding numbers may also be passed, but note that non-integer numbers -may already have lost precision due to the conversion to float. Quote -your input if you want BigInt to see all the digits: - - $x = Math::BigInt->new(12345678890123456789); # bad - $x = Math::BigInt->new('12345678901234567890'); # good - -You can include one underscore between any two digits. - -This means integer values like 1.01E2 or even 1000E-2 are also accepted. -Non-integer values result in NaN. - -Hexadecimal (prefixed with "0x") and binary numbers (prefixed with "0b") -are accepted, too. Please note that octal numbers are not recognized -by new(), so the following will print "123": - - perl -MMath::BigInt -le 'print Math::BigInt->new("0123")' - -To convert an octal number, use from_oct(); - - perl -MMath::BigInt -le 'print Math::BigInt->from_oct("0123")' - -Currently, Math::BigInt::new() defaults to 0, while Math::BigInt::new('') -results in 'NaN'. This might change in the future, so use always the following -explicit forms to get a zero or NaN: - - $zero = Math::BigInt->bzero(); - $nan = Math::BigInt->bnan(); - -C on a BigInt object is now effectively a no-op, since the numbers -are always stored in normalized form. If passed a string, creates a BigInt -object from the input. - -=head2 Output - -Output values are BigInt objects (normalized), except for the methods which -return a string (see L). - -Some routines (C, C, C, C, -C, etc.) return true or false, while others (C, C) -return either undef (if NaN is involved), <0, 0 or >0 and are suited for sort. - -=head1 METHODS - -Each of the methods below (except config(), accuracy() and precision()) -accepts three additional parameters. These arguments C<$A>, C<$P> and C<$R> -are C, C and C. Please see the section about -L for more information. - -=over - -=item config() - - use Data::Dumper; - - print Dumper ( Math::BigInt->config() ); - print Math::BigInt->config()->{lib},"\n"; - -Returns a hash containing the configuration, e.g. the version number, lib -loaded etc. The following hash keys are currently filled in with the -appropriate information. - - key Description - Example - ============================================================ - lib Name of the low-level math library - Math::BigInt::Calc - lib_version Version of low-level math library (see 'lib') - 0.30 - class The class name of config() you just called - Math::BigInt - upgrade To which class math operations might be - upgraded Math::BigFloat - downgrade To which class math operations might be - downgraded undef - precision Global precision - undef - accuracy Global accuracy - undef - round_mode Global round mode - even - version version number of the class you used - 1.61 - div_scale Fallback accuracy for div - 40 - trap_nan If true, traps creation of NaN via croak() - 1 - trap_inf If true, traps creation of +inf/-inf via croak() - 1 - -The following values can be set by passing C a reference to a hash: - - trap_inf trap_nan - upgrade downgrade precision accuracy round_mode div_scale - -Example: - - $new_cfg = Math::BigInt->config( - { trap_inf => 1, precision => 5 } - ); - -=item accuracy() - - $x->accuracy(5); # local for $x - CLASS->accuracy(5); # global for all members of CLASS - # Note: This also applies to new()! - - $A = $x->accuracy(); # read out accuracy that affects $x - $A = CLASS->accuracy(); # read out global accuracy - -Set or get the global or local accuracy, aka how many significant digits the -results have. If you set a global accuracy, then this also applies to new()! - -Warning! The accuracy I, e.g. once you created a number under the -influence of C<< CLASS->accuracy($A) >>, all results from math operations with -that number will also be rounded. - -In most cases, you should probably round the results explicitly using one of -L, L or L or by passing the desired accuracy -to the math operation as additional parameter: - - my $x = Math::BigInt->new(30000); - my $y = Math::BigInt->new(7); - print scalar $x->copy()->bdiv($y, 2); # print 4300 - print scalar $x->copy()->bdiv($y)->bround(2); # print 4300 - -Please see the section about L for further details. - -Value must be greater than zero. Pass an undef value to disable it: - - $x->accuracy(undef); - Math::BigInt->accuracy(undef); - -Returns the current accuracy. For C<< $x->accuracy() >> it will return either -the local accuracy, or if not defined, the global. This means the return value -represents the accuracy that will be in effect for $x: - - $y = Math::BigInt->new(1234567); # unrounded - print Math::BigInt->accuracy(4),"\n"; # set 4, print 4 - $x = Math::BigInt->new(123456); # $x will be automatic- - # ally rounded! - print "$x $y\n"; # '123500 1234567' - print $x->accuracy(),"\n"; # will be 4 - print $y->accuracy(),"\n"; # also 4, since - # global is 4 - print Math::BigInt->accuracy(5),"\n"; # set to 5, print 5 - print $x->accuracy(),"\n"; # still 4 - print $y->accuracy(),"\n"; # 5, since global is 5 - -Note: Works also for subclasses like Math::BigFloat. Each class has it's own -globals separated from Math::BigInt, but it is possible to subclass -Math::BigInt and make the globals of the subclass aliases to the ones from -Math::BigInt. - -=item precision() - - $x->precision(-2); # local for $x, round at the second - # digit right of the dot - $x->precision(2); # ditto, round at the second digit - # left of the dot - - CLASS->precision(5); # Global for all members of CLASS - # This also applies to new()! - CLASS->precision(-5); # ditto - - $P = CLASS->precision(); # read out global precision - $P = $x->precision(); # read out precision that affects $x - -Note: You probably want to use L instead. With L you -set the number of digits each result should have, with L you -set the place where to round! - -C sets or gets the global or local precision, aka at which digit -before or after the dot to round all results. A set global precision also -applies to all newly created numbers! - -In Math::BigInt, passing a negative number precision has no effect since no -numbers have digits after the dot. In L, it will round all -results to P digits after the dot. - -Please see the section about L for further details. - -Pass an undef value to disable it: - - $x->precision(undef); - Math::BigInt->precision(undef); - -Returns the current precision. For C<< $x->precision() >> it will return either -the local precision of $x, or if not defined, the global. This means the return -value represents the prevision that will be in effect for $x: - - $y = Math::BigInt->new(1234567); # unrounded - print Math::BigInt->precision(4),"\n"; # set 4, print 4 - $x = Math::BigInt->new(123456); # will be automatically rounded - print $x; # print "120000"! - -Note: Works also for subclasses like L. Each class has its -own globals separated from Math::BigInt, but it is possible to subclass -Math::BigInt and make the globals of the subclass aliases to the ones from -Math::BigInt. - -=item brsft() - - $x->brsft($y,$n); - -Shifts $x right by $y in base $n. Default is base 2, used are usually 10 and -2, but others work, too. - -Right shifting usually amounts to dividing $x by $n ** $y and truncating the -result: - - - $x = Math::BigInt->new(10); - $x->brsft(1); # same as $x >> 1: 5 - $x = Math::BigInt->new(1234); - $x->brsft(2,10); # result 12 - -There is one exception, and that is base 2 with negative $x: - - - $x = Math::BigInt->new(-5); - print $x->brsft(1); - -This will print -3, not -2 (as it would if you divide -5 by 2 and truncate the -result). - -=item new() - - $x = Math::BigInt->new($str,$A,$P,$R); - -Creates a new BigInt object from a scalar or another BigInt object. The -input is accepted as decimal, hex (with leading '0x') or binary (with leading -'0b'). - -See L for more info on accepted input formats. - -=item from_oct() - - $x = Math::BigInt->from_oct("0775"); # input is octal - -Interpret the input as an octal string and return the corresponding value. A -"0" (zero) prefix is optional. A single underscore character may be placed -right after the prefix, if present, or between any two digits. If the input is -invalid, a NaN is returned. - -=item from_hex() - - $x = Math::BigInt->from_hex("0xcafe"); # input is hexadecimal - -Interpret input as a hexadecimal string. A "0x" or "x" prefix is optional. A -single underscore character may be placed right after the prefix, if present, -or between any two digits. If the input is invalid, a NaN is returned. - -=item from_bin() - - $x = Math::BigInt->from_bin("0b10011"); # input is binary - -Interpret the input as a binary string. A "0b" or "b" prefix is optional. A -single underscore character may be placed right after the prefix, if present, -or between any two digits. If the input is invalid, a NaN is returned. - -=item bnan() - - $x = Math::BigInt->bnan(); - -Creates a new BigInt object representing NaN (Not A Number). -If used on an object, it will set it to NaN: - - $x->bnan(); - -=item bzero() - - $x = Math::BigInt->bzero(); - -Creates a new BigInt object representing zero. -If used on an object, it will set it to zero: - - $x->bzero(); - -=item binf() - - $x = Math::BigInt->binf($sign); - -Creates a new BigInt object representing infinity. The optional argument is -either '-' or '+', indicating whether you want infinity or minus infinity. -If used on an object, it will set it to infinity: - - $x->binf(); - $x->binf('-'); - -=item bone() - - $x = Math::BigInt->binf($sign); - -Creates a new BigInt object representing one. The optional argument is -either '-' or '+', indicating whether you want one or minus one. -If used on an object, it will set it to one: - - $x->bone(); # +1 - $x->bone('-'); # -1 - -=item is_one()/is_zero()/is_nan()/is_inf() - - $x->is_zero(); # true if arg is +0 - $x->is_nan(); # true if arg is NaN - $x->is_one(); # true if arg is +1 - $x->is_one('-'); # true if arg is -1 - $x->is_inf(); # true if +inf - $x->is_inf('-'); # true if -inf (sign is default '+') - -These methods all test the BigInt for being one specific value and return -true or false depending on the input. These are faster than doing something -like: - - if ($x == 0) - -=item is_pos()/is_neg()/is_positive()/is_negative() - - $x->is_pos(); # true if > 0 - $x->is_neg(); # true if < 0 - -The methods return true if the argument is positive or negative, respectively. -C is neither positive nor negative, while C<+inf> counts as positive, and -C<-inf> is negative. A C is neither positive nor negative. - -These methods are only testing the sign, and not the value. - -C and C are aliases to C and -C, respectively. C and C were -introduced in v1.36, while C and C were only introduced -in v1.68. - -=item is_odd()/is_even()/is_int() - - $x->is_odd(); # true if odd, false for even - $x->is_even(); # true if even, false for odd - $x->is_int(); # true if $x is an integer - -The return true when the argument satisfies the condition. C, C<+inf>, -C<-inf> are not integers and are neither odd nor even. - -In BigInt, all numbers except C, C<+inf> and C<-inf> are integers. - -=item bcmp() - - $x->bcmp($y); - -Compares $x with $y and takes the sign into account. -Returns -1, 0, 1 or undef. - -=item bacmp() - - $x->bacmp($y); - -Compares $x with $y while ignoring their sign. Returns -1, 0, 1 or undef. - -=item sign() - - $x->sign(); - -Return the sign, of $x, meaning either C<+>, C<->, C<-inf>, C<+inf> or NaN. - -If you want $x to have a certain sign, use one of the following methods: - - $x->babs(); # '+' - $x->babs()->bneg(); # '-' - $x->bnan(); # 'NaN' - $x->binf(); # '+inf' - $x->binf('-'); # '-inf' - -=item digit() - - $x->digit($n); # return the nth digit, counting from right - -If C<$n> is negative, returns the digit counting from left. - -=item bneg() - - $x->bneg(); - -Negate the number, e.g. change the sign between '+' and '-', or between '+inf' -and '-inf', respectively. Does nothing for NaN or zero. - -=item babs() - - $x->babs(); - -Set the number to its absolute value, e.g. change the sign from '-' to '+' -and from '-inf' to '+inf', respectively. Does nothing for NaN or positive -numbers. - -=item bsgn() - - $x->bsgn(); - -Signum function. Set the number to -1, 0, or 1, depending on whether the -number is negative, zero, or positive, respectively. Does not modify NaNs. - -=item bnorm() - - $x->bnorm(); # normalize (no-op) - -=item bnot() - - $x->bnot(); - -Two's complement (bitwise not). This is equivalent to - - $x->binc()->bneg(); - -but faster. - -=item binc() - - $x->binc(); # increment x by 1 - -=item bdec() - - $x->bdec(); # decrement x by 1 - -=item badd() - - $x->badd($y); # addition (add $y to $x) - -=item bsub() - - $x->bsub($y); # subtraction (subtract $y from $x) - -=item bmul() - - $x->bmul($y); # multiplication (multiply $x by $y) - -=item bmuladd() - - $x->bmuladd($y,$z); - -Multiply $x by $y, and then add $z to the result, - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item bdiv() - - $x->bdiv($y); # divide, set $x to quotient - -Returns $x divided by $y. In list context, does floored division (F-division), -where the quotient is the greatest integer less than or equal to the quotient -of the two operands. Consequently, the remainder is either zero or has the same -sign as the second operand. In scalar context, only the quotient is returned. - -=item bmod() - - $x->bmod($y); # modulus (x % y) - -Returns $x modulo $y. When $x is finite, and $y is finite and non-zero, the -result is identical to the remainder after floored division (F-division), i.e., -identical to the result from Perl's % operator. - -=item bmodinv() - - $x->bmodinv($mod); # modular multiplicative inverse - -Returns the multiplicative inverse of C<$x> modulo C<$mod>. If - - $y = $x -> copy() -> bmodinv($mod) - -then C<$y> is the number closest to zero, and with the same sign as C<$mod>, -satisfying - - ($x * $y) % $mod = 1 % $mod - -If C<$x> and C<$y> are non-zero, they must be relative primes, i.e., -C. 'C' is returned when no modular multiplicative -inverse exists. - -=item bmodpow() - - $num->bmodpow($exp,$mod); # modular exponentiation - # ($num**$exp % $mod) - -Returns the value of C<$num> taken to the power C<$exp> in the modulus -C<$mod> using binary exponentiation. C is far superior to -writing - - $num ** $exp % $mod - -because it is much faster - it reduces internal variables into -the modulus whenever possible, so it operates on smaller numbers. - -C also supports negative exponents. - - bmodpow($num, -1, $mod) - -is exactly equivalent to - - bmodinv($num, $mod) - -=item bpow() - - $x->bpow($y); # power of arguments (x ** y) - -=item blog() - - $x->blog($base, $accuracy); # logarithm of x to the base $base - -If C<$base> is not defined, Euler's number (e) is used: - - print $x->blog(undef, 100); # log(x) to 100 digits - -=item bexp() - - $x->bexp($accuracy); # calculate e ** X - -Calculates the expression C where C is Euler's number. - -This method was added in v1.82 of Math::BigInt (April 2007). - -See also L. - -=item bnok() - - $x->bnok($y); # x over y (binomial coefficient n over k) - -Calculates the binomial coefficient n over k, also called the "choose" -function. The result is equivalent to: - - ( n ) n! - | - | = ------- - ( k ) k!(n-k)! - -This method was added in v1.84 of Math::BigInt (April 2007). - -=item bpi() - - print Math::BigInt->bpi(100), "\n"; # 3 - -Returns PI truncated to an integer, with the argument being ignored. This means -under BigInt this always returns C<3>. - -If upgrading is in effect, returns PI, rounded to N digits with the -current rounding mode: - - use Math::BigFloat; - use Math::BigInt upgrade => Math::BigFloat; - print Math::BigInt->bpi(3), "\n"; # 3.14 - print Math::BigInt->bpi(100), "\n"; # 3.1415.... - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item bcos() - - my $x = Math::BigInt->new(1); - print $x->bcos(100), "\n"; - -Calculate the cosinus of $x, modifying $x in place. - -In BigInt, unless upgrading is in effect, the result is truncated to an -integer. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item bsin() - - my $x = Math::BigInt->new(1); - print $x->bsin(100), "\n"; - -Calculate the sinus of $x, modifying $x in place. - -In BigInt, unless upgrading is in effect, the result is truncated to an -integer. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item batan2() - - my $x = Math::BigInt->new(1); - my $y = Math::BigInt->new(1); - print $y->batan2($x), "\n"; - -Calculate the arcus tangens of C<$y> divided by C<$x>, modifying $y in place. - -In BigInt, unless upgrading is in effect, the result is truncated to an -integer. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item batan() - - my $x = Math::BigFloat->new(0.5); - print $x->batan(100), "\n"; - -Calculate the arcus tangens of $x, modifying $x in place. - -In BigInt, unless upgrading is in effect, the result is truncated to an -integer. - -This method was added in v1.87 of Math::BigInt (June 2007). - -=item blsft() - - $x->blsft($y); # left shift in base 2 - $x->blsft($y,$n); # left shift, in base $n (like 10) - -=item brsft() - - $x->brsft($y); # right shift in base 2 - $x->brsft($y,$n); # right shift, in base $n (like 10) - -=item band() - - $x->band($y); # bitwise and - -=item bior() - - $x->bior($y); # bitwise inclusive or - -=item bxor() - - $x->bxor($y); # bitwise exclusive or - -=item bnot() - - $x->bnot(); # bitwise not (two's complement) - -=item bsqrt() - - $x->bsqrt(); # calculate square-root - -=item broot() - - $x->broot($N); - -Calculates the N'th root of C<$x>. - -=item bfac() - - $x->bfac(); # factorial of $x (1*2*3*4*..$x) - -=item round() - - $x->round($A,$P,$round_mode); - -Round $x to accuracy C<$A> or precision C<$P> using the round mode -C<$round_mode>. - -=item bround() - - $x->bround($N); # accuracy: preserve $N digits - -=item bfround() - - $x->bfround($N); - -If N is > 0, rounds to the Nth digit from the left. If N < 0, rounds to -the Nth digit after the dot. Since BigInts are integers, the case N < 0 -is a no-op for them. - -Examples: - - Input N Result - =================================================== - 123456.123456 3 123500 - 123456.123456 2 123450 - 123456.123456 -2 123456.12 - 123456.123456 -3 123456.123 - -=item bfloor() - - $x->bfloor(); - -Round $x towards minus infinity (i.e., set $x to the largest integer less than -or equal to $x). This is a no-op in BigInt, but changes $x in BigFloat, if $x -is not an integer. - -=item bceil() - - $x->bceil(); - -Round $x towards plus infinity (i.e., set $x to the smallest integer greater -than or equal to $x). This is a no-op in BigInt, but changes $x in BigFloat, if -$x is not an integer. - -=item bint() - - $x->bint(); - -Round $x towards zero. This is a no-op in BigInt, but changes $x in BigFloat, -if $x is not an integer. - -=item bgcd() - - bgcd(@values); # greatest common divisor (no OO style) - -=item blcm() - - blcm(@values); # lowest common multiple (no OO style) - -=item length() - - $x->length(); - ($xl,$fl) = $x->length(); - -Returns the number of digits in the decimal representation of the number. -In list context, returns the length of the integer and fraction part. For -BigInt's, the length of the fraction part will always be 0. - -=item exponent() - - $x->exponent(); - -Return the exponent of $x as BigInt. - -=item mantissa() - - $x->mantissa(); - -Return the signed mantissa of $x as BigInt. - -=item parts() - - $x->parts(); # return (mantissa,exponent) as BigInt - -=item copy() - - $x->copy(); # make a true copy of $x (unlike $y = $x;) - -=item as_int()/as_number() - - $x->as_int(); - -Returns $x as a BigInt (truncated towards zero). In BigInt this is the same as -C. - -C is an alias to this method. C was introduced in -v1.22, while C was only introduced in v1.68. - -=item bstr() - - $x->bstr(); - -Returns a normalized string representation of C<$x>. - -=item bsstr() - - $x->bsstr(); # normalized string in scientific notation - -=item as_hex() - - $x->as_hex(); # as signed hexadecimal string with prefixed 0x - -=item as_bin() - - $x->as_bin(); # as signed binary string with prefixed 0b - -=item as_oct() - - $x->as_oct(); # as signed octal string with prefixed 0 - -=item numify() - - print $x->numify(); - -This returns a normal Perl scalar from $x. It is used automatically -whenever a scalar is needed, for instance in array index operations. - -This loses precision, to avoid this use L instead. - -=item modify() - - $x->modify('bpowd'); - -This method returns 0 if the object can be modified with the given -operation, or 1 if not. - -This is used for instance by L. - -=item upgrade()/downgrade() - -Set/get the class for downgrade/upgrade operations. Thuis is used -for instance by L. The defaults are '', thus the following -operation will create a BigInt, not a BigFloat: - - my $i = Math::BigInt->new(123); - my $f = Math::BigFloat->new('123.1'); - - print $i + $f,"\n"; # print 246 - -=item div_scale() - -Set/get the number of digits for the default precision in divide -operations. - -=item round_mode() - -Set/get the current round mode. - -=back - -=head1 ACCURACY and PRECISION - -Since version v1.33, Math::BigInt and Math::BigFloat have full support for -accuracy and precision based rounding, both automatically after every -operation, as well as manually. - -This section describes the accuracy/precision handling in Math::Big* as it -used to be and as it is now, complete with an explanation of all terms and -abbreviations. - -Not yet implemented things (but with correct description) are marked with '!', -things that need to be answered are marked with '?'. - -In the next paragraph follows a short description of terms used here (because -these may differ from terms used by others people or documentation). - -During the rest of this document, the shortcuts A (for accuracy), P (for -precision), F (fallback) and R (rounding mode) will be used. - -=head2 Precision P - -A fixed number of digits before (positive) or after (negative) -the decimal point. For example, 123.45 has a precision of -2. 0 means an -integer like 123 (or 120). A precision of 2 means two digits to the left -of the decimal point are zero, so 123 with P = 1 becomes 120. Note that -numbers with zeros before the decimal point may have different precisions, -because 1200 can have p = 0, 1 or 2 (depending on what the initial value -was). It could also have p < 0, when the digits after the decimal point -are zero. - -The string output (of floating point numbers) will be padded with zeros: - - Initial value P A Result String - ------------------------------------------------------------ - 1234.01 -3 1000 1000 - 1234 -2 1200 1200 - 1234.5 -1 1230 1230 - 1234.001 1 1234 1234.0 - 1234.01 0 1234 1234 - 1234.01 2 1234.01 1234.01 - 1234.01 5 1234.01 1234.01000 - -For BigInts, no padding occurs. - -=head2 Accuracy A - -Number of significant digits. Leading zeros are not counted. A -number may have an accuracy greater than the non-zero digits -when there are zeros in it or trailing zeros. For example, 123.456 has -A of 6, 10203 has 5, 123.0506 has 7, 123.450000 has 8 and 0.000123 has 3. - -The string output (of floating point numbers) will be padded with zeros: - - Initial value P A Result String - ------------------------------------------------------------ - 1234.01 3 1230 1230 - 1234.01 6 1234.01 1234.01 - 1234.1 8 1234.1 1234.1000 - -For BigInts, no padding occurs. - -=head2 Fallback F - -When both A and P are undefined, this is used as a fallback accuracy when -dividing numbers. - -=head2 Rounding mode R - -When rounding a number, different 'styles' or 'kinds' -of rounding are possible. (Note that random rounding, as in -Math::Round, is not implemented.) - -=over - -=item 'trunc' - -truncation invariably removes all digits following the -rounding place, replacing them with zeros. Thus, 987.65 rounded -to tens (P=1) becomes 980, and rounded to the fourth sigdig -becomes 987.6 (A=4). 123.456 rounded to the second place after the -decimal point (P=-2) becomes 123.46. - -All other implemented styles of rounding attempt to round to the -"nearest digit." If the digit D immediately to the right of the -rounding place (skipping the decimal point) is greater than 5, the -number is incremented at the rounding place (possibly causing a -cascade of incrementation): e.g. when rounding to units, 0.9 rounds -to 1, and -19.9 rounds to -20. If D < 5, the number is similarly -truncated at the rounding place: e.g. when rounding to units, 0.4 -rounds to 0, and -19.4 rounds to -19. - -However the results of other styles of rounding differ if the -digit immediately to the right of the rounding place (skipping the -decimal point) is 5 and if there are no digits, or no digits other -than 0, after that 5. In such cases: - -=item 'even' - -rounds the digit at the rounding place to 0, 2, 4, 6, or 8 -if it is not already. E.g., when rounding to the first sigdig, 0.45 -becomes 0.4, -0.55 becomes -0.6, but 0.4501 becomes 0.5. - -=item 'odd' - -rounds the digit at the rounding place to 1, 3, 5, 7, or 9 if -it is not already. E.g., when rounding to the first sigdig, 0.45 -becomes 0.5, -0.55 becomes -0.5, but 0.5501 becomes 0.6. - -=item '+inf' - -round to plus infinity, i.e. always round up. E.g., when -rounding to the first sigdig, 0.45 becomes 0.5, -0.55 becomes -0.5, -and 0.4501 also becomes 0.5. - -=item '-inf' - -round to minus infinity, i.e. always round down. E.g., when -rounding to the first sigdig, 0.45 becomes 0.4, -0.55 becomes -0.6, -but 0.4501 becomes 0.5. - -=item 'zero' - -round to zero, i.e. positive numbers down, negative ones up. -E.g., when rounding to the first sigdig, 0.45 becomes 0.4, -0.55 -becomes -0.5, but 0.4501 becomes 0.5. - -=item 'common' - -round up if the digit immediately to the right of the rounding place -is 5 or greater, otherwise round down. E.g., 0.15 becomes 0.2 and -0.149 becomes 0.1. - -=back - -The handling of A & P in MBI/MBF (the old core code shipped with Perl -versions <= 5.7.2) is like this: - -=over - -=item Precision - - * ffround($p) is able to round to $p number of digits after the decimal - point - * otherwise P is unused - -=item Accuracy (significant digits) - - * fround($a) rounds to $a significant digits - * only fdiv() and fsqrt() take A as (optional) parameter - + other operations simply create the same number (fneg etc), or - more (fmul) of digits - + rounding/truncating is only done when explicitly calling one - of fround or ffround, and never for BigInt (not implemented) - * fsqrt() simply hands its accuracy argument over to fdiv. - * the documentation and the comment in the code indicate two - different ways on how fdiv() determines the maximum number - of digits it should calculate, and the actual code does yet - another thing - POD: - max($Math::BigFloat::div_scale,length(dividend)+length(divisor)) - Comment: - result has at most max(scale, length(dividend), length(divisor)) digits - Actual code: - scale = max(scale, length(dividend)-1,length(divisor)-1); - scale += length(divisor) - length(dividend); - So for lx = 3, ly = 9, scale = 10, scale will actually be 16 (10 - So for lx = 3, ly = 9, scale = 10, scale will actually be 16 - (10+9-3). Actually, the 'difference' added to the scale is cal- - culated from the number of "significant digits" in dividend and - divisor, which is derived by looking at the length of the man- - tissa. Which is wrong, since it includes the + sign (oops) and - actually gets 2 for '+100' and 4 for '+101'. Oops again. Thus - 124/3 with div_scale=1 will get you '41.3' based on the strange - assumption that 124 has 3 significant digits, while 120/7 will - get you '17', not '17.1' since 120 is thought to have 2 signif- - icant digits. The rounding after the division then uses the - remainder and $y to determine whether it must round up or down. - ? I have no idea which is the right way. That's why I used a slightly more - ? simple scheme and tweaked the few failing testcases to match it. - -=back - -This is how it works now: - -=over - -=item Setting/Accessing - - * You can set the A global via Math::BigInt->accuracy() or - Math::BigFloat->accuracy() or whatever class you are using. - * You can also set P globally by using Math::SomeClass->precision() - likewise. - * Globals are classwide, and not inherited by subclasses. - * to undefine A, use Math::SomeCLass->accuracy(undef); - * to undefine P, use Math::SomeClass->precision(undef); - * Setting Math::SomeClass->accuracy() clears automatically - Math::SomeClass->precision(), and vice versa. - * To be valid, A must be > 0, P can have any value. - * If P is negative, this means round to the P'th place to the right of the - decimal point; positive values mean to the left of the decimal point. - P of 0 means round to integer. - * to find out the current global A, use Math::SomeClass->accuracy() - * to find out the current global P, use Math::SomeClass->precision() - * use $x->accuracy() respective $x->precision() for the local - setting of $x. - * Please note that $x->accuracy() respective $x->precision() - return eventually defined global A or P, when $x's A or P is not - set. - -=item Creating numbers - - * When you create a number, you can give the desired A or P via: - $x = Math::BigInt->new($number,$A,$P); - * Only one of A or P can be defined, otherwise the result is NaN - * If no A or P is give ($x = Math::BigInt->new($number) form), then the - globals (if set) will be used. Thus changing the global defaults later on - will not change the A or P of previously created numbers (i.e., A and P of - $x will be what was in effect when $x was created) - * If given undef for A and P, NO rounding will occur, and the globals will - NOT be used. This is used by subclasses to create numbers without - suffering rounding in the parent. Thus a subclass is able to have its own - globals enforced upon creation of a number by using - $x = Math::BigInt->new($number,undef,undef): - - use Math::BigInt::SomeSubclass; - use Math::BigInt; - - Math::BigInt->accuracy(2); - Math::BigInt::SomeSubClass->accuracy(3); - $x = Math::BigInt::SomeSubClass->new(1234); - - $x is now 1230, and not 1200. A subclass might choose to implement - this otherwise, e.g. falling back to the parent's A and P. - -=item Usage - - * If A or P are enabled/defined, they are used to round the result of each - operation according to the rules below - * Negative P is ignored in Math::BigInt, since BigInts never have digits - after the decimal point - * Math::BigFloat uses Math::BigInt internally, but setting A or P inside - Math::BigInt as globals does not tamper with the parts of a BigFloat. - A flag is used to mark all Math::BigFloat numbers as 'never round'. - -=item Precedence - - * It only makes sense that a number has only one of A or P at a time. - If you set either A or P on one object, or globally, the other one will - be automatically cleared. - * If two objects are involved in an operation, and one of them has A in - effect, and the other P, this results in an error (NaN). - * A takes precedence over P (Hint: A comes before P). - If neither of them is defined, nothing is used, i.e. the result will have - as many digits as it can (with an exception for fdiv/fsqrt) and will not - be rounded. - * There is another setting for fdiv() (and thus for fsqrt()). If neither of - A or P is defined, fdiv() will use a fallback (F) of $div_scale digits. - If either the dividend's or the divisor's mantissa has more digits than - the value of F, the higher value will be used instead of F. - This is to limit the digits (A) of the result (just consider what would - happen with unlimited A and P in the case of 1/3 :-) - * fdiv will calculate (at least) 4 more digits than required (determined by - A, P or F), and, if F is not used, round the result - (this will still fail in the case of a result like 0.12345000000001 with A - or P of 5, but this can not be helped - or can it?) - * Thus you can have the math done by on Math::Big* class in two modi: - + never round (this is the default): - This is done by setting A and P to undef. No math operation - will round the result, with fdiv() and fsqrt() as exceptions to guard - against overflows. You must explicitly call bround(), bfround() or - round() (the latter with parameters). - Note: Once you have rounded a number, the settings will 'stick' on it - and 'infect' all other numbers engaged in math operations with it, since - local settings have the highest precedence. So, to get SaferRound[tm], - use a copy() before rounding like this: - - $x = Math::BigFloat->new(12.34); - $y = Math::BigFloat->new(98.76); - $z = $x * $y; # 1218.6984 - print $x->copy()->fround(3); # 12.3 (but A is now 3!) - $z = $x * $y; # still 1218.6984, without - # copy would have been 1210! - - + round after each op: - After each single operation (except for testing like is_zero()), the - method round() is called and the result is rounded appropriately. By - setting proper values for A and P, you can have all-the-same-A or - all-the-same-P modes. For example, Math::Currency might set A to undef, - and P to -2, globally. - - ?Maybe an extra option that forbids local A & P settings would be in order, - ?so that intermediate rounding does not 'poison' further math? - -=item Overriding globals - - * you will be able to give A, P and R as an argument to all the calculation - routines; the second parameter is A, the third one is P, and the fourth is - R (shift right by one for binary operations like badd). P is used only if - the first parameter (A) is undefined. These three parameters override the - globals in the order detailed as follows, i.e. the first defined value - wins: - (local: per object, global: global default, parameter: argument to sub) - + parameter A - + parameter P - + local A (if defined on both of the operands: smaller one is taken) - + local P (if defined on both of the operands: bigger one is taken) - + global A - + global P - + global F - * fsqrt() will hand its arguments to fdiv(), as it used to, only now for two - arguments (A and P) instead of one - -=item Local settings - - * You can set A or P locally by using $x->accuracy() or - $x->precision() - and thus force different A and P for different objects/numbers. - * Setting A or P this way immediately rounds $x to the new value. - * $x->accuracy() clears $x->precision(), and vice versa. - -=item Rounding - - * the rounding routines will use the respective global or local settings. - fround()/bround() is for accuracy rounding, while ffround()/bfround() - is for precision - * the two rounding functions take as the second parameter one of the - following rounding modes (R): - 'even', 'odd', '+inf', '-inf', 'zero', 'trunc', 'common' - * you can set/get the global R by using Math::SomeClass->round_mode() - or by setting $Math::SomeClass::round_mode - * after each operation, $result->round() is called, and the result may - eventually be rounded (that is, if A or P were set either locally, - globally or as parameter to the operation) - * to manually round a number, call $x->round($A,$P,$round_mode); - this will round the number by using the appropriate rounding function - and then normalize it. - * rounding modifies the local settings of the number: - - $x = Math::BigFloat->new(123.456); - $x->accuracy(5); - $x->bround(4); - - Here 4 takes precedence over 5, so 123.5 is the result and $x->accuracy() - will be 4 from now on. - -=item Default values - - * R: 'even' - * F: 40 - * A: undef - * P: undef - -=item Remarks - - * The defaults are set up so that the new code gives the same results as - the old code (except in a few cases on fdiv): - + Both A and P are undefined and thus will not be used for rounding - after each operation. - + round() is thus a no-op, unless given extra parameters A and P - -=back - -=head1 Infinity and Not a Number - -While BigInt has extensive handling of inf and NaN, certain quirks remain. - -=over - -=item oct()/hex() - -These perl routines currently (as of Perl v.5.8.6) cannot handle passed -inf. - - te@linux:~> perl -wle 'print 2 ** 3333' - inf - te@linux:~> perl -wle 'print 2 ** 3333 == 2 ** 3333' - 1 - te@linux:~> perl -wle 'print oct(2 ** 3333)' - 0 - te@linux:~> perl -wle 'print hex(2 ** 3333)' - Illegal hexadecimal digit 'i' ignored at -e line 1. - 0 - -The same problems occur if you pass them Math::BigInt->binf() objects. Since -overloading these routines is not possible, this cannot be fixed from BigInt. - -=item ==, !=, <, >, <=, >= with NaNs - -BigInt's bcmp() routine currently returns undef to signal that a NaN was -involved in a comparison. However, the overload code turns that into -either 1 or '' and thus operations like C<< NaN != NaN >> might return -wrong values. - -=item log(-inf) - -C<< log(-inf) >> is highly weird. Since log(-x)=pi*i+log(x), then -log(-inf)=pi*i+inf. However, since the imaginary part is finite, the real -infinity "overshadows" it, so the number might as well just be infinity. -However, the result is a complex number, and since BigInt/BigFloat can only -have real numbers as results, the result is NaN. - -=item exp(), cos(), sin(), atan2() - -These all might have problems handling infinity right. - -=back - -=head1 INTERNALS - -The actual numbers are stored as unsigned big integers (with separate sign). - -You should neither care about nor depend on the internal representation; it -might change without notice. Use B method calls like C<< $x->sign(); >> -instead relying on the internal representation. - -=head2 MATH LIBRARY - -Math with the numbers is done (by default) by a module called -C. This is equivalent to saying: - - use Math::BigInt try => 'Calc'; - -You can change this backend library by using: - - use Math::BigInt try => 'GMP'; - -B: General purpose packages should not be explicit about the library -to use; let the script author decide which is best. - -If your script works with huge numbers and Calc is too slow for them, -you can also for the loading of one of these libraries and if none -of them can be used, the code will die: - - use Math::BigInt only => 'GMP,Pari'; - -The following would first try to find Math::BigInt::Foo, then -Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: - - use Math::BigInt try => 'Foo,Math::BigInt::Bar'; - -The library that is loaded last will be used. Note that this can be -overwritten at any time by loading a different library, and numbers -constructed with different libraries cannot be used in math operations -together. - -=head3 What library to use? - -B: General purpose packages should not be explicit about the library -to use; let the script author decide which is best. - -L and L are in cases involving big -numbers much faster than Calc, however it is slower when dealing with very -small numbers (less than about 20 digits) and when converting very large -numbers to decimal (for instance for printing, rounding, calculating their -length in decimal etc). - -So please select carefully what library you want to use. - -Different low-level libraries use different formats to store the numbers. -However, you should B depend on the number having a specific format -internally. - -See the respective math library module documentation for further details. - -=head2 SIGN - -The sign is either '+', '-', 'NaN', '+inf' or '-inf'. - -A sign of 'NaN' is used to represent the result when input arguments are not -numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively -minus infinity. You will get '+inf' when dividing a positive number by 0, and -'-inf' when dividing any negative number by 0. - -=head2 mantissa(), exponent() and parts() - -C and C return the said parts of the BigInt such -that: - - $m = $x->mantissa(); - $e = $x->exponent(); - $y = $m * ( 10 ** $e ); - print "ok\n" if $x == $y; - -C<< ($m,$e) = $x->parts() >> is just a shortcut that gives you both of them -in one go. Both the returned mantissa and exponent have a sign. - -Currently, for BigInts C<$e> is always 0, except +inf and -inf, where it is -C<+inf>; and for NaN, where it is C; and for C<$x == 0>, where it is C<1> -(to be compatible with Math::BigFloat's internal representation of a zero as -C<0E1>). - -C<$m> is currently just a copy of the original number. The relation between -C<$e> and C<$m> will stay always the same, though their real values might -change. - -=head1 EXAMPLES - - use Math::BigInt; - - sub bigint { Math::BigInt->new(shift); } - - $x = Math::BigInt->bstr("1234") # string "1234" - $x = "$x"; # same as bstr() - $x = Math::BigInt->bneg("1234"); # BigInt "-1234" - $x = Math::BigInt->babs("-12345"); # BigInt "12345" - $x = Math::BigInt->bnorm("-0.00"); # BigInt "0" - $x = bigint(1) + bigint(2); # BigInt "3" - $x = bigint(1) + "2"; # ditto (auto-BigIntify of "2") - $x = bigint(1); # BigInt "1" - $x = $x + 5 / 2; # BigInt "3" - $x = $x ** 3; # BigInt "27" - $x *= 2; # BigInt "54" - $x = Math::BigInt->new(0); # BigInt "0" - $x--; # BigInt "-1" - $x = Math::BigInt->badd(4,5) # BigInt "9" - print $x->bsstr(); # 9e+0 - -Examples for rounding: - - use Math::BigFloat; - use Test::More; - - $x = Math::BigFloat->new(123.4567); - $y = Math::BigFloat->new(123.456789); - Math::BigFloat->accuracy(4); # no more A than 4 - - is ($x->copy()->fround(),123.4); # even rounding - print $x->copy()->fround(),"\n"; # 123.4 - Math::BigFloat->round_mode('odd'); # round to odd - print $x->copy()->fround(),"\n"; # 123.5 - Math::BigFloat->accuracy(5); # no more A than 5 - Math::BigFloat->round_mode('odd'); # round to odd - print $x->copy()->fround(),"\n"; # 123.46 - $y = $x->copy()->fround(4),"\n"; # A = 4: 123.4 - print "$y, ",$y->accuracy(),"\n"; # 123.4, 4 - - Math::BigFloat->accuracy(undef); # A not important now - Math::BigFloat->precision(2); # P important - print $x->copy()->bnorm(),"\n"; # 123.46 - print $x->copy()->fround(),"\n"; # 123.46 - -Examples for converting: - - my $x = Math::BigInt->new('0b1'.'01' x 123); - print "bin: ",$x->as_bin()," hex:",$x->as_hex()," dec: ",$x,"\n"; - -=head1 Autocreating constants - -After C all the B decimal, hexadecimal -and binary constants in the given scope are converted to C. -This conversion happens at compile time. - -In particular, - - perl -MMath::BigInt=:constant -e 'print 2**100,"\n"' - -prints the integer value of C<2**100>. Note that without conversion of -constants the expression 2**100 will be calculated as perl scalar. - -Please note that strings and floating point constants are not affected, -so that - - use Math::BigInt qw/:constant/; - - $x = 1234567890123456789012345678901234567890 - + 123456789123456789; - $y = '1234567890123456789012345678901234567890' - + '123456789123456789'; - -do not work. You need an explicit Math::BigInt->new() around one of the -operands. You should also quote large constants to protect loss of precision: - - use Math::BigInt; - - $x = Math::BigInt->new('1234567889123456789123456789123456789'); - -Without the quotes Perl would convert the large number to a floating point -constant at compile time and then hand the result to BigInt, which results in -an truncated result or a NaN. - -This also applies to integers that look like floating point constants: - - use Math::BigInt ':constant'; - - print ref(123e2),"\n"; - print ref(123.2e2),"\n"; - -will print nothing but newlines. Use either L or L -to get this to work. - -=head1 PERFORMANCE - -Using the form $x += $y; etc over $x = $x + $y is faster, since a copy of $x -must be made in the second case. For long numbers, the copy can eat up to 20% -of the work (in the case of addition/subtraction, less for -multiplication/division). If $y is very small compared to $x, the form -$x += $y is MUCH faster than $x = $x + $y since making the copy of $x takes -more time then the actual addition. - -With a technique called copy-on-write, the cost of copying with overload could -be minimized or even completely avoided. A test implementation of COW did show -performance gains for overloaded math, but introduced a performance loss due -to a constant overhead for all other operations. So Math::BigInt does currently -not COW. - -The rewritten version of this module (vs. v0.01) is slower on certain -operations, like C, C and C. The reason are that it -does now more work and handles much more cases. The time spent in these -operations is usually gained in the other math operations so that code on -the average should get (much) faster. If they don't, please contact the author. - -Some operations may be slower for small numbers, but are significantly faster -for big numbers. Other operations are now constant (O(1), like C, -C etc), instead of O(N) and thus nearly always take much less time. -These optimizations were done on purpose. - -If you find the Calc module to slow, try to install any of the replacement -modules and see if they help you. - -=head2 Alternative math libraries - -You can use an alternative library to drive Math::BigInt. See the section -L for more information. - -For more benchmark results see L. - -=head1 SUBCLASSING - -=head2 Subclassing Math::BigInt - -The basic design of Math::BigInt allows simple subclasses with very little -work, as long as a few simple rules are followed: - -=over - -=item * - -The public API must remain consistent, i.e. if a sub-class is overloading -addition, the sub-class must use the same name, in this case badd(). The -reason for this is that Math::BigInt is optimized to call the object methods -directly. - -=item * - -The private object hash keys like C<< $x->{sign} >> may not be changed, but -additional keys can be added, like C<< $x->{_custom} >>. - -=item * - -Accessor functions are available for all existing object hash keys and should -be used instead of directly accessing the internal hash keys. The reason for -this is that Math::BigInt itself has a pluggable interface which permits it -to support different storage methods. - -=back - -More complex sub-classes may have to replicate more of the logic internal of -Math::BigInt if they need to change more basic behaviors. A subclass that -needs to merely change the output only needs to overload C. - -All other object methods and overloaded functions can be directly inherited -from the parent class. - -At the very minimum, any subclass will need to provide its own C and can -store additional hash keys in the object. There are also some package globals -that must be defined, e.g.: - - # Globals - $accuracy = undef; - $precision = -2; # round to 2 decimal places - $round_mode = 'even'; - $div_scale = 40; - -Additionally, you might want to provide the following two globals to allow -auto-upgrading and auto-downgrading to work correctly: - - $upgrade = undef; - $downgrade = undef; - -This allows Math::BigInt to correctly retrieve package globals from the -subclass, like C<$SubClass::precision>. See t/Math/BigInt/Subclass.pm or -t/Math/BigFloat/SubClass.pm completely functional subclass examples. - -Don't forget to - - use overload; - -in your subclass to automatically inherit the overloading from the parent. If -you like, you can change part of the overloading, look at Math::String for an -example. - -=head1 UPGRADING - -When used like this: - - use Math::BigInt upgrade => 'Foo::Bar'; - -certain operations will 'upgrade' their calculation and thus the result to -the class Foo::Bar. Usually this is used in conjunction with Math::BigFloat: - - use Math::BigInt upgrade => 'Math::BigFloat'; - -As a shortcut, you can use the module L: - - use bignum; - -Also good for one-liners: - - perl -Mbignum -le 'print 2 ** 255' - -This makes it possible to mix arguments of different classes (as in 2.5 + 2) -as well es preserve accuracy (as in sqrt(3)). - -Beware: This feature is not fully implemented yet. - -=head2 Auto-upgrade - -The following methods upgrade themselves unconditionally; that is if upgrade -is in effect, they will always hand up their work: - -=over - -=item bsqrt() - -=item div() - -=item blog() - -=item bexp() - -=back - -Beware: This list is not complete. - -All other methods upgrade themselves only when one (or all) of their -arguments are of the class mentioned in $upgrade (This might change in later -versions to a more sophisticated scheme): - -=head1 EXPORTS - -C exports nothing by default, but can export the following methods: - - bgcd - blcm - -=head1 CAVEATS - -Some things might not work as you expect them. Below is documented what is -known to be troublesome: - -=over - -=item bstr(), bsstr() and 'cmp' - -Both C and C as well as automated stringify via overload now -drop the leading '+'. The old code would return '+3', the new returns '3'. -This is to be consistent with Perl and to make C (especially with -overloading) to work as you expect. It also solves problems with C -and L, which stringify arguments before comparing them. - -Mark Biggar said, when asked about to drop the '+' altogether, or make only -C work: - - I agree (with the first alternative), don't add the '+' on positive - numbers. It's not as important anymore with the new internal - form for numbers. It made doing things like abs and neg easier, - but those have to be done differently now anyway. - -So, the following examples will now work all as expected: - - use Test::More tests => 1; - use Math::BigInt; - - my $x = new Math::BigInt 3*3; - my $y = new Math::BigInt 3*3; - - is ($x,3*3, 'multiplication'); - print "$x eq 9" if $x eq $y; - print "$x eq 9" if $x eq '9'; - print "$x eq 9" if $x eq 3*3; - -Additionally, the following still works: - - print "$x == 9" if $x == $y; - print "$x == 9" if $x == 9; - print "$x == 9" if $x == 3*3; - -There is now a C method to get the string in scientific notation aka -C<1e+2> instead of C<100>. Be advised that overloaded 'eq' always uses bstr() -for comparison, but Perl will represent some numbers as 100 and others -as 1e+308. If in doubt, convert both arguments to Math::BigInt before -comparing them as strings: - - use Test::More tests => 3; - use Math::BigInt; - - $x = Math::BigInt->new('1e56'); $y = 1e56; - is ($x,$y); # will fail - is ($x->bsstr(),$y); # okay - $y = Math::BigInt->new($y); - is ($x,$y); # okay - -Alternatively, simply use C<< <=> >> for comparisons, this will get it -always right. There is not yet a way to get a number automatically represented -as a string that matches exactly the way Perl represents it. - -See also the section about L for problems in -comparing NaNs. - -=item int() - -C will return (at least for Perl v5.7.1 and up) another BigInt, not a -Perl scalar: - - $x = Math::BigInt->new(123); - $y = int($x); # BigInt 123 - $x = Math::BigFloat->new(123.45); - $y = int($x); # BigInt 123 - -In all Perl versions you can use C or C for the same -effect: - - $x = Math::BigFloat->new(123.45); - $y = $x->as_number(); # BigInt 123 - $y = $x->as_int(); # ditto - -This also works for other subclasses, like Math::String. - -If you want a real Perl scalar, use C: - - $y = $x->numify(); # 123 as scalar - -This is seldom necessary, though, because this is done automatically, like -when you access an array: - - $z = $array[$x]; # does work automatically - -=item length() - -The following will probably not do what you expect: - - $c = Math::BigInt->new(123); - print $c->length(),"\n"; # prints 30 - -It prints both the number of digits in the number and in the fraction part -since print calls C in list context. Use something like: - - print scalar $c->length(),"\n"; # prints 3 - -=item bdiv() - -The following will probably not do what you expect: - - print $c->bdiv(10000),"\n"; - -It prints both quotient and remainder since print calls C in list -context. Also, C will modify $c, so be careful. You probably want -to use - - print $c / 10000,"\n"; - -or, if you want to modify $c instead, - - print scalar $c->bdiv(10000),"\n"; - -The quotient is always the greatest integer less than or equal to the -real-valued quotient of the two operands, and the remainder (when it is -non-zero) always has the same sign as the second operand; so, for -example, - - 1 / 4 => ( 0, 1) - 1 / -4 => (-1,-3) - -3 / 4 => (-1, 1) - -3 / -4 => ( 0,-3) - -11 / 2 => (-5,1) - 11 /-2 => (-5,-1) - -As a consequence, the behavior of the operator % agrees with the -behavior of Perl's built-in % operator (as documented in the perlop -manpage), and the equation - - $x == ($x / $y) * $y + ($x % $y) - -holds true for any $x and $y, which justifies calling the two return -values of bdiv() the quotient and remainder. The only exception to this rule -are when $y == 0 and $x is negative, then the remainder will also be -negative. See below under "infinity handling" for the reasoning behind this. - -Perl's 'use integer;' changes the behaviour of % and / for scalars, but will -not change BigInt's way to do things. This is because under 'use integer' Perl -will do what the underlying C thinks is right and this is different for each -system. If you need BigInt's behaving exactly like Perl's 'use integer', bug -the author to implement it ;) - -=item infinity handling - -Here are some examples that explain the reasons why certain results occur while -handling infinity: - -The following table shows the result of the division and the remainder, so that -the equation above holds true. Some "ordinary" cases are strewn in to show more -clearly the reasoning: - - A / B = C, R so that C * B + R = A - ========================================================= - 5 / 8 = 0, 5 0 * 8 + 5 = 5 - 0 / 8 = 0, 0 0 * 8 + 0 = 0 - 0 / inf = 0, 0 0 * inf + 0 = 0 - 0 /-inf = 0, 0 0 * -inf + 0 = 0 - 5 / inf = 0, 5 0 * inf + 5 = 5 - 5 /-inf = 0, 5 0 * -inf + 5 = 5 - -5/ inf = 0, -5 0 * inf + -5 = -5 - -5/-inf = 0, -5 0 * -inf + -5 = -5 - inf/ 5 = inf, 0 inf * 5 + 0 = inf - -inf/ 5 = -inf, 0 -inf * 5 + 0 = -inf - inf/ -5 = -inf, 0 -inf * -5 + 0 = inf - -inf/ -5 = inf, 0 inf * -5 + 0 = -inf - 5/ 5 = 1, 0 1 * 5 + 0 = 5 - -5/ -5 = 1, 0 1 * -5 + 0 = -5 - inf/ inf = 1, 0 1 * inf + 0 = inf - -inf/-inf = 1, 0 1 * -inf + 0 = -inf - inf/-inf = -1, 0 -1 * -inf + 0 = inf - -inf/ inf = -1, 0 1 * -inf + 0 = -inf - 8/ 0 = inf, 8 inf * 0 + 8 = 8 - inf/ 0 = inf, inf inf * 0 + inf = inf - 0/ 0 = NaN - -These cases below violate the "remainder has the sign of the second of the two -arguments", since they wouldn't match up otherwise. - - A / B = C, R so that C * B + R = A - ======================================================== - -inf/ 0 = -inf, -inf -inf * 0 + inf = -inf - -8/ 0 = -inf, -8 -inf * 0 + 8 = -8 - -=item Modifying and = - -Beware of: - - $x = Math::BigFloat->new(5); - $y = $x; - -It will not do what you think, e.g. making a copy of $x. Instead it just makes -a second reference to the B object and stores it in $y. Thus anything -that modifies $x (except overloaded operators) will modify $y, and vice versa. -Or in other words, C<=> is only safe if you modify your BigInts only via -overloaded math. As soon as you use a method call it breaks: - - $x->bmul(2); - print "$x, $y\n"; # prints '10, 10' - -If you want a true copy of $x, use: - - $y = $x->copy(); - -You can also chain the calls like this, this will make first a copy and then -multiply it by 2: - - $y = $x->copy()->bmul(2); - -See also the documentation for overload.pm regarding C<=>. - -=item bpow - -C (and the rounding functions) now modifies the first argument and -returns it, unlike the old code which left it alone and only returned the -result. This is to be consistent with C etc. The first three will -modify $x, the last one won't: - - print bpow($x,$i),"\n"; # modify $x - print $x->bpow($i),"\n"; # ditto - print $x **= $i,"\n"; # the same - print $x ** $i,"\n"; # leave $x alone - -The form C<$x **= $y> is faster than C<$x = $x ** $y;>, though. - -=item Overloading -$x - -The following: - - $x = -$x; - -is slower than - - $x->bneg(); - -since overload calls C instead of C. The first variant -needs to preserve $x since it does not know that it later will get overwritten. -This makes a copy of $x and takes O(N), but $x->bneg() is O(1). - -=item Mixing different object types - -In Perl you will get a floating point value if you do one of the following: - - $float = 5.0 + 2; - $float = 2 + 5.0; - $float = 5 / 2; - -With overloaded math, only the first two variants will result in a BigFloat: - - use Math::BigInt; - use Math::BigFloat; - - $mbf = Math::BigFloat->new(5); - $mbi2 = Math::BigInteger->new(5); - $mbi = Math::BigInteger->new(2); - - # what actually gets called: - $float = $mbf + $mbi; # $mbf->badd() - $float = $mbf / $mbi; # $mbf->bdiv() - $integer = $mbi + $mbf; # $mbi->badd() - $integer = $mbi2 / $mbi; # $mbi2->bdiv() - $integer = $mbi2 / $mbf; # $mbi2->bdiv() - -This is because math with overloaded operators follows the first (dominating) -operand, and the operation of that is called and returns thus the result. So, -Math::BigInt::bdiv() will always return a Math::BigInt, regardless whether -the result should be a Math::BigFloat or the second operant is one. - -To get a Math::BigFloat you either need to call the operation manually, -make sure the operands are already of the proper type or casted to that type -via Math::BigFloat->new(): - - $float = Math::BigFloat->new($mbi2) / $mbi; # = 2.5 - -Beware of simple "casting" the entire expression, this would only convert -the already computed result: - - $float = Math::BigFloat->new($mbi2 / $mbi); # = 2.0 thus wrong! - -Beware also of the order of more complicated expressions like: - - $integer = ($mbi2 + $mbi) / $mbf; # int / float => int - $integer = $mbi2 / Math::BigFloat->new($mbi); # ditto - -If in doubt, break the expression into simpler terms, or cast all operands -to the desired resulting type. - -Scalar values are a bit different, since: - - $float = 2 + $mbf; - $float = $mbf + 2; - -will both result in the proper type due to the way the overloaded math works. - -This section also applies to other overloaded math packages, like Math::String. - -One solution to you problem might be autoupgrading|upgrading. See the -pragmas L, L and L for an easy way to do this. - -=item bsqrt() - -C works only good if the result is a big integer, e.g. the square -root of 144 is 12, but from 12 the square root is 3, regardless of rounding -mode. The reason is that the result is always truncated to an integer. - -If you want a better approximation of the square root, then use: - - $x = Math::BigFloat->new(12); - Math::BigFloat->precision(0); - Math::BigFloat->round_mode('even'); - print $x->copy->bsqrt(),"\n"; # 4 - - Math::BigFloat->precision(2); - print $x->bsqrt(),"\n"; # 3.46 - print $x->bsqrt(3),"\n"; # 3.464 - -=item brsft() - -For negative numbers in base see also L. - -=back - -=head1 BUGS - -Please report any bugs or feature requests to -C, or through the web interface at -L -(requires login). -We will be notified, and then you'll automatically be notified of progress on -your bug as I make changes. - -=head1 SUPPORT - -You can find documentation for this module with the perldoc command. - - perldoc Math::BigInt - -You can also look for information at: - -=over 4 - -=item * RT: CPAN's request tracker - -L - -=item * AnnoCPAN: Annotated CPAN documentation - -L - -=item * CPAN Ratings - -L - -=item * Search CPAN - -L - -=item * CPAN Testers Matrix - -L - -=item * The Bignum mailing list - -=over 4 - -=item * Post to mailing list - -C - -=item * View mailing list - -L - -=item * Subscribe/Unsubscribe - -L - -=back - -=back - -=head1 LICENSE - -This program is free software; you may redistribute it and/or modify it under -the same terms as Perl itself. - -=head1 SEE ALSO - -L and L as well as the backends -L, L, and L. - -The pragmas L, L and L also might be of interest -because they solve the autoupgrading/downgrading issue, at least partly. - -=head1 AUTHORS - -Original code by Mark Biggar, overloaded interface by Ilya Zakharevich. -Completely rewritten by Tels http://bloodgate.com in late 2000, 2001 - 2006 -and still at it in 2007. - -Many people contributed in one or more ways to the final beast, see the file -CREDITS for an (incomplete) list. If you miss your name, please drop me a -mail. Thank you! - -=cut diff --git a/dist/Math-BigInt/lib/Math/BigInt/Calc.pm b/dist/Math-BigInt/lib/Math/BigInt/Calc.pm deleted file mode 100644 index ce9bf3ab8b..0000000000 --- a/dist/Math-BigInt/lib/Math/BigInt/Calc.pm +++ /dev/null @@ -1,3029 +0,0 @@ -package Math::BigInt::Calc; - -use 5.006002; -use strict; -# use warnings; # do not use warnings for older Perls - -our $VERSION = '1.999701'; - -# Package to store unsigned big integers in decimal and do math with them - -# Internally the numbers are stored in an array with at least 1 element, no -# leading zero parts (except the first) and in base 1eX where X is determined -# automatically at loading time to be the maximum possible value - -# todo: -# - fully remove funky $# stuff in div() (maybe - that code scares me...) - -# USE_MUL: due to problems on certain os (os390, posix-bc) "* 1e-5" is used -# instead of "/ 1e5" at some places, (marked with USE_MUL). Other platforms -# BS2000, some Crays need USE_DIV instead. -# The BEGIN block is used to determine which of the two variants gives the -# correct result. - -# Beware of things like: -# $i = $i * $y + $car; $car = int($i / $BASE); $i = $i % $BASE; -# This works on x86, but fails on ARM (SA1100, iPAQ) due to who knows what -# reasons. So, use this instead (slower, but correct): -# $i = $i * $y + $car; $car = int($i / $BASE); $i -= $BASE * $car; - -############################################################################## -# global constants, flags and accessory - -# announce that we are compatible with MBI v1.83 and up -sub api_version () { 2; } - -# constants for easier life -my ($BASE,$BASE_LEN,$RBASE,$MAX_VAL); -my ($AND_BITS,$XOR_BITS,$OR_BITS); -my ($AND_MASK,$XOR_MASK,$OR_MASK); - -sub _base_len - { - # Set/get the BASE_LEN and assorted other, connected values. - # Used only by the testsuite, the set variant is used only by the BEGIN - # block below: - shift; - - my ($b, $int) = @_; - if (defined $b) - { - # avoid redefinitions - undef &_mul; - undef &_div; - - if ($] >= 5.008 && $int && $b > 7) - { - $BASE_LEN = $b; - *_mul = \&_mul_use_div_64; - *_div = \&_div_use_div_64; - $BASE = int("1e".$BASE_LEN); - $MAX_VAL = $BASE-1; - return $BASE_LEN unless wantarray; - return ($BASE_LEN, $BASE, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL,); - } - - # find whether we can use mul or div in mul()/div() - $BASE_LEN = $b+1; - my $caught = 0; - while (--$BASE_LEN > 5) - { - $BASE = int("1e".$BASE_LEN); - $RBASE = abs('1e-'.$BASE_LEN); # see USE_MUL - $caught = 0; - $caught += 1 if (int($BASE * $RBASE) != 1); # should be 1 - $caught += 2 if (int($BASE / $BASE) != 1); # should be 1 - last if $caught != 3; - } - $BASE = int("1e".$BASE_LEN); - $RBASE = abs('1e-'.$BASE_LEN); # see USE_MUL - $MAX_VAL = $BASE-1; - - # ($caught & 1) != 0 => cannot use MUL - # ($caught & 2) != 0 => cannot use DIV - if ($caught == 2) # 2 - { - # must USE_MUL since we cannot use DIV - *_mul = \&_mul_use_mul; - *_div = \&_div_use_mul; - } - else # 0 or 1 - { - # can USE_DIV instead - *_mul = \&_mul_use_div; - *_div = \&_div_use_div; - } - } - return $BASE_LEN unless wantarray; - return ($BASE_LEN, $BASE, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN, $MAX_VAL); - } - -sub _new - { - # (ref to string) return ref to num_array - # Convert a number from string format (without sign) to internal base - # 1ex format. Assumes normalized value as input. - my $il = length($_[1])-1; - - # < BASE_LEN due len-1 above - return [ int($_[1]) ] if $il < $BASE_LEN; # shortcut for short numbers - - # this leaves '00000' instead of int 0 and will be corrected after any op - [ reverse(unpack("a" . ($il % $BASE_LEN+1) - . ("a$BASE_LEN" x ($il / $BASE_LEN)), $_[1])) ]; - } - -BEGIN - { - # from Daniel Pfeiffer: determine largest group of digits that is precisely - # multipliable with itself plus carry - # Test now changed to expect the proper pattern, not a result off by 1 or 2 - my ($e, $num) = 3; # lowest value we will use is 3+1-1 = 3 - do - { - $num = ('9' x ++$e) + 0; - $num *= $num + 1.0; - } while ("$num" =~ /9{$e}0{$e}/); # must be a certain pattern - $e--; # last test failed, so retract one step - # the limits below brush the problems with the test above under the rug: - # the test should be able to find the proper $e automatically - $e = 5 if $^O =~ /^uts/; # UTS get's some special treatment - $e = 5 if $^O =~ /^unicos/; # unicos is also problematic (6 seems to work - # there, but we play safe) - - my $int = 0; - if ($e > 7) - { - use integer; - my $e1 = 7; - $num = 7; - do - { - $num = ('9' x ++$e1) + 0; - $num *= $num + 1; - } while ("$num" =~ /9{$e1}0{$e1}/); # must be a certain pattern - $e1--; # last test failed, so retract one step - if ($e1 > 7) - { - $int = 1; $e = $e1; - } - } - - __PACKAGE__->_base_len($e,$int); # set and store - - use integer; - # find out how many bits _and, _or and _xor can take (old default = 16) - # I don't think anybody has yet 128 bit scalars, so let's play safe. - local $^W = 0; # don't warn about 'nonportable number' - $AND_BITS = 15; $XOR_BITS = 15; $OR_BITS = 15; - - # find max bits, we will not go higher than numberofbits that fit into $BASE - # to make _and etc simpler (and faster for smaller, slower for large numbers) - my $max = 16; - while (2 ** $max < $BASE) { $max++; } - { - no integer; - $max = 16 if $] < 5.006; # older Perls might not take >16 too well - } - my ($x,$y,$z); - do { - $AND_BITS++; - $x = CORE::oct('0b' . '1' x $AND_BITS); $y = $x & $x; - $z = (2 ** $AND_BITS) - 1; - } while ($AND_BITS < $max && $x == $z && $y == $x); - $AND_BITS --; # retreat one step - do { - $XOR_BITS++; - $x = CORE::oct('0b' . '1' x $XOR_BITS); $y = $x ^ 0; - $z = (2 ** $XOR_BITS) - 1; - } while ($XOR_BITS < $max && $x == $z && $y == $x); - $XOR_BITS --; # retreat one step - do { - $OR_BITS++; - $x = CORE::oct('0b' . '1' x $OR_BITS); $y = $x | $x; - $z = (2 ** $OR_BITS) - 1; - } while ($OR_BITS < $max && $x == $z && $y == $x); - $OR_BITS --; # retreat one step - - $AND_MASK = __PACKAGE__->_new( ( 2 ** $AND_BITS )); - $XOR_MASK = __PACKAGE__->_new( ( 2 ** $XOR_BITS )); - $OR_MASK = __PACKAGE__->_new( ( 2 ** $OR_BITS )); - - # We can compute the approximate length no faster than the real length: - *_alen = \&_len; - } - -############################################################################### - -sub _zero - { - # create a zero - [ 0 ]; - } - -sub _one - { - # create a one - [ 1 ]; - } - -sub _two - { - # create a two (used internally for shifting) - [ 2 ]; - } - -sub _ten - { - # create a 10 (used internally for shifting) - [ 10 ]; - } - -sub _1ex - { - # create a 1Ex - my $rem = $_[1] % $BASE_LEN; # remainder - my $parts = $_[1] / $BASE_LEN; # parts - - # 000000, 000000, 100 - [ (0) x $parts, '1' . ('0' x $rem) ]; - } - -sub _copy - { - # make a true copy - [ @{$_[1]} ]; - } - -# catch and throw away -sub import { } - -############################################################################## -# convert back to string and number - -sub _str - { - # (ref to BINT) return num_str - # Convert number from internal base 100000 format to string format. - # internal format is always normalized (no leading zeros, "-0" => "+0") - my $ar = $_[1]; - - my $l = scalar @$ar; # number of parts - if ($l < 1) # should not happen - { - require Carp; - Carp::croak("$_[1] has no elements"); - } - - my $ret = ""; - # handle first one different to strip leading zeros from it (there are no - # leading zero parts in internal representation) - $l --; $ret .= int($ar->[$l]); $l--; - # Interestingly, the pre-padd method uses more time - # the old grep variant takes longer (14 vs. 10 sec) - my $z = '0' x ($BASE_LEN-1); - while ($l >= 0) - { - $ret .= substr($z.$ar->[$l],-$BASE_LEN); # fastest way I could think of - $l--; - } - $ret; - } - -sub _num - { - # Make a Perl scalar number (int/float) from a BigInt object. - my $x = $_[1]; - - return 0 + $x->[0] if scalar @$x == 1; # below $BASE - - # Start with the most significant element and work towards the least - # significant element. Avoid multiplying "inf" (which happens if the number - # overflows) with "0" (if there are zero elements in $x) since this gives - # "nan" which propagates to the output. - - my $num = 0; - for (my $i = $#$x ; $i >= 0 ; --$i) { - $num *= $BASE; - $num += $x -> [$i]; - } - return $num; - } - -############################################################################## -# actual math code - -sub _add - { - # (ref to int_num_array, ref to int_num_array) - # routine to add two base 1eX numbers - # stolen from Knuth Vol 2 Algorithm A pg 231 - # there are separate routines to add and sub as per Knuth pg 233 - # This routine modifies array x, but not y. - - my ($c,$x,$y) = @_; - - return $x if (@$y == 1) && $y->[0] == 0; # $x + 0 => $x - if ((@$x == 1) && $x->[0] == 0) # 0 + $y => $y->copy - { - # twice as slow as $x = [ @$y ], but nec. to retain $x as ref :( - @$x = @$y; return $x; - } - - # for each in Y, add Y to X and carry. If after that, something is left in - # X, foreach in X add carry to X and then return X, carry - # Trades one "$j++" for having to shift arrays - my $i; my $car = 0; my $j = 0; - for $i (@$y) - { - $x->[$j] -= $BASE if $car = (($x->[$j] += $i + $car) >= $BASE) ? 1 : 0; - $j++; - } - while ($car != 0) - { - $x->[$j] -= $BASE if $car = (($x->[$j] += $car) >= $BASE) ? 1 : 0; $j++; - } - $x; - } - -sub _inc - { - # (ref to int_num_array, ref to int_num_array) - # Add 1 to $x, modify $x in place - my ($c,$x) = @_; - - for my $i (@$x) - { - return $x if (($i += 1) < $BASE); # early out - $i = 0; # overflow, next - } - push @$x,1 if (($x->[-1] || 0) == 0); # last overflowed, so extend - $x; - } - -sub _dec - { - # (ref to int_num_array, ref to int_num_array) - # Sub 1 from $x, modify $x in place - my ($c,$x) = @_; - - my $MAX = $BASE-1; # since MAX_VAL based on BASE - for my $i (@$x) - { - last if (($i -= 1) >= 0); # early out - $i = $MAX; # underflow, next - } - pop @$x if $x->[-1] == 0 && @$x > 1; # last underflowed (but leave 0) - $x; - } - -sub _sub - { - # (ref to int_num_array, ref to int_num_array, swap) - # subtract base 1eX numbers -- stolen from Knuth Vol 2 pg 232, $x > $y - # subtract Y from X by modifying x in place - my ($c,$sx,$sy,$s) = @_; - - my $car = 0; my $i; my $j = 0; - if (!$s) - { - for $i (@$sx) - { - last unless defined $sy->[$j] || $car; - $i += $BASE if $car = (($i -= ($sy->[$j] || 0) + $car) < 0); $j++; - } - # might leave leading zeros, so fix that - return __strip_zeros($sx); - } - for $i (@$sx) - { - # we can't do an early out if $x is < than $y, since we - # need to copy the high chunks from $y. Found by Bob Mathews. - #last unless defined $sy->[$j] || $car; - $sy->[$j] += $BASE - if $car = (($sy->[$j] = $i-($sy->[$j]||0) - $car) < 0); - $j++; - } - # might leave leading zeros, so fix that - __strip_zeros($sy); - } - -sub _mul_use_mul - { - # (ref to int_num_array, ref to int_num_array) - # multiply two numbers in internal representation - # modifies first arg, second need not be different from first - my ($c,$xv,$yv) = @_; - - if (@$yv == 1) - { - # shortcut for two very short numbers (improved by Nathan Zook) - # works also if xv and yv are the same reference, and handles also $x == 0 - if (@$xv == 1) - { - if (($xv->[0] *= $yv->[0]) >= $BASE) - { - $xv->[0] = $xv->[0] - ($xv->[1] = int($xv->[0] * $RBASE)) * $BASE; - }; - return $xv; - } - # $x * 0 => 0 - if ($yv->[0] == 0) - { - @$xv = (0); - return $xv; - } - # multiply a large number a by a single element one, so speed up - my $y = $yv->[0]; my $car = 0; - foreach my $i (@$xv) - { - $i = $i * $y + $car; $car = int($i * $RBASE); $i -= $car * $BASE; - } - push @$xv, $car if $car != 0; - return $xv; - } - # shortcut for result $x == 0 => result = 0 - return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); - - # since multiplying $x with $x fails, make copy in this case - $yv = [@$xv] if $xv == $yv; # same references? - - my @prod = (); my ($prod,$car,$cty,$xi,$yi); - - for $xi (@$xv) - { - $car = 0; $cty = 0; - - # slow variant -# for $yi (@$yv) -# { -# $prod = $xi * $yi + ($prod[$cty] || 0) + $car; -# $prod[$cty++] = -# $prod - ($car = int($prod * RBASE)) * $BASE; # see USE_MUL -# } -# $prod[$cty] += $car if $car; # need really to check for 0? -# $xi = shift @prod; - - # faster variant - # looping through this if $xi == 0 is silly - so optimize it away! - $xi = (shift @prod || 0), next if $xi == 0; - for $yi (@$yv) - { - $prod = $xi * $yi + ($prod[$cty] || 0) + $car; -## this is actually a tad slower -## $prod = $prod[$cty]; $prod += ($car + $xi * $yi); # no ||0 here - $prod[$cty++] = - $prod - ($car = int($prod * $RBASE)) * $BASE; # see USE_MUL - } - $prod[$cty] += $car if $car; # need really to check for 0? - $xi = shift @prod || 0; # || 0 makes v5.005_3 happy - } - push @$xv, @prod; - # can't have leading zeros -# __strip_zeros($xv); - $xv; - } - -sub _mul_use_div_64 - { - # (ref to int_num_array, ref to int_num_array) - # multiply two numbers in internal representation - # modifies first arg, second need not be different from first - # works for 64 bit integer with "use integer" - my ($c,$xv,$yv) = @_; - - use integer; - if (@$yv == 1) - { - # shortcut for two small numbers, also handles $x == 0 - if (@$xv == 1) - { - # shortcut for two very short numbers (improved by Nathan Zook) - # works also if xv and yv are the same reference, and handles also $x == 0 - if (($xv->[0] *= $yv->[0]) >= $BASE) - { - $xv->[0] = - $xv->[0] - ($xv->[1] = $xv->[0] / $BASE) * $BASE; - }; - return $xv; - } - # $x * 0 => 0 - if ($yv->[0] == 0) - { - @$xv = (0); - return $xv; - } - # multiply a large number a by a single element one, so speed up - my $y = $yv->[0]; my $car = 0; - foreach my $i (@$xv) - { - #$i = $i * $y + $car; $car = $i / $BASE; $i -= $car * $BASE; - $i = $i * $y + $car; $i -= ($car = $i / $BASE) * $BASE; - } - push @$xv, $car if $car != 0; - return $xv; - } - # shortcut for result $x == 0 => result = 0 - return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); - - # since multiplying $x with $x fails, make copy in this case - $yv = [@$xv] if $xv == $yv; # same references? - - my @prod = (); my ($prod,$car,$cty,$xi,$yi); - for $xi (@$xv) - { - $car = 0; $cty = 0; - # looping through this if $xi == 0 is silly - so optimize it away! - $xi = (shift @prod || 0), next if $xi == 0; - for $yi (@$yv) - { - $prod = $xi * $yi + ($prod[$cty] || 0) + $car; - $prod[$cty++] = $prod - ($car = $prod / $BASE) * $BASE; - } - $prod[$cty] += $car if $car; # need really to check for 0? - $xi = shift @prod || 0; # || 0 makes v5.005_3 happy - } - push @$xv, @prod; - $xv; - } - -sub _mul_use_div - { - # (ref to int_num_array, ref to int_num_array) - # multiply two numbers in internal representation - # modifies first arg, second need not be different from first - my ($c,$xv,$yv) = @_; - - if (@$yv == 1) - { - # shortcut for two small numbers, also handles $x == 0 - if (@$xv == 1) - { - # shortcut for two very short numbers (improved by Nathan Zook) - # works also if xv and yv are the same reference, and handles also $x == 0 - if (($xv->[0] *= $yv->[0]) >= $BASE) - { - $xv->[0] = - $xv->[0] - ($xv->[1] = int($xv->[0] / $BASE)) * $BASE; - }; - return $xv; - } - # $x * 0 => 0 - if ($yv->[0] == 0) - { - @$xv = (0); - return $xv; - } - # multiply a large number a by a single element one, so speed up - my $y = $yv->[0]; my $car = 0; - foreach my $i (@$xv) - { - $i = $i * $y + $car; $car = int($i / $BASE); $i -= $car * $BASE; - # This (together with use integer;) does not work on 32-bit Perls - #$i = $i * $y + $car; $i -= ($car = $i / $BASE) * $BASE; - } - push @$xv, $car if $car != 0; - return $xv; - } - # shortcut for result $x == 0 => result = 0 - return $xv if ( ((@$xv == 1) && ($xv->[0] == 0)) ); - - # since multiplying $x with $x fails, make copy in this case - $yv = [@$xv] if $xv == $yv; # same references? - - my @prod = (); my ($prod,$car,$cty,$xi,$yi); - for $xi (@$xv) - { - $car = 0; $cty = 0; - # looping through this if $xi == 0 is silly - so optimize it away! - $xi = (shift @prod || 0), next if $xi == 0; - for $yi (@$yv) - { - $prod = $xi * $yi + ($prod[$cty] || 0) + $car; - $prod[$cty++] = $prod - ($car = int($prod / $BASE)) * $BASE; - } - $prod[$cty] += $car if $car; # need really to check for 0? - $xi = shift @prod || 0; # || 0 makes v5.005_3 happy - } - push @$xv, @prod; - # can't have leading zeros -# __strip_zeros($xv); - $xv; - } - -sub _div_use_mul - { - # ref to array, ref to array, modify first array and return remainder if - # in list context - - # see comments in _div_use_div() for more explanations - - my ($c,$x,$yorg) = @_; - - # the general div algorithm here is about O(N*N) and thus quite slow, so - # we first check for some special cases and use shortcuts to handle them. - - # This works, because we store the numbers in a chunked format where each - # element contains 5..7 digits (depending on system). - - # if both numbers have only one element: - if (@$x == 1 && @$yorg == 1) - { - # shortcut, $yorg and $x are two small numbers - if (wantarray) - { - my $r = [ $x->[0] % $yorg->[0] ]; - $x->[0] = int($x->[0] / $yorg->[0]); - return ($x,$r); - } - else - { - $x->[0] = int($x->[0] / $yorg->[0]); - return $x; - } - } - - # if x has more than one, but y has only one element: - if (@$yorg == 1) - { - my $rem; - $rem = _mod($c,[ @$x ],$yorg) if wantarray; - - # shortcut, $y is < $BASE - my $j = scalar @$x; my $r = 0; - my $y = $yorg->[0]; my $b; - while ($j-- > 0) - { - $b = $r * $BASE + $x->[$j]; - $x->[$j] = int($b/$y); - $r = $b % $y; - } - pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero - return ($x,$rem) if wantarray; - return $x; - } - - # now x and y have more than one element - - # check whether y has more elements than x, if yet, the result will be 0 - if (@$yorg > @$x) - { - my $rem; - $rem = [@$x] if wantarray; # make copy - splice (@$x,1); # keep ref to original array - $x->[0] = 0; # set to 0 - return ($x,$rem) if wantarray; # including remainder? - return $x; # only x, which is [0] now - } - # check whether the numbers have the same number of elements, in that case - # the result will fit into one element and can be computed efficiently - if (@$yorg == @$x) - { - my $rem; - # if $yorg has more digits than $x (it's leading element is longer than - # the one from $x), the result will also be 0: - if (length(int($yorg->[-1])) > length(int($x->[-1]))) - { - $rem = [@$x] if wantarray; # make copy - splice (@$x,1); # keep ref to org array - $x->[0] = 0; # set to 0 - return ($x,$rem) if wantarray; # including remainder? - return $x; - } - # now calculate $x / $yorg - if (length(int($yorg->[-1])) == length(int($x->[-1]))) - { - # same length, so make full compare - - my $a = 0; my $j = scalar @$x - 1; - # manual way (abort if unequal, good for early ne) - while ($j >= 0) - { - last if ($a = $x->[$j] - $yorg->[$j]); $j--; - } - # $a contains the result of the compare between X and Y - # a < 0: x < y, a == 0: x == y, a > 0: x > y - if ($a <= 0) - { - $rem = [ 0 ]; # a = 0 => x == y => rem 0 - $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x - splice(@$x,1); # keep single element - $x->[0] = 0; # if $a < 0 - $x->[0] = 1 if $a == 0; # $x == $y - return ($x,$rem) if wantarray; - return $x; - } - # $x >= $y, so proceed normally - } - } - - # all other cases: - - my $y = [ @$yorg ]; # always make copy to preserve - - my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); - - $car = $bar = $prd = 0; - if (($dd = int($BASE/($y->[-1]+1))) != 1) - { - for $xi (@$x) - { - $xi = $xi * $dd + $car; - $xi -= ($car = int($xi * $RBASE)) * $BASE; # see USE_MUL - } - push(@$x, $car); $car = 0; - for $yi (@$y) - { - $yi = $yi * $dd + $car; - $yi -= ($car = int($yi * $RBASE)) * $BASE; # see USE_MUL - } - } - else - { - push(@$x, 0); - } - @q = (); ($v2,$v1) = @$y[-2,-1]; - $v2 = 0 unless $v2; - while ($#$x > $#$y) - { - ($u2,$u1,$u0) = @$x[-3..-1]; - $u2 = 0 unless $u2; - #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" - # if $v1 == 0; - $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); - --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); - if ($q) - { - ($car, $bar) = (0,0); - for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) - { - $prd = $q * $y->[$yi] + $car; - $prd -= ($car = int($prd * $RBASE)) * $BASE; # see USE_MUL - $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); - } - if ($x->[-1] < $car + $bar) - { - $car = 0; --$q; - for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) - { - $x->[$xi] -= $BASE - if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); - } - } - } - pop(@$x); - unshift(@q, $q); - } - if (wantarray) - { - @d = (); - if ($dd != 1) - { - $car = 0; - for $xi (reverse @$x) - { - $prd = $car * $BASE + $xi; - $car = $prd - ($tmp = int($prd / $dd)) * $dd; # see USE_MUL - unshift(@d, $tmp); - } - } - else - { - @d = @$x; - } - @$x = @q; - my $d = \@d; - __strip_zeros($x); - __strip_zeros($d); - return ($x,$d); - } - @$x = @q; - __strip_zeros($x); - $x; - } - -sub _div_use_div_64 - { - # ref to array, ref to array, modify first array and return remainder if - # in list context - # This version works on 64 bit integers - my ($c,$x,$yorg) = @_; - - use integer; - # the general div algorithm here is about O(N*N) and thus quite slow, so - # we first check for some special cases and use shortcuts to handle them. - - # This works, because we store the numbers in a chunked format where each - # element contains 5..7 digits (depending on system). - - # if both numbers have only one element: - if (@$x == 1 && @$yorg == 1) - { - # shortcut, $yorg and $x are two small numbers - if (wantarray) - { - my $r = [ $x->[0] % $yorg->[0] ]; - $x->[0] = int($x->[0] / $yorg->[0]); - return ($x,$r); - } - else - { - $x->[0] = int($x->[0] / $yorg->[0]); - return $x; - } - } - # if x has more than one, but y has only one element: - if (@$yorg == 1) - { - my $rem; - $rem = _mod($c,[ @$x ],$yorg) if wantarray; - - # shortcut, $y is < $BASE - my $j = scalar @$x; my $r = 0; - my $y = $yorg->[0]; my $b; - while ($j-- > 0) - { - $b = $r * $BASE + $x->[$j]; - $x->[$j] = int($b/$y); - $r = $b % $y; - } - pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero - return ($x,$rem) if wantarray; - return $x; - } - # now x and y have more than one element - - # check whether y has more elements than x, if yet, the result will be 0 - if (@$yorg > @$x) - { - my $rem; - $rem = [@$x] if wantarray; # make copy - splice (@$x,1); # keep ref to original array - $x->[0] = 0; # set to 0 - return ($x,$rem) if wantarray; # including remainder? - return $x; # only x, which is [0] now - } - # check whether the numbers have the same number of elements, in that case - # the result will fit into one element and can be computed efficiently - if (@$yorg == @$x) - { - my $rem; - # if $yorg has more digits than $x (it's leading element is longer than - # the one from $x), the result will also be 0: - if (length(int($yorg->[-1])) > length(int($x->[-1]))) - { - $rem = [@$x] if wantarray; # make copy - splice (@$x,1); # keep ref to org array - $x->[0] = 0; # set to 0 - return ($x,$rem) if wantarray; # including remainder? - return $x; - } - # now calculate $x / $yorg - - if (length(int($yorg->[-1])) == length(int($x->[-1]))) - { - # same length, so make full compare - - my $a = 0; my $j = scalar @$x - 1; - # manual way (abort if unequal, good for early ne) - while ($j >= 0) - { - last if ($a = $x->[$j] - $yorg->[$j]); $j--; - } - # $a contains the result of the compare between X and Y - # a < 0: x < y, a == 0: x == y, a > 0: x > y - if ($a <= 0) - { - $rem = [ 0 ]; # a = 0 => x == y => rem 0 - $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x - splice(@$x,1); # keep single element - $x->[0] = 0; # if $a < 0 - $x->[0] = 1 if $a == 0; # $x == $y - return ($x,$rem) if wantarray; # including remainder? - return $x; - } - # $x >= $y, so proceed normally - - } - } - - # all other cases: - - my $y = [ @$yorg ]; # always make copy to preserve - - my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); - - $car = $bar = $prd = 0; - if (($dd = int($BASE/($y->[-1]+1))) != 1) - { - for $xi (@$x) - { - $xi = $xi * $dd + $car; - $xi -= ($car = int($xi / $BASE)) * $BASE; - } - push(@$x, $car); $car = 0; - for $yi (@$y) - { - $yi = $yi * $dd + $car; - $yi -= ($car = int($yi / $BASE)) * $BASE; - } - } - else - { - push(@$x, 0); - } - - # @q will accumulate the final result, $q contains the current computed - # part of the final result - - @q = (); ($v2,$v1) = @$y[-2,-1]; - $v2 = 0 unless $v2; - while ($#$x > $#$y) - { - ($u2,$u1,$u0) = @$x[-3..-1]; - $u2 = 0 unless $u2; - #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" - # if $v1 == 0; - $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); - --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); - if ($q) - { - ($car, $bar) = (0,0); - for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) - { - $prd = $q * $y->[$yi] + $car; - $prd -= ($car = int($prd / $BASE)) * $BASE; - $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); - } - if ($x->[-1] < $car + $bar) - { - $car = 0; --$q; - for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) - { - $x->[$xi] -= $BASE - if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); - } - } - } - pop(@$x); unshift(@q, $q); - } - if (wantarray) - { - @d = (); - if ($dd != 1) - { - $car = 0; - for $xi (reverse @$x) - { - $prd = $car * $BASE + $xi; - $car = $prd - ($tmp = int($prd / $dd)) * $dd; - unshift(@d, $tmp); - } - } - else - { - @d = @$x; - } - @$x = @q; - my $d = \@d; - __strip_zeros($x); - __strip_zeros($d); - return ($x,$d); - } - @$x = @q; - __strip_zeros($x); - $x; - } - -sub _div_use_div - { - # ref to array, ref to array, modify first array and return remainder if - # in list context - my ($c,$x,$yorg) = @_; - - # the general div algorithm here is about O(N*N) and thus quite slow, so - # we first check for some special cases and use shortcuts to handle them. - - # This works, because we store the numbers in a chunked format where each - # element contains 5..7 digits (depending on system). - - # if both numbers have only one element: - if (@$x == 1 && @$yorg == 1) - { - # shortcut, $yorg and $x are two small numbers - if (wantarray) - { - my $r = [ $x->[0] % $yorg->[0] ]; - $x->[0] = int($x->[0] / $yorg->[0]); - return ($x,$r); - } - else - { - $x->[0] = int($x->[0] / $yorg->[0]); - return $x; - } - } - # if x has more than one, but y has only one element: - if (@$yorg == 1) - { - my $rem; - $rem = _mod($c,[ @$x ],$yorg) if wantarray; - - # shortcut, $y is < $BASE - my $j = scalar @$x; my $r = 0; - my $y = $yorg->[0]; my $b; - while ($j-- > 0) - { - $b = $r * $BASE + $x->[$j]; - $x->[$j] = int($b/$y); - $r = $b % $y; - } - pop @$x if @$x > 1 && $x->[-1] == 0; # splice up a leading zero - return ($x,$rem) if wantarray; - return $x; - } - # now x and y have more than one element - - # check whether y has more elements than x, if yet, the result will be 0 - if (@$yorg > @$x) - { - my $rem; - $rem = [@$x] if wantarray; # make copy - splice (@$x,1); # keep ref to original array - $x->[0] = 0; # set to 0 - return ($x,$rem) if wantarray; # including remainder? - return $x; # only x, which is [0] now - } - # check whether the numbers have the same number of elements, in that case - # the result will fit into one element and can be computed efficiently - if (@$yorg == @$x) - { - my $rem; - # if $yorg has more digits than $x (it's leading element is longer than - # the one from $x), the result will also be 0: - if (length(int($yorg->[-1])) > length(int($x->[-1]))) - { - $rem = [@$x] if wantarray; # make copy - splice (@$x,1); # keep ref to org array - $x->[0] = 0; # set to 0 - return ($x,$rem) if wantarray; # including remainder? - return $x; - } - # now calculate $x / $yorg - - if (length(int($yorg->[-1])) == length(int($x->[-1]))) - { - # same length, so make full compare - - my $a = 0; my $j = scalar @$x - 1; - # manual way (abort if unequal, good for early ne) - while ($j >= 0) - { - last if ($a = $x->[$j] - $yorg->[$j]); $j--; - } - # $a contains the result of the compare between X and Y - # a < 0: x < y, a == 0: x == y, a > 0: x > y - if ($a <= 0) - { - $rem = [ 0 ]; # a = 0 => x == y => rem 0 - $rem = [@$x] if $a != 0; # a < 0 => x < y => rem = x - splice(@$x,1); # keep single element - $x->[0] = 0; # if $a < 0 - $x->[0] = 1 if $a == 0; # $x == $y - return ($x,$rem) if wantarray; # including remainder? - return $x; - } - # $x >= $y, so proceed normally - - } - } - - # all other cases: - - my $y = [ @$yorg ]; # always make copy to preserve - - my ($car,$bar,$prd,$dd,$xi,$yi,@q,$v2,$v1,@d,$tmp,$q,$u2,$u1,$u0); - - $car = $bar = $prd = 0; - if (($dd = int($BASE/($y->[-1]+1))) != 1) - { - for $xi (@$x) - { - $xi = $xi * $dd + $car; - $xi -= ($car = int($xi / $BASE)) * $BASE; - } - push(@$x, $car); $car = 0; - for $yi (@$y) - { - $yi = $yi * $dd + $car; - $yi -= ($car = int($yi / $BASE)) * $BASE; - } - } - else - { - push(@$x, 0); - } - - # @q will accumulate the final result, $q contains the current computed - # part of the final result - - @q = (); ($v2,$v1) = @$y[-2,-1]; - $v2 = 0 unless $v2; - while ($#$x > $#$y) - { - ($u2,$u1,$u0) = @$x[-3..-1]; - $u2 = 0 unless $u2; - #warn "oups v1 is 0, u0: $u0 $y->[-2] $y->[-1] l ",scalar @$y,"\n" - # if $v1 == 0; - $q = (($u0 == $v1) ? $MAX_VAL : int(($u0*$BASE+$u1)/$v1)); - --$q while ($v2*$q > ($u0*$BASE+$u1-$q*$v1)*$BASE+$u2); - if ($q) - { - ($car, $bar) = (0,0); - for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) - { - $prd = $q * $y->[$yi] + $car; - $prd -= ($car = int($prd / $BASE)) * $BASE; - $x->[$xi] += $BASE if ($bar = (($x->[$xi] -= $prd + $bar) < 0)); - } - if ($x->[-1] < $car + $bar) - { - $car = 0; --$q; - for ($yi = 0, $xi = $#$x-$#$y-1; $yi <= $#$y; ++$yi,++$xi) - { - $x->[$xi] -= $BASE - if ($car = (($x->[$xi] += $y->[$yi] + $car) >= $BASE)); - } - } - } - pop(@$x); unshift(@q, $q); - } - if (wantarray) - { - @d = (); - if ($dd != 1) - { - $car = 0; - for $xi (reverse @$x) - { - $prd = $car * $BASE + $xi; - $car = $prd - ($tmp = int($prd / $dd)) * $dd; - unshift(@d, $tmp); - } - } - else - { - @d = @$x; - } - @$x = @q; - my $d = \@d; - __strip_zeros($x); - __strip_zeros($d); - return ($x,$d); - } - @$x = @q; - __strip_zeros($x); - $x; - } - -############################################################################## -# testing - -sub _acmp - { - # internal absolute post-normalized compare (ignore signs) - # ref to array, ref to array, return <0, 0, >0 - # arrays must have at least one entry; this is not checked for - my ($c,$cx,$cy) = @_; - - # shortcut for short numbers - return (($cx->[0] <=> $cy->[0]) <=> 0) - if scalar @$cx == scalar @$cy && scalar @$cx == 1; - - # fast comp based on number of array elements (aka pseudo-length) - my $lxy = (scalar @$cx - scalar @$cy) - # or length of first element if same number of elements (aka difference 0) - || - # need int() here because sometimes the last element is '00018' vs '18' - (length(int($cx->[-1])) - length(int($cy->[-1]))); - return -1 if $lxy < 0; # already differs, ret - return 1 if $lxy > 0; # ditto - - # manual way (abort if unequal, good for early ne) - my $a; my $j = scalar @$cx; - while (--$j >= 0) - { - last if ($a = $cx->[$j] - $cy->[$j]); - } - $a <=> 0; - } - -sub _len - { - # compute number of digits in base 10 - - # int() because add/sub sometimes leaves strings (like '00005') instead of - # '5' in this place, thus causing length() to report wrong length - my $cx = $_[1]; - - (@$cx-1)*$BASE_LEN+length(int($cx->[-1])); - } - -sub _digit - { - # Return the nth digit. Zero is rightmost, so _digit(123,0) gives 3. - # Negative values count from the left, so _digit(123, -1) gives 1. - my ($c,$x,$n) = @_; - - my $len = _len('',$x); - - $n += $len if $n < 0; # -1 last, -2 second-to-last - return "0" if $n < 0 || $n >= $len; # return 0 for digits out of range - - my $elem = int($n / $BASE_LEN); # which array element - my $digit = $n % $BASE_LEN; # which digit in this element - substr("$x->[$elem]", -$digit-1, 1); - } - -sub _zeros - { - # return amount of trailing zeros in decimal - # check each array elem in _m for having 0 at end as long as elem == 0 - # Upon finding a elem != 0, stop - my $x = $_[1]; - - return 0 if scalar @$x == 1 && $x->[0] == 0; - - my $zeros = 0; my $elem; - foreach my $e (@$x) - { - if ($e != 0) - { - $elem = "$e"; # preserve x - $elem =~ s/.*?(0*$)/$1/; # strip anything not zero - $zeros *= $BASE_LEN; # elems * 5 - $zeros += length($elem); # count trailing zeros - last; # early out - } - $zeros ++; # real else branch: 50% slower! - } - $zeros; - } - -############################################################################## -# _is_* routines - -sub _is_zero - { - # return true if arg is zero - (((scalar @{$_[1]} == 1) && ($_[1]->[0] == 0))) <=> 0; - } - -sub _is_even - { - # return true if arg is even - (!($_[1]->[0] & 1)) <=> 0; - } - -sub _is_odd - { - # return true if arg is odd - (($_[1]->[0] & 1)) <=> 0; - } - -sub _is_one - { - # return true if arg is one - (scalar @{$_[1]} == 1) && ($_[1]->[0] == 1) <=> 0; - } - -sub _is_two - { - # return true if arg is two - (scalar @{$_[1]} == 1) && ($_[1]->[0] == 2) <=> 0; - } - -sub _is_ten - { - # return true if arg is ten - (scalar @{$_[1]} == 1) && ($_[1]->[0] == 10) <=> 0; - } - -sub __strip_zeros - { - # internal normalization function that strips leading zeros from the array - # args: ref to array - my $s = shift; - - my $cnt = scalar @$s; # get count of parts - my $i = $cnt-1; - push @$s,0 if $i < 0; # div might return empty results, so fix it - - return $s if @$s == 1; # early out - - #print "strip: cnt $cnt i $i\n"; - # '0', '3', '4', '0', '0', - # 0 1 2 3 4 - # cnt = 5, i = 4 - # i = 4 - # i = 3 - # => fcnt = cnt - i (5-2 => 3, cnt => 5-1 = 4, throw away from 4th pos) - # >= 1: skip first part (this can be zero) - while ($i > 0) { last if $s->[$i] != 0; $i--; } - $i++; splice @$s,$i if ($i < $cnt); # $i cant be 0 - $s; - } - -############################################################################### -# check routine to test internal state for corruptions - -sub _check - { - # used by the test suite - my $x = $_[1]; - - return "$x is not a reference" if !ref($x); - - # are all parts are valid? - my $i = 0; my $j = scalar @$x; my ($e,$try); - while ($i < $j) - { - $e = $x->[$i]; $e = 'undef' unless defined $e; - $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e)"; - last if $e !~ /^[+]?[0-9]+$/; - $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e) (stringify)"; - last if "$e" !~ /^[+]?[0-9]+$/; - $try = '=~ /^[\+]?[0-9]+\$/; '."($x, $e) (cat-stringify)"; - last if '' . "$e" !~ /^[+]?[0-9]+$/; - $try = ' < 0 || >= $BASE; '."($x, $e)"; - last if $e <0 || $e >= $BASE; - # this test is disabled, since new/bnorm and certain ops (like early out - # in add/sub) are allowed/expected to leave '00000' in some elements - #$try = '=~ /^00+/; '."($x, $e)"; - #last if $e =~ /^00+/; - $i++; - } - return "Illegal part '$e' at pos $i (tested: $try)" if $i < $j; - 0; - } - - -############################################################################### - -sub _mod - { - # if possible, use mod shortcut - my ($c,$x,$yo) = @_; - - # slow way since $y too big - if (scalar @$yo > 1) - { - my ($xo,$rem) = _div($c,$x,$yo); - @$x = @$rem; - return $x; - } - - my $y = $yo->[0]; - - # if both are single element arrays - if (scalar @$x == 1) - { - $x->[0] %= $y; - return $x; - } - - # if @$x has more than one element, but @$y is a single element - my $b = $BASE % $y; - if ($b == 0) - { - # when BASE % Y == 0 then (B * BASE) % Y == 0 - # (B * BASE) % $y + A % Y => A % Y - # so need to consider only last element: O(1) - $x->[0] %= $y; - } - elsif ($b == 1) - { - # else need to go through all elements in @$x: O(N), but loop is a bit - # simplified - my $r = 0; - foreach (@$x) - { - $r = ($r + $_) % $y; # not much faster, but heh... - #$r += $_ % $y; $r %= $y; - } - $r = 0 if $r == $y; - $x->[0] = $r; - } - else - { - # else need to go through all elements in @$x: O(N) - my $r = 0; - my $bm = 1; - foreach (@$x) - { - $r = ($_ * $bm + $r) % $y; - $bm = ($bm * $b) % $y; - - #$r += ($_ % $y) * $bm; - #$bm *= $b; - #$bm %= $y; - #$r %= $y; - } - $r = 0 if $r == $y; - $x->[0] = $r; - } - @$x = $x->[0]; # keep one element of @$x - return $x; - } - -############################################################################## -# shifts - -sub _rsft - { - my ($c,$x,$y,$n) = @_; - - if ($n != 10) - { - $n = _new($c,$n); return _div($c,$x, _pow($c,$n,$y)); - } - - # shortcut (faster) for shifting by 10) - # multiples of $BASE_LEN - my $dst = 0; # destination - my $src = _num($c,$y); # as normal int - my $xlen = (@$x-1)*$BASE_LEN+length(int($x->[-1])); # len of x in digits - if ($src >= $xlen or ($src == $xlen and ! defined $x->[1])) - { - # 12345 67890 shifted right by more than 10 digits => 0 - splice (@$x,1); # leave only one element - $x->[0] = 0; # set to zero - return $x; - } - my $rem = $src % $BASE_LEN; # remainder to shift - $src = int($src / $BASE_LEN); # source - if ($rem == 0) - { - splice (@$x,0,$src); # even faster, 38.4 => 39.3 - } - else - { - my $len = scalar @$x - $src; # elems to go - my $vd; my $z = '0'x $BASE_LEN; - $x->[scalar @$x] = 0; # avoid || 0 test inside loop - while ($dst < $len) - { - $vd = $z.$x->[$src]; - $vd = substr($vd,-$BASE_LEN,$BASE_LEN-$rem); - $src++; - $vd = substr($z.$x->[$src],-$rem,$rem) . $vd; - $vd = substr($vd,-$BASE_LEN,$BASE_LEN) if length($vd) > $BASE_LEN; - $x->[$dst] = int($vd); - $dst++; - } - splice (@$x,$dst) if $dst > 0; # kill left-over array elems - pop @$x if $x->[-1] == 0 && @$x > 1; # kill last element if 0 - } # else rem == 0 - $x; - } - -sub _lsft - { - my ($c,$x,$y,$n) = @_; - - if ($n != 10) - { - $n = _new($c,$n); return _mul($c,$x, _pow($c,$n,$y)); - } - - # shortcut (faster) for shifting by 10) since we are in base 10eX - # multiples of $BASE_LEN: - my $src = scalar @$x; # source - my $len = _num($c,$y); # shift-len as normal int - my $rem = $len % $BASE_LEN; # remainder to shift - my $dst = $src + int($len/$BASE_LEN); # destination - my $vd; # further speedup - $x->[$src] = 0; # avoid first ||0 for speed - my $z = '0' x $BASE_LEN; - while ($src >= 0) - { - $vd = $x->[$src]; $vd = $z.$vd; - $vd = substr($vd,-$BASE_LEN+$rem,$BASE_LEN-$rem); - $vd .= $src > 0 ? substr($z.$x->[$src-1],-$BASE_LEN,$rem) : '0' x $rem; - $vd = substr($vd,-$BASE_LEN,$BASE_LEN) if length($vd) > $BASE_LEN; - $x->[$dst] = int($vd); - $dst--; $src--; - } - # set lowest parts to 0 - while ($dst >= 0) { $x->[$dst--] = 0; } - # fix spurious last zero element - splice @$x,-1 if $x->[-1] == 0; - $x; - } - -sub _pow - { - # power of $x to $y - # ref to array, ref to array, return ref to array - my ($c,$cx,$cy) = @_; - - if (scalar @$cy == 1 && $cy->[0] == 0) - { - splice (@$cx,1); $cx->[0] = 1; # y == 0 => x => 1 - return $cx; - } - if ((scalar @$cx == 1 && $cx->[0] == 1) || # x == 1 - (scalar @$cy == 1 && $cy->[0] == 1)) # or y == 1 - { - return $cx; - } - if (scalar @$cx == 1 && $cx->[0] == 0) - { - splice (@$cx,1); $cx->[0] = 0; # 0 ** y => 0 (if not y <= 0) - return $cx; - } - - my $pow2 = _one(); - - my $y_bin = _as_bin($c,$cy); $y_bin =~ s/^0b//; - my $len = length($y_bin); - while (--$len > 0) - { - _mul($c,$pow2,$cx) if substr($y_bin,$len,1) eq '1'; # is odd? - _mul($c,$cx,$cx); - } - - _mul($c,$cx,$pow2); - $cx; - } - -sub _nok { - # Return binomial coefficient (n over k). - # Given refs to arrays, return ref to array. - # First input argument is modified. - - my ($c, $n, $k) = @_; - - # If k > n/2, or, equivalently, 2*k > n, compute nok(n, k) as - # nok(n, n-k), to minimize the number if iterations in the loop. - - { - my $twok = _mul($c, _two($c), _copy($c, $k)); # 2 * k - if (_acmp($c, $twok, $n) > 0) { # if 2*k > n - $k = _sub($c, _copy($c, $n), $k); # k = n - k - } - } - - # Example: - # - # / 7 \ 7! 1*2*3*4 * 5*6*7 5 * 6 * 7 6 7 - # | | = --------- = --------------- = --------- = 5 * - * - - # \ 3 / (7-3)! 3! 1*2*3*4 * 1*2*3 1 * 2 * 3 2 3 - - if (_is_zero($c, $k)) { - @$n = 1; - } - - else { - - # Make a copy of the original n, since we'll be modifying n in-place. - - my $n_orig = _copy($c, $n); - - # n = 5, f = 6, d = 2 (cf. example above) - - _sub($c, $n, $k); - _inc($c, $n); - - my $f = _copy($c, $n); - _inc($c, $f); - - my $d = _two($c); - - # while f <= n (the original n, that is) ... - - while (_acmp($c, $f, $n_orig) <= 0) { - - # n = (n * f / d) == 5 * 6 / 2 (cf. example above) - - _mul($c, $n, $f); - _div($c, $n, $d); - - # f = 7, d = 3 (cf. example above) - - _inc($c, $f); - _inc($c, $d); - } - - } - - return $n; -} - -my @factorials = ( - 1, - 1, - 2, - 2*3, - 2*3*4, - 2*3*4*5, - 2*3*4*5*6, - 2*3*4*5*6*7, -); - -sub _fac - { - # factorial of $x - # ref to array, return ref to array - my ($c,$cx) = @_; - - if ((@$cx == 1) && ($cx->[0] <= 7)) - { - $cx->[0] = $factorials[$cx->[0]]; # 0 => 1, 1 => 1, 2 => 2 etc. - return $cx; - } - - if ((@$cx == 1) && # we do this only if $x >= 12 and $x <= 7000 - ($cx->[0] >= 12 && $cx->[0] < 7000)) - { - - # Calculate (k-j) * (k-j+1) ... k .. (k+j-1) * (k + j) - # See http://blogten.blogspot.com/2007/01/calculating-n.html - # The above series can be expressed as factors: - # k * k - (j - i) * 2 - # We cache k*k, and calculate (j * j) as the sum of the first j odd integers - - # This will not work when N exceeds the storage of a Perl scalar, however, - # in this case the algorithm would be way to slow to terminate, anyway. - - # As soon as the last element of $cx is 0, we split it up and remember - # how many zeors we got so far. The reason is that n! will accumulate - # zeros at the end rather fast. - my $zero_elements = 0; - - # If n is even, set n = n -1 - my $k = _num($c,$cx); my $even = 1; - if (($k & 1) == 0) - { - $even = $k; $k --; - } - # set k to the center point - $k = ($k + 1) / 2; -# print "k $k even: $even\n"; - # now calculate k * k - my $k2 = $k * $k; - my $odd = 1; my $sum = 1; - my $i = $k - 1; - # keep reference to x - my $new_x = _new($c, $k * $even); - @$cx = @$new_x; - if ($cx->[0] == 0) - { - $zero_elements ++; shift @$cx; - } -# print STDERR "x = ", _str($c,$cx),"\n"; - my $BASE2 = int(sqrt($BASE))-1; - my $j = 1; - while ($j <= $i) - { - my $m = ($k2 - $sum); $odd += 2; $sum += $odd; $j++; - while ($j <= $i && ($m < $BASE2) && (($k2 - $sum) < $BASE2)) - { - $m *= ($k2 - $sum); - $odd += 2; $sum += $odd; $j++; -# print STDERR "\n k2 $k2 m $m sum $sum odd $odd\n"; sleep(1); - } - if ($m < $BASE) - { - _mul($c,$cx,[$m]); - } - else - { - _mul($c,$cx,$c->_new($m)); - } - if ($cx->[0] == 0) - { - $zero_elements ++; shift @$cx; - } -# print STDERR "Calculate $k2 - $sum = $m (x = ", _str($c,$cx),")\n"; - } - # multiply in the zeros again - unshift @$cx, (0) x $zero_elements; - return $cx; - } - - # go forward until $base is exceeded - # limit is either $x steps (steps == 100 means a result always too high) or - # $base. - my $steps = 100; $steps = $cx->[0] if @$cx == 1; - my $r = 2; my $cf = 3; my $step = 2; my $last = $r; - while ($r*$cf < $BASE && $step < $steps) - { - $last = $r; $r *= $cf++; $step++; - } - if ((@$cx == 1) && $step == $cx->[0]) - { - # completely done, so keep reference to $x and return - $cx->[0] = $r; - return $cx; - } - - # now we must do the left over steps - my $n; # steps still to do - if (scalar @$cx == 1) - { - $n = $cx->[0]; - } - else - { - $n = _copy($c,$cx); - } - - # Set $cx to the last result below $BASE (but keep ref to $x) - $cx->[0] = $last; splice (@$cx,1); - # As soon as the last element of $cx is 0, we split it up and remember - # how many zeors we got so far. The reason is that n! will accumulate - # zeros at the end rather fast. - my $zero_elements = 0; - - # do left-over steps fit into a scalar? - if (ref $n eq 'ARRAY') - { - # No, so use slower inc() & cmp() - # ($n is at least $BASE here) - my $base_2 = int(sqrt($BASE)) - 1; - #print STDERR "base_2: $base_2\n"; - while ($step < $base_2) - { - if ($cx->[0] == 0) - { - $zero_elements ++; shift @$cx; - } - my $b = $step * ($step + 1); $step += 2; - _mul($c,$cx,[$b]); - } - $step = [$step]; - while (_acmp($c,$step,$n) <= 0) - { - if ($cx->[0] == 0) - { - $zero_elements ++; shift @$cx; - } - _mul($c,$cx,$step); _inc($c,$step); - } - } - else - { - # Yes, so we can speed it up slightly - -# print "# left over steps $n\n"; - - my $base_4 = int(sqrt(sqrt($BASE))) - 2; - #print STDERR "base_4: $base_4\n"; - my $n4 = $n - 4; - while ($step < $n4 && $step < $base_4) - { - if ($cx->[0] == 0) - { - $zero_elements ++; shift @$cx; - } - my $b = $step * ($step + 1); $step += 2; $b *= $step * ($step + 1); $step += 2; - _mul($c,$cx,[$b]); - } - my $base_2 = int(sqrt($BASE)) - 1; - my $n2 = $n - 2; - #print STDERR "base_2: $base_2\n"; - while ($step < $n2 && $step < $base_2) - { - if ($cx->[0] == 0) - { - $zero_elements ++; shift @$cx; - } - my $b = $step * ($step + 1); $step += 2; - _mul($c,$cx,[$b]); - } - # do what's left over - while ($step <= $n) - { - _mul($c,$cx,[$step]); $step++; - if ($cx->[0] == 0) - { - $zero_elements ++; shift @$cx; - } - } - } - # multiply in the zeros again - unshift @$cx, (0) x $zero_elements; - $cx; # return result - } - -############################################################################# - -sub _log_int - { - # calculate integer log of $x to base $base - # ref to array, ref to array - return ref to array - my ($c,$x,$base) = @_; - - # X == 0 => NaN - return if (scalar @$x == 1 && $x->[0] == 0); - # BASE 0 or 1 => NaN - return if (scalar @$base == 1 && $base->[0] < 2); - my $cmp = _acmp($c,$x,$base); # X == BASE => 1 - if ($cmp == 0) - { - splice (@$x,1); $x->[0] = 1; - return ($x,1) - } - # X < BASE - if ($cmp < 0) - { - splice (@$x,1); $x->[0] = 0; - return ($x,undef); - } - - my $x_org = _copy($c,$x); # preserve x - splice(@$x,1); $x->[0] = 1; # keep ref to $x - - # Compute a guess for the result based on: - # $guess = int ( length_in_base_10(X) / ( log(base) / log(10) ) ) - my $len = _len($c,$x_org); - my $log = log($base->[-1]) / log(10); - - # for each additional element in $base, we add $BASE_LEN to the result, - # based on the observation that log($BASE,10) is BASE_LEN and - # log(x*y) == log(x) + log(y): - $log += ((scalar @$base)-1) * $BASE_LEN; - - # calculate now a guess based on the values obtained above: - my $res = int($len / $log); - - $x->[0] = $res; - my $trial = _pow ($c, _copy($c, $base), $x); - my $a = _acmp($c,$trial,$x_org); - -# print STDERR "# trial ", _str($c,$x)," was: $a (0 = exact, -1 too small, +1 too big)\n"; - - # found an exact result? - return ($x,1) if $a == 0; - - if ($a > 0) - { - # or too big - _div($c,$trial,$base); _dec($c, $x); - while (($a = _acmp($c,$trial,$x_org)) > 0) - { -# print STDERR "# big _log_int at ", _str($c,$x), "\n"; - _div($c,$trial,$base); _dec($c, $x); - } - # result is now exact (a == 0), or too small (a < 0) - return ($x, $a == 0 ? 1 : 0); - } - - # else: result was to small - _mul($c,$trial,$base); - - # did we now get the right result? - $a = _acmp($c,$trial,$x_org); - - if ($a == 0) # yes, exactly - { - _inc($c, $x); - return ($x,1); - } - return ($x,0) if $a > 0; - - # Result still too small (we should come here only if the estimate above - # was very off base): - - # Now let the normal trial run obtain the real result - # Simple loop that increments $x by 2 in each step, possible overstepping - # the real result - - my $base_mul = _mul($c, _copy($c,$base), $base); # $base * $base - - while (($a = _acmp($c,$trial,$x_org)) < 0) - { -# print STDERR "# small _log_int at ", _str($c,$x), "\n"; - _mul($c,$trial,$base_mul); _add($c, $x, [2]); - } - - my $exact = 1; - if ($a > 0) - { - # overstepped the result - _dec($c, $x); - _div($c,$trial,$base); - $a = _acmp($c,$trial,$x_org); - if ($a > 0) - { - _dec($c, $x); - } - $exact = 0 if $a != 0; # a = -1 => not exact result, a = 0 => exact - } - - ($x,$exact); # return result - } - -# for debugging: - use constant DEBUG => 0; - my $steps = 0; - sub steps { $steps }; - -sub _sqrt - { - # square-root of $x in place - # Compute a guess of the result (by rule of thumb), then improve it via - # Newton's method. - my ($c,$x) = @_; - - if (scalar @$x == 1) - { - # fits into one Perl scalar, so result can be computed directly - $x->[0] = int(sqrt($x->[0])); - return $x; - } - my $y = _copy($c,$x); - # hopefully _len/2 is < $BASE, the -1 is to always undershot the guess - # since our guess will "grow" - my $l = int((_len($c,$x)-1) / 2); - - my $lastelem = $x->[-1]; # for guess - my $elems = scalar @$x - 1; - # not enough digits, but could have more? - if ((length($lastelem) <= 3) && ($elems > 1)) - { - # right-align with zero pad - my $len = length($lastelem) & 1; - print "$lastelem => " if DEBUG; - $lastelem .= substr($x->[-2] . '0' x $BASE_LEN,0,$BASE_LEN); - # former odd => make odd again, or former even to even again - $lastelem = $lastelem / 10 if (length($lastelem) & 1) != $len; - print "$lastelem\n" if DEBUG; - } - - # construct $x (instead of _lsft($c,$x,$l,10) - my $r = $l % $BASE_LEN; # 10000 00000 00000 00000 ($BASE_LEN=5) - $l = int($l / $BASE_LEN); - print "l = $l " if DEBUG; - - splice @$x,$l; # keep ref($x), but modify it - - # we make the first part of the guess not '1000...0' but int(sqrt($lastelem)) - # that gives us: - # 14400 00000 => sqrt(14400) => guess first digits to be 120 - # 144000 000000 => sqrt(144000) => guess 379 - - print "$lastelem (elems $elems) => " if DEBUG; - $lastelem = $lastelem / 10 if ($elems & 1 == 1); # odd or even? - my $g = sqrt($lastelem); $g =~ s/\.//; # 2.345 => 2345 - $r -= 1 if $elems & 1 == 0; # 70 => 7 - - # padd with zeros if result is too short - $x->[$l--] = int(substr($g . '0' x $r,0,$r+1)); - print "now ",$x->[-1] if DEBUG; - print " would have been ", int('1' . '0' x $r),"\n" if DEBUG; - - # If @$x > 1, we could compute the second elem of the guess, too, to create - # an even better guess. Not implemented yet. Does it improve performance? - $x->[$l--] = 0 while ($l >= 0); # all other digits of guess are zero - - print "start x= ",_str($c,$x),"\n" if DEBUG; - my $two = _two(); - my $last = _zero(); - my $lastlast = _zero(); - $steps = 0 if DEBUG; - while (_acmp($c,$last,$x) != 0 && _acmp($c,$lastlast,$x) != 0) - { - $steps++ if DEBUG; - $lastlast = _copy($c,$last); - $last = _copy($c,$x); - _add($c,$x, _div($c,_copy($c,$y),$x)); - _div($c,$x, $two ); - print " x= ",_str($c,$x),"\n" if DEBUG; - } - print "\nsteps in sqrt: $steps, " if DEBUG; - _dec($c,$x) if _acmp($c,$y,_mul($c,_copy($c,$x),$x)) < 0; # overshot? - print " final ",$x->[-1],"\n" if DEBUG; - $x; - } - -sub _root - { - # take n'th root of $x in place (n >= 3) - my ($c,$x,$n) = @_; - - if (scalar @$x == 1) - { - if (scalar @$n > 1) - { - # result will always be smaller than 2 so trunc to 1 at once - $x->[0] = 1; - } - else - { - # fits into one Perl scalar, so result can be computed directly - # cannot use int() here, because it rounds wrongly (try - # (81 ** 3) ** (1/3) to see what I mean) - #$x->[0] = int( $x->[0] ** (1 / $n->[0]) ); - # round to 8 digits, then truncate result to integer - $x->[0] = int ( sprintf ("%.8f", $x->[0] ** (1 / $n->[0]) ) ); - } - return $x; - } - - # we know now that X is more than one element long - - # if $n is a power of two, we can repeatedly take sqrt($X) and find the - # proper result, because sqrt(sqrt($x)) == root($x,4) - my $b = _as_bin($c,$n); - if ($b =~ /0b1(0+)$/) - { - my $count = CORE::length($1); # 0b100 => len('00') => 2 - my $cnt = $count; # counter for loop - unshift (@$x, 0); # add one element, together with one - # more below in the loop this makes 2 - while ($cnt-- > 0) - { - # 'inflate' $X by adding one element, basically computing - # $x * $BASE * $BASE. This gives us more $BASE_LEN digits for result - # since len(sqrt($X)) approx == len($x) / 2. - unshift (@$x, 0); - # calculate sqrt($x), $x is now one element to big, again. In the next - # round we make that two, again. - _sqrt($c,$x); - } - # $x is now one element to big, so truncate result by removing it - splice (@$x,0,1); - } - else - { - # trial computation by starting with 2,4,8,16 etc until we overstep - my $step; - my $trial = _two(); - - # while still to do more than X steps - do - { - $step = _two(); - while (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) < 0) - { - _mul ($c, $step, [2]); - _add ($c, $trial, $step); - } - - # hit exactly? - if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) == 0) - { - @$x = @$trial; # make copy while preserving ref to $x - return $x; - } - # overstepped, so go back on step - _sub($c, $trial, $step); - } while (scalar @$step > 1 || $step->[0] > 128); - - # reset step to 2 - $step = _two(); - # add two, because $trial cannot be exactly the result (otherwise we would - # already have found it) - _add($c, $trial, $step); - - # and now add more and more (2,4,6,8,10 etc) - while (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) < 0) - { - _add ($c, $trial, $step); - } - - # hit not exactly? (overstepped) - if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) > 0) - { - _dec($c,$trial); - } - - # hit not exactly? (overstepped) - # 80 too small, 81 slightly too big, 82 too big - if (_acmp($c, _pow($c, _copy($c, $trial), $n), $x) > 0) - { - _dec ($c, $trial); - } - - @$x = @$trial; # make copy while preserving ref to $x - return $x; - } - $x; - } - -############################################################################## -# binary stuff - -sub _and - { - my ($c,$x,$y) = @_; - - # the shortcut makes equal, large numbers _really_ fast, and makes only a - # very small performance drop for small numbers (e.g. something with less - # than 32 bit) Since we optimize for large numbers, this is enabled. - return $x if _acmp($c,$x,$y) == 0; # shortcut - - my $m = _one(); my ($xr,$yr); - my $mask = $AND_MASK; - - my $x1 = $x; - my $y1 = _copy($c,$y); # make copy - $x = _zero(); - my ($b,$xrr,$yrr); - use integer; - while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) - { - ($x1, $xr) = _div($c,$x1,$mask); - ($y1, $yr) = _div($c,$y1,$mask); - - # make ints() from $xr, $yr - # this is when the AND_BITS are greater than $BASE and is slower for - # small (<256 bits) numbers, but faster for large numbers. Disabled - # due to KISS principle - -# $b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } -# $b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } -# _add($c,$x, _mul($c, _new( $c, ($xrr & $yrr) ), $m) ); - - # 0+ due to '&' doesn't work in strings - _add($c,$x, _mul($c, [ 0+$xr->[0] & 0+$yr->[0] ], $m) ); - _mul($c,$m,$mask); - } - $x; - } - -sub _xor - { - my ($c,$x,$y) = @_; - - return _zero() if _acmp($c,$x,$y) == 0; # shortcut (see -and) - - my $m = _one(); my ($xr,$yr); - my $mask = $XOR_MASK; - - my $x1 = $x; - my $y1 = _copy($c,$y); # make copy - $x = _zero(); - my ($b,$xrr,$yrr); - use integer; - while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) - { - ($x1, $xr) = _div($c,$x1,$mask); - ($y1, $yr) = _div($c,$y1,$mask); - # make ints() from $xr, $yr (see _and()) - #$b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } - #$b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } - #_add($c,$x, _mul($c, _new( $c, ($xrr ^ $yrr) ), $m) ); - - # 0+ due to '^' doesn't work in strings - _add($c,$x, _mul($c, [ 0+$xr->[0] ^ 0+$yr->[0] ], $m) ); - _mul($c,$m,$mask); - } - # the loop stops when the shorter of the two numbers is exhausted - # the remainder of the longer one will survive bit-by-bit, so we simple - # multiply-add it in - _add($c,$x, _mul($c, $x1, $m) ) if !_is_zero($c,$x1); - _add($c,$x, _mul($c, $y1, $m) ) if !_is_zero($c,$y1); - - $x; - } - -sub _or - { - my ($c,$x,$y) = @_; - - return $x if _acmp($c,$x,$y) == 0; # shortcut (see _and) - - my $m = _one(); my ($xr,$yr); - my $mask = $OR_MASK; - - my $x1 = $x; - my $y1 = _copy($c,$y); # make copy - $x = _zero(); - my ($b,$xrr,$yrr); - use integer; - while (!_is_zero($c,$x1) && !_is_zero($c,$y1)) - { - ($x1, $xr) = _div($c,$x1,$mask); - ($y1, $yr) = _div($c,$y1,$mask); - # make ints() from $xr, $yr (see _and()) -# $b = 1; $xrr = 0; foreach (@$xr) { $xrr += $_ * $b; $b *= $BASE; } -# $b = 1; $yrr = 0; foreach (@$yr) { $yrr += $_ * $b; $b *= $BASE; } -# _add($c,$x, _mul($c, _new( $c, ($xrr | $yrr) ), $m) ); - - # 0+ due to '|' doesn't work in strings - _add($c,$x, _mul($c, [ 0+$xr->[0] | 0+$yr->[0] ], $m) ); - _mul($c,$m,$mask); - } - # the loop stops when the shorter of the two numbers is exhausted - # the remainder of the longer one will survive bit-by-bit, so we simple - # multiply-add it in - _add($c,$x, _mul($c, $x1, $m) ) if !_is_zero($c,$x1); - _add($c,$x, _mul($c, $y1, $m) ) if !_is_zero($c,$y1); - - $x; - } - -sub _as_hex - { - # convert a decimal number to hex (ref to array, return ref to string) - my ($c,$x) = @_; - - # fits into one element (handle also 0x0 case) - return sprintf("0x%x",$x->[0]) if @$x == 1; - - my $x1 = _copy($c,$x); - - my $es = ''; - my ($xr, $h, $x10000); - if ($] >= 5.006) - { - $x10000 = [ 0x10000 ]; $h = 'h4'; - } - else - { - $x10000 = [ 0x1000 ]; $h = 'h3'; - } - while (@$x1 != 1 || $x1->[0] != 0) # _is_zero() - { - ($x1, $xr) = _div($c,$x1,$x10000); - $es .= unpack($h,pack('V',$xr->[0])); - } - $es = reverse $es; - $es =~ s/^[0]+//; # strip leading zeros - '0x' . $es; # return result prepended with 0x - } - -sub _as_bin - { - # convert a decimal number to bin (ref to array, return ref to string) - my ($c,$x) = @_; - - # fits into one element (and Perl recent enough), handle also 0b0 case - # handle zero case for older Perls - if ($] <= 5.005 && @$x == 1 && $x->[0] == 0) - { - my $t = '0b0'; return $t; - } - if (@$x == 1 && $] >= 5.006) - { - my $t = sprintf("0b%b",$x->[0]); - return $t; - } - my $x1 = _copy($c,$x); - - my $es = ''; - my ($xr, $b, $x10000); - if ($] >= 5.006) - { - $x10000 = [ 0x10000 ]; $b = 'b16'; - } - else - { - $x10000 = [ 0x1000 ]; $b = 'b12'; - } - while (!(@$x1 == 1 && $x1->[0] == 0)) # _is_zero() - { - ($x1, $xr) = _div($c,$x1,$x10000); - $es .= unpack($b,pack('v',$xr->[0])); - } - $es = reverse $es; - $es =~ s/^[0]+//; # strip leading zeros - '0b' . $es; # return result prepended with 0b - } - -sub _as_oct - { - # convert a decimal number to octal (ref to array, return ref to string) - my ($c,$x) = @_; - - # fits into one element (handle also 0 case) - return sprintf("0%o",$x->[0]) if @$x == 1; - - my $x1 = _copy($c,$x); - - my $es = ''; - my $xr; - my $x1000 = [ 0100000 ]; - while (@$x1 != 1 || $x1->[0] != 0) # _is_zero() - { - ($x1, $xr) = _div($c,$x1,$x1000); - $es .= reverse sprintf("%05o", $xr->[0]); - } - $es = reverse $es; - $es =~ s/^[0]+//; # strip leading zeros - '0' . $es; # return result prepended with 0 - } - -sub _from_oct - { - # convert a octal number to decimal (string, return ref to array) - my ($c,$os) = @_; - - # for older Perls, play safe - my $m = [ 0100000 ]; - my $d = 5; # 5 digits at a time - - my $mul = _one(); - my $x = _zero(); - - my $len = int( (length($os)-1)/$d ); # $d digit parts, w/o the '0' - my $val; my $i = -$d; - while ($len >= 0) - { - $val = substr($os,$i,$d); # get oct digits - $val = CORE::oct($val); - $i -= $d; $len --; - my $adder = [ $val ]; - _add ($c, $x, _mul ($c, $adder, $mul ) ) if $val != 0; - _mul ($c, $mul, $m ) if $len >= 0; # skip last mul - } - $x; - } - -sub _from_hex - { - # convert a hex number to decimal (string, return ref to array) - my ($c,$hs) = @_; - - my $m = _new($c, 0x10000000); # 28 bit at a time (<32 bit!) - my $d = 7; # 7 digits at a time - if ($] <= 5.006) - { - # for older Perls, play safe - $m = [ 0x10000 ]; # 16 bit at a time (<32 bit!) - $d = 4; # 4 digits at a time - } - - my $mul = _one(); - my $x = _zero(); - - my $len = int( (length($hs)-2)/$d ); # $d digit parts, w/o the '0x' - my $val; my $i = -$d; - while ($len >= 0) - { - $val = substr($hs,$i,$d); # get hex digits - $val =~ s/^0x// if $len == 0; # for last part only because - $val = CORE::hex($val); # hex does not like wrong chars - $i -= $d; $len --; - my $adder = [ $val ]; - # if the resulting number was to big to fit into one element, create a - # two-element version (bug found by Mark Lakata - Thanx!) - if (CORE::length($val) > $BASE_LEN) - { - $adder = _new($c,$val); - } - _add ($c, $x, _mul ($c, $adder, $mul ) ) if $val != 0; - _mul ($c, $mul, $m ) if $len >= 0; # skip last mul - } - $x; - } - -sub _from_bin - { - # convert a hex number to decimal (string, return ref to array) - my ($c,$bs) = @_; - - # instead of converting X (8) bit at a time, it is faster to "convert" the - # number to hex, and then call _from_hex. - - my $hs = $bs; - $hs =~ s/^[+-]?0b//; # remove sign and 0b - my $l = length($hs); # bits - $hs = '0' x (8-($l % 8)) . $hs if ($l % 8) != 0; # padd left side w/ 0 - my $h = '0x' . unpack('H*', pack ('B*', $hs)); # repack as hex - - $c->_from_hex($h); - } - -############################################################################## -# special modulus functions - -sub _modinv - { - # modular multiplicative inverse - my ($c,$x,$y) = @_; - - # modulo zero - if (_is_zero($c, $y)) { - return (undef, undef); - } - - # modulo one - if (_is_one($c, $y)) { - return (_zero($c), '+'); - } - - my $u = _zero($c); - my $v = _one($c); - my $a = _copy($c,$y); - my $b = _copy($c,$x); - - # Euclid's Algorithm for bgcd(), only that we calc bgcd() ($a) and the result - # ($u) at the same time. See comments in BigInt for why this works. - my $q; - my $sign = 1; - { - ($a, $q, $b) = ($b, _div($c, $a, $b)); # step 1 - last if _is_zero($c, $b); - - my $t = _add($c, # step 2: - _mul($c, _copy($c, $v), $q) , # t = v * q - $u ); # + u - $u = $v; # u = v - $v = $t; # v = t - $sign = -$sign; - redo; - } - - # if the gcd is not 1, then return NaN - return (undef, undef) unless _is_one($c, $a); - - ($v, $sign == 1 ? '+' : '-'); - } - -sub _modpow - { - # modulus of power ($x ** $y) % $z - my ($c,$num,$exp,$mod) = @_; - - # a^b (mod 1) = 0 for all a and b - if (_is_one($c,$mod)) - { - @$num = 0; - return $num; - } - - # 0^a (mod m) = 0 if m != 0, a != 0 - # 0^0 (mod m) = 1 if m != 0 - if (_is_zero($c, $num)) { - if (_is_zero($c, $exp)) { - @$num = 1; - } else { - @$num = 0; - } - return $num; - } - -# $num = _mod($c,$num,$mod); # this does not make it faster - - my $acc = _copy($c,$num); my $t = _one(); - - my $expbin = _as_bin($c,$exp); $expbin =~ s/^0b//; - my $len = length($expbin); - while (--$len >= 0) - { - if ( substr($expbin,$len,1) eq '1') # is_odd - { - _mul($c,$t,$acc); - $t = _mod($c,$t,$mod); - } - _mul($c,$acc,$acc); - $acc = _mod($c,$acc,$mod); - } - @$num = @$t; - $num; - } - -sub _gcd { - # Greatest common divisor. - - my ($c, $x, $y) = @_; - - # gcd(0,0) = 0 - # gcd(0,a) = a, if a != 0 - - if (@$x == 1 && $x->[0] == 0) { - if (@$y == 1 && $y->[0] == 0) { - @$x = 0; - } else { - @$x = @$y; - } - return $x; - } - - # Until $y is zero ... - - until (@$y == 1 && $y->[0] == 0) { - - # Compute remainder. - - _mod($c, $x, $y); - - # Swap $x and $y. - - my $tmp = [ @$x ]; - @$x = @$y; - $y = $tmp; # no deref here; that would modify input $y - } - - return $x; -} - -############################################################################## -############################################################################## - -1; -__END__ - -=pod - -=head1 NAME - -Math::BigInt::Calc - Pure Perl module to support Math::BigInt - -=head1 SYNOPSIS - -This library provides support for big integer calculations. It is not -intended to be used by other modules. Other modules which support the same -API (see below) can also be used to support Math::BigInt, like -Math::BigInt::GMP and Math::BigInt::Pari. - -=head1 DESCRIPTION - -In this library, the numbers are represented in base B = 10**N, where N is -the largest possible value that does not cause overflow in the intermediate -computations. The base B elements are stored in an array, with the least -significant element stored in array element zero. There are no leading zero -elements, except a single zero element when the number is zero. - -For instance, if B = 10000, the number 1234567890 is represented internally -as [3456, 7890, 12]. - -=head1 THE Math::BigInt API - -In order to allow for multiple big integer libraries, Math::BigInt was -rewritten to use a plug-in library for core math routines. Any module which -conforms to the API can be used by Math::BigInt by using this in your program: - - use Math::BigInt lib => 'libname'; - -'libname' is either the long name, like 'Math::BigInt::Pari', or only the short -version, like 'Pari'. - -=head2 General Notes - -A library only needs to deal with unsigned big integers. Testing of input -parameter validity is done by the caller, so there is no need to worry about -underflow (e.g., in C<_sub()> and C<_dec()>) nor about division by zero (e.g., -in C<_div()>) or similar cases. - -For some methods, the first parameter can be modified. That includes the -possibility that you return a reference to a completely different object -instead. Although keeping the reference and just changing its contents is -preferred over creating and returning a different reference. - -Return values are always objects, strings, Perl scalars, or true/false for -comparison routines. - -=head2 API version 1 - -The following methods must be defined in order to support the use by -Math::BigInt v1.70 or later. - -=head3 API version - -=over 4 - -=item I - -Return API version as a Perl scalar, 1 for Math::BigInt v1.70, 2 for -Math::BigInt v1.83. - -=back - -=head3 Constructors - -=over 4 - -=item I<_new(STR)> - -Convert a string representing an unsigned decimal number to an object -representing the same number. The input is normalize, i.e., it matches -C<^(0|[1-9]\d*)$>. - -=item I<_zero()> - -Return an object representing the number zero. - -=item I<_one()> - -Return an object representing the number one. - -=item I<_two()> - -Return an object representing the number two. - -=item I<_ten()> - -Return an object representing the number ten. - -=item I<_from_bin(STR)> - -Return an object given a string representing a binary number. The input has a -'0b' prefix and matches the regular expression C<^0[bB](0|1[01]*)$>. - -=item I<_from_oct(STR)> - -Return an object given a string representing an octal number. The input has a -'0' prefix and matches the regular expression C<^0[1-7]*$>. - -=item I<_from_hex(STR)> - -Return an object given a string representing a hexadecimal number. The input -has a '0x' prefix and matches the regular expression -C<^0x(0|[1-9a-fA-F][\da-fA-F]*)$>. - -=back - -=head3 Mathematical functions - -Each of these methods may modify the first input argument, except I<_bgcd()>, -which shall not modify any input argument, and I<_sub()> which may modify the -second input argument. - -=over 4 - -=item I<_add(OBJ1, OBJ2)> - -Returns the result of adding OBJ2 to OBJ1. - -=item I<_mul(OBJ1, OBJ2)> - -Returns the result of multiplying OBJ2 and OBJ1. - -=item I<_div(OBJ1, OBJ2)> - -Returns the result of dividing OBJ1 by OBJ2 and truncating the result to an -integer. - -=item I<_sub(OBJ1, OBJ2, FLAG)> - -=item I<_sub(OBJ1, OBJ2)> - -Returns the result of subtracting OBJ2 by OBJ1. If C is false or omitted, -OBJ1 might be modified. If C is true, OBJ2 might be modified. - -=item I<_dec(OBJ)> - -Decrement OBJ by one. - -=item I<_inc(OBJ)> - -Increment OBJ by one. - -=item I<_mod(OBJ1, OBJ2)> - -Return OBJ1 modulo OBJ2, i.e., the remainder after dividing OBJ1 by OBJ2. - -=item I<_sqrt(OBJ)> - -Return the square root of the object, truncated to integer. - -=item I<_root(OBJ, N)> - -Return Nth root of the object, truncated to int. N is E= 3. - -=item I<_fac(OBJ)> - -Return factorial of object (1*2*3*4*...). - -=item I<_pow(OBJ1, OBJ2)> - -Return OBJ1 to the power of OBJ2. By convention, 0**0 = 1. - -=item I<_modinv(OBJ1, OBJ2)> - -Return modular multiplicative inverse, i.e., return OBJ3 so that - - (OBJ3 * OBJ1) % OBJ2 = 1 % OBJ2 - -The result is returned as two arguments. If the modular multiplicative -inverse does not exist, both arguments are undefined. Otherwise, the -arguments are a number (object) and its sign ("+" or "-"). - -The output value, with its sign, must either be a positive value in the -range 1,2,...,OBJ2-1 or the same value subtracted OBJ2. For instance, if the -input arguments are objects representing the numbers 7 and 5, the method -must either return an object representing the number 3 and a "+" sign, since -(3*7) % 5 = 1 % 5, or an object representing the number 2 and "-" sign, -since (-2*7) % 5 = 1 % 5. - -=item I<_modpow(OBJ1, OBJ2, OBJ3)> - -Return modular exponentiation, (OBJ1 ** OBJ2) % OBJ3. - -=item I<_rsft(OBJ, N, B)> - -Shift object N digits right in base B and return the resulting object. This is -equivalent to performing integer division by B**N and discarding the remainder, -except that it might be much faster, depending on how the number is represented -internally. - -For instance, if the object $obj represents the hexadecimal number 0xabcde, -then C<_rsft($obj, 2, 16)> returns an object representing the number 0xabc. The -"remainer", 0xde, is discarded and not returned. - -=item I<_lsft(OBJ, N, B)> - -Shift the object N digits left in base B. This is equivalent to multiplying by -B**N, except that it might be much faster, depending on how the number is -represented internally. - -=item I<_log_int(OBJ, B)> - -Return integer log of OBJ to base BASE. This method has two output arguments, -the OBJECT and a STATUS. The STATUS is Perl scalar; it is 1 if OBJ is the exact -result, 0 if the result was truncted to give OBJ, and undef if it is unknown -whether OBJ is the exact result. - -=item I<_gcd(OBJ1, OBJ2)> - -Return the greatest common divisor of OBJ1 and OBJ2. - -=back - -=head3 Bitwise operators - -Each of these methods may modify the first input argument. - -=over 4 - -=item I<_and(OBJ1, OBJ2)> - -Return bitwise and. If necessary, the smallest number is padded with leading -zeros. - -=item I<_or(OBJ1, OBJ2)> - -Return bitwise or. If necessary, the smallest number is padded with leading -zeros. - -=item I<_xor(OBJ1, OBJ2)> - -Return bitwise exclusive or. If necessary, the smallest number is padded -with leading zeros. - -=back - -=head3 Boolean operators - -=over 4 - -=item I<_is_zero(OBJ)> - -Returns a true value if OBJ is zero, and false value otherwise. - -=item I<_is_one(OBJ)> - -Returns a true value if OBJ is one, and false value otherwise. - -=item I<_is_two(OBJ)> - -Returns a true value if OBJ is two, and false value otherwise. - -=item I<_is_ten(OBJ)> - -Returns a true value if OBJ is ten, and false value otherwise. - -=item I<_is_even(OBJ)> - -Return a true value if OBJ is an even integer, and a false value otherwise. - -=item I<_is_odd(OBJ)> - -Return a true value if OBJ is an even integer, and a false value otherwise. - -=item I<_acmp(OBJ1, OBJ2)> - -Compare OBJ1 and OBJ2 and return -1, 0, or 1, if OBJ1 is less than, equal -to, or larger than OBJ2, respectively. - -=back - -=head3 String conversion - -=over 4 - -=item I<_str(OBJ)> - -Return a string representing the object. The returned string should have no -leading zeros, i.e., it should match C<^(0|[1-9]\d*)$>. - -=item I<_as_bin(OBJ)> - -Return the binary string representation of the number. The string must have a -'0b' prefix. - -=item I<_as_oct(OBJ)> - -Return the octal string representation of the number. The string must have -a '0x' prefix. - -Note: This method was required from Math::BigInt version 1.78, but the required -API version number was not incremented, so there are older libraries that -support API version 1, but do not support C<_as_oct()>. - -=item I<_as_hex(OBJ)> - -Return the hexadecimal string representation of the number. The string must -have a '0x' prefix. - -=back - -=head3 Numeric conversion - -=over 4 - -=item I<_num(OBJ)> - -Given an object, return a Perl scalar number (int/float) representing this -number. - -=back - -=head3 Miscellaneous - -=over 4 - -=item I<_copy(OBJ)> - -Return a true copy of the object. - -=item I<_len(OBJ)> - -Returns the number of the decimal digits in the number. The output is a -Perl scalar. - -=item I<_zeros(OBJ)> - -Return the number of trailing decimal zeros. The output is a Perl scalar. - -=item I<_digit(OBJ, N)> - -Return the Nth digit as a Perl scalar. N is a Perl scalar, where zero refers to -the rightmost (least significant) digit, and negative values count from the -left (most significant digit). If $obj represents the number 123, then -I<_digit($obj, 0)> is 3 and I<_digit(123, -1)> is 1. - -=item I<_check(OBJ)> - -Return a true value if the object is OK, and a false value otherwise. This is a -check routine to test the internal state of the object for corruption. - -=back - -=head2 API version 2 - -The following methods are required for an API version of 2 or greater. - -=head3 Constructors - -=over 4 - -=item I<_1ex(N)> - -Return an object representing the number 10**N where N E= 0 is a Perl -scalar. - -=back - -=head3 Mathematical functions - -=over 4 - -=item I<_nok(OBJ1, OBJ2)> - -Return the binomial coefficient OBJ1 over OBJ1. - -=back - -=head3 Miscellaneous - -=over 4 - -=item I<_alen(OBJ)> - -Return the approximate number of decimal digits of the object. The -output is one Perl scalar. This estimate must be greater than or equal -to what C<_len()> returns. - -=back - -=head2 API optional methods - -The following methods are optional, and can be defined if the underlying lib -has a fast way to do them. If undefined, Math::BigInt will use pure Perl (hence -slow) fallback routines to emulate these: - -=head3 Signed bitwise operators. - -Each of these methods may modify the first input argument. - -=over 4 - -=item I<_signed_or(OBJ1, OBJ2, SIGN1, SIGN2)> - -Return the signed bitwise or. - -=item I<_signed_and(OBJ1, OBJ2, SIGN1, SIGN2)> - -Return the signed bitwise and. - -=item I<_signed_xor(OBJ1, OBJ2, SIGN1, SIGN2)> - -Return the signed bitwise exclusive or. - -=back - -=head1 WRAP YOUR OWN - -If you want to port your own favourite c-lib for big numbers to the -Math::BigInt interface, you can take any of the already existing modules as -a rough guideline. You should really wrap up the latest BigInt and BigFloat -testsuites with your module, and replace in them any of the following: - - use Math::BigInt; - -by this: - - use Math::BigInt lib => 'yourlib'; - -This way you ensure that your library really works 100% within Math::BigInt. - -=head1 BUGS - -Please report any bugs or feature requests to -C, or through the web interface at -L -(requires login). -We will be notified, and then you'll automatically be notified of progress on -your bug as I make changes. - -=head1 SUPPORT - -You can find documentation for this module with the perldoc command. - - perldoc Math::BigInt::Calc - -You can also look for information at: - -=over 4 - -=item * RT: CPAN's request tracker - -L - -=item * AnnoCPAN: Annotated CPAN documentation - -L - -=item * CPAN Ratings - -L - -=item * Search CPAN - -L - -=item * CPAN Testers Matrix - -L - -=item * The Bignum mailing list - -=over 4 - -=item * Post to mailing list - -C - -=item * View mailing list - -L - -=item * Subscribe/Unsubscribe - -L - -=back - -=back - -=head1 LICENSE - -This program is free software; you may redistribute it and/or modify it under -the same terms as Perl itself. - -=head1 AUTHORS - -=over 4 - -=item * - -Original math code by Mark Biggar, rewritten by Tels L -in late 2000. - -=item * - -Separated from BigInt and shaped API with the help of John Peacock. - -=item * - -Fixed, speed-up, streamlined and enhanced by Tels 2001 - 2007. - -=item * - -API documentation corrected and extended by Peter John Acklam, -Epjacklam@online.noE - -=back - -=head1 SEE ALSO - -L, L, -L, L and L. - -=cut diff --git a/dist/Math-BigInt/lib/Math/BigInt/CalcEmu.pm b/dist/Math-BigInt/lib/Math/BigInt/CalcEmu.pm deleted file mode 100644 index 0ff9dcca17..0000000000 --- a/dist/Math-BigInt/lib/Math/BigInt/CalcEmu.pm +++ /dev/null @@ -1,395 +0,0 @@ -package Math::BigInt::CalcEmu; - -use 5.006002; -use strict; -# use warnings; # do not use warnings for older Perls -use vars qw/$VERSION/; - -$VERSION = '1.999701'; - -package Math::BigInt; - -# See SYNOPSIS below. - -my $CALC_EMU; - -BEGIN - { - $CALC_EMU = Math::BigInt->config()->{'lib'}; - # register us with MBI to get notified of future lib changes - Math::BigInt::_register_callback( __PACKAGE__, sub { $CALC_EMU = $_[0]; } ); - } - -sub __emu_band - { - my ($self,$x,$y,$sx,$sy,@r) = @_; - - return $x->bzero(@r) if $y->is_zero() || $x->is_zero(); - - my $sign = 0; # sign of result - $sign = 1 if $sx == -1 && $sy == -1; - - my ($bx,$by); - - if ($sx == -1) # if x is negative - { - # two's complement: inc and flip all "bits" in $bx - $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc - $bx =~ s/-?0x//; - $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - else - { - $bx = $x->as_hex(); # get binary representation - $bx =~ s/-?0x//; - $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - if ($sy == -1) # if y is negative - { - # two's complement: inc and flip all "bits" in $by - $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc - $by =~ s/-?0x//; - $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - else - { - $by = $y->as_hex(); # get binary representation - $by =~ s/-?0x//; - $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - # now we have bit-strings from X and Y, reverse them for padding - $bx = reverse $bx; - $by = reverse $by; - - # padd the shorter string - my $xx = "\x00"; $xx = "\x0f" if $sx == -1; - my $yy = "\x00"; $yy = "\x0f" if $sy == -1; - my $diff = CORE::length($bx) - CORE::length($by); - if ($diff > 0) - { - # if $yy eq "\x00", we can cut $bx, otherwise we need to padd $by - $by .= $yy x $diff; - } - elsif ($diff < 0) - { - # if $xx eq "\x00", we can cut $by, otherwise we need to padd $bx - $bx .= $xx x abs($diff); - } - - # and the strings together - my $r = $bx & $by; - - # and reverse the result again - $bx = reverse $r; - - # One of $x or $y was negative, so need to flip bits in the result. - # In both cases (one or two of them negative, or both positive) we need - # to get the characters back. - if ($sign == 1) - { - $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; - } - else - { - $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; - } - - # leading zeros will be stripped by _from_hex() - $bx = '0x' . $bx; - $x->{value} = $CALC_EMU->_from_hex( $bx ); - - # calculate sign of result - $x->{sign} = '+'; - $x->{sign} = '-' if $sign == 1 && !$x->is_zero(); - - $x->bdec() if $sign == 1; - - $x->round(@r); - } - -sub __emu_bior - { - my ($self,$x,$y,$sx,$sy,@r) = @_; - - return $x->round(@r) if $y->is_zero(); - - my $sign = 0; # sign of result - $sign = 1 if ($sx == -1) || ($sy == -1); - - my ($bx,$by); - - if ($sx == -1) # if x is negative - { - # two's complement: inc and flip all "bits" in $bx - $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc - $bx =~ s/-?0x//; - $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - else - { - $bx = $x->as_hex(); # get binary representation - $bx =~ s/-?0x//; - $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - if ($sy == -1) # if y is negative - { - # two's complement: inc and flip all "bits" in $by - $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc - $by =~ s/-?0x//; - $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - else - { - $by = $y->as_hex(); # get binary representation - $by =~ s/-?0x//; - $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - # now we have bit-strings from X and Y, reverse them for padding - $bx = reverse $bx; - $by = reverse $by; - - # padd the shorter string - my $xx = "\x00"; $xx = "\x0f" if $sx == -1; - my $yy = "\x00"; $yy = "\x0f" if $sy == -1; - my $diff = CORE::length($bx) - CORE::length($by); - if ($diff > 0) - { - $by .= $yy x $diff; - } - elsif ($diff < 0) - { - $bx .= $xx x abs($diff); - } - - # or the strings together - my $r = $bx | $by; - - # and reverse the result again - $bx = reverse $r; - - # one of $x or $y was negative, so need to flip bits in the result - # in both cases (one or two of them negative, or both positive) we need - # to get the characters back. - if ($sign == 1) - { - $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; - } - else - { - $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; - } - - # leading zeros will be stripped by _from_hex() - $bx = '0x' . $bx; - $x->{value} = $CALC_EMU->_from_hex( $bx ); - - # calculate sign of result - $x->{sign} = '+'; - $x->{sign} = '-' if $sign == 1 && !$x->is_zero(); - - # if one of X or Y was negative, we need to decrement result - $x->bdec() if $sign == 1; - - $x->round(@r); - } - -sub __emu_bxor - { - my ($self,$x,$y,$sx,$sy,@r) = @_; - - return $x->round(@r) if $y->is_zero(); - - my $sign = 0; # sign of result - $sign = 1 if $x->{sign} ne $y->{sign}; - - my ($bx,$by); - - if ($sx == -1) # if x is negative - { - # two's complement: inc and flip all "bits" in $bx - $bx = $x->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc - $bx =~ s/-?0x//; - $bx =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - else - { - $bx = $x->as_hex(); # get binary representation - $bx =~ s/-?0x//; - $bx =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - if ($sy == -1) # if y is negative - { - # two's complement: inc and flip all "bits" in $by - $by = $y->copy()->binc()->as_hex(); # -1 => 0, -2 => 1, -3 => 2 etc - $by =~ s/-?0x//; - $by =~ tr/0123456789abcdef/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - else - { - $by = $y->as_hex(); # get binary representation - $by =~ s/-?0x//; - $by =~ tr/fedcba9876543210/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/; - } - # now we have bit-strings from X and Y, reverse them for padding - $bx = reverse $bx; - $by = reverse $by; - - # padd the shorter string - my $xx = "\x00"; $xx = "\x0f" if $sx == -1; - my $yy = "\x00"; $yy = "\x0f" if $sy == -1; - my $diff = CORE::length($bx) - CORE::length($by); - if ($diff > 0) - { - $by .= $yy x $diff; - } - elsif ($diff < 0) - { - $bx .= $xx x abs($diff); - } - - # xor the strings together - my $r = $bx ^ $by; - - # and reverse the result again - $bx = reverse $r; - - # one of $x or $y was negative, so need to flip bits in the result - # in both cases (one or two of them negative, or both positive) we need - # to get the characters back. - if ($sign == 1) - { - $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/0123456789abcdef/; - } - else - { - $bx =~ tr/\x0f\x0e\x0d\x0c\x0b\x0a\x09\x08\x07\x06\x05\x04\x03\x02\x01\x00/fedcba9876543210/; - } - - # leading zeros will be stripped by _from_hex() - $bx = '0x' . $bx; - $x->{value} = $CALC_EMU->_from_hex( $bx ); - - # calculate sign of result - $x->{sign} = '+'; - $x->{sign} = '-' if $sx != $sy && !$x->is_zero(); - - $x->bdec() if $sign == 1; - - $x->round(@r); - } - -############################################################################## -############################################################################## - -1; - -__END__ - -=pod - -=head1 NAME - -Math::BigInt::CalcEmu - Emulate low-level math with BigInt code - -=head1 SYNOPSIS - - use Math::BigInt::CalcEmu; - -=head1 DESCRIPTION - -Contains routines that emulate low-level math functions in BigInt, e.g. -optional routines the low-level math package does not provide on its own. - -Will be loaded on demand and called automatically by BigInt. - -Stuff here is really low-priority to optimize, since it is far better to -implement the operation in the low-level math library directly, possible even -using a call to the native lib. - -=head1 METHODS - -=over - -=item __emu_bxor - -=item __emu_band - -=item __emu_bior - -=back - -=head1 BUGS - -Please report any bugs or feature requests to -C, or through the web interface at -L -(requires login). -We will be notified, and then you'll automatically be notified of progress on -your bug as I make changes. - -=head1 SUPPORT - -You can find documentation for this module with the perldoc command. - - perldoc Math::BigInt::CalcEmu - -You can also look for information at: - -=over 4 - -=item * RT: CPAN's request tracker - -L - -=item * AnnoCPAN: Annotated CPAN documentation - -L - -=item * CPAN Ratings - -L - -=item * Search CPAN - -L - -=item * CPAN Testers Matrix - -L - -=item * The Bignum mailing list - -=over 4 - -=item * Post to mailing list - -C - -=item * View mailing list - -L - -=item * Subscribe/Unsubscribe - -L - -=back - -=back - -=head1 LICENSE - -This program is free software; you may redistribute it and/or modify it under -the same terms as Perl itself. - -=head1 AUTHORS - -(c) Tels http://bloodgate.com 2003, 2004 - based on BigInt code by -Tels from 2001-2003. - -=head1 SEE ALSO - -L, L, -L and L. - -=cut diff --git a/dist/Math-BigInt/t/Math/BigFloat/Subclass.pm b/dist/Math-BigInt/t/Math/BigFloat/Subclass.pm deleted file mode 100644 index 94d3f2a624..0000000000 --- a/dist/Math-BigInt/t/Math/BigFloat/Subclass.pm +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/perl -w - -# for testing subclassing Math::BigFloat - -package Math::BigFloat::Subclass; - -require 5.005_02; -use strict; - -use Exporter; -use Math::BigFloat(1.38); -use vars qw($VERSION @ISA $PACKAGE - $accuracy $precision $round_mode $div_scale); - -@ISA = qw(Exporter Math::BigFloat); - -$VERSION = 0.05; - -use overload; # inherit overload from BigInt - -# Globals -$accuracy = $precision = undef; -$round_mode = 'even'; -$div_scale = 40; - -sub new -{ - my $proto = shift; - my $class = ref($proto) || $proto; - - my $value = shift; - my $a = $accuracy; $a = $_[0] if defined $_[0]; - my $p = $precision; $p = $_[1] if defined $_[1]; - # Store the floating point value - my $self = Math::BigFloat->new($value,$a,$p,$round_mode); - bless $self, $class; - $self->{'_custom'} = 1; # make sure this never goes away - return $self; -} - -BEGIN - { - *objectify = \&Math::BigInt::objectify; - # to allow Math::BigFloat::Subclass::bgcd( ... ) style calls - *bgcd = \&Math::BigFloat::bgcd; - *blcm = \&Math::BigFloat::blcm; - } - -1; diff --git a/dist/Math-BigInt/t/Math/BigInt/BareCalc.pm b/dist/Math-BigInt/t/Math/BigInt/BareCalc.pm deleted file mode 100644 index 0bbe861cf8..0000000000 --- a/dist/Math-BigInt/t/Math/BigInt/BareCalc.pm +++ /dev/null @@ -1,44 +0,0 @@ -package Math::BigInt::BareCalc; - -use 5.005; -use strict; -# use warnings; # dont use warnings for older Perls - -require Exporter; -use vars qw/@ISA $VERSION/; -@ISA = qw(Exporter); - -$VERSION = '0.05'; - -sub api_version () { 1; } - -# Package to to test Bigint's simulation of Calc - -# uses Calc, but only features the strictly necc. methods. - -use Math::BigInt::Calc '0.51'; - -BEGIN - { - no strict 'refs'; - foreach (qw/ - base_len new zero one two ten copy str num add sub mul div mod inc dec - acmp alen len digit zeros - rsft lsft - fac pow gcd log_int sqrt root - is_zero is_one is_odd is_even is_one is_two is_ten check - as_hex as_bin as_oct from_hex from_bin from_oct - modpow modinv - and xor or - /) - { - my $name = "Math::BigInt::Calc::_$_"; - *{"Math::BigInt::BareCalc::_$_"} = \&$name; - } - print "# BareCalc using Calc v$Math::BigInt::Calc::VERSION\n"; - } - -# catch and throw away -sub import { } - -1; diff --git a/dist/Math-BigInt/t/Math/BigInt/Scalar.pm b/dist/Math-BigInt/t/Math/BigInt/Scalar.pm deleted file mode 100644 index c20a3e377e..0000000000 --- a/dist/Math-BigInt/t/Math/BigInt/Scalar.pm +++ /dev/null @@ -1,355 +0,0 @@ -############################################################################### -# core math lib for BigInt, representing big numbers by normal int/float's -# for testing only, will fail any bignum test if range is exceeded - -package Math::BigInt::Scalar; - -use 5.005; -use strict; -# use warnings; # dont use warnings for older Perls - -require Exporter; - -use vars qw/@ISA $VERSION/; -@ISA = qw(Exporter); - -$VERSION = '0.13'; - -sub api_version() { 1; } - -############################################################################## -# global constants, flags and accessory - -# constants for easier life -my $nan = 'NaN'; - -############################################################################## -# create objects from various representations - -sub _new - { - # create scalar ref from string - my $d = $_[1]; - my $x = $d; # make copy - \$x; - } - -sub _from_hex - { - # not used - } - -sub _from_oct - { - # not used - } - -sub _from_bin - { - # not used - } - -sub _zero - { - my $x = 0; \$x; - } - -sub _one - { - my $x = 1; \$x; - } - -sub _two - { - my $x = 2; \$x; - } - -sub _ten - { - my $x = 10; \$x; - } - -sub _copy - { - my $x = $_[1]; - my $z = $$x; - \$z; - } - -# catch and throw away -sub import { } - -############################################################################## -# convert back to string and number - -sub _str - { - # make string - "${$_[1]}"; - } - -sub _num - { - # make a number - 0+${$_[1]}; - } - -sub _zeros - { - my $x = $_[1]; - - $x =~ /\d(0*)$/; - length($1 || ''); - } - -sub _rsft - { - # not used - } - -sub _lsft - { - # not used - } - -sub _mod - { - # not used - } - -sub _gcd - { - # not used - } - -sub _sqrt - { - # not used - } - -sub _root - { - # not used - } - -sub _fac - { - # not used - } - -sub _modinv - { - # not used - } - -sub _modpow - { - # not used - } - -sub _log_int - { - # not used - } - -sub _as_hex - { - sprintf("0x%x",${$_[1]}); - } - -sub _as_bin - { - sprintf("0b%b",${$_[1]}); - } - -sub _as_oct - { - sprintf("0%o",${$_[1]}); - } - -############################################################################## -# actual math code - -sub _add - { - my ($c,$x,$y) = @_; - $$x += $$y; - return $x; - } - -sub _sub - { - my ($c,$x,$y) = @_; - $$x -= $$y; - return $x; - } - -sub _mul - { - my ($c,$x,$y) = @_; - $$x *= $$y; - return $x; - } - -sub _div - { - my ($c,$x,$y) = @_; - - my $u = int($$x / $$y); my $r = $$x % $$y; $$x = $u; - return ($x,\$r) if wantarray; - return $x; - } - -sub _pow - { - my ($c,$x,$y) = @_; - my $u = $$x ** $$y; $$x = $u; - return $x; - } - -sub _and - { - my ($c,$x,$y) = @_; - my $u = int($$x) & int($$y); $$x = $u; - return $x; - } - -sub _xor - { - my ($c,$x,$y) = @_; - my $u = int($$x) ^ int($$y); $$x = $u; - return $x; - } - -sub _or - { - my ($c,$x,$y) = @_; - my $u = int($$x) | int($$y); $$x = $u; - return $x; - } - -sub _inc - { - my ($c,$x) = @_; - my $u = int($$x)+1; $$x = $u; - return $x; - } - -sub _dec - { - my ($c,$x) = @_; - my $u = int($$x)-1; $$x = $u; - return $x; - } - -############################################################################## -# testing - -sub _acmp - { - my ($c,$x, $y) = @_; - return ($$x <=> $$y); - } - -sub _len - { - return length("${$_[1]}"); - } - -sub _digit - { - # return the nth digit, negative values count backward - # 0 is the rightmost digit - my ($c,$x,$n) = @_; - - $n ++; # 0 => 1, 1 => 2 - return substr($$x,-$n,1); # 1 => -1, -2 => 2 etc - } - -############################################################################## -# _is_* routines - -sub _is_zero - { - # return true if arg is zero - my ($c,$x) = @_; - ($$x == 0) <=> 0; - } - -sub _is_even - { - # return true if arg is even - my ($c,$x) = @_; - (!($$x & 1)) <=> 0; - } - -sub _is_odd - { - # return true if arg is odd - my ($c,$x) = @_; - ($$x & 1) <=> 0; - } - -sub _is_one - { - # return true if arg is one - my ($c,$x) = @_; - ($$x == 1) <=> 0; - } - -sub _is_two - { - # return true if arg is one - my ($c,$x) = @_; - ($$x == 2) <=> 0; - } - -sub _is_ten - { - # return true if arg is one - my ($c,$x) = @_; - ($$x == 10) <=> 0; - } - -############################################################################### -# check routine to test internal state of corruptions - -sub _check - { - # no checks yet, pull it out from the test suite - my ($c,$x) = @_; - return "$x is not a reference" if !ref($x); - return 0; - } - -1; -__END__ - -=head1 NAME - -Math::BigInt::Scalar - Pure Perl module to test Math::BigInt with scalars - -=head1 SYNOPSIS - -Provides support for big integer calculations via means of 'small' int/floats. -Only for testing purposes, since it will fail at large values. But it is simple -enough not to introduce bugs on it's own and to serve as a testbed. - -=head1 DESCRIPTION - -Please see Math::BigInt::Calc. - -=head1 LICENSE - -This program is free software; you may redistribute it and/or modify it under -the same terms as Perl itself. - -=head1 AUTHOR - -Tels http://bloodgate.com in 2001 - 2007. - -=head1 SEE ALSO - -L, L. - -=cut diff --git a/dist/Math-BigInt/t/Math/BigInt/Subclass.pm b/dist/Math-BigInt/t/Math/BigInt/Subclass.pm deleted file mode 100644 index d45e9e53ad..0000000000 --- a/dist/Math-BigInt/t/Math/BigInt/Subclass.pm +++ /dev/null @@ -1,90 +0,0 @@ -#!/usr/bin/perl -w - -package Math::BigInt::Subclass; - -require 5.005_02; -use strict; - -use Exporter; -use Math::BigInt (1.64); -# $lib is for the "lib => " test -use vars qw($VERSION @ISA $PACKAGE @EXPORT_OK - $lib - $accuracy $precision $round_mode $div_scale); - -@ISA = qw(Exporter Math::BigInt); -@EXPORT_OK = qw(bgcd objectify); - -$VERSION = 0.04; - -use overload; # inherit overload from BigInt - -# Globals -$accuracy = $precision = undef; -$round_mode = 'even'; -$div_scale = 40; -$lib = ''; - -sub new -{ - my $proto = shift; - my $class = ref($proto) || $proto; - - my $value = shift; - my $a = $accuracy; $a = $_[0] if defined $_[0]; - my $p = $precision; $p = $_[1] if defined $_[1]; - my $self = Math::BigInt->new($value,$a,$p,$round_mode); - bless $self,$class; - $self->{'_custom'} = 1; # make sure this never goes away - return $self; -} - -sub bgcd - { - Math::BigInt::bgcd(@_); - } - -sub blcm - { - Math::BigInt::blcm(@_); - } - -sub as_int - { - Math::BigInt->new($_[0]); - } - -BEGIN - { - *objectify = \&Math::BigInt::objectify; - - # these are called by AUTOLOAD from BigFloat, so we need at least these. - # We cheat, of course.. - *bneg = \&Math::BigInt::bneg; - *babs = \&Math::BigInt::babs; - *bnan = \&Math::BigInt::bnan; - *binf = \&Math::BigInt::binf; - *bzero = \&Math::BigInt::bzero; - *bone = \&Math::BigInt::bone; - } - -sub import - { - my $self = shift; - - my @a; my $t = 0; - foreach (@_) - { - # remove the "lib => foo" parameters and store it - $lib = $_, $t = 0, next if $t == 1; - if ($_ eq 'lib') - { - $t = 1; next; - } - push @a,$_; - } - $self->SUPER::import(@a); # need it for subclasses - $self->export_to_level(1,$self,@a); # need this ? - } - -1; diff --git a/dist/Math-BigInt/t/_e_math.t b/dist/Math-BigInt/t/_e_math.t deleted file mode 100644 index bae9e2df09..0000000000 --- a/dist/Math-BigInt/t/_e_math.t +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/perl -w - -# test the helper math routines in Math::BigFloat - -use strict; -use Test::More tests => 26; - -use Math::BigFloat lib => 'Calc'; - -############################################################################# -# add - -my $a = Math::BigInt::Calc->_new("123"); -my $b = Math::BigInt::Calc->_new("321"); - -my ($x, $xs) = Math::BigFloat::_e_add($a,$b,'+','+'); -is (_str($x,$xs), '+444', 'add two positive numbers'); -is (_str($a,''), '444', 'a modified'); - -($x,$xs) = _add (123,321,'+','+'); -is (_str($x,$xs), '+444', 'add two positive numbers'); - -($x,$xs) = _add (123,321,'+','-'); -is (_str($x,$xs), '-198', 'add +x + -y'); -($x,$xs) = _add (123,321,'-','+'); -is (_str($x,$xs), '+198', 'add -x + +y'); - -($x,$xs) = _add (321,123,'-','+'); -is (_str($x,$xs), '-198', 'add -x + +y'); -($x,$xs) = _add (321,123,'+','-'); -is (_str($x,$xs), '+198', 'add +x + -y'); - -($x,$xs) = _add (10,1,'+','-'); -is (_str($x,$xs), '+9', 'add 10 + -1'); -($x,$xs) = _add (10,1,'-','+'); -is (_str($x,$xs), '-9', 'add -10 + +1'); -($x,$xs) = _add (1,10,'-','+'); -is (_str($x,$xs), '+9', 'add -1 + 10'); -($x,$xs) = _add (1,10,'+','-'); -is (_str($x,$xs), '-9', 'add 1 + -10'); - -############################################################################# -# sub - -$a = Math::BigInt::Calc->_new("123"); -$b = Math::BigInt::Calc->_new("321"); -($x, $xs) = Math::BigFloat::_e_sub($b,$a,'+','+'); -is (_str($x,$xs), '+198', 'sub two positive numbers'); -is (_str($b,''), '198', 'a modified'); - -($x,$xs) = _sub (123,321,'+','-'); -is (_str($x,$xs), '+444', 'sub +x + -y'); -($x,$xs) = _sub (123,321,'-','+'); -is (_str($x,$xs), '-444', 'sub -x + +y'); - -sub _add - { - my ($a,$b,$as,$bs) = @_; - - my $aa = Math::BigInt::Calc->_new($a); - my $bb = Math::BigInt::Calc->_new($b); - my ($x, $xs) = Math::BigFloat::_e_add($aa,$bb,$as,$bs); - is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), - 'param0 modified'); - ($x,$xs); - } - -sub _sub - { - my ($a,$b,$as,$bs) = @_; - - my $aa = Math::BigInt::Calc->_new($a); - my $bb = Math::BigInt::Calc->_new($b); - my ($x, $xs) = Math::BigFloat::_e_sub($aa,$bb,$as,$bs); - is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), - 'param0 modified'); - ($x,$xs); - } - -sub _str - { - my ($x,$s) = @_; - - $s . Math::BigInt::Calc->_str($x); - } diff --git a/dist/Math-BigInt/t/alias.inc b/dist/Math-BigInt/t/alias.inc deleted file mode 100644 index 746a20c99e..0000000000 --- a/dist/Math-BigInt/t/alias.inc +++ /dev/null @@ -1,12 +0,0 @@ - -# alias subroutine testing, included by sub_ali.t and mbi_ali.t - -my $x = $CL->new(123); - -is ($x->is_pos(), 1, '123 is positive'); -is ($x->is_neg(), 0, '123 is not negative'); -is ($x->as_int(), 123, '123 is 123 as int'); -is (ref($x->as_int()), 'Math::BigInt', "as_int(123) is of class Math::BigInt"); -$x->bneg(); -is ($x->is_pos(), 0, '-123 is not positive'); -is ($x->is_neg(), 1, '-123 is negative'); diff --git a/dist/Math-BigInt/t/bare_mbf.t b/dist/Math-BigInt/t/bare_mbf.t deleted file mode 100644 index 69dcc80f92..0000000000 --- a/dist/Math-BigInt/t/bare_mbf.t +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 2340; - -BEGIN { unshift @INC, 't'; } - -use Math::BigFloat lib => 'BareCalc'; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigFloat"; -$CL = "Math::BigInt::BareCalc"; - -require 't/bigfltpm.inc'; # all tests here for sharing diff --git a/dist/Math-BigInt/t/bare_mbi.t b/dist/Math-BigInt/t/bare_mbi.t deleted file mode 100644 index 8aedf4350d..0000000000 --- a/dist/Math-BigInt/t/bare_mbi.t +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 3649; - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt lib => 'BareCalc'; - -print "# ",Math::BigInt->config()->{lib},"\n"; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigInt"; -$CL = "Math::BigInt::BareCalc"; - -my $version = '1.84'; # for $VERSION tests, match current release (by hand!) - -require 't/bigintpm.inc'; # perform same tests as bigintpm diff --git a/dist/Math-BigInt/t/bare_mif.t b/dist/Math-BigInt/t/bare_mif.t deleted file mode 100644 index 2e533241ea..0000000000 --- a/dist/Math-BigInt/t/bare_mif.t +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/perl -w - -# test rounding, accuracy, precision and fallback, round_mode and mixing -# of classes under BareCalc - -use strict; -use Test::More tests => 684 - + 1; # our own tests - -BEGIN { unshift @INC, 't'; } - -print "# ",Math::BigInt->config()->{lib},"\n"; - -use Math::BigInt lib => 'BareCalc'; -use Math::BigFloat lib => 'BareCalc'; - -use vars qw/$mbi $mbf/; - -$mbi = 'Math::BigInt'; -$mbf = 'Math::BigFloat'; - -is (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); - -require 't/mbimbf.inc'; diff --git a/dist/Math-BigInt/t/big_pi_e.t b/dist/Math-BigInt/t/big_pi_e.t deleted file mode 100644 index 9cc4751aa4..0000000000 --- a/dist/Math-BigInt/t/big_pi_e.t +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/perl -w - -# Test bpi() and bexp() - -use strict; -use Test::More tests => 8; - -use Math::BigFloat; - -############################################################################# - -my $pi = Math::BigFloat::bpi(); - -ok (!exists $pi->{_a}, 'A not set'); -ok (!exists $pi->{_p}, 'P not set'); - -$pi = Math::BigFloat->bpi(); - -ok (!exists $pi->{_a}, 'A not set'); -ok (!exists $pi->{_p}, 'P not set'); - -$pi = Math::BigFloat->bpi(10); - -is ($pi->{_a}, 10, 'A set'); -is ($pi->{_p}, undef, 'P not set'); - -############################################################################# -my $e = Math::BigFloat->new(1)->bexp(); - -ok (!exists $e->{_a}, 'A not set'); -ok (!exists $e->{_p}, 'P not set'); - - diff --git a/dist/Math-BigInt/t/bigfltpm.inc b/dist/Math-BigInt/t/bigfltpm.inc deleted file mode 100644 index 3eb2e21e0d..0000000000 --- a/dist/Math-BigInt/t/bigfltpm.inc +++ /dev/null @@ -1,1836 +0,0 @@ -#include this file into another test for subclass testing... - -is ($class->config()->{lib},$CL); - -use strict; - -my $z; - -while () - { - $_ =~ s/[\n\r]//g; # remove newlines - $_ =~ s/#.*$//; # remove comments - $_ =~ s/\s+$//; # trailing spaces - next if /^$/; # skip empty lines & comments - if (s/^&//) - { - $f = $_; - } - elsif (/^\$/) - { - $setup = $_; $setup =~ s/\$/\$${class}::/g; # round_mode, div_scale - #print "\$setup== $setup\n"; - } - else - { - if (m|^(.*?):(/.+)$|) - { - $ans = $2; - @args = split(/:/,$1,99); - } - else - { - @args = split(/:/,$_,99); $ans = pop(@args); - } - $try = "\$x = $class->new(\"$args[0]\");"; - if ($f eq "fnorm") - { - $try .= "\$x;"; - } elsif ($f eq "finf") { - $try .= "\$x->finf('$args[1]');"; - } elsif ($f eq "is_inf") { - $try .= "\$x->is_inf('$args[1]');"; - } elsif ($f eq "fone") { - $try .= "\$x->bone('$args[1]');"; - } elsif ($f eq "fstr") { - $try .= "\$x->accuracy($args[1]); \$x->precision($args[2]);"; - $try .= '$x->fstr();'; - } elsif ($f eq "parts") { - # ->bstr() to see if an object is returned - $try .= '($a,$b) = $x->parts(); $a = $a->bstr(); $b = $b->bstr();'; - $try .= '"$a $b";'; - } elsif ($f eq "exponent") { - # ->bstr() to see if an object is returned - $try .= '$x->exponent()->bstr();'; - } elsif ($f eq "mantissa") { - # ->bstr() to see if an object is returned - $try .= '$x->mantissa()->bstr();'; - } elsif ($f =~ /^(numify|length|as_number|as_hex|as_bin)$/) { - $try .= "\$x->$f();"; - # some unary ops (test the fxxx form, since that is done by AUTOLOAD) - } elsif ($f =~ /^f(nan|sstr|neg|floor|ceil|int|abs)$/) { - $try .= "\$x->f$1();"; - # some is_xxx test function - } elsif ($f =~ /^is_(zero|one|negative|positive|odd|even|nan|int)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "bpi") { - $try .= '$class->bpi($x);'; - } elsif ($f eq "finc") { - $try .= '++$x;'; - } elsif ($f eq "fdec") { - $try .= '--$x;'; - }elsif ($f eq "fround") { - $try .= "$setup; \$x->fround($args[1]);"; - } elsif ($f eq "ffround") { - $try .= "$setup; \$x->ffround($args[1]);"; - } elsif ($f eq "fsqrt") { - $try .= "$setup; \$x->fsqrt();"; - } elsif ($f eq "ffac") { - $try .= "$setup; \$x->ffac();"; - } elsif ($f eq "flog") { - if (defined $args[1] && $args[1] ne '') - { - $try .= "\$y = $class->new($args[1]);"; - $try .= "$setup; \$x->flog(\$y);"; - } - else - { - $try .= "$setup; \$x->flog();"; - } - } - else - { - $try .= "\$y = $class->new(\"$args[1]\");"; - - if ($f eq "bgcd") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new(\"$args[2]\"); "; - } - $try .= "$class\::bgcd(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } - elsif ($f eq "blcm") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new(\"$args[2]\"); "; - } - $try .= "$class\::blcm(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } elsif ($f eq "fcmp") { - $try .= '$x->fcmp($y);'; - } elsif ($f eq "facmp") { - $try .= '$x->facmp($y);'; - } elsif ($f eq "fpow") { - $try .= '$x ** $y;'; - } elsif ($f eq "bnok") { - $try .= '$x->bnok($y);'; - } elsif ($f eq "bcos") { - $try .= '$x->bcos($y);'; - } elsif ($f eq "bsin") { - $try .= '$x->bsin($y);'; - } elsif ($f eq "batan") { - $try .= '$x->batan($y);'; - } elsif ($f eq "froot") { - $try .= "$setup; \$x->froot(\$y);"; - } elsif ($f eq "fadd") { - $try .= '$x + $y;'; - } elsif ($f eq "fsub") { - $try .= '$x - $y;'; - } elsif ($f eq "fmul") { - $try .= '$x * $y;'; - } elsif ($f eq "fdiv") { - $try .= "$setup; \$x / \$y;"; - } elsif ($f eq "fdiv-list") { - $try .= "$setup; join(',',\$x->fdiv(\$y));"; - } elsif ($f eq "frsft") { - $try .= '$x >> $y;'; - } elsif ($f eq "flsft") { - $try .= '$x << $y;'; - } elsif ($f eq "fmod") { - $try .= '$x % $y;'; - } else { - # Functions with three arguments - $try .= "\$z = $class->new(\"$args[2]\");"; - - if( $f eq "bmodpow") { - $try .= '$x->bmodpow($y,$z);'; - } elsif ($f eq "bmuladd"){ - $try .= '$x->bmuladd($y,$z);'; - } elsif ($f eq "batan2"){ - $try .= '$x->batan2($y,$z);'; - } else { warn "Unknown op '$f'"; } - } - } - # print "# Trying: '$try'\n"; - $ans1 = eval $try; - print "# Error: $@\n" if $@; - if ($ans =~ m|^/(.*)$|) - { - my $pat = $1; - if ($ans1 =~ /$pat/) - { - is (1,1); - } - else - { - print "# '$try' expected: /$pat/ got: '$ans1'\n" if !is (1,0); - } - } - else - { - if ($ans eq "") - { - is ($ans1, undef); - } - else - { - print "# Tried: '$try'\n" if !is ($ans1, $ans); - if (ref($ans1) eq "$class") - { - # float numbers are normalized (for now), so mantissa shouldn't have - # trailing zeros - #print $ans1->_trailing_zeros(),"\n"; - print "# Has trailing zeros after '$try'\n" - if !is ($CL->_zeros( $ans1->{_m}), 0); - } - } - } # end pattern or string - } - } # end while - -# check whether $class->new( Math::BigInt->new()) destroys it -# ($y == 12 in this case) -$x = Math::BigInt->new(1200); $y = $class->new($x); -is ($y,1200); is ($x,1200); - -############################################################################### -# Really huge, big, ultra-mega-biggy-monster exponents -# Technically, the exponents should not be limited (they are BigInts), but -# practically there are a few places were they are limited to a Perl scalar. -# This is sometimes for speed, sometimes because otherwise the number wouldn't -# fit into your memory (just think of 1e123456789012345678901234567890 + 1!) -# anyway. We don't test everything here, but let's make sure it just basically -# works. - -my $monster = '1e1234567890123456789012345678901234567890'; - -# new and exponent -is ($class->new($monster)->bsstr(), - '1e+1234567890123456789012345678901234567890'); -is ($class->new($monster)->exponent(), - '1234567890123456789012345678901234567890'); -# cmp -is ($class->new($monster) > 0,1); - -# sub/mul -is ($class->new($monster)->bsub( $monster),0); -is ($class->new($monster)->bmul(2)->bsstr(), - '2e+1234567890123456789012345678901234567890'); - -# mantissa -$monster = '1234567890123456789012345678901234567890e2'; -is ($class->new($monster)->mantissa(), - '123456789012345678901234567890123456789'); - -############################################################################### -# zero,inf,one,nan - -$x = $class->new(2); $x->fzero(); is ($x->{_a}, undef); is ($x->{_p}, undef); -$x = $class->new(2); $x->finf(); is ($x->{_a}, undef); is ($x->{_p}, undef); -$x = $class->new(2); $x->fone(); is ($x->{_a}, undef); is ($x->{_p}, undef); -$x = $class->new(2); $x->fnan(); is ($x->{_a}, undef); is ($x->{_p}, undef); - -############################################################################### -# bone/binf etc as plain calls (Lite failed them) - -is ($class->fzero(),0); -is ($class->fone(),1); -is ($class->fone('+'),1); -is ($class->fone('-'),-1); -is ($class->fnan(),'NaN'); -is ($class->finf(),'inf'); -is ($class->finf('+'),'inf'); -is ($class->finf('-'),'-inf'); -is ($class->finf('-inf'),'-inf'); - -$class->accuracy(undef); $class->precision(undef); # reset - -############################################################################### -# bug in bsstr()/numify() showed up in after-rounding in bdiv() - -$x = $class->new('0.008'); $y = $class->new(2); -$x->bdiv(3,$y); -is ($x,'0.0027'); - -############################################################################### -# Verify that numify() returns a normalized value, and underflows and -# overflows when given "extreme" values. - -like($class->new("12345e67")->numify(), qr/^1\.2345e\+?0*71$/); -like($class->new("1e-9999")->numify(), qr/^\+?0$/); # underflow -unlike($class->new("1e9999")->numify(), qr/^1(\.0*)?e\+?9+$/); # overflow - -############################################################################### -# fsqrt() with set global A/P or A/P enabled on $x, also a test whether fsqrt() -# correctly modifies $x - - -$x = $class->new(12); $class->precision(-2); $x->fsqrt(); is ($x,'3.46'); - -$class->precision(undef); -$x = $class->new(12); $class->precision(0); $x->fsqrt(); is ($x,'3'); - -$class->precision(-3); $x = $class->new(12); $x->fsqrt(); is ($x,'3.464'); - -{ - no strict 'refs'; - # A and P set => NaN - ${${class}.'::accuracy'} = 4; $x = $class->new(12); - $x->fsqrt(3); is ($x,'NaN'); - # supplied arg overrides set global - $class->precision(undef); $x = $class->new(12); $x->fsqrt(3); is ($x,'3.46'); - $class->accuracy(undef); $class->precision(undef); # reset for further tests -} - -############################################################################# -# can we call objectify (broken until v1.52) - -{ - no strict; - $try = - '@args' . " = $class" . "::objectify(2,$class,4,5);".'join(" ",@args);'; - $ans = eval $try; - is ($ans,"$class 4 5"); -} - -############################################################################# -# is_one('-') (broken until v1.64) - -is ($class->new(-1)->is_one(),0); -is ($class->new(-1)->is_one('-'),1); - -############################################################################# -# bug 1/0.5 leaving 2e-0 instead of 2e0 - -is ($class->new(1)->fdiv('0.5')->bsstr(),'2e+0'); - -############################################################################### -# [perl #30609] bug with $x -= $x not being 0, but 2*$x - -$x = $class->new(3); $x -= $x; is ($x, 0); -$x = $class->new(-3); $x -= $x; is ($x, 0); -$x = $class->new(3); $x += $x; is ($x, 6); -$x = $class->new(-3); $x += $x; is ($x, -6); - -$x = $class->new('NaN'); $x -= $x; is ($x->is_nan(), 1); -$x = $class->new('inf'); $x -= $x; is ($x->is_nan(), 1); -$x = $class->new('-inf'); $x -= $x; is ($x->is_nan(), 1); - -$x = $class->new('NaN'); $x += $x; is ($x->is_nan(), 1); -$x = $class->new('inf'); $x += $x; is ($x->is_inf(), 1); -$x = $class->new('-inf'); $x += $x; is ($x->is_inf('-'), 1); - -$x = $class->new('3.14'); $x -= $x; is ($x, 0); -$x = $class->new('-3.14'); $x -= $x; is ($x, 0); -$x = $class->new('3.14'); $x += $x; is ($x, '6.28'); -$x = $class->new('-3.14'); $x += $x; is ($x, '-6.28'); - -$x = $class->new('3.14'); $x *= $x; is ($x, '9.8596'); -$x = $class->new('-3.14'); $x *= $x; is ($x, '9.8596'); -$x = $class->new('3.14'); $x /= $x; is ($x, '1'); -$x = $class->new('-3.14'); $x /= $x; is ($x, '1'); -$x = $class->new('3.14'); $x %= $x; is ($x, '0'); -$x = $class->new('-3.14'); $x %= $x; is ($x, '0'); - -############################################################################### -# the following two were reported by "kenny" via hotmail.com: - -#perl -MMath::BigFloat -wle 'print Math::BigFloat->new(0)->bpow(".1")' -#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. - -$x = $class->new(0); $y = $class->new('0.1'); -is ($x ** $y, 0, 'no warnings and zero result'); - -#perl -MMath::BigFloat -lwe 'print Math::BigFloat->new(".222222222222222222222222222222222222222222")->bceil()' -#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. - -$x = $class->new(".222222222222222222222222222222222222222222"); -is ($x->bceil(), 1, 'no warnings and one as result'); - -############################################################################### -# test **=, <<=, >>= - -# ((2^148)-1)/17 -$x = $class->new(2); $x **= 148; $x++; $x->bdiv(17, 60)->bfloor(); $x->accuracy(undef); -is ($x,"20988936657440586486151264256610222593863921"); -is ($x->length(),length "20988936657440586486151264256610222593863921"); - -$x = $class->new('2'); -my $y = $class->new('18'); -is ($x <<= $y, 2 << 18); -is ($x, 2 << 18); -is ($x >>= $y, 2); -is ($x, 2); - -$x = $class->new('2'); -$y = $class->new('18.2'); -$x <<= $y; # 2 * (2 ** 18.2); - -is ($x->copy()->bfround(-9), '602248.763144685'); -is ($x >>= $y, 2); # 2 * (2 ** 18.2) / (2 ** 18.2) => 2 -is ($x, 2); - -1; # all done - -__DATA__ -&bgcd -inf:12:NaN --inf:12:NaN -12:inf:NaN -12:-inf:NaN -inf:inf:NaN -inf:-inf:NaN --inf:-inf:NaN -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:0 -+0:+1:1 -+1:+0:1 -+1:+1:1 -+2:+3:1 -+3:+2:1 --3:+2:1 --3:-2:1 --144:-60:12 -144:-60:12 -144:60:12 -100:625:25 -4096:81:1 -1034:804:2 -27:90:56:1 -27:90:54:9 -&blcm -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:NaN -+1:+0:0 -+0:+1:0 -+27:+90:270 -+1034:+804:415668 -$div_scale = 40; -&bcos -1.2:10:0.3623577545 -2.4:12:-0.737393715541 -0:10:1 -0:20:1 -1:10:0.5403023059 -1:12:0.540302305868 -&bsin -1:10:0.8414709848 -0:10:0 -0:20:0 -2.1:12:0.863209366649 -1.2:13:0.9320390859672 -0.2:13:0.1986693307951 -3.2:12:-0.0583741434276 -&batan -NaN:10:NaN -inf:14:1.5707963267949 --inf:14:-1.5707963267949 -0.2:13:0.1973955598499 -0.2:14:0.19739555984988 -0:10:0 -1:14:0.78539816339744 --1:14:-0.78539816339744 -# test an argument X > 1 -2:14:1.1071487177941 -&batan2 -NaN:1:10:NaN -NaN:NaN:10:NaN -1:NaN:10:NaN -inf:1:14:1.5707963267949 --inf:1:14:-1.5707963267949 -0:-inf:14:3.1415926535898 --1:-inf:14:-3.1415926535898 -1:-inf:14:3.1415926535898 -0:inf:14:0 -inf:-inf:14:2.3561944901923 --inf:-inf:14:-2.3561944901923 -inf:+inf:14:0.7853981633974 --inf:+inf:14:-0.7853981633974 -1:5:13:0.1973955598499 -1:5:14:0.19739555984988 -0:0:10:0 -0:1:14:0 -0:2:14:0 -1:0:14:1.5707963267949 -5:0:14:1.5707963267949 --1:0:11:-1.5707963268 --2:0:77:-1.5707963267948966192313216916397514420985846996875529104874722961539082031431 -2:0:77:1.5707963267948966192313216916397514420985846996875529104874722961539082031431 --1:5:14:-0.19739555984988 -1:5:14:0.19739555984988 --1:8:14:-0.12435499454676 -1:8:14:0.12435499454676 --1:1:14:-0.78539816339744 -# test an argument X > 1 and one X < 1 -1:2:24:0.463647609000806116214256 -2:1:14:1.1071487177941 --2:1:14:-1.1071487177941 -&bpi -150:3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940813 -77:3.1415926535897932384626433832795028841971693993751058209749445923078164062862 -+0:3.141592653589793238462643383279502884197 -11:3.1415926536 -&bnok -+inf:10:inf -NaN:NaN:NaN -NaN:1:NaN -1:NaN:NaN -1:1:1 -# k > n -1:2:0 -2:3:0 -# k < 0 -1:-2:0 -# 7 over 3 = 35 -7:3:35 -7:6:7 -100:90:17310309456440 -100:95:75287520 -2:0:1 -7:0:1 -2:1:2 -&flog -0::NaN --1::NaN --2::NaN -# base > 0, base != 1 -2:-1:NaN -2:0:NaN -2:1:NaN -# log(1) is always 1, regardless of $base -1::0 -1:1:0 -1:2:0 -2::0.6931471805599453094172321214581765680755 -2.718281828::0.9999999998311266953289851340574956564911 -$div_scale = 20; -2.718281828::0.99999999983112669533 -$div_scale = 15; -123::4.81218435537242 -10::2.30258509299405 -1000::6.90775527898214 -100::4.60517018598809 -2::0.693147180559945 -3.1415::1.14470039286086 -12345::9.42100640177928 -0.001::-6.90775527898214 -# bug until v1.71: -10:10:1 -100:100:1 -# reset for further tests -$div_scale = 40; -1::0 -&frsft -NaNfrsft:2:NaN -0:2:0 -1:1:0.5 -2:1:1 -4:1:2 -123:1:61.5 -32:3:4 -&flsft -NaNflsft:0:NaN -2:1:4 -4:3:32 -5:3:40 -1:2:4 -0:5:0 -&fnorm -1:1 --0:0 -fnormNaN:NaN -+inf:inf --inf:-inf -123:123 --123.4567:-123.4567 -# invalid inputs -1__2:NaN -1E1__2:NaN -11__2E2:NaN -.2E-3.:NaN -1e3e4:NaN -# strange, but valid -.2E2:20 -1.E3:1000 -# some inputs that result in zero -0e0:0 -+0e0:0 -+0e+0:0 --0e+0:0 -0e-0:0 --0e-0:0 -+0e-0:0 -000:0 -00e2:0 -00e02:0 -000e002:0 -000e1230:0 -00e-3:0 -00e+3:0 -00e-03:0 -00e+03:0 --000:0 --00e2:0 --00e02:0 --000e002:0 --000e1230:0 --00e-3:0 --00e+3:0 --00e-03:0 --00e+03:0 -&as_number -0:0 -1:1 -1.2:1 -2.345:2 --2:-2 --123.456:-123 --200:-200 --inf:-inf -inf:inf -NaN:NaN -71243225429896467497217836789578596379:71243225429896467497217836789578596379 -# test for bug in brsft() not handling cases that return 0 -0.000641:0 -0.0006412:0 -0.00064123:0 -0.000641234:0 -0.0006412345:0 -0.00064123456:0 -0.000641234567:0 -0.0006412345678:0 -0.00064123456789:0 -0.1:0 -0.01:0 -0.001:0 -0.0001:0 -0.00001:0 -0.000001:0 -0.0000001:0 -0.00000001:0 -0.000000001:0 -0.0000000001:0 -0.00000000001:0 -0.12345:0 -0.123456:0 -0.1234567:0 -0.12345678:0 -0.123456789:0 -&finf -1:+:inf -2:-:-inf -3:abc:inf -&as_hex -+inf:inf --inf:-inf -hexNaN:NaN -0:0x0 -5:0x5 --5:-0x5 -&as_bin -+inf:inf --inf:-inf -hexNaN:NaN -0:0b0 -5:0b101 --5:-0b101 -&numify -# uses bsstr() so 5 => 5e+0 to be compatible w/ Perls output -0:0 -+1:1 -1234:1234 --5:-5 -100:100 --100:-100 -&fnan -abc:NaN -2:NaN --2:NaN -0:NaN -&fone -2:+:1 --2:-:-1 --2:+:1 -2:-:-1 -0::1 --2::1 -abc::1 -2:abc:1 -&fsstr -+inf:inf --inf:-inf -abcfsstr:NaN --abcfsstr:NaN -1234.567:1234567e-3 -123:123e+0 --5:-5e+0 --100:-1e+2 -&fstr -+inf:::inf --inf:::-inf -abcfstr:::NaN -1234.567:9::1234.56700 -1234.567::-6:1234.567000 -12345:5::12345 -0.001234:6::0.00123400 -0.001234::-8:0.00123400 -0:4::0 -0::-4:0.0000 -&fnorm -inf:inf -+inf:inf --inf:-inf -+infinity:NaN -+-inf:NaN -abc:NaN - 1 a:NaN -1bcd2:NaN -11111b:NaN -+1z:NaN --1z:NaN -0e999:0 -0e-999:0 --0e999:0 --0e-999:0 -0:0 -+0:0 -+00:0 -+0_0_0:0 -000000_0000000_00000:0 --0:0 --0000:0 -+1:1 -+01:1 -+001:1 -+00000100000:100000 -123456789:123456789 --1:-1 --01:-1 --001:-1 --123456789:-123456789 --00000100000:-100000 -123.456a:NaN -123.456:123.456 -0.01:0.01 -.002:0.002 -+.2:0.2 --0.0003:-0.0003 --.0000000004:-0.0000000004 -123456E2:12345600 -123456E-2:1234.56 --123456E2:-12345600 --123456E-2:-1234.56 -1e1:10 -2e-11:0.00000000002 -# exercise _split - .02e-1:0.002 - 000001:1 - -00001:-1 - -1:-1 - 000.01:0.01 - -000.0023:-0.0023 - 1.1e1:11 --3e111:-3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 --4e-1111:-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004 -&fpow -NaN:1:NaN -1:NaN:NaN -NaN:-1:NaN --1:NaN:NaN -NaN:-21:NaN --21:NaN:NaN -NaN:21:NaN -21:NaN:NaN -0:0:1 -0:1:0 -0:9:0 -0:-2:inf -2:2:4 -1:2:1 -1:3:1 --1:2:1 --1:3:-1 -123.456:2:15241.383936 -2:-2:0.25 -2:-3:0.125 -128:-2:0.00006103515625 -abc:123.456:NaN -123.456:abc:NaN -+inf:123.45:inf --inf:123.45:-inf -+inf:-123.45:inf --inf:-123.45:-inf --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 --3:2:9 --3:3:-27 --3:4:81 --3:5:-243 -# 2 ** 0.5 == sqrt(2) -# 1.41..7 and not 1.4170 since fallback (bsqrt(9) is '3', not 3.0...0) -2:0.5:1.41421356237309504880168872420969807857 -#2:0.2:1.148698354997035006798626946777927589444 -#6:1.5:14.6969384566990685891837044482353483518 -$div_scale = 20; -#62.5:12.5:26447206647554886213592.3959144 -$div_scale = 40; -&fneg -fnegNaN:NaN -+inf:-inf --inf:inf -+0:0 -+1:-1 --1:1 -+123456789:-123456789 --123456789:123456789 -+123.456789:-123.456789 --123456.789:123456.789 -&fabs -fabsNaN:NaN -+inf:inf --inf:inf -+0:0 -+1:1 --1:1 -+123456789:123456789 --123456789:123456789 -+123.456789:123.456789 --123456.789:123456.789 -&fround -$round_mode = "trunc" -+inf:5:inf --inf:5:-inf -0:5:0 -NaNfround:5:NaN -+10123456789:5:10123000000 --10123456789:5:-10123000000 -+10123456789.123:5:10123000000 --10123456789.123:5:-10123000000 -+10123456789:9:10123456700 --10123456789:9:-10123456700 -+101234500:6:101234000 --101234500:6:-101234000 -$round_mode = "zero" -+20123456789:5:20123000000 --20123456789:5:-20123000000 -+20123456789.123:5:20123000000 --20123456789.123:5:-20123000000 -+20123456789:9:20123456800 --20123456789:9:-20123456800 -+201234500:6:201234000 --201234500:6:-201234000 -$round_mode = "+inf" -+30123456789:5:30123000000 --30123456789:5:-30123000000 -+30123456789.123:5:30123000000 --30123456789.123:5:-30123000000 -+30123456789:9:30123456800 --30123456789:9:-30123456800 -+301234500:6:301235000 --301234500:6:-301234000 -$round_mode = "-inf" -+40123456789:5:40123000000 --40123456789:5:-40123000000 -+40123456789.123:5:40123000000 --40123456789.123:5:-40123000000 -+40123456789:9:40123456800 --40123456789:9:-40123456800 -+401234500:6:401234000 --401234500:6:-401235000 -$round_mode = "odd" -+50123456789:5:50123000000 --50123456789:5:-50123000000 -+50123456789.123:5:50123000000 --50123456789.123:5:-50123000000 -+50123456789:9:50123456800 --50123456789:9:-50123456800 -+501234500:6:501235000 --501234500:6:-501235000 -$round_mode = "even" -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601234000 --601234500:6:-601234000 -+60123456789.0123:5:60123000000 --60123456789.0123:5:-60123000000 -$round_mode = "common" -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:6:60123500000 --60123456789:6:-60123500000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601235000 --601234500:6:-601235000 -+601234400:6:601234000 --601234400:6:-601234000 -+601234600:6:601235000 --601234600:6:-601235000 -+601234300:6:601234000 -+60123456789.0123:5:60123000000 --60123456789.0123:5:-60123000000 -&ffround -$round_mode = "trunc" -+inf:5:inf --inf:5:-inf -0:5:0 -NaNffround:5:NaN -+1.23:-1:1.2 -+1.234:-1:1.2 -+1.2345:-1:1.2 -+1.23:-2:1.23 -+1.234:-2:1.23 -+1.2345:-2:1.23 -+1.23:-3:1.230 -+1.234:-3:1.234 -+1.2345:-3:1.234 --1.23:-1:-1.2 -+1.27:-1:1.2 --1.27:-1:-1.2 -+1.25:-1:1.2 --1.25:-1:-1.2 -+1.35:-1:1.3 --1.35:-1:-1.3 --0.0061234567890:-1:0.0 --0.0061:-1:0.0 --0.00612:-1:0.0 --0.00612:-2:0.00 --0.006:-1:0.0 --0.006:-2:0.00 --0.0006:-2:0.00 --0.0006:-3:0.000 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:0 -0.41:0:0 -$round_mode = "zero" -+2.23:-1:/2.2(?:0{5}\d+)? --2.23:-1:/-2.2(?:0{5}\d+)? -+2.27:-1:/2.(?:3|29{5}\d+) --2.27:-1:/-2.(?:3|29{5}\d+) -+2.25:-1:/2.2(?:0{5}\d+)? --2.25:-1:/-2.2(?:0{5}\d+)? -+2.35:-1:/2.(?:3|29{5}\d+) --2.35:-1:/-2.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -$round_mode = "+inf" -+3.23:-1:/3.2(?:0{5}\d+)? --3.23:-1:/-3.2(?:0{5}\d+)? -+3.27:-1:/3.(?:3|29{5}\d+) --3.27:-1:/-3.(?:3|29{5}\d+) -+3.25:-1:/3.(?:3|29{5}\d+) --3.25:-1:/-3.2(?:0{5}\d+)? -+3.35:-1:/3.(?:4|39{5}\d+) --3.35:-1:/-3.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:1 -0.51:0:1 -0.41:0:0 -$round_mode = "-inf" -+4.23:-1:/4.2(?:0{5}\d+)? --4.23:-1:/-4.2(?:0{5}\d+)? -+4.27:-1:/4.(?:3|29{5}\d+) --4.27:-1:/-4.(?:3|29{5}\d+) -+4.25:-1:/4.2(?:0{5}\d+)? --4.25:-1:/-4.(?:3|29{5}\d+) -+4.35:-1:/4.(?:3|29{5}\d+) --4.35:-1:/-4.(?:4|39{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.007|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -$round_mode = "odd" -+5.23:-1:/5.2(?:0{5}\d+)? --5.23:-1:/-5.2(?:0{5}\d+)? -+5.27:-1:/5.(?:3|29{5}\d+) --5.27:-1:/-5.(?:3|29{5}\d+) -+5.25:-1:/5.(?:3|29{5}\d+) --5.25:-1:/-5.(?:3|29{5}\d+) -+5.35:-1:/5.(?:3|29{5}\d+) --5.35:-1:/-5.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.007|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:1 -0.51:0:1 -0.41:0:0 -$round_mode = "even" -+6.23:-1:/6.2(?:0{5}\d+)? --6.23:-1:/-6.2(?:0{5}\d+)? -+6.27:-1:/6.(?:3|29{5}\d+) --6.27:-1:/-6.(?:3|29{5}\d+) -+6.25:-1:/6.(?:2(?:0{5}\d+)?|29{5}\d+) --6.25:-1:/-6.(?:2(?:0{5}\d+)?|29{5}\d+) -+6.35:-1:/6.(?:4|39{5}\d+|29{8}\d+) --6.35:-1:/-6.(?:4|39{5}\d+|29{8}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -0.01234567:-3:0.012 -0.01234567:-4:0.0123 -0.01234567:-5:0.01235 -0.01234567:-6:0.012346 -0.01234567:-7:0.0123457 -0.01234567:-8:0.01234567 -0.01234567:-9:0.012345670 -0.01234567:-12:0.012345670000 -&fcmp -fcmpNaN:fcmpNaN: -fcmpNaN:+0: -+0:fcmpNaN: -+0:+0:0 --1:+0:-1 -+0:-1:1 -+1:+0:1 -+0:+1:-1 --1:+1:-1 -+1:-1:1 --1:-1:0 -+1:+1:0 --1.1:0:-1 -+0:-1.1:1 -+1.1:+0:1 -+0:+1.1:-1 -+123:+123:0 -+123:+12:1 -+12:+123:-1 --123:-123:0 --123:-12:-1 --12:-123:1 -+123:+124:-1 -+124:+123:1 --123:-124:1 --124:-123:-1 -0:0.01:-1 -0:0.0001:-1 -0:-0.0001:1 -0:-0.1:1 -0.1:0:1 -0.00001:0:1 --0.0001:0:-1 --0.1:0:-1 -0:0.0001234:-1 -0:-0.0001234:1 -0.0001234:0:1 --0.0001234:0:-1 -0.0001:0.0005:-1 -0.0005:0.0001:1 -0.005:0.0001:1 -0.001:0.0005:1 -0.000001:0.0005:-1 -0.00000123:0.0005:-1 -0.00512:0.0001:1 -0.005:0.000112:1 -0.00123:0.0005:1 -1.5:2:-1 -2:1.5:1 -1.54321:234:-1 -234:1.54321:1 -1e1234567890987654321:1e1234567890987654320:1 -1e-1234567890987654321:1e-1234567890987654320:-1 -# infinity --inf:5432112345:-1 -+inf:5432112345:1 --inf:-5432112345:-1 -+inf:-5432112345:1 --inf:54321.12345:-1 -+inf:54321.12345:1 --inf:-54321.12345:-1 -+inf:-54321.12345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:1 --inf:+inf:-1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&facmp -fcmpNaN:fcmpNaN: -fcmpNaN:+0: -+0:fcmpNaN: -+0:+0:0 --1:+0:1 -+0:-1:-1 -+1:+0:1 -+0:+1:-1 --1:+1:0 -+1:-1:0 --1:-1:0 -+1:+1:0 --1.1:0:1 -+0:-1.1:-1 -+1.1:+0:1 -+0:+1.1:-1 -+123:+123:0 -+123:+12:1 -+12:+123:-1 --123:-123:0 --123:-12:1 --12:-123:-1 -+123:+124:-1 -+124:+123:1 --123:-124:-1 --124:-123:1 -0:0.01:-1 -0:0.0001:-1 -0:-0.0001:-1 -0:-0.1:-1 -0.1:0:1 -0.00001:0:1 --0.0001:0:1 --0.1:0:1 -0:0.0001234:-1 -0:-0.0001234:-1 -0.0001234:0:1 --0.0001234:0:1 -0.0001:0.0005:-1 -0.0005:0.0001:1 -0.005:0.0001:1 -0.001:0.0005:1 -0.000001:0.0005:-1 -0.00000123:0.0005:-1 -0.00512:0.0001:1 -0.005:0.000112:1 -0.00123:0.0005:1 -1.5:2:-1 -2:1.5:1 -1.54321:234:-1 -234:1.54321:1 -# infinity --inf:5432112345:1 -+inf:5432112345:1 --inf:-5432112345:1 -+inf:-5432112345:1 --inf:54321.12345:1 -+inf:54321.12345:1 --inf:-54321.12345:1 -+inf:-54321.12345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:0 --inf:+inf:0 -5:inf:-1 --1:inf:-1 -5:-inf:-1 --1:-inf:-1 -# return undef -+inf:facmpNaN: -facmpNaN:inf: --inf:facmpNaN: -facmpNaN:-inf: -&fdec -fdecNaN:NaN -+inf:inf --inf:-inf -+0:-1 -+1:0 --1:-2 -1.23:0.23 --1.23:-2.23 -100:99 -101:100 --100:-101 --99:-100 --98:-99 -99:98 -&finc -fincNaN:NaN -+inf:inf --inf:-inf -+0:1 -+1:2 --1:0 -1.23:2.23 --1.23:-0.23 -100:101 --100:-99 --99:-98 --101:-100 -99:100 -&fadd -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:NaN --inf:+inf:NaN -+inf:+inf:inf --inf:-inf:-inf -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -+0:+0:0 -+1:+0:1 -+0:+1:1 -+1:+1:2 --1:+0:-1 -+0:-1:-1 --1:-1:-2 --1:+1:0 -+1:-1:0 -+9:+1:10 -+99:+1:100 -+999:+1:1000 -+9999:+1:10000 -+99999:+1:100000 -+999999:+1:1000000 -+9999999:+1:10000000 -+99999999:+1:100000000 -+999999999:+1:1000000000 -+9999999999:+1:10000000000 -+99999999999:+1:100000000000 -+10:-1:9 -+100:-1:99 -+1000:-1:999 -+10000:-1:9999 -+100000:-1:99999 -+1000000:-1:999999 -+10000000:-1:9999999 -+100000000:-1:99999999 -+1000000000:-1:999999999 -+10000000000:-1:9999999999 -+123456789:+987654321:1111111110 --123456789:+987654321:864197532 --123456789:-987654321:-1111111110 -+123456789:-987654321:-864197532 -0.001234:0.0001234:0.0013574 -&fsub -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:inf --inf:+inf:-inf -+inf:+inf:NaN --inf:-inf:NaN -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -+0:+0:0 -+1:+0:1 -+0:+1:-1 -+1:+1:0 --1:+0:-1 -+0:-1:1 --1:-1:0 --1:+1:-2 -+1:-1:2 -+9:+1:8 -+99:+1:98 -+999:+1:998 -+9999:+1:9998 -+99999:+1:99998 -+999999:+1:999998 -+9999999:+1:9999998 -+99999999:+1:99999998 -+999999999:+1:999999998 -+9999999999:+1:9999999998 -+99999999999:+1:99999999998 -+10:-1:11 -+100:-1:101 -+1000:-1:1001 -+10000:-1:10001 -+100000:-1:100001 -+1000000:-1:1000001 -+10000000:-1:10000001 -+100000000:-1:100000001 -+1000000000:-1:1000000001 -+10000000000:-1:10000000001 -+123456789:+987654321:-864197532 --123456789:+987654321:-1111111110 --123456789:-987654321:864197532 -+123456789:-987654321:1111111110 -&bmuladd -abc:abc:0:NaN -abc:+0:0:NaN -+0:abc:0:NaN -+0:0:abc:NaN -NaNmul:+inf:0:NaN -NaNmul:-inf:0:NaN --inf:NaNmul:0:NaN -+inf:NaNmul:0:NaN -+inf:+inf:0:inf -+inf:-inf:0:-inf --inf:+inf:0:-inf --inf:-inf:0:inf -+0:+0:0:0 -+0:+1:0:0 -+1:+0:0:0 -+0:-1:0:0 --1:+0:0:0 -123456789123456789:0:0:0 -0:123456789123456789:0:0 --1:-1:0:1 --1:-1:0:1 --1:+1:0:-1 -+1:-1:0:-1 -+1:+1:0:1 -+2:+3:0:6 --2:+3:0:-6 -+2:-3:0:-6 --2:-3:0:6 -111:111:0:12321 -10101:10101:0:102030201 -1001001:1001001:0:1002003002001 -100010001:100010001:0:10002000300020001 -10000100001:10000100001:0:100002000030000200001 -11111111111:9:0:99999999999 -22222222222:9:0:199999999998 -33333333333:9:0:299999999997 -44444444444:9:0:399999999996 -55555555555:9:0:499999999995 -66666666666:9:0:599999999994 -77777777777:9:0:699999999993 -88888888888:9:0:799999999992 -99999999999:9:0:899999999991 -11111111111:9:1:100000000000 -22222222222:9:1:199999999999 -33333333333:9:1:299999999998 -44444444444:9:1:399999999997 -55555555555:9:1:499999999996 -66666666666:9:1:599999999995 -77777777777:9:1:699999999994 -88888888888:9:1:799999999993 -99999999999:9:1:899999999992 --3:-4:-5:7 -3:-4:-5:-17 --3:4:-5:-17 -3:4:-5:7 --3:4:5:-7 -3:-4:5:-7 -9999999999999999999:10000000000000000000:1234567890:99999999999999999990000000001234567890 -3.2:5.7:8.9:27.14 --3.2:5.197:6.05:-10.5804 -&bmodpow -3:4:8:1 -3:4:7:4 -3:4:7:4 -77777:777:123456789:99995084 -3.2:6.2:5.2:2.970579856718063040273642739529400818 -&fmul -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:NaNmul:NaN -+inf:NaNmul:NaN -NaNmul:+inf:NaN -NaNmul:-inf:NaN -+inf:+inf:inf -+inf:-inf:-inf -+inf:-inf:-inf -+inf:+inf:inf -+inf:123.34:inf -+inf:-123.34:-inf --inf:123.34:-inf --inf:-123.34:inf -123.34:+inf:inf --123.34:+inf:-inf -123.34:-inf:-inf --123.34:-inf:inf -+0:+0:0 -+0:+1:0 -+1:+0:0 -+0:-1:0 --1:+0:0 -+123456789123456789:+0:0 -+0:+123456789123456789:0 --1:-1:1 --1:+1:-1 -+1:-1:-1 -+1:+1:1 -+2:+3:6 --2:+3:-6 -+2:-3:-6 --2:-3:6 -+111:+111:12321 -+10101:+10101:102030201 -+1001001:+1001001:1002003002001 -+100010001:+100010001:10002000300020001 -+10000100001:+10000100001:100002000030000200001 -+11111111111:+9:99999999999 -+22222222222:+9:199999999998 -+33333333333:+9:299999999997 -+44444444444:+9:399999999996 -+55555555555:+9:499999999995 -+66666666666:+9:599999999994 -+77777777777:+9:699999999993 -+88888888888:+9:799999999992 -+99999999999:+9:899999999991 -6:120:720 -10:10000:100000 -&fdiv-list -0:0:NaN,0 -0:1:0,0 -9:4:2,1 -9:5:1,4 -# bug in v1.74 with bdiv in list context, when $y is 1 or -1 -2.1:-1:-2.1,0 -2.1:1:2.1,0 --2.1:-1:2.1,0 --2.1:1:-2.1,0 -&fdiv -$div_scale = 40; $round_mode = 'even' -abc:abc:NaN -abc:+1:abc:NaN -+1:abc:NaN --1:abc:NaN -0:abc:NaN -+0:+0:NaN -+0:+1:0 -+1:+0:inf -+3214:+0:inf -+0:-1:0 --1:+0:-inf --3214:+0:-inf -+1:+1:1 --1:-1:1 -+1:-1:-1 --1:+1:-1 -+1:+2:0.5 -+2:+1:2 -123:+inf:0 -123:-inf:0 -+10:+5:2 -+100:+4:25 -+1000:+8:125 -+10000:+16:625 -+10000:-16:-625 -+999999999999:+9:111111111111 -+999999999999:+99:10101010101 -+999999999999:+999:1001001001 -+999999999999:+9999:100010001 -+999999999999999:+99999:10000100001 -+1000000000:+9:111111111.1111111111111111111111111111111 -+2000000000:+9:222222222.2222222222222222222222222222222 -+3000000000:+9:333333333.3333333333333333333333333333333 -+4000000000:+9:444444444.4444444444444444444444444444444 -+5000000000:+9:555555555.5555555555555555555555555555556 -+6000000000:+9:666666666.6666666666666666666666666666667 -+7000000000:+9:777777777.7777777777777777777777777777778 -+8000000000:+9:888888888.8888888888888888888888888888889 -+9000000000:+9:1000000000 -+35500000:+113:314159.2920353982300884955752212389380531 -+71000000:+226:314159.2920353982300884955752212389380531 -+106500000:+339:314159.2920353982300884955752212389380531 -+1000000000:+3:333333333.3333333333333333333333333333333 -2:25.024996000799840031993601279744051189762:0.07992009269196593320152084692285869265447 -123456:1:123456 -$div_scale = 20 -+1000000000:+9:111111111.11111111111 -+2000000000:+9:222222222.22222222222 -+3000000000:+9:333333333.33333333333 -+4000000000:+9:444444444.44444444444 -+5000000000:+9:555555555.55555555556 -+6000000000:+9:666666666.66666666667 -+7000000000:+9:777777777.77777777778 -+8000000000:+9:888888888.88888888889 -+9000000000:+9:1000000000 -1:10:0.1 -1:100:0.01 -1:1000:0.001 -1:10000:0.0001 -1:504:0.001984126984126984127 -2:1.987654321:1.0062111801179738436 -123456789.123456789123456789123456789:1:123456789.12345678912 -# the next two cases are the "old" behaviour, but are now (>v0.01) different -#+35500000:+113:314159.292035398230088 -#+71000000:+226:314159.292035398230088 -+35500000:+113:314159.29203539823009 -+71000000:+226:314159.29203539823009 -+106500000:+339:314159.29203539823009 -+1000000000:+3:333333333.33333333333 -$div_scale = 1 -# round to accuracy 1 after bdiv -+124:+3:40 -123456789.1234:1:100000000 -# reset scale for further tests -$div_scale = 40 -&fmod -+9:4:1 -+9:5:4 -+9000:56:40 -+56:9000:56 -# inf handling, see table in doc -0:inf:0 -0:-inf:0 -5:inf:5 -5:-inf:-inf --5:inf:inf --5:-inf:-5 -inf:5:NaN --inf:5:NaN -inf:-5:NaN --inf:-5:NaN -5:5:0 --5:-5:0 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:8 -inf:0:inf --inf:0:-inf --8:0:-8 -0:0:0 -abc:abc:NaN -abc:1:abc:NaN -1:abc:NaN -0:1:0 -1:0:1 -0:-1:0 --1:0:-1 -1:1:0 --1:-1:0 -1:-1:0 --1:1:0 -1:2:1 -2:1:0 -1000000000:9:1 -2000000000:9:2 -3000000000:9:3 -4000000000:9:4 -5000000000:9:5 -6000000000:9:6 -7000000000:9:7 -8000000000:9:8 -9000000000:9:0 -35500000:113:33 -71000000:226:66 -106500000:339:99 -1000000000:3:1 -10:5:0 -100:4:0 -1000:8:0 -10000:16:0 -999999999999:9:0 -999999999999:99:0 -999999999999:999:0 -999999999999:9999:0 -999999999999999:99999:0 --9:+5:1 -+9:-5:-1 --9:-5:-4 --5:3:1 --2:3:1 -4:3:1 -1:3:1 --5:-3:-2 --2:-3:-2 -4:-3:-2 -1:-3:-2 -4095:4095:0 -100041000510123:3:0 -152403346:12345:4321 -87654321:87654321:0 -# now some floating point tests -123:2.5:0.5 -1230:2.5:0 -123.4:2.5:0.9 -123e1:25:5 --2.1:1:0.9 -2.1:1:0.1 --2.1:-1:-0.1 -2.1:-1:-0.9 --3:1:0 -3:1:0 --3:-1:0 -3:-1:0 -&ffac -Nanfac:NaN --1:NaN -+inf:inf --inf:NaN -0:1 -1:1 -2:2 -3:6 -4:24 -5:120 -6:720 -10:3628800 -11:39916800 -12:479001600 -&froot -# sqrt() -+0:2:0 -+1:2:1 --1:2:NaN -# -$x ** (1/2) => -$y, but not in froot() --123.456:2:NaN -+inf:2:inf --inf:2:NaN -2:2:1.41421356237309504880168872420969807857 --2:2:NaN -4:2:2 -9:2:3 -16:2:4 -100:2:10 -123.456:2:11.11107555549866648462149404118219234119 -15241.38393:2:123.4559999756998444766131352122991626468 -1.44:2:1.2 -12:2:3.464101615137754587054892683011744733886 -0.49:2:0.7 -0.0049:2:0.07 -# invalid ones -1:NaN:NaN --1:NaN:NaN -0:NaN:NaN --inf:NaN:NaN -+inf:NaN:NaN -NaN:0:NaN -NaN:2:NaN -NaN:inf:NaN -NaN:inf:NaN -12:-inf:NaN -12:inf:NaN -+0:0:NaN -+1:0:NaN --1:0:NaN --2:0:NaN --123.45:0:NaN -+inf:0:NaN -12:1:12 --12:1:NaN -8:-1:NaN --8:-1:NaN -# cubic root -8:3:2 --8:3:NaN -# fourths root -16:4:2 -81:4:3 -# see t/bigroot() for more tests -&fsqrt -+0:0 --1:NaN --2:NaN --16:NaN --123.45:NaN -nanfsqrt:NaN -+inf:inf --inf:NaN -1:1 -2:1.41421356237309504880168872420969807857 -4:2 -9:3 -16:4 -100:10 -123.456:11.11107555549866648462149404118219234119 -15241.38393:123.4559999756998444766131352122991626468 -1.44:1.2 -# sqrt(1.44) = 1.2, sqrt(e10) = e5 => 12e4 -1.44E10:120000 -2e10:141421.356237309504880168872420969807857 -144e20:120000000000 -# proved to be an endless loop under 7-9 -12:3.464101615137754587054892683011744733886 -0.49:0.7 -0.0049:0.07 -&is_nan -123:0 -abc:1 -NaN:1 --123:0 -&is_inf -+inf::1 --inf::1 -abc::0 -1::0 -NaN::0 --1::0 -+inf:-:0 -+inf:+:1 --inf:-:1 --inf:+:0 -# it must be exactly /^[+-]inf$/ -+infinity::0 --infinity::0 -&is_odd -abc:0 -0:0 --1:1 --3:1 -1:1 -3:1 -1000001:1 -1000002:0 -+inf:0 --inf:0 -123.45:0 --123.45:0 -2:0 -&is_int -NaNis_int:0 -0:1 -1:1 -2:1 --2:1 --1:1 --inf:0 -+inf:0 -123.4567:0 --0.1:0 --0.002:0 -&is_even -abc:0 -0:1 --1:0 --3:0 -1:0 -3:0 -1000001:0 -1000002:1 -2:1 -+inf:0 --inf:0 -123.456:0 --123.456:0 -0.01:0 --0.01:0 -120:1 -1200:1 --1200:1 -&is_positive -0:0 -1:1 --1:0 --123:0 -NaN:0 --inf:0 -+inf:1 -&is_negative -0:0 -1:0 --1:1 --123:1 -NaN:0 --inf:1 -+inf:0 -&parts -0:0 1 -1:1 0 -123:123 0 --123:-123 0 --1200:-12 2 -NaNparts:NaN NaN -+inf:inf inf --inf:-inf inf -&exponent -0:1 -1:0 -123:0 --123:0 --1200:2 -+inf:inf --inf:inf -NaNexponent:NaN -&mantissa -0:0 -1:1 -123:123 --123:-123 --1200:-12 -+inf:inf --inf:-inf -NaNmantissa:NaN -&length -123:3 --123:3 -0:1 -1:1 -12345678901234567890:20 -&is_zero -NaNzero:0 -+inf:0 --inf:0 -0:1 --1:0 -1:0 -&is_one -NaNone:0 -+inf:0 --inf:0 -0:0 -2:0 -1:1 --1:0 --2:0 -&ffloor -0:0 -abc:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-52 -12.2:12 -0.12345:0 -0.123456:0 -0.1234567:0 -0.12345678:0 -0.123456789:0 -&fceil -0:0 -abc:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-51 -12.2:13 --0.4:0 -&fint -0:0 -NaN:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-51 -12.2:12 --0.4:0 diff --git a/dist/Math-BigInt/t/bigfltpm.t b/dist/Math-BigInt/t/bigfltpm.t deleted file mode 100644 index 8653f77ad1..0000000000 --- a/dist/Math-BigInt/t/bigfltpm.t +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 2340 - + 5; # own tests - - -use Math::BigInt lib => 'Calc'; -use Math::BigFloat; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigFloat"; -$CL = "Math::BigInt::Calc"; - -is ($class->config()->{class},$class); -is ($class->config()->{with}, $CL); - -# bug #17447: Can't call method Math::BigFloat->bsub, not a valid method -my $c = Math::BigFloat->new( '123.3' ); -is ($c->fsub(123), '0.3'); # calling fsub on a BigFloat works - -# Bug until BigInt v1.86, the scale wasn't treated as a scalar: -$c = Math::BigFloat->new('0.008'); my $d = Math::BigFloat->new(3); -my $e = $c->bdiv(Math::BigFloat->new(3),$d); - -is ($e,'0.00267'); # '0.008 / 3 => 0.0027'); -is (ref($e->{_e}->[0]), ''); # 'Not a BigInt'); - -require 't/bigfltpm.inc'; # all tests here for sharing diff --git a/dist/Math-BigInt/t/bigintc.t b/dist/Math-BigInt/t/bigintc.t deleted file mode 100644 index d5837f0890..0000000000 --- a/dist/Math-BigInt/t/bigintc.t +++ /dev/null @@ -1,454 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 379; - -use Math::BigInt::Calc; - -my ($BASE_LEN, undef, $AND_BITS, $XOR_BITS, $OR_BITS, $BASE_LEN_SMALL, $MAX_VAL) = - Math::BigInt::Calc->_base_len(); - -print "# BASE_LEN = $BASE_LEN\n"; -print "# MAX_VAL = $MAX_VAL\n"; -print "# AND_BITS = $AND_BITS\n"; -print "# XOR_BITS = $XOR_BITS\n"; -print "# IOR_BITS = $OR_BITS\n"; - -# testing of Math::BigInt::Calc - -my $C = 'Math::BigInt::Calc'; # pass classname to sub's - -# _new and _str -my $x = $C->_new("123"); my $y = $C->_new("321"); -is (ref($x),'ARRAY'); is ($C->_str($x),123); is ($C->_str($y),321); - -############################################################################### -# _add, _sub, _mul, _div -is ($C->_str($C->_add($x,$y)),444); -is ($C->_str($C->_sub($x,$y)),123); -is ($C->_str($C->_mul($x,$y)),39483); -is ($C->_str($C->_div($x,$y)),123); - -############################################################################### -# check that mul/div doesn't change $y -# and returns the same reference, not something new -is ($C->_str($C->_mul($x,$y)),39483); -is ($C->_str($x),39483); is ($C->_str($y),321); - -is ($C->_str($C->_div($x,$y)),123); -is ($C->_str($x),123); is ($C->_str($y),321); - -$x = $C->_new("39483"); -my ($x1,$r1) = $C->_div($x,$y); -is ("$x1","$x"); -$C->_inc($x1); -is ("$x1","$x"); -is ($C->_str($r1),'0'); - -$x = $C->_new("39483"); # reset - -############################################################################### -my $z = $C->_new("2"); -is ($C->_str($C->_add($x,$z)),39485); -my ($re,$rr) = $C->_div($x,$y); - -is ($C->_str($re),123); is ($C->_str($rr),2); - -# is_zero, _is_one, _one, _zero -is ($C->_is_zero($x)||0,0); -is ($C->_is_one($x)||0,0); - -is ($C->_str($C->_zero()),"0"); -is ($C->_str($C->_one()),"1"); - -# _two() and _ten() -is ($C->_str($C->_two()),"2"); -is ($C->_str($C->_ten()),"10"); -is ($C->_is_ten($C->_two()),0); -is ($C->_is_two($C->_two()),1); -is ($C->_is_ten($C->_ten()),1); -is ($C->_is_two($C->_ten()),0); - -is ($C->_is_one($C->_one()),1); -is ($C->_is_one($C->_two()),0); -is ($C->_is_one($C->_ten()),0); - -is ($C->_is_one($C->_zero()) || 0,0); - -is ($C->_is_zero($C->_zero()),1); - -is ($C->_is_zero($C->_one()) || 0,0); - -# is_odd, is_even -is ($C->_is_odd($C->_one()),1); is ($C->_is_odd($C->_zero())||0,0); -is ($C->_is_even($C->_one()) || 0,0); is ($C->_is_even($C->_zero()),1); - -# _len -for my $method (qw/_alen _len/) - { - $x = $C->_new("1"); is ($C->$method($x),1); - $x = $C->_new("12"); is ($C->$method($x),2); - $x = $C->_new("123"); is ($C->$method($x),3); - $x = $C->_new("1234"); is ($C->$method($x),4); - $x = $C->_new("12345"); is ($C->$method($x),5); - $x = $C->_new("123456"); is ($C->$method($x),6); - $x = $C->_new("1234567"); is ($C->$method($x),7); - $x = $C->_new("12345678"); is ($C->$method($x),8); - $x = $C->_new("123456789"); is ($C->$method($x),9); - - $x = $C->_new("8"); is ($C->$method($x),1); - $x = $C->_new("21"); is ($C->$method($x),2); - $x = $C->_new("321"); is ($C->$method($x),3); - $x = $C->_new("4321"); is ($C->$method($x),4); - $x = $C->_new("54321"); is ($C->$method($x),5); - $x = $C->_new("654321"); is ($C->$method($x),6); - $x = $C->_new("7654321"); is ($C->$method($x),7); - $x = $C->_new("87654321"); is ($C->$method($x),8); - $x = $C->_new("987654321"); is ($C->$method($x),9); - - $x = $C->_new("0"); is ($C->$method($x),1); - $x = $C->_new("20"); is ($C->$method($x),2); - $x = $C->_new("320"); is ($C->$method($x),3); - $x = $C->_new("4320"); is ($C->$method($x),4); - $x = $C->_new("54320"); is ($C->$method($x),5); - $x = $C->_new("654320"); is ($C->$method($x),6); - $x = $C->_new("7654320"); is ($C->$method($x),7); - $x = $C->_new("87654320"); is ($C->$method($x),8); - $x = $C->_new("987654320"); is ($C->$method($x),9); - - for (my $i = 1; $i < 9; $i++) - { - my $a = "$i" . '0' x ($i-1); - $x = $C->_new($a); - print "# Tried len '$a'\n" unless is ($C->_len($x),$i); - } - } - -# _digit -$x = $C->_new("123456789"); -is ($C->_digit($x,0),9); -is ($C->_digit($x,1),8); -is ($C->_digit($x,2),7); -is ($C->_digit($x,8),1); -is ($C->_digit($x,9),0); -is ($C->_digit($x,-1),1); -is ($C->_digit($x,-2),2); -is ($C->_digit($x,-3),3); -is ($C->_digit($x,-9),9); -is ($C->_digit($x,-10),0); - -# _copy -foreach (qw/ 1 12 123 1234 12345 123456 1234567 12345678 123456789/) - { - $x = $C->_new("$_"); - is ($C->_str($C->_copy($x)),"$_"); - is ($C->_str($x),"$_"); # did _copy destroy original x? - } - -# _zeros -$x = $C->_new("1256000000"); is ($C->_zeros($x),6); -$x = $C->_new("152"); is ($C->_zeros($x),0); -$x = $C->_new("123000"); is ($C->_zeros($x),3); -$x = $C->_new("0"); is ($C->_zeros($x),0); - -# _lsft, _rsft -$x = $C->_new("10"); $y = $C->_new("3"); -is ($C->_str($C->_lsft($x,$y,10)),10000); -$x = $C->_new("20"); $y = $C->_new("3"); -is ($C->_str($C->_lsft($x,$y,10)),20000); - -$x = $C->_new("128"); $y = $C->_new("4"); -is ($C->_str($C->_lsft($x,$y,2)), 128 << 4); - -$x = $C->_new("1000"); $y = $C->_new("3"); -is ($C->_str($C->_rsft($x,$y,10)),1); -$x = $C->_new("20000"); $y = $C->_new("3"); -is ($C->_str($C->_rsft($x,$y,10)),20); -$x = $C->_new("256"); $y = $C->_new("4"); -is ($C->_str($C->_rsft($x,$y,2)),256 >> 4); - -$x = $C->_new("6411906467305339182857313397200584952398"); -$y = $C->_new("45"); -is ($C->_str($C->_rsft($x,$y,10)),0); - -# _acmp -$x = $C->_new("123456789"); -$y = $C->_new("987654321"); -is ($C->_acmp($x,$y),-1); -is ($C->_acmp($y,$x),1); -is ($C->_acmp($x,$x),0); -is ($C->_acmp($y,$y),0); -$x = $C->_new("12"); -$y = $C->_new("12"); -is ($C->_acmp($x,$y),0); -$x = $C->_new("21"); -is ($C->_acmp($x,$y),1); -is ($C->_acmp($y,$x),-1); -$x = $C->_new("123456789"); -$y = $C->_new("1987654321"); -is ($C->_acmp($x,$y),-1); -is ($C->_acmp($y,$x),+1); - -$x = $C->_new("1234567890123456789"); -$y = $C->_new("987654321012345678"); -is ($C->_acmp($x,$y),1); -is ($C->_acmp($y,$x),-1); -is ($C->_acmp($x,$x),0); -is ($C->_acmp($y,$y),0); - -$x = $C->_new("1234"); -$y = $C->_new("987654321012345678"); -is ($C->_acmp($x,$y),-1); -is ($C->_acmp($y,$x),1); -is ($C->_acmp($x,$x),0); -is ($C->_acmp($y,$y),0); - -# _modinv -$x = $C->_new("8"); -$y = $C->_new("5033"); -my ($xmod,$sign) = $C->_modinv($x,$y); -is ($C->_str($xmod),'629'); # -629 % 5033 == 4404 -is ($sign, '-'); - -# _div -$x = $C->_new("3333"); $y = $C->_new("1111"); -is ($C->_str(scalar $C->_div($x,$y)),3); -$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); -is ($C->_str($x),30); is ($C->_str($y),3); -$x = $C->_new("123"); $y = $C->_new("1111"); -($x,$y) = $C->_div($x,$y); is ($C->_str($x),0); is ($C->_str($y),123); - -# _num -foreach (qw/1 12 123 1234 12345 1234567 12345678 123456789 1234567890/) - { - $x = $C->_new("$_"); - is (ref($x),'ARRAY'); is ($C->_str($x),"$_"); - $x = $C->_num($x); is (ref($x),''); is ($x,$_); - } - -# _sqrt -$x = $C->_new("144"); is ($C->_str($C->_sqrt($x)),'12'); -$x = $C->_new("144000000000000"); is ($C->_str($C->_sqrt($x)),'12000000'); - -# _root -$x = $C->_new("81"); my $n = $C->_new("3"); # 4*4*4 = 64, 5*5*5 = 125 -is ($C->_str($C->_root($x,$n)),'4'); # 4.xx => 4.0 -$x = $C->_new("81"); $n = $C->_new("4"); # 3*3*3*3 == 81 -is ($C->_str($C->_root($x,$n)),'3'); - -# _pow (and _root) -$x = $C->_new("0"); $n = $C->_new("3"); # 0 ** y => 0 -is ($C->_str($C->_pow($x,$n)), 0); -$x = $C->_new("3"); $n = $C->_new("0"); # x ** 0 => 1 -is ($C->_str($C->_pow($x,$n)), 1); -$x = $C->_new("1"); $n = $C->_new("3"); # 1 ** y => 1 -is ($C->_str($C->_pow($x,$n)), 1); -$x = $C->_new("5"); $n = $C->_new("1"); # x ** 1 => x -is ($C->_str($C->_pow($x,$n)), 5); - -$x = $C->_new("81"); $n = $C->_new("3"); # 81 ** 3 == 531441 -is ($C->_str($C->_pow($x,$n)),81 ** 3); - -is ($C->_str($C->_root($x,$n)),81); - -$x = $C->_new("81"); -is ($C->_str($C->_pow($x,$n)),81 ** 3); -is ($C->_str($C->_pow($x,$n)),'150094635296999121'); # 531441 ** 3 == - -is ($C->_str($C->_root($x,$n)),'531441'); -is ($C->_str($C->_root($x,$n)),'81'); - -$x = $C->_new("81"); $n = $C->_new("14"); -is ($C->_str($C->_pow($x,$n)),'523347633027360537213511521'); -is ($C->_str($C->_root($x,$n)),'81'); - -$x = $C->_new("523347633027360537213511520"); -is ($C->_str($C->_root($x,$n)),'80'); - -$x = $C->_new("523347633027360537213511522"); -is ($C->_str($C->_root($x,$n)),'81'); - -my $res = [ qw/9 31 99 316 999 3162 9999 31622 99999/ ]; - -# 99 ** 2 = 9801, 999 ** 2 = 998001 etc -for my $i (2 .. 9) - { - $x = '9' x $i; $x = $C->_new($x); - $n = $C->_new("2"); - my $rc = '9' x ($i-1). '8' . '0' x ($i-1) . '1'; - print "# _pow( ", '9' x $i, ", 2) \n" unless - is ($C->_str($C->_pow($x,$n)),$rc); - - # if $i > $BASE_LEN, the test takes a really long time: - if ($i <= $BASE_LEN) - { - $x = '9' x $i; $x = $C->_new($x); - $n = '9' x $i; $n = $C->_new($n); - print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n"; - print "# _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless - is ($C->_str($C->_root($x,$n)),'1'); - - $x = '9' x $i; $x = $C->_new($x); - $n = $C->_new("2"); - print "# BASE_LEN $BASE_LEN _root( ", '9' x $i, ", ", 9 x $i, ") \n" unless - is ($C->_str($C->_root($x,$n)), $res->[$i-2]); - } - else - { - is ("skipped $i", "skipped $i"); - is ("skipped $i", "skipped $i"); - } - } - -############################################################################## -# _fac -$x = $C->_new("0"); is ($C->_str($C->_fac($x)),'1'); -$x = $C->_new("1"); is ($C->_str($C->_fac($x)),'1'); -$x = $C->_new("2"); is ($C->_str($C->_fac($x)),'2'); -$x = $C->_new("3"); is ($C->_str($C->_fac($x)),'6'); -$x = $C->_new("4"); is ($C->_str($C->_fac($x)),'24'); -$x = $C->_new("5"); is ($C->_str($C->_fac($x)),'120'); -$x = $C->_new("10"); is ($C->_str($C->_fac($x)),'3628800'); -$x = $C->_new("11"); is ($C->_str($C->_fac($x)),'39916800'); -$x = $C->_new("12"); is ($C->_str($C->_fac($x)),'479001600'); -$x = $C->_new("13"); is ($C->_str($C->_fac($x)),'6227020800'); - -# test that _fac modifies $x in place for small arguments -$x = $C->_new("3"); $C->_fac($x); is ($C->_str($x),'6'); -$x = $C->_new("13"); $C->_fac($x); is ($C->_str($x),'6227020800'); - -############################################################################## -# _inc and _dec -foreach (qw/1 11 121 1231 12341 1234561 12345671 123456781 1234567891/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless is ($C->_str($x),substr($_,0,length($_)-1) . '2'); - $C->_dec($x); is ($C->_str($x),$_); - } -foreach (qw/19 119 1219 12319 1234519 12345619 123456719 1234567819/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless is ($C->_str($x),substr($_,0,length($_)-2) . '20'); - $C->_dec($x); is ($C->_str($x),$_); - } -foreach (qw/999 9999 99999 9999999 99999999 999999999 9999999999 99999999999/) - { - $x = $C->_new("$_"); $C->_inc($x); - print "# \$x = ",$C->_str($x),"\n" - unless is ($C->_str($x), '1' . '0' x (length($_))); - $C->_dec($x); is ($C->_str($x),$_); - } - -$x = $C->_new("1000"); $C->_inc($x); is ($C->_str($x),'1001'); -$C->_dec($x); is ($C->_str($x),'1000'); - -my $BL; -{ - no strict 'refs'; - $BL = &{"$C"."::_base_len"}(); -} - -$x = '1' . '0' x $BL; -$z = '1' . '0' x ($BL-1); $z .= '1'; -$x = $C->_new($x); $C->_inc($x); is ($C->_str($x),$z); - -$x = '1' . '0' x $BL; $z = '9' x $BL; -$x = $C->_new($x); $C->_dec($x); is ($C->_str($x),$z); - -# should not happen: -# $x = $C->_new("-2"); $y = $C->_new("4"); is ($C->_acmp($x,$y),-1); - -############################################################################### -# _mod -$x = $C->_new("1000"); $y = $C->_new("3"); -is ($C->_str(scalar $C->_mod($x,$y)),1); -$x = $C->_new("1000"); $y = $C->_new("2"); -is ($C->_str(scalar $C->_mod($x,$y)),0); - -# _and, _or, _xor -$x = $C->_new("5"); $y = $C->_new("2"); -is ($C->_str(scalar $C->_xor($x,$y)),7); -$x = $C->_new("5"); $y = $C->_new("2"); -is ($C->_str(scalar $C->_or($x,$y)),7); -$x = $C->_new("5"); $y = $C->_new("3"); -is ($C->_str(scalar $C->_and($x,$y)),1); - -# _from_hex, _from_bin, _from_oct -is ($C->_str( $C->_from_hex("0xFf")),255); -is ($C->_str( $C->_from_bin("0b10101011")),160+11); -is ($C->_str( $C->_from_oct("0100")), 8*8); -is ($C->_str( $C->_from_oct("01000")), 8*8*8); -is ($C->_str( $C->_from_oct("010001")), 8*8*8*8+1); -is ($C->_str( $C->_from_oct("010007")), 8*8*8*8+7); - -# _as_hex, _as_bin, as_oct -is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("128")))), 128); -is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("128")))), 128); -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("128")))), 128); - -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456")))), 123456); -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("123456789")))), "123456789"); -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("1234567890123")))), "1234567890123"); - -my $long = '123456789012345678901234567890'; -is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new($long)))), $long); -is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new($long)))), $long); -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new($long)))), $long); -is ($C->_str( $C->_from_hex( $C->_as_hex( $C->_new("0")))), 0); -is ($C->_str( $C->_from_bin( $C->_as_bin( $C->_new("0")))), 0); -is ($C->_str( $C->_from_oct( $C->_as_oct( $C->_new("0")))), 0); -is ($C->_as_hex( $C->_new("0")), '0x0'); -is ($C->_as_bin( $C->_new("0")), '0b0'); -is ($C->_as_oct( $C->_new("0")), '00'); -is ($C->_as_hex( $C->_new("12")), '0xc'); -is ($C->_as_bin( $C->_new("12")), '0b1100'); -is ($C->_as_oct( $C->_new("64")), '0100'); - -# _1ex -is ($C->_str($C->_1ex(0)), "1"); -is ($C->_str($C->_1ex(1)), "10"); -is ($C->_str($C->_1ex(2)), "100"); -is ($C->_str($C->_1ex(12)), "1000000000000"); -is ($C->_str($C->_1ex(16)), "10000000000000000"); - -# _check -$x = $C->_new("123456789"); -is ($C->_check($x),0); -is ($C->_check(123),'123 is not a reference'); - -############################################################################### -# __strip_zeros - -{ - no strict 'refs'; - # correct empty arrays - $x = &{$C."::__strip_zeros"}([]); is (@$x,1); is ($x->[0],0); - # don't strip single elements - $x = &{$C."::__strip_zeros"}([0]); is (@$x,1); is ($x->[0],0); - $x = &{$C."::__strip_zeros"}([1]); is (@$x,1); is ($x->[0],1); - # don't strip non-zero elements - $x = &{$C."::__strip_zeros"}([0,1]); - is (@$x,2); is ($x->[0],0); is ($x->[1],1); - $x = &{$C."::__strip_zeros"}([0,1,2]); - is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); - - # but strip leading zeros - $x = &{$C."::__strip_zeros"}([0,1,2,0]); - is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); - - $x = &{$C."::__strip_zeros"}([0,1,2,0,0]); - is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); - - $x = &{$C."::__strip_zeros"}([0,1,2,0,0,0]); - is (@$x,3); is ($x->[0],0); is ($x->[1],1); is ($x->[2],2); - - # collapse multiple zeros - $x = &{$C."::__strip_zeros"}([0,0,0,0]); - is (@$x,1); is ($x->[0],0); -} - -# done - -1; diff --git a/dist/Math-BigInt/t/bigintpm.inc b/dist/Math-BigInt/t/bigintpm.inc deleted file mode 100644 index 08a98acae5..0000000000 --- a/dist/Math-BigInt/t/bigintpm.inc +++ /dev/null @@ -1,2707 +0,0 @@ -#include this file into another for subclass testing - -my $version = ${"$class\::VERSION"}; - -use strict; - -############################################################################## -# for testing inheritance of _swap - -package Math::Foo; - -use Math::BigInt lib => $main::CL; -use vars qw/@ISA/; -@ISA = (qw/Math::BigInt/); - -use overload -# customized overload for sub, since original does not use swap there -'-' => sub { my @a = ref($_[0])->_swap(@_); - $a[0]->bsub($a[1])}; - -sub _swap - { - # a fake _swap, which reverses the params - my $self = shift; # for override in subclass - if ($_[2]) - { - my $c = ref ($_[0] ) || 'Math::Foo'; - return ( $_[0]->copy(), $_[1] ); - } - else - { - return ( Math::Foo->new($_[1]), $_[0] ); - } - } - -############################################################################## -package main; - -my $CALC = $class->config()->{lib}; is ($CALC,$CL); - -my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class); - -while () - { - $_ =~ s/[\n\r]//g; # remove newlines - next if /^#/; # skip comments - if (s/^&//) - { - $f = $_; next; - } - elsif (/^\$/) - { - $round_mode = $_; $round_mode =~ s/^\$/$class\->/; next; - } - - @args = split(/:/,$_,99); $ans = pop(@args); - $expected_class = $class; - if ($ans =~ /(.*?)=(.*)/) - { - $expected_class = $2; $ans = $1; - } - $try = "\$x = $class->new(\"$args[0]\");"; - if ($f eq "bnorm") - { - $try = "\$x = $class->bnorm(\"$args[0]\");"; - # some is_xxx tests - } elsif ($f =~ /^is_(zero|one|odd|even|negative|positive|nan|int)$/) { - $try .= "\$x->$f() || 0;"; - } elsif ($f eq "is_inf") { - $try .= "\$x->is_inf('$args[1]');"; - } elsif ($f eq "binf") { - $try .= "\$x->binf('$args[1]');"; - } elsif ($f eq "bone") { - $try .= "\$x->bone('$args[1]');"; - # some unary ops - } elsif ($f =~ /^b(nan|floor|ceil|int|sstr|neg|abs|sgn|inc|dec|not|sqrt|fac)$/) { - $try .= "\$x->$f();"; - } elsif ($f =~ /^(numify|length|stringify|as_hex|as_bin)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "exponent"){ - # ->bstr() to see if an object is returned - $try .= '$x = $x->exponent()->bstr();'; - } elsif ($f eq "mantissa"){ - # ->bstr() to see if an object is returned - $try .= '$x = $x->mantissa()->bstr();'; - } elsif ($f eq "parts"){ - $try .= '($m,$e) = $x->parts();'; - # ->bstr() to see if an object is returned - $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;'; - $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;'; - $try .= '"$m,$e";'; - }elsif ($f eq "bexp"){ - $try .= "\$x->bexp();"; - } elsif ($f eq "bpi"){ - $try .= "$class\->bpi(\$x);"; - } else { - # binary ops - $try .= "\$y = $class->new('$args[1]');"; - if ($f eq "bcmp") - { - $try .= '$x->bcmp($y);'; - } elsif ($f eq "bround") { - $try .= "$round_mode; \$x->bround(\$y);"; - } elsif ($f eq "bacmp"){ - $try .= '$x->bacmp($y);'; - } elsif ($f eq "badd"){ - $try .= '$x + $y;'; - } elsif ($f eq "bsub"){ - $try .= '$x - $y;'; - } elsif ($f eq "bmul"){ - $try .= '$x * $y;'; - } elsif ($f eq "bdiv"){ - $try .= '$x / $y;'; - } elsif ($f eq "bdiv-list"){ - $try .= 'join (",",$x->bdiv($y));'; - # overload via x= - } elsif ($f =~ /^.=$/){ - $try .= "\$x $f \$y;"; - # overload via x - } elsif ($f =~ /^.$/){ - $try .= "\$x $f \$y;"; - } elsif ($f eq "bmod"){ - $try .= '$x % $y;'; - } elsif ($f eq "bgcd") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new('$args[2]'); "; - } - $try .= "$class\::bgcd(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } - elsif ($f eq "blcm") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new('$args[2]'); "; - } - $try .= "$class\::blcm(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - }elsif ($f eq "blsft"){ - if (defined $args[2]) - { - $try .= "\$x->blsft(\$y,$args[2]);"; - } - else - { - $try .= "\$x << \$y;"; - } - }elsif ($f eq "brsft"){ - if (defined $args[2]) - { - $try .= "\$x->brsft(\$y,$args[2]);"; - } - else - { - $try .= "\$x >> \$y;"; - } - }elsif ($f eq "bnok"){ - $try .= "\$x->bnok(\$y);"; - }elsif ($f eq "broot"){ - $try .= "\$x->broot(\$y);"; - }elsif ($f eq "blog"){ - $try .= "\$x->blog(\$y);"; - }elsif ($f eq "band"){ - $try .= "\$x & \$y;"; - }elsif ($f eq "bior"){ - $try .= "\$x | \$y;"; - }elsif ($f eq "bxor"){ - $try .= "\$x ^ \$y;"; - }elsif ($f eq "bpow"){ - $try .= "\$x ** \$y;"; - } elsif( $f eq "bmodinv") { - $try .= "\$x->bmodinv(\$y);"; - }elsif ($f eq "digit"){ - $try .= "\$x->digit(\$y);"; - }elsif ($f eq "batan2"){ - $try .= "\$x->batan2(\$y);"; - } else { - # Functions with three arguments - $try .= "\$z = $class->new(\"$args[2]\");"; - - if( $f eq "bmodpow") { - $try .= "\$x->bmodpow(\$y,\$z);"; - } elsif ($f eq "bmuladd"){ - $try .= "\$x->bmuladd(\$y,\$z);"; - } else { warn "Unknown op '$f'"; } - } - } # end else all other ops - - $ans1 = eval $try; - # convert hex/binary targets to decimal - if ($ans =~ /^(0x0x|0b0b)/) - { - $ans =~ s/^0[xb]//; $ans = Math::BigInt->new($ans)->bstr(); - } - if ($ans eq "") - { - is ($ans1, undef); - } - else - { - # print "try: $try ans: $ans1 $ans\n"; - print "# Tried: '$try'\n" if !is ($ans1, $ans); - is (ref($ans),$expected_class) if $expected_class ne $class; - } - # check internal state of number objects - is_valid($ans1,$f) if ref $ans1; - } # endwhile data tests -close DATA; - -# test some more -@a = (); -for (my $i = 1; $i < 10; $i++) - { - push @a, $i; - } -is("@a", "1 2 3 4 5 6 7 8 9"); - -# test whether self-multiplication works correctly (result is 2**64) -$try = "\$x = $class->new('4294967296');"; -$try .= '$a = $x->bmul($x);'; -$ans1 = eval $try; -print "# Tried: '$try'\n" if !is ($ans1, $class->new(2) ** 64); -# test self-pow -$try = "\$x = $class->new(10);"; -$try .= '$a = $x->bpow($x);'; -$ans1 = eval $try; -print "# Tried: '$try'\n" if !is ($ans1, $class->new(10) ** 10); - -############################################################################### -# test whether op destroys args or not (should better not) - -$x = $class->new(3); -$y = $class->new(4); -$z = $x & $y; -is ($x,3); -is ($y,4); -is ($z,0); -$z = $x | $y; -is ($x,3); -is ($y,4); -is ($z,7); -$x = $class->new(1); -$y = $class->new(2); -$z = $x | $y; -is ($x,1); -is ($y,2); -is ($z,3); - -$x = $class->new(5); -$y = $class->new(4); -$z = $x ^ $y; -is ($x,5); -is ($y,4); -is ($z,1); - -$x = $class->new(-5); $y = -$x; -is ($x, -5); - -$x = $class->new(-5); $y = abs($x); -is ($x, -5); - -$x = $class->new(8); -$y = $class->new(-1); -$z = $class->new(5033); -my $u = $x->copy()->bmodpow($y,$z); -is ($u,4404); -is ($y,-1); -is ($z,5033); - -$x = $class->new(-5); $y = -$x; is ($x,-5); is ($y,5); -$x = $class->new(-5); $y = $x->copy()->bneg(); is ($x,-5); is ($y,5); - -$x = $class->new(-5); $y = $class->new(3); $x->bmul($y); is ($x,-15); is ($y,3); -$x = $class->new(-5); $y = $class->new(3); $x->badd($y); is ($x,-2); is ($y,3); -$x = $class->new(-5); $y = $class->new(3); $x->bsub($y); is ($x,-8); is ($y,3); -$x = $class->new(-15); $y = $class->new(3); $x->bdiv($y); is ($x,-5); is ($y,3); -$x = $class->new(-5); $y = $class->new(3); $x->bmod($y); is ($x,1); is ($y,3); - -$x = $class->new(5); $y = $class->new(3); $x->bmul($y); is ($x,15); is ($y,3); -$x = $class->new(5); $y = $class->new(3); $x->badd($y); is ($x,8); is ($y,3); -$x = $class->new(5); $y = $class->new(3); $x->bsub($y); is ($x,2); is ($y,3); -$x = $class->new(15); $y = $class->new(3); $x->bdiv($y); is ($x,5); is ($y,3); -$x = $class->new(5); $y = $class->new(3); $x->bmod($y); is ($x,2); is ($y,3); - -$x = $class->new(5); $y = $class->new(-3); $x->bmul($y); is ($x,-15); is ($y,-3); -$x = $class->new(5); $y = $class->new(-3); $x->badd($y); is ($x,2); is ($y,-3); -$x = $class->new(5); $y = $class->new(-3); $x->bsub($y); is ($x,8); is ($y,-3); -$x = $class->new(15); $y = $class->new(-3); $x->bdiv($y); is ($x,-5); is ($y,-3); -$x = $class->new(5); $y = $class->new(-3); $x->bmod($y); is ($x,-1); is ($y,-3); - -############################################################################### -# check whether overloading cmp works -$try = "\$x = $class->new(0);"; -$try .= "\$y = 10;"; -$try .= "'false' if \$x ne \$y;"; -$ans = eval $try; -print "# For '$try'\n" if (!is ("$ans" , "false") ); - -# we cant test for working cmpt with other objects here, we would need a dummy -# object with stringify overload for this. see Math::String tests as example - -############################################################################### -# check reversed order of arguments - -$try = "\$x = $class->new(10); \$x = 2 ** \$x;"; -$try .= "'ok' if \$x == 1024;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class->new(10); \$x = 2 * \$x;"; -$try .= "'ok' if \$x == 20;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class->new(10); \$x = 2 + \$x;"; -$try .= "'ok' if \$x == 12;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(10); \$x = 2 - \$x;"; -$try .= "'ok' if \$x == -8;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(10); \$x = 20 / \$x;"; -$try .= "'ok' if \$x == 2;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(3); \$x = 20 % \$x;"; -$try .= "'ok' if \$x == 2;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(7); \$x = 20 & \$x;"; -$try .= "'ok' if \$x == 4;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(7); \$x = 0x20 | \$x;"; -$try .= "'ok' if \$x == 0x27;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -$try = "\$x = $class\->new(7); \$x = 0x20 ^ \$x;"; -$try .= "'ok' if \$x == 0x27;"; $ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -############################################################################### -# check badd(4,5) form - -$try = "\$x = $class\->badd(4,5);"; -$try .= "'ok' if \$x == 9;"; -$ans = eval $try; -print "# For '$try'\n" if (!ok "$ans" , "ok" ); - -############################################################################### -# check undefs: NOT DONE YET - -############################################################################### -# bool - -$x = $class->new(1); if ($x) { is (1,1); } else { is ($x,'to be true') } -$x = $class->new(0); if (!$x) { is (1,1); } else { is ($x,'to be false') } - -############################################################################### -# objectify() - -@args = Math::BigInt::objectify(2,4,5); -is (scalar @args,3); # $class, 4, 5 -like ($args[0], qr/^Math::BigInt/); -is ($args[1],4); -is ($args[2],5); - -@args = Math::BigInt::objectify(0,4,5); -is (scalar @args,3); # $class, 4, 5 -like ($args[0], qr/^Math::BigInt/); -is ($args[1],4); -is ($args[2],5); - -@args = Math::BigInt::objectify(2,4,5); -is (scalar @args,3); # $class, 4, 5 -like ($args[0], qr/^Math::BigInt/); -is ($args[1],4); -is ($args[2],5); - -@args = Math::BigInt::objectify(2,4,5,6,7); -is (scalar @args,5); # $class, 4, 5, 6, 7 -like ($args[0], qr/^Math::BigInt/); -is ($args[1],4); is (ref($args[1]),$args[0]); -is ($args[2],5); is (ref($args[2]),$args[0]); -is ($args[3],6); is (ref($args[3]),''); -is ($args[4],7); is (ref($args[4]),''); - -@args = Math::BigInt::objectify(2,$class,4,5,6,7); -is (scalar @args,5); # $class, 4, 5, 6, 7 -is ($args[0],$class); -is ($args[1],4); is (ref($args[1]),$args[0]); -is ($args[2],5); is (ref($args[2]),$args[0]); -is ($args[3],6); is (ref($args[3]),''); -is ($args[4],7); is (ref($args[4]),''); - -############################################################################### -# test whether an opp calls objectify properly or not (or at least does what -# it should do given non-objects, w/ or w/o objectify()) - -is ($class->new(123)->badd(123),246); -is ($class->badd(123,321),444); -is ($class->badd(123,$class->new(321)),444); - -is ($class->new(123)->bsub(122),1); -is ($class->bsub(321,123),198); -is ($class->bsub(321,$class->new(123)),198); - -is ($class->new(123)->bmul(123),15129); -is ($class->bmul(123,123),15129); -is ($class->bmul(123,$class->new(123)),15129); - -is ($class->new(15129)->bdiv(123),123); -is ($class->bdiv(15129,123),123); -is ($class->bdiv(15129,$class->new(123)),123); - -is ($class->new(15131)->bmod(123),2); -is ($class->bmod(15131,123),2); -is ($class->bmod(15131,$class->new(123)),2); - -is ($class->new(2)->bpow(16),65536); -is ($class->bpow(2,16),65536); -is ($class->bpow(2,$class->new(16)),65536); - -is ($class->new(2**15)->brsft(1),2**14); -is ($class->brsft(2**15,1),2**14); -is ($class->brsft(2**15,$class->new(1)),2**14); - -is ($class->new(2**13)->blsft(1),2**14); -is ($class->blsft(2**13,1),2**14); -is ($class->blsft(2**13,$class->new(1)),2**14); - -############################################################################### -# test for floating-point input (other tests in bnorm() below) - -$z = 1050000000000000; # may be int on systems with 64bit? -$x = $class->new($z); is ($x->bsstr(),'105e+13'); # not 1.05e+15 -$z = 1e+129; # definitely a float (may fail on UTS) -# don't compare to $z, since some Perl versions stringify $z into something -# like '1.e+129' or something equally ugly -$x = $class->new($z); is ($x->bsstr(),'1e+129'); - -############################################################################### -# test for whitespace including newlines to be handled correctly - -# is ($Math::BigInt::strict,1); # the default - -foreach my $c ( - qw/1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567890/) - { - my $m = $class->new($c); - is ($class->new("$c"),$m); - is ($class->new(" $c"),$m); - is ($class->new("$c "),$m); - is ($class->new(" $c "),$m); - is ($class->new("\n$c"),$m); - is ($class->new("$c\n"),$m); - is ($class->new("\n$c\n"),$m); - is ($class->new(" \n$c\n"),$m); - is ($class->new(" \n$c \n"),$m); - is ($class->new(" \n$c\n "),$m); - is ($class->new(" \n$c\n1"),'NaN'); - is ($class->new("1 \n$c\n1"),'NaN'); - } - -############################################################################### -# prime number tests, also test for **= and length() -# found on: http://www.utm.edu/research/primes/notes/by_year.html - -# ((2^148)-1)/17 -$x = $class->new(2); $x **= 148; $x++; $x = $x / 17; -is ($x,"20988936657440586486151264256610222593863921"); -is ($x->length(),length "20988936657440586486151264256610222593863921"); - -# MM7 = 2^127-1 -$x = $class->new(2); $x **= 127; $x--; -is ($x,"170141183460469231731687303715884105727"); - -$x = $class->new('215960156869840440586892398248'); -($x,$y) = $x->length(); -is ($x,30); is ($y,0); - -$x = $class->new('1_000_000_000_000'); -($x,$y) = $x->length(); -is ($x,13); is ($y,0); - -# test <<=, >>= -$x = $class->new('2'); -my $y = $class->new('18'); -is ($x <<= $y, 2 << 18); -is ($x, 2 << 18); -is ($x >>= $y, 2); -is ($x, 2); - -# I am afraid the following is not yet possible due to slowness -# Also, testing for 2 meg output is a bit hard ;) -#$x = $class->new(2); $x **= 6972593; $x--; - -# 593573509*2^332162+1 has exactly 1,000,000 digits -# takes about 24 mins on 300 Mhz, so cannot be done yet ;) -#$x = $class->new(2); $x **= 332162; $x *= "593573509"; $x++; -#is ($x->length(),1_000_000); - -############################################################################### -# inheritance and overriding of _swap - -$x = Math::Foo->new(5); -$x = $x - 8; # 8 - 5 instead of 5-8 -is ($x,3); -is (ref($x),'Math::Foo'); - -$x = Math::Foo->new(5); -$x = 8 - $x; # 5 - 8 instead of 8 - 5 -is ($x,-3); -is (ref($x),'Math::Foo'); - -############################################################################### -# Test whether +inf eq inf -# This tried to test whether BigInt inf equals Perl inf. Unfortunately, Perl -# hasn't (before 5.7.3 at least) a consistent way to say inf, and some things -# like 1e100000 crash on some platforms. So simple test for the string 'inf' -$x = $class->new('+inf'); is ($x,'inf'); - -############################################################################### -# numify() and 64 bit integer support - -require Config; -SKIP: { - skip("no 64 bit integer support", 4) - unless $Config::Config{use64bitint} || $Config::Config{use64bitall}; - - # The following should not give "1.84467440737096e+19". - - $x = $class -> new(2) -> bpow(64) -> bdec(); - is($x -> bstr(), "18446744073709551615", "bigint 2**64-1 as string"); - is($x -> numify(), "18446744073709551615", "bigint 2**64-1 as number"); - - # The following should not give "-9.22337203685478e+18". - - $x = $class -> new(2) -> bpow(63) -> bneg(); - is($x -> bstr(), "-9223372036854775808", "bigint -2**63 as string"); - is($x -> numify(), "-9223372036854775808", "bigint -2**63 as number"); -}; - -############################################################################### -############################################################################### -# the following tests only make sense with Math::BigInt::Calc or BareCalc or -# FastCalc - -exit if $CALC !~ /^Math::BigInt::(|Bare|Fast)Calc$/; # for Pari et al. - -############################################################################### -# check proper length of internal arrays - -my $bl = $CL->_base_len(); -my $BASE = '9' x $bl; -my $MAX = $BASE; -$BASE++; - -$x = $class->new($MAX); is_valid($x); # f.i. 9999 -$x += 1; is ($x,$BASE); is_valid($x); # 10000 -$x -= 1; is ($x,$MAX); is_valid($x); # 9999 again - -############################################################################### -# check numify - -$x = $class->new($BASE-1); is ($x->numify(),$BASE-1); -$x = $class->new(-($BASE-1)); is ($x->numify(),-($BASE-1)); - -# +0 is to protect from 1e15 vs 100000000 (stupid to_string aaaarglburblll...) -$x = $class->new($BASE); is ($x->numify()+0,$BASE+0); -$x = $class->new(-$BASE); is ($x->numify(),-$BASE); -$x = $class->new( -($BASE*$BASE*1+$BASE*1+1) ); -is ($x->numify(),-($BASE*$BASE*1+$BASE*1+1)); - -############################################################################### -# test bug in _digits with length($c[-1]) where $c[-1] was "00001" instead of 1 - -$x = $class->new($BASE-2); $x++; $x++; $x++; $x++; -if ($x > $BASE) { is (1,1) } else { is ("$x < $BASE","$x > $BASE"); } - -$x = $class->new($BASE+3); $x++; -if ($x > $BASE) { is (1,1) } else { is ("$x > $BASE","$x < $BASE"); } - -# test for +0 instead of int(): -$x = $class->new($MAX); is ($x->length(), length($MAX)); - -############################################################################### -# test bug that $class->digit($string) did not work - -is ($class->digit(123,2),1); - -############################################################################### -# bug in sub where number with at least 6 trailing zeros after any op failed - -$x = $class->new(123456); $z = $class->new(10000); $z *= 10; $x -= $z; -is ($z, 100000); -is ($x, 23456); - -############################################################################### -# bug in shortcut in mul() - -# construct a number with a zero-hole of BASE_LEN_SMALL -{ - my @bl = $CL->_base_len(); my $bl = $bl[5]; - - $x = '1' x $bl . '0' x $bl . '1' x $bl . '0' x $bl; - $y = '1' x (2*$bl); - $x = $class->new($x)->bmul($y); - # result is 123..$bl . $bl x (3*bl-1) . $bl...321 . '0' x $bl - $y = ''; my $d = ''; - for (my $i = 1; $i <= $bl; $i++) - { - $y .= $i; $d = $i.$d; - } - $y .= $bl x (3*$bl-1) . $d . '0' x $bl; - is ($x,$y); - - - ############################################################################# - # see if mul shortcut for small numbers works - - $x = '9' x $bl; - $x = $class->new($x); - # 999 * 999 => 998 . 001, 9999*9999 => 9998 . 0001 - is ($x*$x, '9' x ($bl-1) . '8' . '0' x ($bl-1) . '1'); -} - -############################################################################### -# bug with rest "-0" in div, causing further div()s to fail - -$x = $class->new('-322056000'); ($x,$y) = $x->bdiv('-12882240'); - -is ($y,'0'); is_valid($y); # $y not '-0' - -############################################################################### -# bug in $x->bmod($y) - -# if $x < 0 and $y > 0 -$x = $class->new('-629'); is ($x->bmod(5033),4404); - -############################################################################### -# bone/binf etc as plain calls (Lite failed them) - -is ($class->bzero(),0); -is ($class->bone(),1); -is ($class->bone('+'),1); -is ($class->bone('-'),-1); -is ($class->bnan(),'NaN'); -is ($class->binf(),'inf'); -is ($class->binf('+'),'inf'); -is ($class->binf('-'),'-inf'); -is ($class->binf('-inf'),'-inf'); - -############################################################################### -# is_one('-') - -is ($class->new(1)->is_one('-'),0); -is ($class->new(-1)->is_one('-'),1); -is ($class->new(1)->is_one(),1); -is ($class->new(-1)->is_one(),0); - -############################################################################### -# [perl #30609] bug with $x -= $x not being 0, but 2*$x - -$x = $class->new(3); $x -= $x; is ($x, 0); -$x = $class->new(-3); $x -= $x; is ($x, 0); -$x = $class->new('NaN'); $x -= $x; is ($x->is_nan(), 1); -$x = $class->new('inf'); $x -= $x; is ($x->is_nan(), 1); -$x = $class->new('-inf'); $x -= $x; is ($x->is_nan(), 1); - -$x = $class->new('NaN'); $x += $x; is ($x->is_nan(), 1); -$x = $class->new('inf'); $x += $x; is ($x->is_inf(), 1); -$x = $class->new('-inf'); $x += $x; is ($x->is_inf('-'), 1); -$x = $class->new(3); $x += $x; is ($x, 6); -$x = $class->new(-3); $x += $x; is ($x, -6); - -$x = $class->new(3); $x *= $x; is ($x, 9); -$x = $class->new(-3); $x *= $x; is ($x, 9); -$x = $class->new(3); $x /= $x; is ($x, 1); -$x = $class->new(-3); $x /= $x; is ($x, 1); -$x = $class->new(3); $x %= $x; is ($x, 0); -$x = $class->new(-3); $x %= $x; is ($x, 0); - -############################################################################### -# all tests done - -1; - -############################################################################### -# sub to check validity of a BigInt internally, to ensure that no op leaves a -# number object in an invalid state (f.i. "-0") - -sub is_valid - { - my ($x,$f) = @_; - - my $e = 0; # error? - - # allow the check to pass for all Lite, and all MBI and subclasses - # ok as reference? - $e = 'Not a reference to Math::BigInt' if ref($x) !~ /^Math::BigInt/; - - if (ref($x) ne 'Math::BigInt::Lite') - { - # has ok sign? - $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" - if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; - - $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; - $e = $CALC->_check($x->{value}) if $e eq '0'; - } - - # test done, see if error did crop up - is (1,1), return if ($e eq '0'); - - is (1,$e." after op '$f'"); - } - -__DATA__ -&.= -1234:-345:1234-345 -&+= -1:2:3 --1:-2:-3 -&-= -1:2:-1 --1:-2:1 -&*= -2:3:6 --1:5:-5 -&%= -100:3:1 -8:9:8 --629:5033:4404 -&/= -100:3:33 --8:2:-4 -&|= -2:1:3 -&&= -5:7:5 -&^= -5:7:2 -&blog -NaNlog:2:NaN -122:NaNlog:NaN -NaNlog1:NaNlog:NaN -122:inf:NaN -inf:122:NaN -122:-inf:NaN --inf:122:NaN --inf:-inf:NaN -inf:inf:NaN -0:4:NaN --21:4:NaN -21:-21:NaN -# normal results -1024:2:10 -81:3:4 -# 3.01.. truncate -82:3:4 -# 3.9... truncate -80:3:3 -15625:5:6 -15626:5:6 -15624:5:5 -1000:10:3 -10000:10:4 -100000:10:5 -1000000:10:6 -10000000:10:7 -100000000:10:8 -8916100448256:12:12 -8916100448257:12:12 -8916100448255:12:11 -2251799813685248:8:17 -72057594037927936:2:56 -144115188075855872:2:57 -288230376151711744:2:58 -576460752303423488:2:59 -4096:2:12 -1329227995784915872903807060280344576:2:120 -# $x == $base => result 1 -3:3:1 -# $x < $base => result 0 ($base ** 0 <= $x) -3:4:0 -# $x == 1 => result 0 -1:5:0 -&is_negative -0:0 --1:1 -1:0 -+inf:0 --inf:1 -NaNneg:0 -&is_positive -0:0 --1:0 -1:1 -+inf:1 --inf:0 -NaNneg:0 -&is_int --inf:0 -+inf:0 -NaNis_int:0 -1:1 -0:1 -123e12:1 -&is_odd -abc:0 -0:0 -1:1 -3:1 --1:1 --3:1 -10000001:1 -10000002:0 -2:0 -120:0 -121:1 -&is_even -abc:0 -0:1 -1:0 -3:0 --1:0 --3:0 -10000001:0 -10000002:1 -2:1 -120:1 -121:0 -&bacmp -+0:-0:0 -+0:+1:-1 --1:+1:0 -+1:-1:0 --1:+2:-1 -+2:-1:1 --123456789:+987654321:-1 -+123456789:-987654321:-1 -+987654321:+123456789:1 --987654321:+123456789:1 --123:+4567889:-1 -# NaNs -acmpNaN:123: -123:acmpNaN: -acmpNaN:acmpNaN: -# infinity -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:0 --inf:+inf:0 -+inf:123:1 --inf:123:1 -+inf:-123:1 --inf:-123:1 -123:-inf:-1 --123:inf:-1 --123:-inf:-1 -123:inf:-1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&bnorm -0e999:0 -0e-999:0 --0e999:0 --0e-999:0 -123:123 -# binary input -0babc:NaN -0b123:NaN -0b0:0 --0b0:0 --0b1:-1 -0b0001:1 -0b001:1 -0b011:3 -0b101:5 -0b1001:9 -0b10001:17 -0b100001:33 -0b1000001:65 -0b10000001:129 -0b100000001:257 -0b1000000001:513 -0b10000000001:1025 -0b100000000001:2049 -0b1000000000001:4097 -0b10000000000001:8193 -0b100000000000001:16385 -0b1000000000000001:32769 -0b10000000000000001:65537 -0b100000000000000001:131073 -0b1000000000000000001:262145 -0b10000000000000000001:524289 -0b100000000000000000001:1048577 -0b1000000000000000000001:2097153 -0b10000000000000000000001:4194305 -0b100000000000000000000001:8388609 -0b1000000000000000000000001:16777217 -0b10000000000000000000000001:33554433 -0b100000000000000000000000001:67108865 -0b1000000000000000000000000001:134217729 -0b10000000000000000000000000001:268435457 -0b100000000000000000000000000001:536870913 -0b1000000000000000000000000000001:1073741825 -0b10000000000000000000000000000001:2147483649 -0b100000000000000000000000000000001:4294967297 -0b1000000000000000000000000000000001:8589934593 -0b10000000000000000000000000000000001:17179869185 -0b__101:NaN -0b1_0_1:5 -0b0_0_0_1:1 -# hex input --0x0:0 -0xabcdefgh:NaN -0x1234:4660 -0xabcdef:11259375 --0xABCDEF:-11259375 --0x1234:-4660 -0x12345678:305419896 -0x1_2_3_4_56_78:305419896 -0xa_b_c_d_e_f:11259375 -0x__123:NaN -0x9:9 -0x11:17 -0x21:33 -0x41:65 -0x81:129 -0x101:257 -0x201:513 -0x401:1025 -0x801:2049 -0x1001:4097 -0x2001:8193 -0x4001:16385 -0x8001:32769 -0x10001:65537 -0x20001:131073 -0x40001:262145 -0x80001:524289 -0x100001:1048577 -0x200001:2097153 -0x400001:4194305 -0x800001:8388609 -0x1000001:16777217 -0x2000001:33554433 -0x4000001:67108865 -0x8000001:134217729 -0x10000001:268435457 -0x20000001:536870913 -0x40000001:1073741825 -0x80000001:2147483649 -0x100000001:4294967297 -0x200000001:8589934593 -0x400000001:17179869185 -0x800000001:34359738369 -# bug found by Mark Lakata in Calc.pm creating too big one-element numbers in _from_hex() -0x2dd59e18a125dbed30a6ab1d93e9c855569f44f75806f0645dc9a2e98b808c3:1295719234436071846486578237372801883390756472611551858964079371952886122691 -# inf input -inf:inf -+inf:inf --inf:-inf -0inf:NaN -# abnormal input -:NaN -abc:NaN - 1 a:NaN -1bcd2:NaN -11111b:NaN -+1z:NaN --1z:NaN -# only one underscore between two digits -_123:NaN -_123_:NaN -123_:NaN -1__23:NaN -1E1__2:NaN -1_E12:NaN -1E_12:NaN -1_E_12:NaN -+_1E12:NaN -+0_1E2:100 -+0_0_1E2:100 --0_0_1E2:-100 --0_0_1E+0_0_2:-100 -E1:NaN -E23:NaN -1.23E1:NaN -1.23E-1:NaN -# bug with two E's in number being valid -1e2e3:NaN -1e2r:NaN -1e2.0:NaN -# bug with two '.' in number being valid -1.2.2:NaN -1.2.3e1:NaN --1.2.3:NaN --1.2.3e-4:NaN -1.2e3.4:NaN -1.2e-3.4:NaN -1.2.3.4:NaN -1.2.t:NaN -1..2:NaN -1..2e1:NaN -1..2e1..1:NaN -12e1..1:NaN -..2:NaN -.-2:NaN -# leading zeros -012:12 -0123:123 -01234:1234 -012345:12345 -0123456:123456 -01234567:1234567 -012345678:12345678 -0123456789:123456789 -01234567891:1234567891 -012345678912:12345678912 -0123456789123:123456789123 -01234567891234:1234567891234 -# some inputs that result in zero -0e0:0 -+0e0:0 -+0e+0:0 --0e+0:0 -0e-0:0 --0e-0:0 -+0e-0:0 -000:0 -00e2:0 -00e02:0 -000e002:0 -000e1230:0 -00e-3:0 -00e+3:0 -00e-03:0 -00e+03:0 --000:0 --00e2:0 --00e02:0 --000e002:0 --000e1230:0 --00e-3:0 --00e+3:0 --00e-03:0 --00e+03:0 -# normal input -0:0 -+0:0 -+00:0 -+000:0 -000000000000000000:0 --0:0 --0000:0 -+1:1 -+01:1 -+001:1 -+00000100000:100000 -123456789:123456789 --1:-1 --01:-1 --001:-1 --123456789:-123456789 --00000100000:-100000 -1_2_3:123 -10000000000E-1_0:1 -1E2:100 -1E1:10 -1E0:1 -1.23E2:123 -100E-1:10 -# floating point input -# .2e2:20 -1.E3:1000 -1.01E2:101 -1010E-1:101 --1010E0:-1010 --1010E1:-10100 -1234.00:1234 -# non-integer numbers --1010E-2:NaN --1.01E+1:NaN --1.01E-1:NaN -1E-999999:NaN -0.5:NaN -&bnan -1:NaN -2:NaN -abc:NaN -&bone -2:+:1 -2:-:-1 -boneNaN:-:-1 -boneNaN:+:1 -2:abc:1 -3::1 -&binf -1:+:inf -2:-:-inf -3:abc:inf -&is_nan -123:0 -abc:1 -NaN:1 --123:0 -&is_inf -+inf::1 --inf::1 -abc::0 -1::0 -NaN::0 --1::0 -+inf:-:0 -+inf:+:1 --inf:-:1 --inf:+:0 --inf:-inf:1 --inf:+inf:0 -+inf:-inf:0 -+inf:+inf:1 -# it must be exactly /^[+-]inf$/ -+infinity::0 --infinity::0 -&blsft -abc:abc:NaN -+2:+2:8 -+1:+32:4294967296 -+1:+48:281474976710656 -+8:-2:NaN -# exercise base 10 -+12345:4:10:123450000 --1234:0:10:-1234 -+1234:0:10:1234 -+2:2:10:200 -+12:2:10:1200 -+1234:-3:10:NaN -1234567890123:12:10:1234567890123000000000000 --3:1:2:-6 --5:1:2:-10 --2:1:2:-4 --102533203:1:2:-205066406 -&brsft -abc:abc:NaN -+8:+2:2 -+4294967296:+32:1 -+281474976710656:+48:1 -+2:-2:NaN -# exercise base 10 --1234:0:10:-1234 -+1234:0:10:1234 -+200:2:10:2 -+1234:3:10:1 -+1234:2:10:12 -+1234:-3:10:NaN -310000:4:10:31 -12300000:5:10:123 -1230000000000:10:10:123 -09876123456789067890:12:10:9876123 -1234561234567890123:13:10:123456 -820265627:1:2:410132813 -# test shifting negative numbers in base 2 --15:1:2:-8 --14:1:2:-7 --13:1:2:-7 --12:1:2:-6 --11:1:2:-6 --10:1:2:-5 --9:1:2:-5 --8:1:2:-4 --7:1:2:-4 --6:1:2:-3 --5:1:2:-3 --4:1:2:-2 --3:1:2:-2 --2:1:2:-1 --1:1:2:-1 --1640531254:2:2:-410132814 --1640531254:1:2:-820265627 --820265627:1:2:-410132814 --205066405:1:2:-102533203 -&bsstr -+inf:inf --inf:-inf -1e+34:1e+34 -123.456E3:123456e+0 -100:1e+2 -bsstrabc:NaN --5:-5e+0 --100:-1e+2 -&numify -numifyabc:NaN -+inf:inf --inf:-inf -5:5 --5:-5 -100:100 --100:-100 -&bneg -bnegNaN:NaN -+inf:-inf --inf:inf -abd:NaN -0:0 -1:-1 --1:1 -+123456789:-123456789 --123456789:123456789 -&babs -babsNaN:NaN -+inf:inf --inf:inf -0:0 -1:1 --1:1 -+123456789:123456789 --123456789:123456789 -&bsgn -NaN:NaN -+inf:1 --inf:-1 -0:0 -+123456789:1 --123456789:-1 -&bcmp -bcmpNaN:bcmpNaN: -bcmpNaN:0: -0:bcmpNaN: -0:0:0 --1:0:-1 -0:-1:1 -1:0:1 -0:1:-1 --1:1:-1 -1:-1:1 --1:-1:0 -1:1:0 -123:123:0 -123:12:1 -12:123:-1 --123:-123:0 --123:-12:-1 --12:-123:1 -123:124:-1 -124:123:1 --123:-124:1 --124:-123:-1 -100:5:1 --123456789:987654321:-1 -+123456789:-987654321:1 --987654321:123456789:-1 --inf:5432112345:-1 -+inf:5432112345:1 --inf:-5432112345:-1 -+inf:-5432112345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:1 --inf:+inf:-1 -5:inf:-1 -5:inf:-1 --5:-inf:1 --5:-inf:1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&binc -abc:NaN -+inf:inf --inf:-inf -+0:1 -+1:2 --1:0 -&bdec -abc:NaN -+inf:inf --inf:-inf -+0:-1 -+1:0 --1:-2 -&badd -abc:abc:NaN -abc:0:NaN -+0:abc:NaN -+inf:-inf:NaN --inf:+inf:NaN -+inf:+inf:inf --inf:-inf:-inf -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -0:0:0 -1:0:1 -0:1:1 -1:1:2 --1:0:-1 -0:-1:-1 --1:-1:-2 --1:+1:0 -+1:-1:0 -+9:+1:10 -+99:+1:100 -+999:+1:1000 -+9999:+1:10000 -+99999:+1:100000 -+999999:+1:1000000 -+9999999:+1:10000000 -+99999999:+1:100000000 -+999999999:+1:1000000000 -+9999999999:+1:10000000000 -+99999999999:+1:100000000000 -+10:-1:9 -+100:-1:99 -+1000:-1:999 -+10000:-1:9999 -+100000:-1:99999 -+1000000:-1:999999 -+10000000:-1:9999999 -+100000000:-1:99999999 -+1000000000:-1:999999999 -+10000000000:-1:9999999999 -+123456789:987654321:1111111110 --123456789:987654321:864197532 --123456789:-987654321:-1111111110 -+123456789:-987654321:-864197532 --1:10001:10000 --1:100001:100000 --1:1000001:1000000 --1:10000001:10000000 --1:100000001:100000000 --1:1000000001:1000000000 --1:10000000001:10000000000 --1:100000000001:100000000000 --1:1000000000001:1000000000000 --1:10000000000001:10000000000000 --1:-10001:-10002 --1:-100001:-100002 --1:-1000001:-1000002 --1:-10000001:-10000002 --1:-100000001:-100000002 --1:-1000000001:-1000000002 --1:-10000000001:-10000000002 --1:-100000000001:-100000000002 --1:-1000000000001:-1000000000002 --1:-10000000000001:-10000000000002 -&bsub -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:inf --inf:+inf:-inf -+inf:+inf:NaN --inf:-inf:NaN -+0:+0:0 -+1:+0:1 -+0:+1:-1 -+1:+1:0 --1:+0:-1 -+0:-1:1 --1:-1:0 --1:+1:-2 -+1:-1:2 -+9:+1:8 -+99:+1:98 -+999:+1:998 -+9999:+1:9998 -+99999:+1:99998 -+999999:+1:999998 -+9999999:+1:9999998 -+99999999:+1:99999998 -+999999999:+1:999999998 -+9999999999:+1:9999999998 -+99999999999:+1:99999999998 -+10:-1:11 -+100:-1:101 -+1000:-1:1001 -+10000:-1:10001 -+100000:-1:100001 -+1000000:-1:1000001 -+10000000:-1:10000001 -+100000000:-1:100000001 -+1000000000:-1:1000000001 -+10000000000:-1:10000000001 -+123456789:+987654321:-864197532 --123456789:+987654321:-1111111110 --123456789:-987654321:864197532 -+123456789:-987654321:1111111110 -10001:1:10000 -100001:1:100000 -1000001:1:1000000 -10000001:1:10000000 -100000001:1:100000000 -1000000001:1:1000000000 -10000000001:1:10000000000 -100000000001:1:100000000000 -1000000000001:1:1000000000000 -10000000000001:1:10000000000000 -10001:-1:10002 -100001:-1:100002 -1000001:-1:1000002 -10000001:-1:10000002 -100000001:-1:100000002 -1000000001:-1:1000000002 -10000000001:-1:10000000002 -100000000001:-1:100000000002 -1000000000001:-1:1000000000002 -10000000000001:-1:10000000000002 -&bmuladd -abc:abc:0:NaN -abc:+0:0:NaN -+0:abc:0:NaN -+0:0:abc:NaN -NaNmul:+inf:0:NaN -NaNmul:-inf:0:NaN --inf:NaNmul:0:NaN -+inf:NaNmul:0:NaN -+inf:+inf:0:inf -+inf:-inf:0:-inf --inf:+inf:0:-inf --inf:-inf:0:inf -+0:+0:0:0 -+0:+1:0:0 -+1:+0:0:0 -+0:-1:0:0 --1:+0:0:0 -123456789123456789:0:0:0 -0:123456789123456789:0:0 --1:-1:0:1 --1:-1:0:1 --1:+1:0:-1 -+1:-1:0:-1 -+1:+1:0:1 -+2:+3:0:6 --2:+3:0:-6 -+2:-3:0:-6 --2:-3:0:6 -111:111:0:12321 -10101:10101:0:102030201 -1001001:1001001:0:1002003002001 -100010001:100010001:0:10002000300020001 -10000100001:10000100001:0:100002000030000200001 -11111111111:9:0:99999999999 -22222222222:9:0:199999999998 -33333333333:9:0:299999999997 -44444444444:9:0:399999999996 -55555555555:9:0:499999999995 -66666666666:9:0:599999999994 -77777777777:9:0:699999999993 -88888888888:9:0:799999999992 -99999999999:9:0:899999999991 -11111111111:9:1:100000000000 -22222222222:9:1:199999999999 -33333333333:9:1:299999999998 -44444444444:9:1:399999999997 -55555555555:9:1:499999999996 -66666666666:9:1:599999999995 -77777777777:9:1:699999999994 -88888888888:9:1:799999999993 -99999999999:9:1:899999999992 --3:-4:-5:7 -3:-4:-5:-17 --3:4:-5:-17 -3:4:-5:7 --3:4:5:-7 -3:-4:5:-7 -9999999999999999999:10000000000000000000:1234567890:99999999999999999990000000001234567890 -2:3:12345678901234567890:12345678901234567896 -&bmul -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -NaNmul:+inf:NaN -NaNmul:-inf:NaN --inf:NaNmul:NaN -+inf:NaNmul:NaN -+inf:+inf:inf -+inf:-inf:-inf --inf:+inf:-inf --inf:-inf:inf -+0:+0:0 -+0:+1:0 -+1:+0:0 -+0:-1:0 --1:+0:0 -123456789123456789:0:0 -0:123456789123456789:0 --1:-1:1 --1:+1:-1 -+1:-1:-1 -+1:+1:1 -+2:+3:6 --2:+3:-6 -+2:-3:-6 --2:-3:6 -111:111:12321 -10101:10101:102030201 -1001001:1001001:1002003002001 -100010001:100010001:10002000300020001 -10000100001:10000100001:100002000030000200001 -11111111111:9:99999999999 -22222222222:9:199999999998 -33333333333:9:299999999997 -44444444444:9:399999999996 -55555555555:9:499999999995 -66666666666:9:599999999994 -77777777777:9:699999999993 -88888888888:9:799999999992 -99999999999:9:899999999991 -+25:+25:625 -+12345:+12345:152399025 -+99999:+11111:1111088889 -9999:10000:99990000 -99999:100000:9999900000 -999999:1000000:999999000000 -9999999:10000000:99999990000000 -99999999:100000000:9999999900000000 -999999999:1000000000:999999999000000000 -9999999999:10000000000:99999999990000000000 -99999999999:100000000000:9999999999900000000000 -999999999999:1000000000000:999999999999000000000000 -9999999999999:10000000000000:99999999999990000000000000 -99999999999999:100000000000000:9999999999999900000000000000 -999999999999999:1000000000000000:999999999999999000000000000000 -9999999999999999:10000000000000000:99999999999999990000000000000000 -99999999999999999:100000000000000000:9999999999999999900000000000000000 -999999999999999999:1000000000000000000:999999999999999999000000000000000000 -9999999999999999999:10000000000000000000:99999999999999999990000000000000000000 -&bdiv-list -100:20:5,0 -4095:4095:1,0 --4095:-4095:1,0 -4095:-4095:-1,0 --4095:4095:-1,0 -123:2:61,1 -9:5:1,4 -9:4:2,1 -# inf handling and general remainder -5:8:0,5 -0:8:0,0 -11:2:5,1 -11:-2:-6,-1 --11:2:-6,1 -# see table in documentation in MBI -0:inf:0,0 -0:-inf:0,0 -5:inf:0,5 -5:-inf:-1,-inf --5:inf:-1,inf --5:-inf:0,-5 -inf:5:inf,NaN --inf:5:-inf,NaN -inf:-5:-inf,NaN --inf:-5:inf,NaN -5:5:1,0 --5:-5:1,0 -inf:inf:NaN,NaN --inf:-inf:NaN,NaN --inf:inf:NaN,NaN -inf:-inf:NaN,NaN -8:0:inf,8 -inf:0:inf,inf -# exceptions to remainder rule --8:0:-inf,-8 --inf:0:-inf,-inf -0:0:NaN,0 -# test the shortcut in Calc if @$x == @$yorg -1234567812345678:123456712345678:10,688888898 -12345671234567:1234561234567:10,58888897 -123456123456:12345123456:10,4888896 -1234512345:123412345:10,388895 -1234567890999999999:1234567890:1000000000,999999999 -1234567890000000000:1234567890:1000000000,0 -1234567890999999999:9876543210:124999998,9503086419 -1234567890000000000:9876543210:124999998,8503086420 -96969696969696969696969696969678787878626262626262626262626262:484848484848484848484848486666666666666689898989898989898989:199,484848484848484848484848123012121211954972727272727272727451 -# bug in v1.76 -1267650600228229401496703205375:1267650600228229401496703205376:0,1267650600228229401496703205375 -# exercise shortcut for numbers of the same length in div -999999999999999999999999999999999:999999999999999999999999999999999:1,0 -999999999999999999999999999999999:888888888888888888888888888888888:1,111111111111111111111111111111111 -999999999999999999999999999999999:777777777777777777777777777777777:1,222222222222222222222222222222222 -999999999999999999999999999999999:666666666666666666666666666666666:1,333333333333333333333333333333333 -999999999999999999999999999999999:555555555555555555555555555555555:1,444444444444444444444444444444444 -999999999999999999999999999999999:444444444444444444444444444444444:2,111111111111111111111111111111111 -999999999999999999999999999999999:333333333333333333333333333333333:3,0 -999999999999999999999999999999999:222222222222222222222222222222222:4,111111111111111111111111111111111 -999999999999999999999999999999999:111111111111111111111111111111111:9,0 -9999999_9999999_9999999_9999999:3333333_3333333_3333333_3333333:3,0 -9999999_9999999_9999999_9999999:3333333_0000000_0000000_0000000:3,999999999999999999999 -9999999_9999999_9999999_9999999:3000000_0000000_0000000_0000000:3,999999999999999999999999999 -9999999_9999999_9999999_9999999:2000000_0000000_0000000_0000000:4,1999999999999999999999999999 -9999999_9999999_9999999_9999999:1000000_0000000_0000000_0000000:9,999999999999999999999999999 -9999999_9999999_9999999_9999999:100000_0000000_0000000_0000000:99,99999999999999999999999999 -9999999_9999999_9999999_9999999:10000_0000000_0000000_0000000:999,9999999999999999999999999 -9999999_9999999_9999999_9999999:1000_0000000_0000000_0000000:9999,999999999999999999999999 -9999999_9999999_9999999_9999999:100_0000000_0000000_0000000:99999,99999999999999999999999 -9999999_9999999_9999999_9999999:10_0000000_0000000_0000000:999999,9999999999999999999999 -9999999_9999999_9999999_9999999:1_0000000_0000000_0000000:9999999,999999999999999999999 -&bdiv -abc:abc:NaN -abc:1:NaN -1:abc:NaN -0:0:NaN -# inf handling (see table in doc) -0:inf:0 -0:-inf:0 -5:inf:0 -5:-inf:-1 --5:inf:-1 --5:-inf:0 -inf:5:inf --inf:5:-inf -inf:-5:-inf --inf:-5:inf -5:5:1 --5:-5:1 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:inf -inf:0:inf --8:0:-inf --inf:0:-inf -0:0:NaN -11:2:5 --11:-2:5 --11:2:-6 -11:-2:-6 -0:1:0 -0:-1:0 -1:1:1 --1:-1:1 -1:-1:-1 --1:1:-1 -1:2:0 -2:1:2 -1:26:0 -1000000000:9:111111111 -2000000000:9:222222222 -3000000000:9:333333333 -4000000000:9:444444444 -5000000000:9:555555555 -6000000000:9:666666666 -7000000000:9:777777777 -8000000000:9:888888888 -9000000000:9:1000000000 -35500000:113:314159 -71000000:226:314159 -106500000:339:314159 -1000000000:3:333333333 -+10:+5:2 -+100:+4:25 -+1000:+8:125 -+10000:+16:625 -999999999999:9:111111111111 -999999999999:99:10101010101 -999999999999:999:1001001001 -999999999999:9999:100010001 -999999999999999:99999:10000100001 -+1111088889:99999:11111 --5:-3:1 --5:3:-2 -4:3:1 -4:-3:-2 -1:3:0 -1:-3:-1 --2:-3:0 --2:3:-1 -8:3:2 --8:3:-3 -14:-3:-5 --14:3:-5 --14:-3:4 -14:3:4 -# bug in Calc with '99999' vs $BASE-1 -10000000000000000000000000000000000000000000000000000000000000000000000000000000000:10000000375084540248994272022843165711074:999999962491547381984643365663244474111576 -# test the shortcut in Calc if @$x == @$yorg -1234567812345678:123456712345678:10 -12345671234567:1234561234567:10 -123456123456:12345123456:10 -1234512345:123412345:10 -1234567890999999999:1234567890:1000000000 -1234567890000000000:1234567890:1000000000 -1234567890999999999:9876543210:124999998 -1234567890000000000:9876543210:124999998 -96969696969696969696969696969678787878626262626262626262626262:484848484848484848484848486666666666666689898989898989898989:199 -# bug up to v0.35 in Calc (--$q one too many) -84696969696969696956565656566184292929292929292847474747436308080808080808086765396464646464646465:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999999 -84696969696969696943434343434871161616161616161452525252486813131313131313143230042929292929292930:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999998 -84696969696969696969696969697497424242424242424242424242385803030303030303030300750000000000000000:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6450000000000000000 -84696969696969696930303030303558030303030303030057575757537318181818181818199694689393939393939395:13131313131313131313131313131394949494949494949494949494943535353535353535353535:6449999999999999997 -# exercise shortcut for numbers of the same length in div -999999999999999999999999999999999:999999999999999999999999999999999:1 -999999999999999999999999999999999:888888888888888888888888888888888:1 -999999999999999999999999999999999:777777777777777777777777777777777:1 -999999999999999999999999999999999:666666666666666666666666666666666:1 -999999999999999999999999999999999:555555555555555555555555555555555:1 -999999999999999999999999999999999:444444444444444444444444444444444:2 -999999999999999999999999999999999:333333333333333333333333333333333:3 -999999999999999999999999999999999:222222222222222222222222222222222:4 -999999999999999999999999999999999:111111111111111111111111111111111:9 -9999999_9999999_9999999_9999999:3333333_3333333_3333333_3333333:3 -9999999_9999999_9999999_9999999:3333333_0000000_0000000_0000000:3 -9999999_9999999_9999999_9999999:3000000_0000000_0000000_0000000:3 -9999999_9999999_9999999_9999999:2000000_0000000_0000000_0000000:4 -9999999_9999999_9999999_9999999:1000000_0000000_0000000_0000000:9 -9999999_9999999_9999999_9999999:100000_0000000_0000000_0000000:99 -9999999_9999999_9999999_9999999:10000_0000000_0000000_0000000:999 -9999999_9999999_9999999_9999999:1000_0000000_0000000_0000000:9999 -9999999_9999999_9999999_9999999:100_0000000_0000000_0000000:99999 -9999999_9999999_9999999_9999999:10_0000000_0000000_0000000:999999 -9999999_9999999_9999999_9999999:1_0000000_0000000_0000000:9999999 -# bug with shortcut in Calc 0.44 -949418181818187070707070707070707070:181818181853535353535353535353535353:5 -&bmodinv -# format: number:modulus:result -# bmodinv Data errors -abc:abc:NaN -abc:5:NaN -5:abc:NaN -# bmodinv Expected Results from normal use -1:5:1 -3:5:2 -3:-5:-3 --2:5:2 -8:5033:4404 -1234567891:13:6 --1234567891:13:7 -324958749843759385732954874325984357439658735983745:2348249874968739:1741662881064902 --2:1:0 --1:1:0 -0:1:0 -1:1:0 -2:1:0 -3:1:0 -4:1:0 --2:3:1 --1:3:2 -0:3:NaN -1:3:1 -2:3:2 -3:3:NaN -4:3:1 --2:4:NaN --1:4:3 -0:4:NaN -1:4:1 -2:4:NaN -3:4:3 -4:4:NaN -## bmodinv Error cases / useless use of function -inf:5:NaN -5:inf:NaN --inf:5:NaN -5:-inf:NaN -&bmodpow -# format: number:exponent:modulus:result -# bmodpow Data errors -abc:abc:abc:NaN -5:abc:abc:NaN -abc:5:abc:NaN -abc:abc:5:NaN -5:5:abc:NaN -5:abc:5:NaN -abc:5:5:NaN -3:5:0:3 -# bmodpow Expected results -0:0:2:1 -1:0:2:1 -0:3:5:0 --2:-2:1:0 --1:-2:1:0 -0:-2:1:0 -1:-2:1:0 -2:-2:1:0 -3:-2:1:0 -4:-2:1:0 --2:-1:1:0 --1:-1:1:0 -0:-1:1:0 -1:-1:1:0 -2:-1:1:0 -3:-1:1:0 -4:-1:1:0 --2:0:1:0 --1:0:1:0 -0:0:1:0 -1:0:1:0 -2:0:1:0 -3:0:1:0 -4:0:1:0 --2:1:1:0 --1:1:1:0 -0:1:1:0 -1:1:1:0 -2:1:1:0 -3:1:1:0 -4:1:1:0 --2:2:1:0 --1:2:1:0 -0:2:1:0 -1:2:1:0 -2:2:1:0 -3:2:1:0 -4:2:1:0 --2:3:1:0 --1:3:1:0 -0:3:1:0 -1:3:1:0 -2:3:1:0 -3:3:1:0 -4:3:1:0 --2:4:1:0 --1:4:1:0 -0:4:1:0 -1:4:1:0 -2:4:1:0 -3:4:1:0 -4:4:1:0 --2:-2:3:1 --1:-2:3:1 -0:-2:3:NaN -1:-2:3:1 -2:-2:3:1 -3:-2:3:NaN -4:-2:3:1 --2:-1:3:1 --1:-1:3:2 -0:-1:3:NaN -1:-1:3:1 -2:-1:3:2 -3:-1:3:NaN -4:-1:3:1 --2:0:3:1 --1:0:3:1 -0:0:3:1 -1:0:3:1 -2:0:3:1 -3:0:3:1 -4:0:3:1 --2:1:3:1 --1:1:3:2 -0:1:3:0 -1:1:3:1 -2:1:3:2 -3:1:3:0 -4:1:3:1 --2:2:3:1 --1:2:3:1 -0:2:3:0 -1:2:3:1 -2:2:3:1 -3:2:3:0 -4:2:3:1 --2:3:3:1 --1:3:3:2 -0:3:3:0 -1:3:3:1 -2:3:3:2 -3:3:3:0 -4:3:3:1 --2:4:3:1 --1:4:3:1 -0:4:3:0 -1:4:3:1 -2:4:3:1 -3:4:3:0 -4:4:3:1 --2:-2:4:NaN --1:-2:4:1 -0:-2:4:NaN -1:-2:4:1 -2:-2:4:NaN -3:-2:4:1 -4:-2:4:NaN --2:-1:4:NaN --1:-1:4:3 -0:-1:4:NaN -1:-1:4:1 -2:-1:4:NaN -3:-1:4:3 -4:-1:4:NaN --2:0:4:1 --1:0:4:1 -0:0:4:1 -1:0:4:1 -2:0:4:1 -3:0:4:1 -4:0:4:1 --2:1:4:2 --1:1:4:3 -0:1:4:0 -1:1:4:1 -2:1:4:2 -3:1:4:3 -4:1:4:0 --2:2:4:0 --1:2:4:1 -0:2:4:0 -1:2:4:1 -2:2:4:0 -3:2:4:1 -4:2:4:0 --2:3:4:0 --1:3:4:3 -0:3:4:0 -1:3:4:1 -2:3:4:0 -3:3:4:3 -4:3:4:0 --2:4:4:0 --1:4:4:1 -0:4:4:0 -1:4:4:1 -2:4:4:0 -3:4:4:1 -4:4:4:0 -8:-1:16:NaN -8:-1:5033:4404 -8:7:5032:3840 -8:8:-5:-4 -1e50:1:1:0 -98436739867439843769485798542749827593285729587325:43698764986460981048259837659386739857456983759328457:6943857329857295827698367:3104744730915914415259518 -# bmodpow Error cases -inf:5:13:NaN -5:inf:13:NaN -&bmod -# inf handling, see table in doc -0:inf:0 -0:-inf:0 -5:inf:5 -5:-inf:-inf --5:inf:inf --5:-inf:-5 -inf:5:NaN --inf:5:NaN -inf:-5:NaN --inf:-5:NaN -5:5:0 --5:-5:0 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:8 -inf:0:inf --inf:0:-inf --8:0:-8 -0:0:0 -abc:abc:NaN -abc:1:abc:NaN -1:abc:NaN -0:1:0 -1:0:1 -0:-1:0 --1:0:-1 -1:1:0 --1:-1:0 -1:-1:0 --1:1:0 -1:2:1 -2:1:0 -1000000000:9:1 -2000000000:9:2 -3000000000:9:3 -4000000000:9:4 -5000000000:9:5 -6000000000:9:6 -7000000000:9:7 -8000000000:9:8 -9000000000:9:0 -35500000:113:33 -71000000:226:66 -106500000:339:99 -1000000000:3:1 -10:5:0 -100:4:0 -1000:8:0 -10000:16:0 -999999999999:9:0 -999999999999:99:0 -999999999999:999:0 -999999999999:9999:0 -999999999999999:99999:0 --9:+5:1 -+9:-5:-1 --9:-5:-4 --5:3:1 --2:3:1 -4:3:1 -1:3:1 --5:-3:-2 --2:-3:-2 -4:-3:-2 -1:-3:-2 -4095:4095:0 -100041000510123:3:0 -152403346:12345:4321 -9:5:4 -# test shortcuts in Calc -# 1ex % 9 is always == 1, 1ex % 113 is != 1 for x = (4..9), 1ex % 10 = 0 -1234:9:1 -123456:9:3 -12345678:9:0 -1234567891:9:1 -123456789123:9:6 -12345678912345:9:6 -1234567891234567:9:1 -123456789123456789:9:0 -1234:10:4 -123456:10:6 -12345678:10:8 -1234567891:10:1 -123456789123:10:3 -12345678912345:10:5 -1234567891234567:10:7 -123456789123456789:10:9 -1234:113:104 -123456:113:60 -12345678:113:89 -1234567891:113:64 -123456789123:113:95 -12345678912345:113:53 -1234567891234567:113:56 -123456789123456789:113:39 -# bug in bmod() not modifying the variable in place --629:5033:4404 -# bug in bmod() in Calc in the _div_use_div() shortcut code path, -# when X == X and X was big -111111111111111111111111111111:111111111111111111111111111111:0 -12345678901234567890:12345678901234567890:0 -&bgcd -inf:12:NaN --inf:12:NaN -12:inf:NaN -12:-inf:NaN -inf:inf:NaN -inf:-inf:NaN --inf:-inf:NaN -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:0 -+0:+1:1 -+1:+0:1 -+1:+1:1 -+2:+3:1 -+3:+2:1 --3:+2:1 --3:-2:1 --144:-60:12 -144:-60:12 -144:60:12 -100:625:25 -4096:81:1 -1034:804:2 -27:90:56:1 -27:90:54:9 -&blcm -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:NaN -+1:+0:0 -+0:+1:0 -+27:+90:270 -+1034:+804:415668 -&band -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:0 -3:2:2 -+8:+2:0 -+281474976710656:0:0 -+281474976710656:1:0 -+281474976710656:+281474976710656:281474976710656 -281474976710656:-1:281474976710656 --2:-3:-4 --1:-1:-1 --6:-6:-6 --7:-4:-8 --7:4:0 --4:7:4 -# negative argument is bitwise shorter than positive [perl #26559] -30:-3:28 -123:-1:123 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0x0xFFFF -0xFFFFFF:0xFFFFFF:0x0xFFFFFF -0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF -0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0x0xF0F0 -0x0F0F:0x0F0F:0x0x0F0F -0xF0F0F0:0xF0F0F0:0x0xF0F0F0 -0x0F0F0F:0x0F0F0F:0x0x0F0F0F -0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 -0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F -0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F -0x1F0F0F0F0F0F:0x3F0F0F0F0F0F:0x0x1F0F0F0F0F0F -&bior -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:3 -+8:+2:10 -+281474976710656:0:281474976710656 -+281474976710656:1:281474976710657 -+281474976710656:281474976710656:281474976710656 --2:-3:-1 --1:-1:-1 --6:-6:-6 --7:4:-3 --4:7:-1 -+281474976710656:-1:-1 -30:-3:-1 -30:-4:-2 -300:-76:-68 --76:300:-68 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0x0xFFFF -0xFFFFFF:0xFFFFFF:0x0xFFFFFF -0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF -0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0:0xFFFF:0x0xFFFF -0:0xFFFFFF:0x0xFFFFFF -0:0xFFFFFFFF:0x0xFFFFFFFF -0:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xFFFF:0:0x0xFFFF -0xFFFFFF:0:0x0xFFFFFF -0xFFFFFFFF:0:0x0xFFFFFFFF -0xFFFFFFFFFF:0:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0x0xF0F0 -0x0F0F:0x0F0F:0x0x0F0F -0xF0F0:0x0F0F:0x0xFFFF -0xF0F0F0:0xF0F0F0:0x0xF0F0F0 -0x0F0F0F:0x0F0F0F:0x0x0F0F0F -0x0F0F0F:0xF0F0F0:0x0xFFFFFF -0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 -0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F -0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF -0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F -0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F -0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -0x1F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -&bxor -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:3 -+8:+2:10 -+281474976710656:0:281474976710656 -+281474976710656:1:281474976710657 -+281474976710656:281474976710656:0 --2:-3:3 --1:-1:0 --6:-6:0 --7:4:-3 --4:7:-5 -4:-7:-3 --4:-7:5 -30:-3:-29 -30:-4:-30 -300:-76:-360 --76:300:-360 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0 -0xFFFFFF:0xFFFFFF:0 -0xFFFFFFFF:0xFFFFFFFF:0 -0xFFFFFFFFFF:0xFFFFFFFFFF:0 -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0 -0:0xFFFF:0x0xFFFF -0:0xFFFFFF:0x0xFFFFFF -0:0xFFFFFFFF:0x0xFFFFFFFF -0:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xFFFF:0:0x0xFFFF -0xFFFFFF:0:0x0xFFFFFF -0xFFFFFFFF:0:0x0xFFFFFFFF -0xFFFFFFFFFF:0:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0 -0x0F0F:0x0F0F:0 -0xF0F0:0x0F0F:0x0xFFFF -0xF0F0F0:0xF0F0F0:0 -0x0F0F0F:0x0F0F0F:0 -0x0F0F0F:0xF0F0F0:0x0xFFFFFF -0xF0F0F0F0:0xF0F0F0F0:0 -0x0F0F0F0F:0x0F0F0F0F:0 -0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF -0xF0F0F0F0F0:0xF0F0F0F0F0:0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0 -0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0 -0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -&bnot -abc:NaN -+0:-1 -+8:-9 -+281474976710656:-281474976710657 --1:0 --2:1 --12:11 -&digit -0:0:0 -12:0:2 -12:1:1 -123:0:3 -123:1:2 -123:2:1 -123:-1:1 -123:-2:2 -123:-3:3 -123456:0:6 -123456:1:5 -123456:2:4 -123456:3:3 -123456:4:2 -123456:5:1 -123456:-1:1 -123456:-2:2 -123456:-3:3 -100000:-3:0 -100000:0:0 -100000:1:0 -&mantissa -abc:NaN -1e4:1 -2e0:2 -123:123 --1:-1 --2:-2 -+inf:inf --inf:-inf -&exponent -abc:NaN -1e4:4 -2e0:0 -123:0 --1:0 --2:0 -0:1 -+inf:inf --inf:inf -&parts -abc:NaN,NaN -1e4:1,4 -2e0:2,0 -123:123,0 --1:-1,0 --2:-2,0 -0:0,1 -+inf:inf,inf --inf:-inf,inf -&bfac --1:NaN -NaNfac:NaN -+inf:inf --inf:NaN -0:1 -1:1 -2:2 -3:6 -4:24 -5:120 -6:720 -7:5040 -8:40320 -9:362880 -10:3628800 -11:39916800 -12:479001600 -20:2432902008176640000 -22:1124000727777607680000 -69:171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000 -&bpow -abc:12:NaN -12:abc:NaN -0:0:1 -0:1:0 -0:2:0 -0:-1:inf -0:-2:inf -1:0:1 -1:1:1 -1:2:1 -1:3:1 -1:-1:1 -1:-2:1 -1:-3:1 -2:0:1 -2:1:2 -2:2:4 -2:3:8 -3:3:27 --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 -2:-1:NaN --2:-1:NaN -2:-2:NaN --2:-2:NaN -# inf tests -+inf:1234500012:inf --inf:1234500012:inf --inf:1234500013:-inf -+inf:-12345000123:inf --inf:-12345000123:-inf -# -inf * -inf = inf --inf:2:inf --inf:0:NaN --inf:-1:0 --inf:inf:NaN -2:inf:inf -2:-inf:0 -0:inf:0 -0:-inf:inf --1:-inf:NaN --1:inf:NaN --2:inf:NaN --2:-inf:0 -NaN:inf:NaN -NaN:-inf:NaN --inf:NaN:NaN -inf:NaN:NaN -inf:-inf:NaN -1:inf:1 -1:-inf:1 -# 1 ** -x => 1 / (1 ** x) --1:0:1 --2:0:1 --1:1:-1 --1:2:1 --1:3:-1 --1:4:1 --1:5:-1 --1:-1:-1 --1:-2:1 --1:-3:-1 --1:-4:1 -10:2:100 -10:3:1000 -10:4:10000 -10:5:100000 -10:6:1000000 -10:7:10000000 -10:8:100000000 -10:9:1000000000 -10:20:100000000000000000000 -123456:2:15241383936 --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 --3:2:9 --3:3:-27 --3:4:81 --3:5:-243 -&length -100:3 -10:2 -1:1 -0:1 -12345:5 -10000000000000000:17 --123:3 -215960156869840440586892398248:30 -&broot -# sqrt() -+0:2:0 -+1:2:1 --1:2:NaN -# -$x ** (1/2) => -$y, but not in froot() --123:2:NaN -+inf:2:inf --inf:2:NaN -2:2:1 --2:2:NaN -4:2:2 -9:2:3 -16:2:4 -100:2:10 -123:2:11 -15241:2:123 -144:2:12 -12:2:3 -# invalid ones -1:NaN:NaN --1:NaN:NaN -0:NaN:NaN --inf:NaN:NaN -+inf:NaN:NaN -NaN:0:NaN -NaN:2:NaN -NaN:inf:NaN -NaN:inf:NaN -12:-inf:NaN -12:inf:NaN -+0:0:NaN -+1:0:NaN --1:0:NaN --2:0:NaN --123.45:0:NaN -+inf:0:NaN -12:1:12 --12:1:NaN -8:-1:NaN --8:-1:NaN -# cubic root -8:3:2 --8:3:NaN -# fourths root -16:4:2 -81:4:3 -# 2 ** 64 -18446744073709551616:4:65536 -18446744073709551616:8:256 -18446744073709551616:16:16 -18446744073709551616:32:4 -18446744073709551616:64:2 -18446744073709551616:128:1 -# 213 ** 15 -84274086103068221283760416414557757:15:213 -# see t/bigroot.t for more tests -&bsqrt -145:12 -144:12 -143:11 -16:4 -170:13 -169:13 -168:12 -4:2 -3:1 -2:1 -9:3 -12:3 -256:16 -100000000:10000 -4000000000000:2000000 -152399026:12345 -152399025:12345 -152399024:12344 -# 2 ** 64 => 2 ** 32 -18446744073709551616:4294967296 -84274086103068221283760416414557757:290299993288095377 -1:1 -0:0 --2:NaN --123:NaN -Nan:NaN -+inf:inf --inf:NaN -# see t/biglog.t for more tests -&bexp -NaN:NaN -inf:inf -1:2 -2:7 -&batan2 -NaN:1:10:NaN -NaN:NaN:10:NaN -1:NaN:10:NaN -inf:1:14:1 --inf:1:14:-1 -0:-inf:14:3 --1:-inf:14:-3 -1:-inf:14:3 -0:inf:14:0 -inf:-inf:14:2 --inf:-inf:14:-2 -# +- 0.78.... -inf:+inf:14:0 --inf:+inf:14:0 -1:5:13:0 -1:5:14:0 -0:0:10:0 -0:1:14:0 -0:2:14:0 -1:0:14:1 -5:0:14:1 --1:0:11:-1 --2:0:77:-1 -2:0:77:1 --1:5:14:0 -1:5:14:0 --1:8:14:0 -1:8:14:0 --1:1:14:0 -&bpi -77:3 -+0:3 -11:3 -# see t/bignok.t for more tests -&bnok -+inf:10:inf -NaN:NaN:NaN -NaN:1:NaN -1:NaN:NaN -1:1:1 -# k > n -1:2:0 -2:3:0 -# k < 0 -1:-2:0 -# 7 over 3 = 35 -7:3:35 -7:6:7 -100:90:17310309456440 -100:95:75287520 -2:0:1 -7:0:1 -2:1:2 -&bround -$round_mode('trunc') -0:12:0 -NaNbround:12:NaN -+inf:12:inf --inf:12:-inf -1234:0:1234 -1234:2:1200 -123456:4:123400 -123456:5:123450 -123456:6:123456 -+10123456789:5:10123000000 --10123456789:5:-10123000000 -+10123456789:9:10123456700 --10123456789:9:-10123456700 -+101234500:6:101234000 --101234500:6:-101234000 -#+101234500:-4:101234000 -#-101234500:-4:-101234000 -$round_mode('zero') -+20123456789:5:20123000000 --20123456789:5:-20123000000 -+20123456789:9:20123456800 --20123456789:9:-20123456800 -+201234500:6:201234000 --201234500:6:-201234000 -#+201234500:-4:201234000 -#-201234500:-4:-201234000 -+12345000:4:12340000 --12345000:4:-12340000 -$round_mode('+inf') -+30123456789:5:30123000000 --30123456789:5:-30123000000 -+30123456789:9:30123456800 --30123456789:9:-30123456800 -+301234500:6:301235000 --301234500:6:-301234000 -#+301234500:-4:301235000 -#-301234500:-4:-301234000 -+12345000:4:12350000 --12345000:4:-12340000 -$round_mode('-inf') -+40123456789:5:40123000000 --40123456789:5:-40123000000 -+40123456789:9:40123456800 --40123456789:9:-40123456800 -+401234500:6:401234000 -+401234500:6:401234000 -#-401234500:-4:-401235000 -#-401234500:-4:-401235000 -+12345000:4:12340000 --12345000:4:-12350000 -$round_mode('odd') -+50123456789:5:50123000000 --50123456789:5:-50123000000 -+50123456789:9:50123456800 --50123456789:9:-50123456800 -+501234500:6:501235000 --501234500:6:-501235000 -#+501234500:-4:501235000 -#-501234500:-4:-501235000 -+12345000:4:12350000 --12345000:4:-12350000 -$round_mode('even') -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601234000 --601234500:6:-601234000 -#+601234500:-4:601234000 -#-601234500:-4:-601234000 -#-601234500:-9:0 -#-501234500:-9:0 -#-601234500:-8:0 -#-501234500:-8:0 -+1234567:7:1234567 -+1234567:6:1234570 -+12345000:4:12340000 --12345000:4:-12340000 -$round_mode('common') -+60123456789:5:60123000000 -+60123199999:5:60123000000 -+60123299999:5:60123000000 -+60123399999:5:60123000000 -+60123499999:5:60123000000 -+60123500000:5:60124000000 -+60123600000:5:60124000000 -+60123700000:5:60124000000 -+60123800000:5:60124000000 -+60123900000:5:60124000000 --60123456789:5:-60123000000 --60123199999:5:-60123000000 --60123299999:5:-60123000000 --60123399999:5:-60123000000 --60123499999:5:-60123000000 --60123500000:5:-60124000000 --60123600000:5:-60124000000 --60123700000:5:-60124000000 --60123800000:5:-60124000000 --60123900000:5:-60124000000 -&is_zero -0:1 -NaNzero:0 -+inf:0 --inf:0 -123:0 --1:0 -1:0 -&is_one -0:0 -NaNone:0 -+inf:0 --inf:0 -1:1 -2:0 --1:0 --2:0 -# floor, ceil, and int are pretty pointless in integer space, but play safe -&bfloor -0:0 -NaNfloor:NaN -+inf:inf --inf:-inf --1:-1 --2:-2 -2:2 -3:3 -abc:NaN -&bceil -NaNceil:NaN -+inf:inf --inf:-inf -0:0 --1:-1 --2:-2 -2:2 -3:3 -abc:NaN -&bint -NaN:NaN -+inf:inf --inf:-inf -0:0 --1:-1 --2:-2 -2:2 -3:3 -&as_hex -128:0x80 --128:-0x80 -0:0x0 --0:0x0 -1:0x1 -0x123456789123456789:0x123456789123456789 -+inf:inf --inf:-inf -NaNas_hex:NaN -&as_bin -128:0b10000000 --128:-0b10000000 -0:0b0 --0:0b0 -1:0b1 -0b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101 -0x123456789123456789:0b100100011010001010110011110001001000100100011010001010110011110001001 -+inf:inf --inf:-inf -NaNas_bin:NaN diff --git a/dist/Math-BigInt/t/bigintpm.t b/dist/Math-BigInt/t/bigintpm.t deleted file mode 100644 index a03710edf5..0000000000 --- a/dist/Math-BigInt/t/bigintpm.t +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 3649 + 6; - -use Math::BigInt lib => 'Calc'; - -use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigInt"; -$CL = "Math::BigInt::Calc"; - -############################################################################# -# from_hex(), from_bin() and from_oct() tests - -my $x = Math::BigInt->from_hex('0xcafe'); -is ($x, "51966", 'from_hex() works'); - -$x = Math::BigInt->from_hex('0xcafebabedead'); -is ($x, "223195403574957", 'from_hex() works with long numbers'); - -$x = Math::BigInt->from_bin('0b1001'); -is ($x, "9", 'from_bin() works'); - -$x = Math::BigInt->from_bin('0b1001100110011001100110011001'); -is ($x, "161061273", 'from_bin() works with big numbers'); - -$x = Math::BigInt->from_oct('0775'); -is ($x, "509", 'from_oct() works'); - -$x = Math::BigInt->from_oct('07777777777777711111111222222222'); -is ($x, "9903520314281112085086151826", 'from_oct() works with big numbers'); - -############################################################################# -# all the other tests - -require 't/bigintpm.inc'; # all tests here for sharing diff --git a/dist/Math-BigInt/t/bigints.t b/dist/Math-BigInt/t/bigints.t deleted file mode 100644 index a61696877b..0000000000 --- a/dist/Math-BigInt/t/bigints.t +++ /dev/null @@ -1,99 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 51; - -BEGIN { unshift @INC, 't'; } - -# testing of Math::BigInt:Scalar (used by the testsuite), -# primarily for interface/api and not for the math functionality - -use Math::BigInt::Scalar; - -my $C = 'Math::BigInt::Scalar'; # pass classname to sub's - -# _new and _str -my $x = $C->_new("123"); my $y = $C->_new("321"); -is (ref($x),'SCALAR'); is ($C->_str($x),123); is ($C->_str($y),321); - -# _add, _sub, _mul, _div - -is ($C->_str($C->_add($x,$y)),444); -is ($C->_str($C->_sub($x,$y)),123); -is ($C->_str($C->_mul($x,$y)),39483); -is ($C->_str($C->_div($x,$y)),123); - -is ($C->_str($C->_mul($x,$y)),39483); -is ($C->_str($x),39483); -is ($C->_str($y),321); -my $z = $C->_new("2"); -is ($C->_str($C->_add($x,$z)),39485); -my ($re,$rr) = $C->_div($x,$y); - -is ($C->_str($re),123); is ($C->_str($rr),2); - -# is_zero, _is_one, _one, _zero -is ($C->_is_zero($x),0); -is ($C->_is_one($x),0); - -is ($C->_is_one($C->_one()),1); is ($C->_is_one($C->_zero()),0); -is ($C->_is_zero($C->_zero()),1); is ($C->_is_zero($C->_one()),0); - -# is_odd, is_even -is ($C->_is_odd($C->_one()),1); is ($C->_is_odd($C->_zero()),0); -is ($C->_is_even($C->_one()),0); is ($C->_is_even($C->_zero()),1); - -# _digit -$x = $C->_new("123456789"); -is ($C->_digit($x,0),9); -is ($C->_digit($x,1),8); -is ($C->_digit($x,2),7); -is ($C->_digit($x,-1),1); -is ($C->_digit($x,-2),2); -is ($C->_digit($x,-3),3); - -# _copy -$x = $C->_new("12356"); -is ($C->_str($C->_copy($x)),12356); - -# _acmp -$x = $C->_new("123456789"); -$y = $C->_new("987654321"); -is ($C->_acmp($x,$y),-1); -is ($C->_acmp($y,$x),1); -is ($C->_acmp($x,$x),0); -is ($C->_acmp($y,$y),0); - -# _div -$x = $C->_new("3333"); $y = $C->_new("1111"); -is ($C->_str( scalar $C->_div($x,$y)),3); -$x = $C->_new("33333"); $y = $C->_new("1111"); ($x,$y) = $C->_div($x,$y); -is ($C->_str($x),30); is ($C->_str($y),3); -$x = $C->_new("123"); $y = $C->_new("1111"); -($x,$y) = $C->_div($x,$y); is ($C->_str($x),0); is ($C->_str($y),123); - -# _num -$x = $C->_new("12345"); $x = $C->_num($x); is (ref($x)||'',''); is ($x,12345); - -# _len -$x = $C->_new("12345"); $x = $C->_len($x); is (ref($x)||'',''); is ($x,5); - -# _and, _or, _xor -$x = $C->_new("3"); $y = $C->_new("4"); is ($C->_str( $C->_or($x,$y)),7); -$x = $C->_new("1"); $y = $C->_new("4"); is ($C->_str( $C->_xor($x,$y)),5); -$x = $C->_new("7"); $y = $C->_new("3"); is ($C->_str( $C->_and($x,$y)),3); - -# _pow -$x = $C->_new("2"); $y = $C->_new("4"); is ($C->_str( $C->_pow($x,$y)),16); -$x = $C->_new("2"); $y = $C->_new("5"); is ($C->_str( $C->_pow($x,$y)),32); -$x = $C->_new("3"); $y = $C->_new("3"); is ($C->_str( $C->_pow($x,$y)),27); - - -# _check -$x = $C->_new("123456789"); -is ($C->_check($x),0); -is ($C->_check(123),'123 is not a reference'); - -# done - -1; diff --git a/dist/Math-BigInt/t/biglog.t b/dist/Math-BigInt/t/biglog.t deleted file mode 100644 index 7c3b618ce3..0000000000 --- a/dist/Math-BigInt/t/biglog.t +++ /dev/null @@ -1,187 +0,0 @@ -#!/usr/bin/perl -w - -# Test blog function (and bpow, since it uses blog), as well as bexp(). - -# It is too slow to be simple included in bigfltpm.inc, where it would get -# executed 3 times. One time would be under BareCalc, which shouldn't make any -# difference since there is no CALC->_log() function, and one time under a -# subclass, which *should* work. - -# But it is better to test the numerical functionality, instead of not testing -# it at all (which did lead to wrong answers for 0 < $x < 1 in blog() in -# versions up to v1.63, and for bsqrt($x) when $x << 1 for instance). - -use strict; -use Test::More tests => 70; - -use Math::BigFloat; -use Math::BigInt; - -my $cl = "Math::BigInt"; - -############################################################################# -# test log($n) in BigInt (broken until 1.80) - -is ($cl->new(2)->blog(), '0', "blog(2)"); -is ($cl->new(288)->blog(), '5',"blog(288)"); -is ($cl->new(2000)->blog(), '7', "blog(2000)"); - -############################################################################# -# test exp($n) in BigInt - -is ($cl->new(1)->bexp(), '2', "bexp(1)"); -is ($cl->new(2)->bexp(), '7',"bexp(2)"); -is ($cl->new(3)->bexp(), '20', "bexp(3)"); - -############################################################################# -############################################################################# -# BigFloat tests - -############################################################################# -# test log(2, N) where N > 67 (broken until 1.82) - -$cl = "Math::BigFloat"; - -# These tests can take quite a while, but are nec. Maybe protect them with -# some alarm()? - -# this triggers the calculation and caching of ln(2): -is ($cl->new(5)->blog(undef,71), -'1.6094379124341003746007593332261876395256013542685177219126478914741790'); - -# if the cache was correct, we should get this result, fast: -is ($cl->new(2)->blog(undef,71), -'0.69314718055994530941723212145817656807550013436025525412068000949339362'); - -is ($cl->new(11)->blog(undef,71), -'2.3978952727983705440619435779651292998217068539374171752185677091305736'); - -is ($cl->new(21)->blog(undef,71), -'3.0445224377234229965005979803657054342845752874046106401940844835750742'); - -############################################################################# - -# These tests are now really fast, since they collapse to blog(10), basically -# Don't attempt to run them with older versions. You are warned. - -# $x < 0 => NaN -is ($cl->new(-2)->blog(), 'NaN'); -is ($cl->new(-1)->blog(), 'NaN'); -is ($cl->new(-10)->blog(), 'NaN'); -is ($cl->new(-2,2)->blog(), 'NaN'); - -my $ten = $cl->new(10)->blog(); - -# 10 is cached (up to 75 digits) -is ($cl->new(10)->blog(), '2.302585092994045684017991454684364207601'); - -# 0.1 is using the cached value for log(10), too - -is ($cl->new(0.1)->blog(), -$ten); -is ($cl->new(0.01)->blog(), -$ten * 2); -is ($cl->new(0.001)->blog(), -$ten * 3); -is ($cl->new(0.0001)->blog(), -$ten * 4); - -# also cached -is ($cl->new(2)->blog(), '0.6931471805599453094172321214581765680755'); -is ($cl->new(4)->blog(), $cl->new(2)->blog * 2); - -# These are still slow, so do them only to 10 digits - -is ($cl->new('0.2')->blog(undef,10), '-1.609437912'); -is ($cl->new('0.3')->blog(undef,10), '-1.203972804'); -is ($cl->new('0.4')->blog(undef,10), '-0.9162907319'); -is ($cl->new('0.5')->blog(undef,10), '-0.6931471806'); -is ($cl->new('0.6')->blog(undef,10), '-0.5108256238'); -is ($cl->new('0.7')->blog(undef,10), '-0.3566749439'); -is ($cl->new('0.8')->blog(undef,10), '-0.2231435513'); -is ($cl->new('0.9')->blog(undef,10), '-0.1053605157'); - -is ($cl->new('9')->blog(undef,10), '2.197224577'); - -is ($cl->new('10')->blog(10,10), '1.000000000'); -is ($cl->new('20')->blog(20,10), '1.000000000'); -is ($cl->new('100')->blog(100,10), '1.000000000'); - -is ($cl->new('100')->blog(10,10), '2.000000000'); # 10 ** 2 == 100 -is ($cl->new('400')->blog(20,10), '2.000000000'); # 20 ** 2 == 400 - -is ($cl->new('4')->blog(2,10), '2.000000000'); # 2 ** 2 == 4 -is ($cl->new('16')->blog(2,10), '4.000000000'); # 2 ** 4 == 16 - -is ($cl->new('1.2')->bpow('0.3',10), '1.056219968'); -is ($cl->new('10')->bpow('0.6',10), '3.981071706'); - -# blog should handle bigint input -is (Math::BigFloat::blog(Math::BigInt->new(100),10), 2, "blog(100)"); - -############################################################################# -# some integer results -is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32"); -is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32"); -is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65"); - -my $x = Math::BigInt->new( '777' ) ** 256; -my $base = Math::BigInt->new( '12345678901234' ); -is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)'); - -$x = Math::BigInt->new( '777' ) ** 777; -$base = Math::BigInt->new( '777' ); -is ($x->copy()->blog($base), 777, 'blog(777**777, 777)'); - -############################################################################# -# test for bug in bsqrt() not taking negative _e into account -test_bpow ('200','0.5',10, '14.14213562'); -test_bpow ('20','0.5',10, '4.472135955'); -test_bpow ('2','0.5',10, '1.414213562'); -test_bpow ('0.2','0.5',10, '0.4472135955'); -test_bpow ('0.02','0.5',10, '0.1414213562'); -test_bpow ('0.49','0.5',undef , '0.7'); -test_bpow ('0.49','0.5',10 , '0.7000000000'); -test_bpow ('0.002','0.5',10, '0.04472135955'); -test_bpow ('0.0002','0.5',10, '0.01414213562'); -test_bpow ('0.0049','0.5',undef,'0.07'); -test_bpow ('0.0049','0.5',10 , '0.07000000000'); -test_bpow ('0.000002','0.5',10, '0.001414213562'); -test_bpow ('0.021','0.5',10, '0.1449137675'); -test_bpow ('1.2','0.5',10, '1.095445115'); -test_bpow ('1.23','0.5',10, '1.109053651'); -test_bpow ('12.3','0.5',10, '3.507135583'); - -test_bpow ('9.9','0.5',10, '3.146426545'); -test_bpow ('9.86902225','0.5',10, '3.141500000'); -test_bpow ('9.86902225','0.5',undef, '3.1415'); - -test_bpow ('0.2','0.41',10, '0.5169187652'); - -############################################################################# -# test bexp() with cached results - -is ($cl->new(1)->bexp(), '2.718281828459045235360287471352662497757', 'bexp(1)'); -is ($cl->new(2)->bexp(40), $cl->new(1)->bexp(45)->bpow(2,40), 'bexp(2)'); - -is ($cl->new("12.5")->bexp(61), $cl->new(1)->bexp(65)->bpow(12.5,61), 'bexp(12.5)'); - -############################################################################# -# test bexp() with big values (non-cached) - -is ($cl->new(1)->bexp(100), - '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', - 'bexp(100)'); - -is ($cl->new("12.5")->bexp(91), $cl->new(1)->bexp(95)->bpow(12.5,91), - 'bexp(12.5) to 91 digits'); - -# all done -1; - -############################################################################# -sub test_bpow - { - my ($x,$y,$scale,$result) = @_; - - print "# Tried: $x->bpow($y,$scale);\n" - unless ok ($cl->new($x)->bpow($y,$scale),$result); - } - - diff --git a/dist/Math-BigInt/t/bigroot.t b/dist/Math-BigInt/t/bigroot.t deleted file mode 100644 index c90d5ae9af..0000000000 --- a/dist/Math-BigInt/t/bigroot.t +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/perl -w - -# Test broot function (and bsqrt() function, since it is used by broot()). - -# It is too slow to be simple included in bigfltpm.inc, where it would get -# executed 3 times. - -# But it is better to test the numerical functionality, instead of not testing -# it at all. - -use strict; -use Test::More tests => 4 * 2; - -use Math::BigFloat; -use Math::BigInt; - -my $cl = "Math::BigFloat"; -my $c = "Math::BigInt"; - -# 2 ** 240 = -# 1766847064778384329583297500742918515827483896875618958121606201292619776 - -# takes way too long -#test_broot ('2','240', 8, undef, '1073741824'); -#test_broot ('2','240', 9, undef, '106528681.3099908308759836475139583940127'); -#test_broot ('2','120', 9, undef, '10321.27324073880096577298929482324664787'); -#test_broot ('2','120', 17, undef, '133.3268493632747279600707813049418888729'); - -test_broot ('2','120', 8, undef, '32768'); -test_broot ('2','60', 8, undef, '181.0193359837561662466161566988413540569'); -test_broot ('2','60', 9, undef, '101.5936673259647663841091609134277286651'); -test_broot ('2','60', 17, undef, '11.54672461623965153271017217302844672562'); - -sub test_broot - { - my ($x,$n,$y,$scale,$result) = @_; - - my $s = $scale || 'undef'; - is ($cl->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $cl $x->bpow($n)->broot($y,$s) == $result"); - $result =~ s/\..*//; - is ($c->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $c $x->bpow($n)->broot($y,$s) == $result"); - } - diff --git a/dist/Math-BigInt/t/calling.t b/dist/Math-BigInt/t/calling.t deleted file mode 100644 index 6cdb4ac92f..0000000000 --- a/dist/Math-BigInt/t/calling.t +++ /dev/null @@ -1,149 +0,0 @@ -#!/usr/bin/perl -w - -# test calling conventions, and :constant overloading - -use strict; -use Test::More tests => 160; - -BEGIN { unshift @INC, 't'; } - -package Math::BigInt::Test; - -use Math::BigInt; -use vars qw/@ISA/; -@ISA = qw/Math::BigInt/; # child of MBI -use overload; - -package Math::BigFloat::Test; - -use Math::BigFloat; -use vars qw/@ISA/; -@ISA = qw/Math::BigFloat/; # child of MBI -use overload; - -package main; - -use Math::BigInt try => 'Calc'; -use Math::BigFloat; - -my ($x,$y,$z,$u); -my $version = '1.76'; # adjust manually to match latest release - -############################################################################### -# check whether op's accept normal strings, even when inherited by subclasses - -# do one positive and one negative test to avoid false positives by "accident" - -my ($func,@args,$ans,$rc,$class,$try); -while () - { - $_ =~ s/[\n\r]//g; # remove newlines - next if /^#/; # skip comments - if (s/^&//) - { - $func = $_; - } - else - { - @args = split(/:/,$_,99); - $ans = pop @args; - foreach $class (qw/ - Math::BigInt Math::BigFloat Math::BigInt::Test Math::BigFloat::Test/) - { - $try = "'$args[0]'"; # quote it - $try = $args[0] if $args[0] =~ /'/; # already quoted - $try = '' if $args[0] eq ''; # undef, no argument - $try = "$class\->$func($try);"; - $rc = eval $try; - print "# Tried: '$try'\n" if !is ($rc, $ans); - } - } - - } - -$class = 'Math::BigInt'; - -# XXX TODO this test does not work/fail. -# test whether use Math::BigInt qw/version/ works -#$try = "use $class ($version.'1');"; -#$try .= ' $x = $class->new(123); $x = "$x";'; -#eval $try; -#is ( $x, undef ); # should result in error! - -# test whether fallback to calc works -$try = "use $class ($version,'try','foo, bar , ');"; -$try .= "$class\->config()->{lib};"; -$ans = eval $try; -like ( $ans, qr/^Math::BigInt::(Fast)?Calc\z/); - -# test whether constant works or not, also test for qw($version) -# bgcd() is present in subclass, too -$try = "use Math::BigInt ($version,'bgcd',':constant');"; -$try .= ' $x = 2**150; bgcd($x); $x = "$x";'; -$ans = eval $try; -is ( $ans, "1427247692705959881058285969449495136382746624"); - -# test whether Math::BigInt::Scalar via use works (w/ dff. spellings of calc) -$try = "use $class ($version,'lib','Scalar');"; -$try .= ' $x = 2**10; $x = "$x";'; -$ans = eval $try; is ( $ans, "1024"); -$try = "use $class ($version,'lib','$class\::Scalar');"; -$try .= ' $x = 2**10; $x = "$x";'; -$ans = eval $try; is ( $ans, "1024"); - -# all done - -__END__ -&is_zero -1:0 -0:1 -&is_one -1:1 -0:0 -&is_positive -1:1 --1:0 -&is_negative -1:0 --1:1 -&is_nan -abc:1 -1:0 -&is_inf -inf:1 -0:0 -&bstr -5:5 -10:10 --10:-10 -abc:NaN -'+inf':inf -'-inf':-inf -&bsstr -1:1e+0 -0:0e+1 -2:2e+0 -200:2e+2 --5:-5e+0 --100:-1e+2 -abc:NaN -'+inf':inf -&babs --1:1 -1:1 -&bnot --2:1 -1:-2 -&bzero -:0 -&bnan -:NaN -abc:NaN -&bone -:1 -'+':1 -'-':-1 -&binf -:inf -'+':inf -'-':-inf diff --git a/dist/Math-BigInt/t/config.t b/dist/Math-BigInt/t/config.t deleted file mode 100644 index 2d079b99ec..0000000000 --- a/dist/Math-BigInt/t/config.t +++ /dev/null @@ -1,128 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 55; - -# test whether Math::BigInt->config() and Math::BigFloat->config() works - -use Math::BigInt lib => 'Calc'; -use Math::BigFloat; - -my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat'; - -############################################################################## -# BigInt - -ok ($mbi->can('config')); - -my $cfg = $mbi->config(); - -ok (ref($cfg),'HASH'); - -is ($cfg->{lib},'Math::BigInt::Calc', 'lib'); -is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version'); -is ($cfg->{class},$mbi,'class'); -is ($cfg->{upgrade}||'','', 'upgrade'); -is ($cfg->{div_scale},40, 'div_Scale'); - -is ($cfg->{precision}||0,0, 'precision'); # should test for undef -is ($cfg->{accuracy}||0,0,'accuracy'); -is ($cfg->{round_mode},'even','round_mode'); - -is ($cfg->{trap_nan},0, 'trap_nan'); -is ($cfg->{trap_inf},0, 'trap_inf'); - -is ($mbi->config('lib'), 'Math::BigInt::Calc', 'config("lib")'); - -# can set via hash ref? -$cfg = $mbi->config( { trap_nan => 1 } ); -is ($cfg->{trap_nan},1, 'can set via hash ref'); - -# reset for later -$mbi->config( trap_nan => 0 ); - -############################################################################## -# BigFloat - -ok ($mbf->can('config')); - -$cfg = $mbf->config(); - -ok (ref($cfg),'HASH'); - -is ($cfg->{lib},'Math::BigInt::Calc', 'lib'); -is ($cfg->{with},'Math::BigInt::Calc', 'with'); -is ($cfg->{lib_version}, $Math::BigInt::Calc::VERSION, 'lib_version'); -is ($cfg->{class},$mbf,'class'); -is ($cfg->{upgrade}||'','', 'upgrade'); -is ($cfg->{div_scale},40, 'div_Scale'); - -is ($cfg->{precision}||0,0, 'precision'); # should test for undef -is ($cfg->{accuracy}||0,0,'accuracy'); -is ($cfg->{round_mode},'even','round_mode'); - -is ($cfg->{trap_nan},0, 'trap_nan'); -is ($cfg->{trap_inf},0, 'trap_inf'); - -is ($mbf->config('lib'), 'Math::BigInt::Calc', 'config("lib")'); - -# can set via hash ref? -$cfg = $mbf->config( { trap_nan => 1 } ); -is ($cfg->{trap_nan},1, 'can set via hash ref'); - -# reset for later -$mbf->config( trap_nan => 0 ); - -############################################################################## -# test setting values - -my $test = { - trap_nan => 1, - trap_inf => 1, - accuracy => 2, - precision => 3, - round_mode => 'zero', - div_scale => '100', - upgrade => 'Math::BigInt::SomeClass', - downgrade => 'Math::BigInt::SomeClass', - }; - -my $c; - -foreach my $key (keys %$test) - { - # see if setting in MBI works - eval ( "$mbi\->config( $key => '$test->{$key}' );" ); - $c = $mbi->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}"); - $c = $mbf->config(); - # see if setting it in MBI leaves MBF alone - if (($c->{$key}||0) ne $test->{$key}) - { - is (1,1); - } - else - { - is ("$key eq $c->{$key}","$key ne $test->{$key}", "$key"); - } - - # see if setting in MBF works - eval ( "$mbf\->config( $key => '$test->{$key}' );" ); - $c = $mbf->config(); ok ("$key = $c->{$key}", "$key = $test->{$key}"); - } - -############################################################################## -# test setting illegal keys (should croak) - -$@ = ""; my $never_reached = 0; -eval ("$mbi\->config( 'some_garbage' => 1 ); $never_reached = 1;"); -is ($never_reached,0); - -$@ = ""; $never_reached = 0; -eval ("$mbf\->config( 'some_garbage' => 1 ); $never_reached = 1;"); -is ($never_reached,0); - -# this does not work. Why? -#ok ($@ eq "Illegal keys 'some_garbage' passed to Math::BigInt->config() at ./config.t line 104", 1); - -# all tests done - diff --git a/dist/Math-BigInt/t/const_mbf.t b/dist/Math-BigInt/t/const_mbf.t deleted file mode 100644 index 84f7a8cf99..0000000000 --- a/dist/Math-BigInt/t/const_mbf.t +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl -w - -# test BigFloat constants alone (w/o BigInt loading) - -use strict; -use Test::More tests => 2; - -use Math::BigFloat ':constant'; - -is (1.0 / 3.0, '0.3333333333333333333333333333333333333333'); - -# BigInt was not loaded with ':constant', so only floats are handled -is (ref(2 ** 2),''); - diff --git a/dist/Math-BigInt/t/constant.t b/dist/Math-BigInt/t/constant.t deleted file mode 100644 index ad8afeed2d..0000000000 --- a/dist/Math-BigInt/t/constant.t +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 7; - -use Math::BigInt ':constant'; - -is (2 ** 255,'57896044618658097711785492504343953926634992332820282019728792003956564819968'); - -{ - no warnings 'portable'; # protect against "non-portable" warnings -# hexadecimal constants -is (0x123456789012345678901234567890, - Math::BigInt->new('0x123456789012345678901234567890')); -# binary constants -is (0b01010100011001010110110001110011010010010110000101101101, - Math::BigInt->new( - '0b01010100011001010110110001110011010010010110000101101101')); -} - -use Math::BigFloat ':constant'; -is (1.0 / 3.0, '0.3333333333333333333333333333333333333333'); - -# stress-test Math::BigFloat->import() - -Math::BigFloat->import( qw/:constant/ ); -is (1,1); - -Math::BigFloat->import( qw/:constant upgrade Math::BigRat/ ); -is (1,1); - -Math::BigFloat->import( qw/upgrade Math::BigRat :constant/ ); -is (1,1); - -# all tests done diff --git a/dist/Math-BigInt/t/downgrade.t b/dist/Math-BigInt/t/downgrade.t deleted file mode 100644 index f6b011e5a0..0000000000 --- a/dist/Math-BigInt/t/downgrade.t +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 15; - -use Math::BigInt upgrade => 'Math::BigFloat'; -use Math::BigFloat downgrade => 'Math::BigInt', upgrade => 'Math::BigInt'; - -use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup - $ECL $CL); -$class = "Math::BigInt"; -$CL = "Math::BigInt::Calc"; -$ECL = "Math::BigFloat"; - -# simplistic test for now -is (Math::BigFloat->downgrade(),'Math::BigInt'); -is (Math::BigFloat->upgrade(),'Math::BigInt'); - -# these downgrade -is (ref(Math::BigFloat->new('inf')),'Math::BigInt'); -is (ref(Math::BigFloat->new('-inf')),'Math::BigInt'); -is (ref(Math::BigFloat->new('NaN')),'Math::BigInt'); -is (ref(Math::BigFloat->new('0')),'Math::BigInt'); -is (ref(Math::BigFloat->new('1')),'Math::BigInt'); -is (ref(Math::BigFloat->new('10')),'Math::BigInt'); -is (ref(Math::BigFloat->new('-10')),'Math::BigInt'); -is (ref(Math::BigFloat->new('-10.0E1')),'Math::BigInt'); - -# bug until v1.67: -is (Math::BigFloat->new('0.2E0'), '0.2'); -is (Math::BigFloat->new('0.2E1'), '2'); -# until v1.67 resulted in 200: -is (Math::BigFloat->new('0.2E2'), '20'); - -# disable, otherwise it screws calculations -Math::BigFloat->upgrade(undef); -is (Math::BigFloat->upgrade()||'',''); - -Math::BigFloat->div_scale(20); # make it a bit faster -my $x = Math::BigFloat->new(2); # downgrades -# the following test upgrade for bsqrt() and also makes new() NOT downgrade -# for the bpow() side -is (Math::BigFloat->bpow('2','0.5'),$x->bsqrt()); - -#require 'upgrade.inc'; # all tests here for sharing diff --git a/dist/Math-BigInt/t/inf_nan.t b/dist/Math-BigInt/t/inf_nan.t deleted file mode 100644 index 270689bac6..0000000000 --- a/dist/Math-BigInt/t/inf_nan.t +++ /dev/null @@ -1,404 +0,0 @@ -#!/usr/bin/perl -w - -# test inf/NaN handling all in one place -# Thanx to Jarkko for the excellent explanations and the tables - -use strict; - -use Test::More - tests => 7 * 6 * 5 * 4 * 2 + - 7 * 6 * 2 * 4 * 1 # bmod -; -# see bottom: + 4 * 10; # 4 classes * 10 NaN == NaN tests - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt; -use Math::BigFloat; -use Math::BigInt::Subclass; -use Math::BigFloat::Subclass; - -my @biclasses = - qw/ Math::BigInt Math::BigInt::Subclass /; -my @bfclasses = - qw/ Math::BigFloat Math::BigFloat::Subclass /; - -my (@args,$x,$y,$z); - -# + -foreach (qw/ - -inf:-inf:-inf - -1:-inf:-inf - -0:-inf:-inf - 0:-inf:-inf - 1:-inf:-inf - inf:-inf:NaN - NaN:-inf:NaN - - -inf:-1:-inf - -1:-1:-2 - -0:-1:-1 - 0:-1:-1 - 1:-1:0 - inf:-1:inf - NaN:-1:NaN - - -inf:0:-inf - -1:0:-1 - -0:0:0 - 0:0:0 - 1:0:1 - inf:0:inf - NaN:0:NaN - - -inf:1:-inf - -1:1:0 - -0:1:1 - 0:1:1 - 1:1:2 - inf:1:inf - NaN:1:NaN - - -inf:inf:NaN - -1:inf:inf - -0:inf:inf - 0:inf:inf - 1:inf:inf - inf:inf:inf - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@biclasses, @bfclasses) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - my $r = $x->badd($y); - - is($x->bstr(),$args[2],"x $class $args[0] + $args[1]"); - is($x->bstr(),$args[2],"r $class $args[0] + $args[1]"); - } - } - -# - -foreach (qw/ - -inf:-inf:NaN - -1:-inf:inf - -0:-inf:inf - 0:-inf:inf - 1:-inf:inf - inf:-inf:inf - NaN:-inf:NaN - - -inf:-1:-inf - -1:-1:0 - -0:-1:1 - 0:-1:1 - 1:-1:2 - inf:-1:inf - NaN:-1:NaN - - -inf:0:-inf - -1:0:-1 - -0:0:-0 - 0:0:0 - 1:0:1 - inf:0:inf - NaN:0:NaN - - -inf:1:-inf - -1:1:-2 - -0:1:-1 - 0:1:-1 - 1:1:0 - inf:1:inf - NaN:1:NaN - - -inf:inf:-inf - -1:inf:-inf - -0:inf:-inf - 0:inf:-inf - 1:inf:-inf - inf:inf:NaN - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@biclasses, @bfclasses) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - my $r = $x->bsub($y); - - is($x->bstr(),$args[2],"x $class $args[0] - $args[1]"); - is($r->bstr(),$args[2],"r $class $args[0] - $args[1]"); - } - } - -# * -foreach (qw/ - -inf:-inf:inf - -1:-inf:inf - -0:-inf:NaN - 0:-inf:NaN - 1:-inf:-inf - inf:-inf:-inf - NaN:-inf:NaN - - -inf:-1:inf - -1:-1:1 - -0:-1:0 - 0:-1:-0 - 1:-1:-1 - inf:-1:-inf - NaN:-1:NaN - - -inf:0:NaN - -1:0:-0 - -0:0:-0 - 0:0:0 - 1:0:0 - inf:0:NaN - NaN:0:NaN - - -inf:1:-inf - -1:1:-1 - -0:1:-0 - 0:1:0 - 1:1:1 - inf:1:inf - NaN:1:NaN - - -inf:inf:-inf - -1:inf:-inf - -0:inf:NaN - 0:inf:NaN - 1:inf:inf - inf:inf:inf - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@biclasses, @bfclasses) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - $args[2] = '0' if $args[2] eq '-0'; # BigInt hasn't got -0 - my $r = $x->bmul($y); - - is($x->bstr(),$args[2],"x $class $args[0] * $args[1]"); - is($r->bstr(),$args[2],"r $class $args[0] * $args[1]"); - } - } - -# / -foreach (qw/ - -inf:-inf:NaN - -1:-inf:0 - -0:-inf:0 - 0:-inf:-0 - 1:-inf:-1 - inf:-inf:NaN - NaN:-inf:NaN - - -inf:-1:inf - -1:-1:1 - -0:-1:0 - 0:-1:-0 - 1:-1:-1 - inf:-1:-inf - NaN:-1:NaN - - -inf:0:-inf - -1:0:-inf - -0:0:NaN - 0:0:NaN - 1:0:inf - inf:0:inf - NaN:0:NaN - - -inf:1:-inf - -1:1:-1 - -0:1:-0 - 0:1:0 - 1:1:1 - inf:1:inf - NaN:1:NaN - - -inf:inf:NaN - -1:inf:-1 - -0:inf:-0 - 0:inf:0 - 1:inf:0 - inf:inf:NaN - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@biclasses, @bfclasses) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - - my $t = $x->copy(); - my $tmod = $t->copy(); - - # bdiv in scalar context - unless ($class =~ /^Math::BigFloat/) { - my $r = $x->bdiv($y); - is($x->bstr(),$args[2],"x $class $args[0] / $args[1]"); - is($r->bstr(),$args[2],"r $class $args[0] / $args[1]"); - } - - # bmod and bdiv in list context - my ($d,$rem) = $t->bdiv($y); - - # bdiv in list context - is($t->bstr(),$args[2],"t $class $args[0] / $args[1]"); - is($d->bstr(),$args[2],"d $class $args[0] / $args[1]"); - - # bmod - my $m = $tmod->bmod($y); - - # bmod() agrees with bdiv? - is($m->bstr(),$rem->bstr(),"m $class $args[0] % $args[1]"); - # bmod() return agrees with set value? - is($tmod->bstr(),$m->bstr(),"o $class $args[0] % $args[1]"); - - } - } - -# / -foreach (qw/ - -inf:-inf:NaN - -1:-inf:0 - -0:-inf:0 - 0:-inf:-0 - 1:-inf:-0 - inf:-inf:NaN - NaN:-inf:NaN - - -inf:-1:inf - -1:-1:1 - -0:-1:0 - 0:-1:-0 - 1:-1:-1 - inf:-1:-inf - NaN:-1:NaN - - -inf:0:-inf - -1:0:-inf - -0:0:NaN - 0:0:NaN - 1:0:inf - inf:0:inf - NaN:0:NaN - - -inf:1:-inf - -1:1:-1 - -0:1:-0 - 0:1:0 - 1:1:1 - inf:1:inf - NaN:1:NaN - - -inf:inf:NaN - -1:inf:-0 - -0:inf:-0 - 0:inf:0 - 1:inf:0 - inf:inf:NaN - NaN:inf:NaN - - -inf:NaN:NaN - -1:NaN:NaN - -0:NaN:NaN - 0:NaN:NaN - 1:NaN:NaN - inf:NaN:NaN - NaN:NaN:NaN - /) - { - @args = split /:/,$_; - for my $class (@bfclasses) - { - $x = $class->new($args[0]); - $y = $class->new($args[1]); - $args[2] = '0' if $args[2] eq '-0'; # BigInt/Float hasn't got -0 - - my $t = $x->copy(); - my $tmod = $t->copy(); - - # bdiv in scalar context - my $r = $x->bdiv($y); - is($x->bstr(),$args[2],"x $class $args[0] / $args[1]"); - is($r->bstr(),$args[2],"r $class $args[0] / $args[1]"); - - } - } - -############################################################################# -# overloaded comparisons - -# these are disabled for now, since Perl itself can't seem to make up it's -# mind what NaN actually is, see [perl #33106]. - -# -#foreach my $c (@biclasses, @bfclasses) -# { -# my $x = $c->bnan(); -# my $y = $c->bnan(); # test with two different objects, too -# my $a = $c->bzero(); -# -# is ($x == $y, undef, 'NaN == NaN: undef'); -# is ($x != $y, 1, 'NaN != NaN: 1'); -# -# is ($x == $x, undef, 'NaN == NaN: undef'); -# is ($x != $x, 1, 'NaN != NaN: 1'); -# -# is ($a != $x, 1, '0 != NaN: 1'); -# is ($a == $x, undef, '0 == NaN: undef'); -# -# is ($a < $x, undef, '0 < NaN: undef'); -# is ($a <= $x, undef, '0 <= NaN: undef'); -# is ($a >= $x, undef, '0 >= NaN: undef'); -# is ($a > $x, undef, '0 > NaN: undef'); -# } - -# All done. diff --git a/dist/Math-BigInt/t/isa.t b/dist/Math-BigInt/t/isa.t deleted file mode 100644 index 0bdf66fda2..0000000000 --- a/dist/Math-BigInt/t/isa.t +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 7; - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt::Subclass; -use Math::BigFloat::Subclass; -use Math::BigInt; -use Math::BigFloat; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigInt::Subclass"; -$CL = "Math::BigInt::Calc"; - -# Check that a subclass is still considered a BigInt -isa_ok ($class->new(123), 'Math::BigInt'); - -# ditto for plain Math::BigInt -isa_ok (Math::BigInt->new(123), 'Math::BigInt'); - -# But Math::BigFloats aren't -isnt (Math::BigFloat->new(123)->isa('Math::BigInt'), 1); - -# see what happens if we feed a Math::BigFloat into new() -$x = Math::BigInt->new(Math::BigFloat->new(123)); -is (ref($x),'Math::BigInt'); -isa_ok ($x, 'Math::BigInt'); - -# ditto for subclass -$x = Math::BigInt->new(Math::BigFloat->new(123)); -is (ref($x),'Math::BigInt'); -isa_ok ($x, 'Math::BigInt'); diff --git a/dist/Math-BigInt/t/lib_load.t b/dist/Math-BigInt/t/lib_load.t deleted file mode 100644 index 65a913ac35..0000000000 --- a/dist/Math-BigInt/t/lib_load.t +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 4; - -BEGIN { unshift @INC, 't'; } - -# first load BigInt with Calc -use Math::BigInt lib => 'Calc'; - -# BigFloat will remember that we loaded Calc -require Math::BigFloat; -is (Math::BigFloat::config()->{lib}, 'Math::BigInt::Calc', 'BigFloat got Calc'); - -# now load BigInt again with a different lib -Math::BigInt->import( lib => 'BareCalc' ); - -# and finally test that BigFloat knows about BareCalc - -is (Math::BigFloat::config()->{lib}, 'Math::BigInt::BareCalc', 'BigFloat was notified'); - -# See that Math::BigFloat supports "only" -eval "Math::BigFloat->import('only' => 'Calc')"; -is (Math::BigFloat::config()->{lib}, 'Math::BigInt::Calc', '"only" worked'); - -# See that Math::BigFloat supports "try" -eval "Math::BigFloat->import('try' => 'BareCalc')"; -is (Math::BigFloat::config()->{lib}, 'Math::BigInt::BareCalc', '"try" worked'); - diff --git a/dist/Math-BigInt/t/mbf_ali.t b/dist/Math-BigInt/t/mbf_ali.t deleted file mode 100644 index 845fbe94e1..0000000000 --- a/dist/Math-BigInt/t/mbf_ali.t +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl -w - -# test that the new alias names work - -use strict; -use Test::More tests => 6; - -use Math::BigFloat; - -use vars qw/$x $CL/; - -$CL = 'Math::BigFloat'; - -require 't/alias.inc'; diff --git a/dist/Math-BigInt/t/mbi_ali.t b/dist/Math-BigInt/t/mbi_ali.t deleted file mode 100644 index d52812bec9..0000000000 --- a/dist/Math-BigInt/t/mbi_ali.t +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl -w - -# test that the new alias names work - -use strict; -use Test::More tests => 6; - -use Math::BigInt; - -use vars qw/$x $CL/; - -$CL = 'Math::BigInt'; - -require 't/alias.inc'; diff --git a/dist/Math-BigInt/t/mbi_rand.t b/dist/Math-BigInt/t/mbi_rand.t deleted file mode 100644 index a6e3b21c8b..0000000000 --- a/dist/Math-BigInt/t/mbi_rand.t +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/perl -w - -use strict; - -my $count; - -BEGIN { - $count = 128; -} - -use Test::More tests => $count*4; - -use Math::BigInt; -my $c = 'Math::BigInt'; - -my $length = 128; - -# If you get a failure here, please re-run the test with the printed seed -# value as input "perl t/mbi_rand.t seed" and send me the output - -my $seed = ($#ARGV == 0) ? $ARGV[0] : int(rand(1165537)); -print "# seed: $seed\n"; srand($seed); - -print "# lib: ", Math::BigInt->config()->{lib},"\n"; -if (Math::BigInt->config()->{lib} =~ /::Calc/) - { - print "# base len: ", scalar Math::BigInt::Calc->_base_len(),"\n"; - } - -my ($A,$B,$As,$Bs,$ADB,$AMB,$la,$lb); -my $two = Math::BigInt->new(2); -for (my $i = 0; $i < $count; $i++) - { - # length of A and B - $la = int(rand($length)+1); $lb = int(rand($length)+1); - $As = ''; $Bs = ''; - - # we create the numbers from "patterns", e.g. get a random number and a - # random count and string them together. This means things like - # "100000999999999999911122222222" are much more likely. If we just strung - # together digits, we would end up with "1272398823211223" etc. It also means - # that we get more frequently equal numbers or other special cases. - while (length($As) < $la) { $As .= int(rand(100)) x int(rand(16)); } - while (length($Bs) < $lb) { $Bs .= int(rand(100)) x int(rand(16)); } - - $As =~ s/^0+//; $Bs =~ s/^0+//; - $As = $As || '0'; $Bs = $Bs || '0'; -# print "# As $As\n# Bs $Bs\n"; - $A = $c->new($As); $B = $c->new($Bs); - print "# A $A\n# B $B\n"; - if ($A->is_zero() || $B->is_zero()) - { - for (1..4) { is (1,1, 'skipped this test'); } next; - } - - # check that int(A/B)*B + A % B == A holds for all inputs - - # $X = ($A/$B)*$B + 2 * ($A % $B) - ($A % $B); - - ($ADB,$AMB) = $A->copy()->bdiv($B); - print "# ($A / $B, $A % $B ) = $ADB $AMB\n"; - - print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n". - "# tried $ADB * $B + $two*$AMB - $AMB\n" - unless is ($ADB*$B+$two*$AMB-$AMB,$As, "ADB * B + 2 * AMB - AMB == A"); - if (is ($ADB*$B/$B,$ADB, "ADB * B / B == ADB")) - { - print "# seed: $seed, \$ADB * \$B / \$B = ", $ADB * $B / $B, " != $ADB (\$B=$B)\n"; - if (Math::BigInt->config()->{lib} =~ /::Calc/) - { - print "# ADB->[-1]: ", $ADB->{value}->[-1], " B->[-1]: ", $B->{value}->[-1],"\n"; - } - } - # swap 'em and try this, too - # $X = ($B/$A)*$A + $B % $A; - ($ADB,$AMB) = $B->copy()->bdiv($A); - # print "check: $ADB $AMB"; - print "# seed $seed, ". join(' ',Math::BigInt::Calc->_base_len()),"\n". - "# tried $ADB * $A + $two*$AMB - $AMB\n" - unless is ($ADB*$A+$two*$AMB-$AMB,$Bs, "ADB * A + 2 * AMB - AMB == B"); - print "# +$two * $AMB = ",$ADB * $A + $two * $AMB,"\n"; - print "# -$AMB = ",$ADB * $A + $two * $AMB - $AMB,"\n"; - print "# seed $seed, \$ADB * \$A / \$A = ", $ADB * $A / $A, " != $ADB (\$A=$A)\n" - unless is ($ADB*$A/$A,$ADB, "ADB * A/A == ADB"); - } - diff --git a/dist/Math-BigInt/t/mbimbf.inc b/dist/Math-BigInt/t/mbimbf.inc deleted file mode 100644 index 7b2c94613c..0000000000 --- a/dist/Math-BigInt/t/mbimbf.inc +++ /dev/null @@ -1,951 +0,0 @@ -# test rounding, accuracy, precision and fallback, round_mode and mixing -# of classes - -# Make sure you always quote any bare floating-point values, lest 123.46 will -# be stringified to 123.4599999999 due to limited float prevision. - -use strict; -my ($x,$y,$z,$u,$rc); - -############################################################################### -# test defaults and set/get - -{ - no strict 'refs'; - is (${"$mbi\::accuracy"}, undef); - is (${"$mbi\::precision"}, undef); - is ($mbi->accuracy(), undef); - is ($mbi->precision(), undef); - is (${"$mbi\::div_scale"},40); - is (${"$mbi\::round_mode"},'even'); - is ($mbi->round_mode(),'even'); - - is (${"$mbf\::accuracy"}, undef); - is (${"$mbf\::precision"}, undef); - is ($mbf->precision(), undef); - is ($mbf->precision(), undef); - is (${"$mbf\::div_scale"},40); - is (${"$mbf\::round_mode"},'even'); - is ($mbf->round_mode(),'even'); -} - -# accessors -foreach my $class ($mbi,$mbf) - { - is ($class->accuracy(), undef); - is ($class->precision(), undef); - is ($class->round_mode(),'even'); - is ($class->div_scale(),40); - - is ($class->div_scale(20),20); - $class->div_scale(40); is ($class->div_scale(),40); - - is ($class->round_mode('odd'),'odd'); - $class->round_mode('even'); is ($class->round_mode(),'even'); - - is ($class->accuracy(2),2); - $class->accuracy(3); is ($class->accuracy(),3); - is ($class->accuracy(undef), undef); - - is ($class->precision(2),2); - is ($class->precision(-2),-2); - $class->precision(3); is ($class->precision(),3); - is ($class->precision(undef), undef); - } - -{ - no strict 'refs'; - # accuracy - foreach (qw/5 42 -1 0/) - { - is (${"$mbf\::accuracy"} = $_,$_); - is (${"$mbi\::accuracy"} = $_,$_); - } - is (${"$mbf\::accuracy"} = undef, undef); - is (${"$mbi\::accuracy"} = undef, undef); - - # precision - foreach (qw/5 42 -1 0/) - { - is (${"$mbf\::precision"} = $_,$_); - is (${"$mbi\::precision"} = $_,$_); - } - is (${"$mbf\::precision"} = undef, undef); - is (${"$mbi\::precision"} = undef, undef); - - # fallback - foreach (qw/5 42 1/) - { - is (${"$mbf\::div_scale"} = $_,$_); - is (${"$mbi\::div_scale"} = $_,$_); - } - # illegal values are possible for fallback due to no accessor - - # round_mode - foreach (qw/odd even zero trunc +inf -inf/) - { - is (${"$mbf\::round_mode"} = $_,$_); - is (${"$mbi\::round_mode"} = $_,$_); - } - ${"$mbf\::round_mode"} = 'zero'; - is (${"$mbf\::round_mode"},'zero'); - is (${"$mbi\::round_mode"},'-inf'); # from above - - # reset for further tests - ${"$mbi\::accuracy"} = undef; - ${"$mbi\::precision"} = undef; - ${"$mbf\::div_scale"} = 40; -} - -# local copies -$x = $mbf->new('123.456'); -is ($x->accuracy(), undef); -is ($x->accuracy(5),5); -is ($x->accuracy(undef),undef, undef); -is ($x->precision(), undef); -is ($x->precision(5),5); -is ($x->precision(undef),undef, undef); - -{ - no strict 'refs'; - # see if MBF changes MBIs values - is (${"$mbi\::accuracy"} = 42,42); - is (${"$mbf\::accuracy"} = 64,64); - is (${"$mbi\::accuracy"},42); # should be still 42 - is (${"$mbf\::accuracy"},64); # should be now 64 -} - -############################################################################### -# see if creating a number under set A or P will round it - -{ - no strict 'refs'; - ${"$mbi\::accuracy"} = 4; - ${"$mbi\::precision"} = undef; - - is ($mbi->new(123456),123500); # with A - ${"$mbi\::accuracy"} = undef; - ${"$mbi\::precision"} = 3; - is ($mbi->new(123456),123000); # with P - - ${"$mbf\::accuracy"} = 4; - ${"$mbf\::precision"} = undef; - ${"$mbi\::precision"} = undef; - - is ($mbf->new('123.456'),'123.5'); # with A - ${"$mbf\::accuracy"} = undef; - ${"$mbf\::precision"} = -1; - is ($mbf->new('123.456'),'123.5'); # with P from MBF, not MBI! - - ${"$mbf\::precision"} = undef; # reset -} - -############################################################################### -# see if MBI leaves MBF's private parts alone - -{ - no strict 'refs'; - ${"$mbi\::precision"} = undef; ${"$mbf\::precision"} = undef; - ${"$mbi\::accuracy"} = 4; ${"$mbf\::accuracy"} = undef; - is ($mbf->new('123.456'),'123.456'); - ${"$mbi\::accuracy"} = undef; # reset -} - -############################################################################### -# see if setting accuracy/precision actually rounds the number - -$x = $mbf->new('123.456'); $x->accuracy(4); is ($x,'123.5'); -$x = $mbf->new('123.456'); $x->precision(-2); is ($x,'123.46'); - -$x = $mbi->new(123456); $x->accuracy(4); is ($x,123500); -$x = $mbi->new(123456); $x->precision(2); is ($x,123500); - -############################################################################### -# test actual rounding via round() - -$x = $mbf->new('123.456'); -is ($x->copy()->round(5),'123.46'); -is ($x->copy()->round(4),'123.5'); -is ($x->copy()->round(5,2),'NaN'); -is ($x->copy()->round(undef,-2),'123.46'); -is ($x->copy()->round(undef,2),120); - -$x = $mbi->new('123'); -is ($x->round(5,2),'NaN'); - -$x = $mbf->new('123.45000'); -is ($x->copy()->round(undef,-1,'odd'),'123.5'); - -# see if rounding is 'sticky' -$x = $mbf->new('123.4567'); -$y = $x->copy()->bround(); # no-op since nowhere A or P defined - -is ($y,123.4567); -$y = $x->copy()->round(5); -is ($y->accuracy(),5); -is ($y->precision(), undef); # A has precedence, so P still unset -$y = $x->copy()->round(undef,2); -is ($y->precision(),2); -is ($y->accuracy(), undef); # P has precedence, so A still unset - -# see if setting A clears P and vice versa -$x = $mbf->new('123.4567'); -is ($x,'123.4567'); -is ($x->accuracy(4),4); -is ($x->precision(-2),-2); # clear A -is ($x->accuracy(), undef); - -$x = $mbf->new('123.4567'); -is ($x,'123.4567'); -is ($x->precision(-2),-2); -is ($x->accuracy(4),4); # clear P -is ($x->precision(), undef); - -# does copy work? -$x = $mbf->new(123.456); $x->accuracy(4); $x->precision(2); -$z = $x->copy(); is ($z->accuracy(),undef); is ($z->precision(),2); - -# does $x->bdiv($y,d) work when $d > div_scale? -$x = $mbf->new('0.008'); $x->accuracy(8); - -for my $e ( 4, 8, 16, 32 ) - { - print "# Tried: $x->bdiv(3,$e)\n" - unless is (scalar $x->copy()->bdiv(3,$e), '0.002' . ('6' x ($e-2)) . '7'); - } - -# does accuracy()/precision work on zeros? -foreach my $c ($mbi,$mbf) - { - $x = $c->bzero(); $x->accuracy(5); is ($x->{_a},5); - $x = $c->bzero(); $x->precision(5); is ($x->{_p},5); - $x = $c->new(0); $x->accuracy(5); is ($x->{_a},5); - $x = $c->new(0); $x->precision(5); is ($x->{_p},5); - - $x = $c->bzero(); $x->round(5); is ($x->{_a},5); - $x = $c->bzero(); $x->round(undef,5); is ($x->{_p},5); - $x = $c->new(0); $x->round(5); is ($x->{_a},5); - $x = $c->new(0); $x->round(undef,5); is ($x->{_p},5); - - # see if trying to increasing A in bzero() doesn't do something - $x = $c->bzero(); $x->{_a} = 3; $x->round(5); is ($x->{_a},3); - } - -############################################################################### -# test whether an opp calls objectify properly or not (or at least does what -# it should do given non-objects, w/ or w/o objectify()) - -foreach my $c ($mbi,$mbf) - { -# ${"$c\::precision"} = undef; # reset -# ${"$c\::accuracy"} = undef; # reset - - is ($c->new(123)->badd(123),246); - is ($c->badd(123,321),444); - is ($c->badd(123,$c->new(321)),444); - - is ($c->new(123)->bsub(122),1); - is ($c->bsub(321,123),198); - is ($c->bsub(321,$c->new(123)),198); - - is ($c->new(123)->bmul(123),15129); - is ($c->bmul(123,123),15129); - is ($c->bmul(123,$c->new(123)),15129); - -# is ($c->new(15129)->bdiv(123),123); -# is ($c->bdiv(15129,123),123); -# is ($c->bdiv(15129,$c->new(123)),123); - - is ($c->new(15131)->bmod(123),2); - is ($c->bmod(15131,123),2); - is ($c->bmod(15131,$c->new(123)),2); - - is ($c->new(2)->bpow(16),65536); - is ($c->bpow(2,16),65536); - is ($c->bpow(2,$c->new(16)),65536); - - is ($c->new(2**15)->brsft(1),2**14); - is ($c->brsft(2**15,1),2**14); - is ($c->brsft(2**15,$c->new(1)),2**14); - - is ($c->new(2**13)->blsft(1),2**14); - is ($c->blsft(2**13,1),2**14); - is ($c->blsft(2**13,$c->new(1)),2**14); - } - -############################################################################### -# test whether operations round properly afterwards -# These tests are not complete, since they do not exercise every "return" -# statement in the op's. But heh, it's better than nothing... - -$x = $mbf->new('123.456'); -$y = $mbf->new('654.321'); -$x->{_a} = 5; # $x->accuracy(5) would round $x straight away -$y->{_a} = 4; # $y->accuracy(4) would round $x straight away - -$z = $x + $y; is ($z,'777.8'); -$z = $y - $x; is ($z,'530.9'); -$z = $y * $x; is ($z,'80780'); -$z = $x ** 2; is ($z,'15241'); -$z = $x * $x; is ($z,'15241'); - -# not: $z = -$x; is ($z,'-123.46'); is ($x,'123.456'); -$z = $x->copy(); $z->{_a} = 2; $z = $z / 2; is ($z,62); -$x = $mbf->new(123456); $x->{_a} = 4; -$z = $x->copy; $z++; is ($z,123500); - -$x = $mbi->new(123456); -$y = $mbi->new(654321); -$x->{_a} = 5; # $x->accuracy(5) would round $x straight away -$y->{_a} = 4; # $y->accuracy(4) would round $x straight away - -$z = $x + $y; is ($z,777800); -$z = $y - $x; is ($z,530900); -$z = $y * $x; is ($z,80780000000); -$z = $x ** 2; is ($z,15241000000); -# not yet: $z = -$x; is ($z,-123460); is ($x,123456); -$z = $x->copy; $z++; is ($z,123460); -$z = $x->copy(); $z->{_a} = 2; $z = $z / 2; is ($z,62000); - -$x = $mbi->new(123400); $x->{_a} = 4; -is ($x->bnot(),-123400); # not -1234001 - -# both babs() and bneg() don't need to round, since the input will already -# be rounded (either as $x or via new($string)), and they don't change the -# value. The two tests below peek at this by using _a (illegally) directly -$x = $mbi->new(-123401); $x->{_a} = 4; is ($x->babs(),123401); -$x = $mbi->new(-123401); $x->{_a} = 4; is ($x->bneg(),123401); - -# test fdiv rounding to A and R (bug in v1.48 and maybe earlier versions) -$mbf->round_mode('even'); -$x = $mbf->new('740.7')->fdiv('6',4,undef,'zero'); is ($x,'123.4'); - -$x = $mbi->new('123456'); $y = $mbi->new('123456'); $y->{_a} = 6; -is ($x->bdiv($y),1); is ($x->{_a},6); # carried over - -$x = $mbi->new('123456'); $y = $mbi->new('123456'); $x->{_a} = 6; -is ($x->bdiv($y),1); is ($x->{_a},6); # carried over - -$x = $mbi->new('123456'); $y = $mbi->new('223456'); $y->{_a} = 6; -is ($x->bdiv($y),0); is ($x->{_a},6); # carried over - -$x = $mbi->new('123456'); $y = $mbi->new('223456'); $x->{_a} = 6; -is ($x->bdiv($y),0); is ($x->{_a},6); # carried over - -############################################################################### -# test that bop(0) does the same than bop(undef) - -$x = $mbf->new('1234567890'); -is ($x->copy()->bsqrt(0),$x->copy()->bsqrt(undef)); -is ($x->copy->bsqrt(0),'35136.41828644462161665823116758077037159'); - -is ($x->{_a}, undef); - -# test that bsqrt() modifies $x and does not just return something else -# (especially under BareCalc) -$z = $x->bsqrt(); -is ($z,$x); is ($x,'35136.41828644462161665823116758077037159'); - -$x = $mbf->new('1.234567890123456789'); -is ($x->copy()->bpow('0.5',0),$x->copy()->bpow('0.5',undef)); -is ($x->copy()->bpow('0.5',0),$x->copy()->bsqrt(undef)); -is ($x->copy()->bpow('2',0),'1.524157875323883675019051998750190521'); - -############################################################################### -# test (also under Bare) that bfac() rounds at last step - -is ($mbi->new(12)->bfac(),'479001600'); -is ($mbi->new(12)->bfac(2),'480000000'); -$x = $mbi->new(12); $x->accuracy(2); is ($x->bfac(),'480000000'); -$x = $mbi->new(13); $x->accuracy(2); is ($x->bfac(),'6200000000'); -$x = $mbi->new(13); $x->accuracy(3); is ($x->bfac(),'6230000000'); -$x = $mbi->new(13); $x->accuracy(4); is ($x->bfac(),'6227000000'); -# this does 1,2,3...9,10,11,12...20 -$x = $mbi->new(20); $x->accuracy(1); is ($x->bfac(),'2000000000000000000'); - -############################################################################### -# test bsqrt) rounding to given A/P/R (bug prior to v1.60) -$x = $mbi->new('123456')->bsqrt(2,undef); is ($x,'350'); # not 351 -$x = $mbi->new('3')->bsqrt(2,undef); is ($x->accuracy(),2); - -$mbi->round_mode('even'); $x = $mbi->new('126025')->bsqrt(2,undef,'+inf'); -is ($x,'360'); # not 355 nor 350 - -$x = $mbi->new('126025')->bsqrt(undef,2); is ($x,'400'); # not 355 - - -############################################################################### -# test mixed arguments - -$x = $mbf->new(10); -$u = $mbf->new(2.5); -$y = $mbi->new(2); - -$z = $x + $y; is ($z,12); is (ref($z),$mbf); -$z = $x / $y; is ($z,5); is (ref($z),$mbf); -$z = $u * $y; is ($z,5); is (ref($z),$mbf); - -$y = $mbi->new(12345); -$z = $u->copy()->bmul($y,2,undef,'odd'); is ($z,31000); -$z = $u->copy()->bmul($y,3,undef,'odd'); is ($z,30900); -$z = $u->copy()->bmul($y,undef,0,'odd'); is ($z,30863); -$z = $u->copy()->bmul($y,undef,1,'odd'); is ($z,30863); -$z = $u->copy()->bmul($y,undef,2,'odd'); is ($z,30860); -$z = $u->copy()->bmul($y,undef,3,'odd'); is ($z,30900); -$z = $u->copy()->bmul($y,undef,-1,'odd'); is ($z,30862.5); - -my $warn = ''; $SIG{__WARN__} = sub { $warn = shift; }; -# these should no longer warn, even tho '3.17' is a NaN in BigInt (>= returns -# now false, bug until v1.80) -$warn = ''; eval "\$z = 3.17 <= \$y"; is ($z, ''); -unlike ($warn, qr/^Use of uninitialized value (\$y )?(in numeric le \(<=\) |)at/); -$warn = ''; eval "\$z = \$y >= 3.17"; is ($z, ''); -unlike ($warn, qr/^Use of uninitialized value (\$y )?(in numeric ge \(>=\) |)at/); - -# XXX TODO breakage: -# $z = $y->copy()->bmul($u,2,0,'odd'); is ($z,31000); -# $z = $y * $u; is ($z,5); is (ref($z),$mbi); -# $z = $y + $x; is ($z,12); is (ref($z),$mbi); -# $z = $y / $x; is ($z,0); is (ref($z),$mbi); - -############################################################################### -# rounding in bdiv with fallback and already set A or P - -{ - no strict 'refs'; - ${"$mbf\::accuracy"} = undef; - ${"$mbf\::precision"} = undef; - ${"$mbf\::div_scale"} = 40; -} - - $x = $mbf->new(10); $x->{_a} = 4; - is ($x->bdiv(3),'3.333'); - is ($x->{_a},4); # set's it since no fallback - -$x = $mbf->new(10); $x->{_a} = 4; $y = $mbf->new(3); -is ($x->bdiv($y),'3.333'); -is ($x->{_a},4); # set's it since no fallback - -# rounding to P of x -$x = $mbf->new(10); $x->{_p} = -2; -is ($x->bdiv(3),'3.33'); - -# round in div with requested P -$x = $mbf->new(10); -is ($x->bdiv(3,undef,-2),'3.33'); - -# round in div with requested P greater than fallback -{ - no strict 'refs'; - ${"$mbf\::div_scale"} = 5; - $x = $mbf->new(10); - is ($x->bdiv(3,undef,-8),'3.33333333'); - ${"$mbf\::div_scale"} = 40; -} - -$x = $mbf->new(10); $y = $mbf->new(3); $y->{_a} = 4; -is ($x->bdiv($y),'3.333'); -is ($x->{_a},4); is ($y->{_a},4); # set's it since no fallback -is ($x->{_p}, undef); is ($y->{_p}, undef); - -# rounding to P of y -$x = $mbf->new(10); $y = $mbf->new(3); $y->{_p} = -2; -is ($x->bdiv($y),'3.33'); -is ($x->{_p},-2); - is ($y->{_p},-2); -is ($x->{_a}, undef); is ($y->{_a}, undef); - -############################################################################### -# test whether bround(-n) fails in MBF (undocumented in MBI) -eval { $x = $mbf->new(1); $x->bround(-2); }; -like ($@, qr/^bround\(\) needs positive accuracy/); - -# test whether rounding to higher accuracy is no-op -$x = $mbf->new(1); $x->{_a} = 4; -is ($x,'1.000'); -$x->bround(6); # must be no-op -is ($x->{_a},4); -is ($x,'1.000'); - -$x = $mbi->new(1230); $x->{_a} = 3; -is ($x,'1230'); -$x->bround(6); # must be no-op -is ($x->{_a},3); -is ($x,'1230'); - -# bround(n) should set _a -$x->bround(2); # smaller works -is ($x,'1200'); -is ($x->{_a},2); - -# bround(-n) is undocumented and only used by MBF -# bround(-n) should set _a -$x = $mbi->new(12345); -$x->bround(-1); -is ($x,'12300'); -is ($x->{_a},4); - -# bround(-n) should set _a -$x = $mbi->new(12345); -$x->bround(-2); -is ($x,'12000'); -is ($x->{_a},3); - -# bround(-n) should set _a -$x = $mbi->new(12345); $x->{_a} = 5; -$x->bround(-3); -is ($x,'10000'); -is ($x->{_a},2); - -# bround(-n) should set _a -$x = $mbi->new(12345); $x->{_a} = 5; -$x->bround(-4); -is ($x,'0'); -is ($x->{_a},1); - -# bround(-n) should be noop if n too big -$x = $mbi->new(12345); -$x->bround(-5); -is ($x,'0'); # scale to "big" => 0 -is ($x->{_a},0); - -# bround(-n) should be noop if n too big -$x = $mbi->new(54321); -$x->bround(-5); -is ($x,'100000'); # used by MBF to round 0.0054321 at 0.0_6_00000 -is ($x->{_a},0); - -# bround(-n) should be noop if n too big -$x = $mbi->new(54321); $x->{_a} = 5; -$x->bround(-6); -is ($x,'100000'); # no-op -is ($x->{_a},0); - -# bround(n) should set _a -$x = $mbi->new(12345); $x->{_a} = 5; -$x->bround(5); # must be no-op -is ($x,'12345'); -is ($x->{_a},5); - -# bround(n) should set _a -$x = $mbi->new(12345); $x->{_a} = 5; -$x->bround(6); # must be no-op -is ($x,'12345'); - -$x = $mbf->new('0.0061'); $x->bfround(-2); is ($x,'0.01'); -$x = $mbf->new('0.004'); $x->bfround(-2); is ($x,'0.00'); -$x = $mbf->new('0.005'); $x->bfround(-2); is ($x,'0.00'); - -$x = $mbf->new('12345'); $x->bfround(2); is ($x,'12340'); -$x = $mbf->new('12340'); $x->bfround(2); is ($x,'12340'); - -# MBI::bfround should clear A for negative P -$x = $mbi->new('1234'); $x->accuracy(3); $x->bfround(-2); -is ($x->{_a}, undef); - -# test that bfround() and bround() work with large numbers - -$x = $mbf->new(1)->bdiv(5678,undef,-63); -is ($x, '0.000176118351532229658330398027474462839027826699542092286016203'); - -$x = $mbf->new(1)->bdiv(5678,undef,-90); -is ($x, '0.000176118351532229658330398027474462839027826699542092286016202888340965128566396618527651'); - -$x = $mbf->new(1)->bdiv(5678,80); -is ($x, '0.00017611835153222965833039802747446283902782669954209228601620288834096512856639662'); - -############################################################################### -# rounding with already set precision/accuracy - -$x = $mbf->new(1); $x->{_p} = -5; -is ($x,'1.00000'); - -# further rounding donw -is ($x->bfround(-2),'1.00'); -is ($x->{_p},-2); - -$x = $mbf->new(12345); $x->{_a} = 5; -is ($x->bround(2),'12000'); -is ($x->{_a},2); - -$x = $mbf->new('1.2345'); $x->{_a} = 5; -is ($x->bround(2),'1.2'); -is ($x->{_a},2); - -# mantissa/exponent format and A/P -$x = $mbf->new('12345.678'); $x->accuracy(4); -is ($x,'12350'); is ($x->{_a},4); is ($x->{_p}, undef); - -#is ($x->{_m}->{_a}, undef); is ($x->{_e}->{_a}, undef); -#is ($x->{_m}->{_p}, undef); is ($x->{_e}->{_p}, undef); - -# check for no A/P in case of fallback -# result -$x = $mbf->new(100) / 3; -is ($x->{_a}, undef); is ($x->{_p}, undef); - -# result & remainder -$x = $mbf->new(100) / 3; ($x,$y) = $x->bdiv(3); -is ($x->{_a}, undef); is ($x->{_p}, undef); -is ($y->{_a}, undef); is ($y->{_p}, undef); - -############################################################################### -# math with two numbers with different A and P - -$x = $mbf->new(12345); $x->accuracy(4); # '12340' -$y = $mbf->new(12345); $y->accuracy(2); # '12000' -is ($x+$y,24000); # 12340+12000=> 24340 => 24000 - -$x = $mbf->new(54321); $x->accuracy(4); # '12340' -$y = $mbf->new(12345); $y->accuracy(3); # '12000' -is ($x-$y,42000); # 54320+12300=> 42020 => 42000 - -$x = $mbf->new('1.2345'); $x->precision(-2); # '1.23' -$y = $mbf->new('1.2345'); $y->precision(-4); # '1.2345' -is ($x+$y,'2.46'); # 1.2345+1.2300=> 2.4645 => 2.46 - -############################################################################### -# round should find and use proper class - -#$x = Foo->new(); -#is ($x->round($Foo::accuracy),'a' x $Foo::accuracy); -#is ($x->round(undef,$Foo::precision),'p' x $Foo::precision); -#is ($x->bfround($Foo::precision),'p' x $Foo::precision); -#is ($x->bround($Foo::accuracy),'a' x $Foo::accuracy); - -############################################################################### -# find out whether _find_round_parameters is doing what's it's supposed to do - -{ - no strict 'refs'; - ${"$mbi\::accuracy"} = undef; - ${"$mbi\::precision"} = undef; - ${"$mbi\::div_scale"} = 40; - ${"$mbi\::round_mode"} = 'odd'; -} - -$x = $mbi->new(123); -my @params = $x->_find_round_parameters(); -is (scalar @params,1); # nothing to round - -@params = $x->_find_round_parameters(1); -is (scalar @params,4); # a=1 -is ($params[0],$x); # self -is ($params[1],1); # a -is ($params[2], undef); # p -is ($params[3],'odd'); # round_mode - -@params = $x->_find_round_parameters(undef,2); -is (scalar @params,4); # p=2 -is ($params[0],$x); # self -is ($params[1], undef); # a -is ($params[2],2); # p -is ($params[3],'odd'); # round_mode - -eval { @params = $x->_find_round_parameters(undef,2,'foo'); }; -like ($@, qr/^Unknown round mode 'foo'/); - -@params = $x->_find_round_parameters(undef,2,'+inf'); -is (scalar @params,4); # p=2 -is ($params[0],$x); # self -is ($params[1], undef); # a -is ($params[2],2); # p -is ($params[3],'+inf'); # round_mode - -@params = $x->_find_round_parameters(2,-2,'+inf'); -is (scalar @params,1); # error, A and P defined -is ($params[0],$x); # self - -{ - no strict 'refs'; - ${"$mbi\::accuracy"} = 1; - @params = $x->_find_round_parameters(undef,-2); - is (scalar @params,1); # error, A and P defined - is ($params[0],$x); # self - is ($x->is_nan(),1); # and must be NaN - - ${"$mbi\::accuracy"} = undef; - ${"$mbi\::precision"} = 1; - @params = $x->_find_round_parameters(1,undef); - is (scalar @params,1); # error, A and P defined - is ($params[0],$x); # self - is ($x->is_nan(),1); # and must be NaN - - ${"$mbi\::precision"} = undef; # reset -} - -############################################################################### -# test whether bone/bzero take additional A & P, or reset it etc - -foreach my $c ($mbi,$mbf) - { - $x = $c->new(2)->bzero(); is ($x->{_a}, undef); is ($x->{_p}, undef); - $x = $c->new(2)->bone(); is ($x->{_a}, undef); is ($x->{_p}, undef); - $x = $c->new(2)->binf(); is ($x->{_a}, undef); is ($x->{_p}, undef); - $x = $c->new(2)->bnan(); is ($x->{_a}, undef); is ($x->{_p}, undef); - - $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->bnan(); - is ($x->{_a}, undef); is ($x->{_p}, undef); - $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->binf(); - is ($x->{_a}, undef); is ($x->{_p}, undef); - - $x = $c->new(2,1); is ($x->{_a},1); is ($x->{_p}, undef); - $x = $c->new(2,undef,1); is ($x->{_a}, undef); is ($x->{_p},1); - - $x = $c->new(2,1)->bzero(); is ($x->{_a},1); is ($x->{_p}, undef); - $x = $c->new(2,undef,1)->bzero(); is ($x->{_a}, undef); is ($x->{_p},1); - - $x = $c->new(2,1)->bone(); is ($x->{_a},1); is ($x->{_p}, undef); - $x = $c->new(2,undef,1)->bone(); is ($x->{_a}, undef); is ($x->{_p},1); - - $x = $c->new(2); $x->bone('+',2,undef); is ($x->{_a},2); is ($x->{_p}, undef); - $x = $c->new(2); $x->bone('+',undef,2); is ($x->{_a}, undef); is ($x->{_p},2); - $x = $c->new(2); $x->bone('-',2,undef); is ($x->{_a},2); is ($x->{_p}, undef); - $x = $c->new(2); $x->bone('-',undef,2); is ($x->{_a}, undef); is ($x->{_p},2); - - $x = $c->new(2); $x->bzero(2,undef); is ($x->{_a},2); is ($x->{_p}, undef); - $x = $c->new(2); $x->bzero(undef,2); is ($x->{_a}, undef); is ($x->{_p},2); - } - -############################################################################### -# test whether bone/bzero honour globals - -for my $c ($mbi,$mbf) - { - $c->accuracy(2); - $x = $c->bone(); is ($x->accuracy(),2); - $x = $c->bzero(); is ($x->accuracy(),2); - $c->accuracy(undef); - - $c->precision(-2); - $x = $c->bone(); is ($x->precision(),-2); - $x = $c->bzero(); is ($x->precision(),-2); - $c->precision(undef); - } - -############################################################################### -# check whether mixing A and P creates a NaN - -# new with set accuracy/precision and with parameters -{ - no strict 'refs'; - foreach my $c ($mbi,$mbf) - { - is ($c->new(123,4,-3),'NaN'); # with parameters - ${"$c\::accuracy"} = 42; - ${"$c\::precision"} = 2; - is ($c->new(123),'NaN'); # with globals - ${"$c\::accuracy"} = undef; - ${"$c\::precision"} = undef; - } -} - -# binary ops -foreach my $class ($mbi,$mbf) - { - foreach (qw/add sub mul pow mod/) - #foreach (qw/add sub mul div pow mod/) - { - my $try = "my \$x = $class->new(1234); \$x->accuracy(5); "; - $try .= "my \$y = $class->new(12); \$y->precision(-3); "; - $try .= "\$x->b$_(\$y);"; - $rc = eval $try; - print "# Tried: '$try'\n" if !is ($rc, 'NaN'); - } - } - -# unary ops -foreach (qw/new bsqrt/) - { - my $try = 'my $x = $mbi->$_(1234,5,-3); '; - $rc = eval $try; - print "# Tried: '$try'\n" if !is ($rc, 'NaN'); - } - -# see if $x->bsub(0) and $x->badd(0) really round -foreach my $class ($mbi,$mbf) - { - $x = $class->new(123); $class->accuracy(2); $x->bsub(0); - is ($x,120); - $class->accuracy(undef); - $x = $class->new(123); $class->accuracy(2); $x->badd(0); - is ($x,120); - $class->accuracy(undef); - } - -############################################################################### -# test whether shortcuts returning zero/one preserve A and P - -my ($ans1,$f,$a,$p,$xp,$yp,$xa,$ya,$try,$ans,@args); -my $CALC = Math::BigInt->config()->{lib}; -while () - { - $_ =~ s/[\n\r]//g; # remove newlines - next if /^\s*(#|$)/; # skip comments and empty lines - if (s/^&//) - { - $f = $_; next; # function - } - @args = split(/:/,$_,99); - my $ans = pop(@args); - - ($x,$xa,$xp) = split (/,/,$args[0]); - $xa = $xa || ''; $xp = $xp || ''; - $try = "\$x = $mbi->new('$x'); "; - $try .= "\$x->accuracy($xa); " if $xa ne ''; - $try .= "\$x->precision($xp); " if $xp ne ''; - - ($y,$ya,$yp) = split (/,/,$args[1]); - $ya = $ya || ''; $yp = $yp || ''; - $try .= "\$y = $mbi->new('$y'); "; - $try .= "\$y->accuracy($ya); " if $ya ne ''; - $try .= "\$y->precision($yp); " if $yp ne ''; - - $try .= "\$x->$f(\$y);"; - - # print "trying $try\n"; - $rc = eval $try; - # convert hex/binary targets to decimal - if ($ans =~ /^(0x0x|0b0b)/) - { - $ans =~ s/^0[xb]//; - $ans = $mbi->new($ans)->bstr(); - } - print "# Tried: '$try'\n" if !is ($rc, $ans); - # check internal state of number objects - is_valid($rc,$f) if ref $rc; - - # now check whether A and P are set correctly - # only one of $a or $p will be set (no crossing here) - $a = $xa || $ya; $p = $xp || $yp; - - # print "Check a=$a p=$p\n"; - # print "# Tried: '$try'\n"; - if ($a ne '') - { - if (!(is ($x->{_a}, $a) && is ($x->{_p}, undef))) - { - print "# Check: A=$a and P=undef\n"; - print "# Tried: '$try'\n"; - } - } - if ($p ne '') - { - if (!(is ($x->{_p}, $p) && is($x->{_a}, undef))) - { - print "# Check: A=undef and P=$p\n"; - print "# Tried: '$try'\n"; - } - } - } - -# all done -1; - -############################################################################### -# sub to check validity of a BigInt internally, to ensure that no op leaves a -# number object in an invalid state (f.i. "-0") - -sub is_valid - { - my ($x,$f) = @_; - - my $e = 0; # error? - # ok as reference? - $e = 'Not a reference' if !ref($x); - - # has ok sign? - $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" - if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; - - $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; - $e = $CALC->_check($x->{value}) if $e eq '0'; - - # test done, see if error did crop up - is (1,1), return if ($e eq '0'); - - is (1,$e." after op '$f'"); - } - -# format is: -# x,A,P:x,A,P:result -# 123,,3 means 123 with precision 3 (A is undef) -# the A or P of the result is calculated automatically -__DATA__ -&badd -123,,:123,,:246 -123,3,:0,,:123 -123,,-3:0,,:123 -123,,:0,3,:123 -123,,:0,,-3:123 -&bmul -123,,:1,,:123 -123,3,:0,,:0 -123,,-3:0,,:0 -123,,:0,3,:0 -123,,:0,,-3:0 -123,3,:1,,:123 -123,,-3:1,,:123 -123,,:1,3,:123 -123,,:1,,-3:123 -1,3,:123,,:123 -1,,-3:123,,:123 -1,,:123,3,:123 -1,,:123,,-3:123 -&bdiv -123,,:1,,:123 -123,4,:1,,:123 -123,,:1,4,:123 -123,,:1,,-4:123 -123,,-4:1,,:123 -1,4,:123,,:0 -1,,:123,4,:0 -1,,:123,,-4:0 -1,,-4:123,,:0 -&band -1,,:3,,:1 -1234,1,:0,,:0 -1234,,:0,1,:0 -1234,,-1:0,,:0 -1234,,:0,,-1:0 -0xFF,,:0x10,,:0x0x10 -0xFF,2,:0xFF,,:250 -0xFF,,:0xFF,2,:250 -0xFF,,1:0xFF,,:250 -0xFF,,:0xFF,,1:250 -&bxor -1,,:3,,:2 -1234,1,:0,,:1000 -1234,,:0,1,:1000 -1234,,3:0,,:1000 -1234,,:0,,3:1000 -0xFF,,:0x10,,:239 -# 250 ^ 255 => 5 -0xFF,2,:0xFF,,:5 -0xFF,,:0xFF,2,:5 -0xFF,,1:0xFF,,:5 -0xFF,,:0xFF,,1:5 -# 250 ^ 4095 = 3845 => 3800 -0xFF,2,:0xFFF,,:3800 -# 255 ^ 4100 = 4347 => 4300 -0xFF,,:0xFFF,2,:4300 -0xFF,,2:0xFFF,,:3800 -# 255 ^ 4100 = 10fb => 4347 => 4300 -0xFF,,:0xFFF,,2:4300 -&bior -1,,:3,,:3 -1234,1,:0,,:1000 -1234,,:0,1,:1000 -1234,,3:0,,:1000 -1234,,:0,,3:1000 -0xFF,,:0x10,,:0x0xFF -# FF | FA = FF => 250 -250,2,:0xFF,,:250 -0xFF,,:250,2,:250 -0xFF,,1:0xFF,,:250 -0xFF,,:0xFF,,1:250 -&bpow -2,,:3,,:8 -2,,:0,,:1 -2,2,:0,,:1 -2,,:0,2,:1 diff --git a/dist/Math-BigInt/t/mbimbf.t b/dist/Math-BigInt/t/mbimbf.t deleted file mode 100644 index 7d8afb07c9..0000000000 --- a/dist/Math-BigInt/t/mbimbf.t +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/perl -w - -# test rounding, accuracy, precision and fallback, round_mode and mixing -# of classes - -use strict; -use Test::More tests => 684 - + 26; # own tests - -use Math::BigInt 1.70; -use Math::BigFloat 1.43; - -use vars qw/$mbi $mbf/; - -$mbi = 'Math::BigInt'; -$mbf = 'Math::BigFloat'; - -require 't/mbimbf.inc'; - -# some tests that won't work with subclasses, since the things are only -# guaranteed in the Math::BigInt/BigFloat (unless subclass chooses to support -# this) - -Math::BigInt->round_mode('even'); # reset for tests -Math::BigFloat->round_mode('even'); # reset for tests - -is ($Math::BigInt::rnd_mode,'even'); -is ($Math::BigFloat::rnd_mode,'even'); - -my $x = eval '$mbi->round_mode("huhmbi");'; -like ($@, qr/^Unknown round mode 'huhmbi' at/); - -$x = eval '$mbf->round_mode("huhmbf");'; -like ($@, qr/^Unknown round mode 'huhmbf' at/); - -# old way (now with test for validity) -$x = eval '$Math::BigInt::rnd_mode = "huhmbi";'; -like ($@, qr/^Unknown round mode 'huhmbi' at/); -$x = eval '$Math::BigFloat::rnd_mode = "huhmbf";'; -like ($@, qr/^Unknown round mode 'huhmbf' at/); -# see if accessor also changes old variable -$mbi->round_mode('odd'); is ($Math::BigInt::rnd_mode,'odd'); -$mbf->round_mode('odd'); is ($Math::BigInt::rnd_mode,'odd'); - -foreach my $class (qw/Math::BigInt Math::BigFloat/) - { - is ($class->accuracy(5),5); # set A - is ($class->precision(), undef); # and now P must be cleared - is ($class->precision(5),5); # set P - is ($class->accuracy(), undef); # and now A must be cleared - } - -foreach my $class (qw/Math::BigInt Math::BigFloat/) - { - $class->accuracy(42); - my $x = $class->new(123); # $x gets A of 42, too! - is ($x->accuracy(),42); # really? - is ($x->accuracy(undef),42); # $x has no A, but the - # global is still in effect for $x - # so the return value of that operation should - # be 42, not undef - is ($x->accuracy(),42); # so $x should still have A = 42 - $class->accuracy(undef); # reset for further tests - $class->precision(undef); - } -# bug with flog(Math::BigFloat,Math::BigInt) -$x = Math::BigFloat->new(100); -$x = $x->blog(Math::BigInt->new(10)); - -is ($x,2); - -# bug until v1.88 for sqrt() with enough digits -for my $i (80,88,100) - { - $x = Math::BigFloat->new("1." . ("0" x $i) . "1"); - $x = $x->bsqrt; - is ($x, 1); - } diff --git a/dist/Math-BigInt/t/nan_cmp.t b/dist/Math-BigInt/t/nan_cmp.t deleted file mode 100644 index 983edcbddc..0000000000 --- a/dist/Math-BigInt/t/nan_cmp.t +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/perl -w - -# test that overloaded compare works when NaN are involved - -use strict; -use Test::More tests => 26; - -use Math::BigInt; -use Math::BigFloat; - -compare (Math::BigInt->bnan(), Math::BigInt->bone() ); -compare (Math::BigFloat->bnan(), Math::BigFloat->bone() ); - -sub compare - { - my ($nan, $one) = @_; - - is ($one, $one, '1 == 1'); - - is ($one != $nan, 1, "1 != NaN"); - is ($nan != $one, 1, "NaN != 1"); - is ($nan != $nan, 1, "NaN != NaN"); - - is ($nan == $one, '', "NaN == 1"); - is ($one == $nan, '', "1 == NaN"); - is ($nan == $nan, '', "NaN == NaN"); - - is ($nan <= $one, '', "NaN <= 1"); - is ($one <= $nan, '', "1 <= NaN"); - is ($nan <= $nan, '', "NaN <= NaN"); - - is ($nan >= $one, '', "NaN >= 1"); - is ($one >= $nan, '', "1 >= NaN"); - is ($nan >= $nan, '', "NaN >= NaN"); - } - diff --git a/dist/Math-BigInt/t/new_overloaded.t b/dist/Math-BigInt/t/new_overloaded.t deleted file mode 100644 index 08708dc557..0000000000 --- a/dist/Math-BigInt/t/new_overloaded.t +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/perl -w - -# Math::BigFloat->new had a bug where it would assume any object is a -# BigInt which broke overloaded non-BigInts. - -use Test::More tests => 4; - - -package Overloaded::Num; - -use overload '0+' => sub { ${$_[0]} }, - fallback => 1; -sub new { - my($class, $num) = @_; - return bless \$num, $class; -} - - -package main; - -use Math::BigFloat; - -my $overloaded_num = Overloaded::Num->new(2.23); -is $overloaded_num, 2.23; - -my $bigfloat = Math::BigFloat->new($overloaded_num); -is $bigfloat, 2.23, 'BigFloat->new accepts overloaded numbers'; - -my $bigint = Math::BigInt->new(Overloaded::Num->new(3)); -is $bigint, 3, 'BigInt->new accepts overloaded numbers'; - -is( Math::BigFloat->new($bigint), 3, 'BigFloat from BigInt' ); diff --git a/dist/Math-BigInt/t/req_mbf0.t b/dist/Math-BigInt/t/req_mbf0.t deleted file mode 100644 index 4df4d4a24b..0000000000 --- a/dist/Math-BigInt/t/req_mbf0.t +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then bzero() works - -use strict; -use Test::More tests => 1; - -require Math::BigFloat; -my $x = Math::BigFloat->bzero(); $x++; -is ($x,1, '$x is 1'); - -# all tests done - diff --git a/dist/Math-BigInt/t/req_mbf1.t b/dist/Math-BigInt/t/req_mbf1.t deleted file mode 100644 index ac8375c309..0000000000 --- a/dist/Math-BigInt/t/req_mbf1.t +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then bone() works - -use strict; -use Test::More tests => 1; - -require Math::BigFloat; my $x = Math::BigFloat->bone(); is ($x,1); - -# all tests done diff --git a/dist/Math-BigInt/t/req_mbfa.t b/dist/Math-BigInt/t/req_mbfa.t deleted file mode 100644 index eb4d5e10cb..0000000000 --- a/dist/Math-BigInt/t/req_mbfa.t +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then bnan() works - -use strict; -use Test::More tests => 1; - -require Math::BigFloat; my $x = Math::BigFloat->bnan(1); is ($x,'NaN'); - -# all tests done diff --git a/dist/Math-BigInt/t/req_mbfi.t b/dist/Math-BigInt/t/req_mbfi.t deleted file mode 100644 index 1ea5224c23..0000000000 --- a/dist/Math-BigInt/t/req_mbfi.t +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then binf() works - -use strict; -use Test::More tests => 1; - -require Math::BigFloat; my $x = Math::BigFloat->binf(); is ($x,'inf'); - -# all tests done diff --git a/dist/Math-BigInt/t/req_mbfn.t b/dist/Math-BigInt/t/req_mbfn.t deleted file mode 100644 index 1db441798a..0000000000 --- a/dist/Math-BigInt/t/req_mbfn.t +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigFloat and then new() works - -use strict; -use Test::More tests => 1; - -require Math::BigFloat; my $x = Math::BigFloat->new(1); ++$x; is ($x,2); - -# all tests done diff --git a/dist/Math-BigInt/t/req_mbfw.t b/dist/Math-BigInt/t/req_mbfw.t deleted file mode 100644 index 9b075c0a74..0000000000 --- a/dist/Math-BigInt/t/req_mbfw.t +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/perl -w - -# check that requiring BigFloat and then calling import() works - -use strict; -use Test::More tests => 3; - -BEGIN { unshift @INC, 't'; } - -# normal require that calls import automatically (we thus have MBI afterwards) -require Math::BigFloat; -my $x = Math::BigFloat->new(1); ++$x; -is ($x,2, '$x is 2'); - -like (Math::BigFloat->config()->{with}, qr/^Math::BigInt::(Fast)?Calc\z/, 'with ignored' ); - -# now override -Math::BigFloat->import ( with => 'Math::BigInt::Subclass' ); - -# the "with" argument is ignored -like (Math::BigFloat->config()->{with}, qr/^Math::BigInt::(Fast)?Calc\z/, 'with ignored' ); - -# all tests done diff --git a/dist/Math-BigInt/t/require.t b/dist/Math-BigInt/t/require.t deleted file mode 100644 index 66d9687a13..0000000000 --- a/dist/Math-BigInt/t/require.t +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigInt works - -use strict; -use Test::More tests => 1; - -my ($x); - -require Math::BigInt; $x = Math::BigInt->new(1); ++$x; - -is ($x,2); - -# all tests done - diff --git a/dist/Math-BigInt/t/round.t b/dist/Math-BigInt/t/round.t deleted file mode 100644 index 078e2d055b..0000000000 --- a/dist/Math-BigInt/t/round.t +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/perl -w - -# test rounding with non-integer A and P parameters - -use strict; -use Test::More tests => 95; - -use Math::BigFloat; - -my $cf = 'Math::BigFloat'; -my $ci = 'Math::BigInt'; - -my $x = $cf->new('123456.123456'); - -# unary ops with A -_do_a($x, 'round', 3, '123000'); -_do_a($x, 'bfround', 3, '123500'); -_do_a($x, 'bfround', 2, '123460'); -_do_a($x, 'bfround', -2, '123456.12'); -_do_a($x, 'bfround', -3, '123456.123'); - -_do_a($x, 'bround', 4, '123500'); -_do_a($x, 'bround', 3, '123000'); -_do_a($x, 'bround', 2, '120000'); - -_do_a($x, 'bsqrt', 4, '351.4'); -_do_a($x, 'bsqrt', 3, '351'); -_do_a($x, 'bsqrt', 2, '350'); - -# setting P -_do_p($x, 'bsqrt', 2, '350'); -_do_p($x, 'bsqrt', -2, '351.36'); - -# binary ops -_do_2_a($x, 'bdiv', 2, 6, '61728.1'); -_do_2_a($x, 'bdiv', 2, 4, '61730'); -_do_2_a($x, 'bdiv', 2, 3, '61700'); - -_do_2_p($x, 'bdiv', 2, -6, '61728.061728'); -_do_2_p($x, 'bdiv', 2, -4, '61728.0617'); -_do_2_p($x, 'bdiv', 2, -3, '61728.062'); - -# all tests done - -############################################################################# - -sub _do_a - { - my ($x, $method, $A, $result) = @_; - - is ($x->copy->$method($A), $result, "$method($A)"); - is ($x->copy->$method($A.'.1'), $result, "$method(${A}.1)"); - is ($x->copy->$method($A.'.5'), $result, "$method(${A}.5)"); - is ($x->copy->$method($A.'.6'), $result, "$method(${A}.6)"); - is ($x->copy->$method($A.'.9'), $result, "$method(${A}.9)"); - } - -sub _do_p - { - my ($x, $method, $P, $result) = @_; - - is ($x->copy->$method(undef,$P), $result, "$method(undef,$P)"); - is ($x->copy->$method(undef,$P.'.1'), $result, "$method(undef,${P}.1)"); - is ($x->copy->$method(undef,$P.'.5'), $result, "$method(undef.${P}.5)"); - is ($x->copy->$method(undef,$P.'.6'), $result, "$method(undef,${P}.6)"); - is ($x->copy->$method(undef,$P.'.9'), $result, "$method(undef,${P}.9)"); - } - -sub _do_2_a - { - my ($x, $method, $y, $A, $result) = @_; - - my $cy = $cf->new($y); - - is ($x->copy->$method($cy,$A), $result, "$method($cy,$A)"); - is ($x->copy->$method($cy,$A.'.1'), $result, "$method($cy,${A}.1)"); - is ($x->copy->$method($cy,$A.'.5'), $result, "$method($cy,${A}.5)"); - is ($x->copy->$method($cy,$A.'.6'), $result, "$method($cy,${A}.6)"); - is ($x->copy->$method($cy,$A.'.9'), $result, "$method($cy,${A}.9)"); - } - -sub _do_2_p - { - my ($x, $method, $y, $P, $result) = @_; - - my $cy = $cf->new($y); - - is ($x->copy->$method($cy,undef,$P), $result, "$method(undef,$P)"); - is ($x->copy->$method($cy,undef,$P.'.1'), $result, "$method($cy,undef,${P}.1)"); - is ($x->copy->$method($cy,undef,$P.'.5'), $result, "$method($cy,undef.${P}.5)"); - is ($x->copy->$method($cy,undef,$P.'.6'), $result, "$method($cy,undef,${P}.6)"); - is ($x->copy->$method($cy,undef,$P.'.9'), $result, "$method($cy,undef,${P}.9)"); - } - diff --git a/dist/Math-BigInt/t/rt-16221.t b/dist/Math-BigInt/t/rt-16221.t deleted file mode 100644 index a1dc2c6a3a..0000000000 --- a/dist/Math-BigInt/t/rt-16221.t +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/perl -# -# Verify that -# - Math::BigInt::objectify() calls as_int() (or as_number(), as a fallback) -# if the target object class is Math::BigInt. -# - Math::BigInt::objectify() calls as_float() if the target object class is -# Math::BigFloat. -# -# See RT #16221 and RT #52124. - -use strict; -use warnings; - -package main; - -use Test::More tests => 2; -use Math::BigInt; -use Math::BigFloat; - -############################################################################ - -my $int = Math::BigInt->new(10); -my $int_percent = My::Percent::Float->new(100); - -is($int * $int_percent, 10); - -############################################################################ - -my $float = Math::BigFloat->new(10); -my $float_percent = My::Percent::Float->new(100); - -is($float * $float_percent, 10); - -############################################################################ - -package My::Percent::Int; - -sub new { - my $class = shift; - my $num = shift; - return bless \$num, $class; -} - -sub as_number { - my $self = shift; - return Math::BigInt->new($$self / 100); -} - -sub as_string { - my $self = shift; - return $$self; -} - -############################################################################ - -package My::Percent::Float; - -sub new { - my $class = shift; - my $num = shift; - return bless \$num, $class; -} - -sub as_int { - my $self = shift; - return Math::BigInt->new($$self / 100); -} - -sub as_float { - my $self = shift; - return Math::BigFloat->new($$self / 100); -} - -sub as_string { - my $self = shift; - return $$self; -} diff --git a/dist/Math-BigInt/t/sub_ali.t b/dist/Math-BigInt/t/sub_ali.t deleted file mode 100644 index 04512abd6c..0000000000 --- a/dist/Math-BigInt/t/sub_ali.t +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/perl -w - -# test that the new alias names work - -use strict; -use Test::More tests => 6; - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt::Subclass; - -use vars qw/$CL $x/; -$CL = 'Math::BigInt::Subclass'; - -require 't/alias.inc'; diff --git a/dist/Math-BigInt/t/sub_mbf.t b/dist/Math-BigInt/t/sub_mbf.t deleted file mode 100644 index fec4d0708a..0000000000 --- a/dist/Math-BigInt/t/sub_mbf.t +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 2340 - + 6; # + our own tests - - -BEGIN { unshift @INC, 't'; } - -use Math::BigFloat::Subclass; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigFloat::Subclass"; -$CL = Math::BigFloat->config()->{lib}; # "Math::BigInt::Calc"; or FastCalc - -require 't/bigfltpm.inc'; # perform same tests as bigfltpm - -############################################################################### -# Now do custom tests for Subclass itself -my $ms = $class->new(23); -print "# Missing custom attribute \$ms->{_custom}" if !is (1, $ms->{_custom}); - -# Check that subclass is a Math::BigFloat, but not a Math::Bigint -isa_ok ($ms, 'Math::BigFloat'); -isnt ($ms->isa('Math::BigInt'), 1); - -use Math::BigFloat; - -my $bf = Math::BigFloat->new(23); # same as other -$ms += $bf; -print "# Tried: \$ms += \$bf, got $ms" if !is (46, $ms); -print "# Missing custom attribute \$ms->{_custom}" if !is (1, $ms->{_custom}); -print "# Wrong class: ref(\$ms) was ".ref($ms) if !is ($class, ref($ms)); diff --git a/dist/Math-BigInt/t/sub_mbi.t b/dist/Math-BigInt/t/sub_mbi.t deleted file mode 100644 index b8e0a027ea..0000000000 --- a/dist/Math-BigInt/t/sub_mbi.t +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 3649 - + 5; # +5 own tests - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt::Subclass; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigInt::Subclass"; -$CL = "Math::BigInt::Calc"; - -my $version = '0.02'; # for $VERSION tests, match current release (by hand!) - -require 't/bigintpm.inc'; # perform same tests as bigintpm - -############################################################################### -# Now do custom tests for Subclass itself - -my $ms = $class->new(23); -print "# Missing custom attribute \$ms->{_custom}" if !is (1, $ms->{_custom}); - -# Check that a subclass is still considered a BigInt -isa_ok ($ms, 'Math::BigInt'); - -use Math::BigInt; - -my $bi = Math::BigInt->new(23); # same as other -$ms += $bi; -print "# Tried: \$ms += \$bi, got $ms" if !is (46, $ms); -print "# Missing custom attribute \$ms->{_custom}" if !is (1, $ms->{_custom}); -print "# Wrong class: ref(\$ms) was ".ref($ms) if !is ($class, ref($ms)); diff --git a/dist/Math-BigInt/t/sub_mif.t b/dist/Math-BigInt/t/sub_mif.t deleted file mode 100644 index 6317e97cf3..0000000000 --- a/dist/Math-BigInt/t/sub_mif.t +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/perl -w - -# test rounding, accuracy, precision and fallback, round_mode and mixing -# of classes - -use strict; -use Test::More tests => 684; - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt::Subclass; -use Math::BigFloat::Subclass; - -use vars qw/$mbi $mbf/; - -$mbi = 'Math::BigInt::Subclass'; -$mbf = 'Math::BigFloat::Subclass'; - -require 't/mbimbf.inc'; diff --git a/dist/Math-BigInt/t/trap.t b/dist/Math-BigInt/t/trap.t deleted file mode 100644 index c3348b3d1f..0000000000 --- a/dist/Math-BigInt/t/trap.t +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/perl -w - -# test that config ( trap_nan => 1, trap_inf => 1) really works/dies - -use Test::More tests => 43; -use strict; - -use Math::BigInt; -use Math::BigFloat; - -my $mbi = 'Math::BigInt'; my $mbf = 'Math::BigFloat'; -my ($cfg,$x); - -foreach my $class ($mbi, $mbf) - { - # can do and defaults are okay? - ok ($class->can('config'), 'can config()'); - is ($class->config()->{trap_nan}, 0, 'trap_nan defaults to 0'); - is ($class->config()->{trap_inf}, 0, 'trap_inf defaults to 0'); - - # can set? - $cfg = $class->config( trap_nan => 1 ); - is ($cfg->{trap_nan},1, 'trap_nan now true'); - - # also test that new() still works normally - eval ("\$x = \$class->new('42'); \$x->bnan();"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,42,'$x after new() never modified'); - - # can reset? - $cfg = $class->config( trap_nan => 0 ); - is ($cfg->{trap_nan}, 0, 'trap_nan disabled'); - - # can set? - $cfg = $class->config( trap_inf => 1 ); - is ($cfg->{trap_inf}, 1, 'trap_inf enabled'); - - eval ("\$x = \$class->new('4711'); \$x->binf();"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,4711,'$x after new() never modified'); - - eval ("\$x = \$class->new('inf');"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,4711,'$x after new() never modified'); - - eval ("\$x = \$class->new('-inf');"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,4711,'$x after new() never modified'); - - # +$x/0 => +inf - eval ("\$x = \$class->new('4711'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,4711,'$x after new() never modified'); - - # -$x/0 => -inf - eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,'-815', '$x after new not modified'); - - $cfg = $class->config( trap_nan => 1 ); - # 0/0 => NaN - eval ("\$x = \$class->new('0'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/, 'died'); - is ($x,'0', '$x after new not modified'); - } - -############################################################################## -# BigInt - -$x = Math::BigInt->new(2); -eval ("\$x = \$mbi->new('0.1');"); -is ($x,2,'never modified since it dies'); -eval ("\$x = \$mbi->new('0a.1');"); -is ($x,2,'never modified since it dies'); - -############################################################################## -# BigFloat - -$x = Math::BigFloat->new(2); -eval ("\$x = \$mbf->new('0.1a');"); -is ($x,2,'never modified since it dies'); - -# all tests done - diff --git a/dist/Math-BigInt/t/upgrade.inc b/dist/Math-BigInt/t/upgrade.inc deleted file mode 100644 index 16ca05e44a..0000000000 --- a/dist/Math-BigInt/t/upgrade.inc +++ /dev/null @@ -1,1494 +0,0 @@ -# include this file into another for subclass testing - -# This file is nearly identical to bigintpm.t, except that certain results -# are _requird_ to be different due to "upgrading" or "promoting" to BigFloat. -# The reverse is not true, any unmarked results can be either BigInt or -# BigFloat, depending on how good the internal optimization is (e.g. it -# is usually desirable to have 2 ** 2 return a BigInt, not a BigFloat). - -# Results that are required to be BigFloat are marked with C<^> at the end. - -# Please note that the testcount goes up by two for each extra result marked -# with ^, since then we test whether it has the proper class and that it left -# the upgrade variable alone. - -my $version = ${"$class\::VERSION"}; - -############################################################################## -# for testing inheritance of _swap - -package Math::Foo; - -use Math::BigInt lib => $main::CL; -use vars qw/@ISA/; -@ISA = (qw/Math::BigInt/); - -use overload -# customized overload for sub, since original does not use swap there -'-' => sub { my @a = ref($_[0])->_swap(@_); - $a[0]->bsub($a[1])}; - -sub _swap - { - # a fake _swap, which reverses the params - my $self = shift; # for override in subclass - if ($_[2]) - { - my $c = ref ($_[0] ) || 'Math::Foo'; - return ( $_[0]->copy(), $_[1] ); - } - else - { - return ( Math::Foo->new($_[1]), $_[0] ); - } - } - -############################################################################## -package main; - -my $CALC = $class->config()->{lib}; is ($CALC,$CL); - -my ($f,$z,$a,$exp,@a,$m,$e,$round_mode,$expected_class); - -while () - { - $_ =~ s/[\n\r]//g; # remove newlines - next if /^#/; # skip comments - if (s/^&//) - { - $f = $_; next; - } - elsif (/^\$/) - { - $round_mode = $_; $round_mode =~ s/^\$/$class\->/; next; - } - - @args = split(/:/,$_,99); $ans = pop(@args); - $expected_class = $class; - if ($ans =~ /\^$/) - { - $expected_class = $ECL; $ans =~ s/\^$//; - } - $try = "\$x = $class->new(\"$args[0]\");"; - if ($f eq "bnorm") - { - $try = "\$x = $class->bnorm(\"$args[0]\");"; - # some is_xxx tests - } elsif ($f =~ /^is_(zero|one|odd|even|negative|positive|nan|int)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "as_hex") { - $try .= '$x->as_hex();'; - } elsif ($f eq "as_bin") { - $try .= '$x->as_bin();'; - } elsif ($f eq "is_inf") { - $try .= "\$x->is_inf('$args[1]');"; - } elsif ($f eq "binf") { - $try .= "\$x->binf('$args[1]');"; - } elsif ($f eq "bone") { - $try .= "\$x->bone('$args[1]');"; - # some unary ops - } elsif ($f =~ /^b(nan|floor|ceil|int|sstr|neg|abs|inc|dec|not|sqrt|fac)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "length") { - $try .= '$x->length();'; - } elsif ($f eq "exponent"){ - # ->bstr() to see if an object is returned - $try .= '$x = $x->exponent()->bstr();'; - } elsif ($f eq "mantissa"){ - # ->bstr() to see if an object is returned - $try .= '$x = $x->mantissa()->bstr();'; - } elsif ($f eq "parts"){ - $try .= '($m,$e) = $x->parts();'; - # ->bstr() to see if an object is returned - $try .= '$m = $m->bstr(); $m = "NaN" if !defined $m;'; - $try .= '$e = $e->bstr(); $e = "NaN" if !defined $e;'; - $try .= '"$m,$e";'; - } else { - if ($args[1] !~ /\./) - { - $try .= "\$y = $class->new(\"$args[1]\");"; # BigInt - } - else - { - $try .= "\$y = $ECL->new(\"$args[1]\");"; # BigFloat - } - if ($f eq "bcmp") - { - $try .= '$x->bcmp($y);'; - } elsif ($f eq "bround") { - $try .= "$round_mode; \$x->bround(\$y);"; - } elsif ($f eq "broot") { - $try .= "\$x->broot(\$y);"; - } elsif ($f eq "bacmp"){ - $try .= '$x->bacmp($y);'; - } elsif ($f eq "badd"){ - $try .= '$x + $y;'; - } elsif ($f eq "bsub"){ - $try .= '$x - $y;'; - } elsif ($f eq "bmul"){ - $try .= '$x * $y;'; - } elsif ($f eq "bdiv"){ - $try .= '$x / $y;'; - } elsif ($f eq "bdiv-list"){ - $try .= 'join (",",$x->bdiv($y));'; - # overload via x= - } elsif ($f =~ /^.=$/){ - $try .= "\$x $f \$y;"; - # overload via x - } elsif ($f =~ /^.$/){ - $try .= "\$x $f \$y;"; - } elsif ($f eq "bmod"){ - $try .= '$x % $y;'; - } elsif ($f eq "bgcd") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new('$args[2]'); "; - } - $try .= "$class\::bgcd(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } - elsif ($f eq "blcm") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new('$args[2]'); "; - } - $try .= "$class\::blcm(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - }elsif ($f eq "blsft"){ - if (defined $args[2]) - { - $try .= "\$x->blsft(\$y,$args[2]);"; - } - else - { - $try .= "\$x << \$y;"; - } - }elsif ($f eq "brsft"){ - if (defined $args[2]) - { - $try .= "\$x->brsft(\$y,$args[2]);"; - } - else - { - $try .= "\$x >> \$y;"; - } - }elsif ($f eq "band"){ - $try .= "\$x & \$y;"; - }elsif ($f eq "bior"){ - $try .= "\$x | \$y;"; - }elsif ($f eq "bxor"){ - $try .= "\$x ^ \$y;"; - }elsif ($f eq "bpow"){ - $try .= "\$x ** \$y;"; - }elsif ($f eq "digit"){ - $try = "\$x = $class->new('$args[0]'); \$x->digit($args[1]);"; - } else { warn "Unknown op '$f'"; } - } # end else all other ops - - $ans1 = eval $try; - # convert hex/binary targets to decimal - if ($ans =~ /^(0x0x|0b0b)/) - { - $ans =~ s/^0[xb]//; $ans = Math::BigInt->new($ans)->bstr(); - } - if ($ans eq "") - { - is ($ans1, undef); - } - else - { - # print "try: $try ans: $ans1 $ans\n"; - print "# Tried: '$try'\n" if !is ($ans1, $ans); - if ($expected_class ne $class) - { - is (ref($ans1),$expected_class); # also checks that it really is ref! - is ($Math::BigInt::upgrade,'Math::BigFloat'); # still okay? - } - } - # check internal state of number objects - is_valid($ans1,$f) if ref $ans1; - } # endwhile data tests -close DATA; - -my $warn = ''; $SIG{__WARN__} = sub { $warn = shift; }; - -# these should not warn -$warn = ''; eval "\$z = 3.17 <= \$y"; is ($z, 1); is ($warn, ''); -$warn = ''; eval "\$z = \$y >= 3.17"; is ($z, 1); is ($warn, ''); - -# all tests done - -1; - -############################################################################### -# sub to check validity of a BigInt internally, to ensure that no op leaves a -# number object in an invalid state (f.i. "-0") - -sub is_valid - { - my ($x,$f,$c) = @_; - - # The checks here are loosened a bit to allow BigInt or BigFloats to pass - - my $e = 0; # error? - # ok as reference? - # $e = "Not a reference to $c" if (ref($x) || '') ne $c; - - # has ok sign? - $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'" - if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/; - - $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0; - #$e = $CALC->_check($x->{value}) if $e eq '0'; - - # test done, see if error did crop up - is (1,1), return if ($e eq '0'); - - is (1,$e." after op '$f'"); - } - -__DATA__ -&.= -1234:-345:1234-345 -&+= -1:2:3 --1:-2:-3 -&-= -1:2:-1 --1:-2:1 -&*= -2:3:6 --1:5:-5 -&%= -100:3:1 -8:9:8 -&/= -100:3:33.33333333333333333333333333333333333333 --8:2:-4 -&|= -2:1:3 -&&= -5:7:5 -&^= -5:7:2 -&is_negative -0:0 --1:1 -1:0 -+inf:0 --inf:1 -NaNneg:0 -&is_positive -0:0 --1:0 -1:1 -+inf:1 --inf:0 -NaNneg:0 -&is_odd -abc:0 -0:0 -1:1 -3:1 --1:1 --3:1 -10000001:1 -10000002:0 -2:0 -120:0 -121:1 -&is_int -NaN:0 -inf:0 --inf:0 -1:1 -12:1 -123e12:1 -&is_even -abc:0 -0:1 -1:0 -3:0 --1:0 --3:0 -10000001:0 -10000002:1 -2:1 -120:1 -121:0 -&bacmp -+0:-0:0 -+0:+1:-1 --1:+1:0 -+1:-1:0 --1:+2:-1 -+2:-1:1 --123456789:+987654321:-1 -+123456789:-987654321:-1 -+987654321:+123456789:1 --987654321:+123456789:1 --123:+4567889:-1 -# NaNs -acmpNaN:123: -123:acmpNaN: -acmpNaN:acmpNaN: -# infinity -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:0 --inf:+inf:0 -+inf:123:1 --inf:123:1 -+inf:-123:1 --inf:-123:1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&bnorm -123:123 -12.3:12.3^ -# binary input -0babc:NaN -0b123:NaN -0b0:0 --0b0:0 --0b1:-1 -0b0001:1 -0b001:1 -0b011:3 -0b101:5 -0b1001:9 -0b10001:17 -0b100001:33 -0b1000001:65 -0b10000001:129 -0b100000001:257 -0b1000000001:513 -0b10000000001:1025 -0b100000000001:2049 -0b1000000000001:4097 -0b10000000000001:8193 -0b100000000000001:16385 -0b1000000000000001:32769 -0b10000000000000001:65537 -0b100000000000000001:131073 -0b1000000000000000001:262145 -0b10000000000000000001:524289 -0b100000000000000000001:1048577 -0b1000000000000000000001:2097153 -0b10000000000000000000001:4194305 -0b100000000000000000000001:8388609 -0b1000000000000000000000001:16777217 -0b10000000000000000000000001:33554433 -0b100000000000000000000000001:67108865 -0b1000000000000000000000000001:134217729 -0b10000000000000000000000000001:268435457 -0b100000000000000000000000000001:536870913 -0b1000000000000000000000000000001:1073741825 -0b10000000000000000000000000000001:2147483649 -0b100000000000000000000000000000001:4294967297 -0b1000000000000000000000000000000001:8589934593 -0b10000000000000000000000000000000001:17179869185 -0b__101:NaN -0b1_0_1:5 -0b0_0_0_1:1 -# hex input --0x0:0 -0xabcdefgh:NaN -0x1234:4660 -0xabcdef:11259375 --0xABCDEF:-11259375 --0x1234:-4660 -0x12345678:305419896 -0x1_2_3_4_56_78:305419896 -0xa_b_c_d_e_f:11259375 -0x__123:NaN -0x9:9 -0x11:17 -0x21:33 -0x41:65 -0x81:129 -0x101:257 -0x201:513 -0x401:1025 -0x801:2049 -0x1001:4097 -0x2001:8193 -0x4001:16385 -0x8001:32769 -0x10001:65537 -0x20001:131073 -0x40001:262145 -0x80001:524289 -0x100001:1048577 -0x200001:2097153 -0x400001:4194305 -0x800001:8388609 -0x1000001:16777217 -0x2000001:33554433 -0x4000001:67108865 -0x8000001:134217729 -0x10000001:268435457 -0x20000001:536870913 -0x40000001:1073741825 -0x80000001:2147483649 -0x100000001:4294967297 -0x200000001:8589934593 -0x400000001:17179869185 -0x800000001:34359738369 -# inf input -inf:inf -+inf:inf --inf:-inf -0inf:NaN -# abnormal input -:NaN -abc:NaN - 1 a:NaN -1bcd2:NaN -11111b:NaN -+1z:NaN --1z:NaN -# only one underscore between two digits -_123:NaN -_123_:NaN -123_:NaN -1__23:NaN -1E1__2:NaN -1_E12:NaN -1E_12:NaN -1_E_12:NaN -+_1E12:NaN -+0_1E2:100 -+0_0_1E2:100 --0_0_1E2:-100 --0_0_1E+0_0_2:-100 -E1:NaN -E23:NaN -1.23E1:12.3^ -1.23E-1:0.123^ -# bug with two E's in number being valid -1e2e3:NaN -1e2r:NaN -1e2.0:NaN -# leading zeros -012:12 -0123:123 -01234:1234 -012345:12345 -0123456:123456 -01234567:1234567 -012345678:12345678 -0123456789:123456789 -01234567891:1234567891 -012345678912:12345678912 -0123456789123:123456789123 -01234567891234:1234567891234 -# normal input -0:0 -+0:0 -+00:0 -+000:0 -000000000000000000:0 --0:0 --0000:0 -+1:1 -+01:1 -+001:1 -+00000100000:100000 -123456789:123456789 --1:-1 --01:-1 --001:-1 --123456789:-123456789 --00000100000:-100000 -1_2_3:123 -10000000000E-1_0:1 -1E2:100 -1E1:10 -1E0:1 -1.23E2:123 -100E-1:10 -# floating point input -# .2e2:20 -1.E3:1000 -1.01E2:101 -1010E-1:101 --1010E0:-1010 --1010E1:-10100 -1234.00:1234 -# non-integer numbers --1010E-2:-10.1^ --1.01E+1:-10.1^ --1.01E-1:-0.101^ -&bnan -1:NaN -2:NaN -abc:NaN -&bone -2:+:1 -2:-:-1 -boneNaN:-:-1 -boneNaN:+:1 -2:abc:1 -3::1 -&binf -1:+:inf -2:-:-inf -3:abc:inf -&is_nan -123:0 -abc:1 -NaN:1 --123:0 -&is_inf -+inf::1 --inf::1 -abc::0 -1::0 -NaN::0 --1::0 -+inf:-:0 -+inf:+:1 --inf:-:1 --inf:+:0 -# it must be exactly /^[+-]inf$/ -+infinity::0 --infinity::0 -&blsft -abc:abc:NaN -+2:+2:8 -+1:+32:4294967296 -+1:+48:281474976710656 -+8:-2:NaN -# exercise base 10 -+12345:4:10:123450000 --1234:0:10:-1234 -+1234:0:10:1234 -+2:2:10:200 -+12:2:10:1200 -+1234:-3:10:NaN -1234567890123:12:10:1234567890123000000000000 -&brsft -abc:abc:NaN -+8:+2:2 -+4294967296:+32:1 -+281474976710656:+48:1 -+2:-2:NaN -# exercise base 10 --1234:0:10:-1234 -+1234:0:10:1234 -+200:2:10:2 -+1234:3:10:1 -+1234:2:10:12 -+1234:-3:10:NaN -310000:4:10:31 -12300000:5:10:123 -1230000000000:10:10:123 -09876123456789067890:12:10:9876123 -1234561234567890123:13:10:123456 -&bsstr -1e+34:1e+34 -123.456E3:123456e+0 -100:1e+2 -abc:NaN -&bneg -bnegNaN:NaN -+inf:-inf --inf:inf -abd:NaN -0:0 -1:-1 --1:1 -+123456789:-123456789 --123456789:123456789 -&babs -babsNaN:NaN -+inf:inf --inf:inf -0:0 -1:1 --1:1 -+123456789:123456789 --123456789:123456789 -&bcmp -bcmpNaN:bcmpNaN: -bcmpNaN:0: -0:bcmpNaN: -0:0:0 --1:0:-1 -0:-1:1 -1:0:1 -0:1:-1 --1:1:-1 -1:-1:1 --1:-1:0 -1:1:0 -123:123:0 -123:12:1 -12:123:-1 --123:-123:0 --123:-12:-1 --12:-123:1 -123:124:-1 -124:123:1 --123:-124:1 --124:-123:-1 -100:5:1 --123456789:987654321:-1 -+123456789:-987654321:1 --987654321:123456789:-1 --inf:5432112345:-1 -+inf:5432112345:1 --inf:-5432112345:-1 -+inf:-5432112345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:1 --inf:+inf:-1 -5:inf:-1 -5:inf:-1 --5:-inf:1 --5:-inf:1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&binc -abc:NaN -+inf:inf --inf:-inf -+0:1 -+1:2 --1:0 -&bdec -abc:NaN -+inf:inf --inf:-inf -+0:-1 -+1:0 --1:-2 -&badd -abc:abc:NaN -abc:0:NaN -+0:abc:NaN -+inf:-inf:NaN --inf:+inf:NaN -+inf:+inf:inf --inf:-inf:-inf -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -0:0:0 -1:0:1 -0:1:1 -1:1:2 --1:0:-1 -0:-1:-1 --1:-1:-2 --1:+1:0 -+1:-1:0 -+9:+1:10 -+99:+1:100 -+999:+1:1000 -+9999:+1:10000 -+99999:+1:100000 -+999999:+1:1000000 -+9999999:+1:10000000 -+99999999:+1:100000000 -+999999999:+1:1000000000 -+9999999999:+1:10000000000 -+99999999999:+1:100000000000 -+10:-1:9 -+100:-1:99 -+1000:-1:999 -+10000:-1:9999 -+100000:-1:99999 -+1000000:-1:999999 -+10000000:-1:9999999 -+100000000:-1:99999999 -+1000000000:-1:999999999 -+10000000000:-1:9999999999 -+123456789:987654321:1111111110 --123456789:987654321:864197532 --123456789:-987654321:-1111111110 -+123456789:-987654321:-864197532 -2:2.5:4.5^ --123:-1.5:-124.5^ --1.2:1:-0.2^ -&bsub -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:inf --inf:+inf:-inf -+inf:+inf:NaN --inf:-inf:NaN -+0:+0:0 -+1:+0:1 -+0:+1:-1 -+1:+1:0 --1:+0:-1 -+0:-1:1 --1:-1:0 --1:+1:-2 -+1:-1:2 -+9:+1:8 -+99:+1:98 -+999:+1:998 -+9999:+1:9998 -+99999:+1:99998 -+999999:+1:999998 -+9999999:+1:9999998 -+99999999:+1:99999998 -+999999999:+1:999999998 -+9999999999:+1:9999999998 -+99999999999:+1:99999999998 -+10:-1:11 -+100:-1:101 -+1000:-1:1001 -+10000:-1:10001 -+100000:-1:100001 -+1000000:-1:1000001 -+10000000:-1:10000001 -+100000000:-1:100000001 -+1000000000:-1:1000000001 -+10000000000:-1:10000000001 -+123456789:+987654321:-864197532 --123456789:+987654321:-1111111110 --123456789:-987654321:864197532 -+123456789:-987654321:1111111110 -&bmul -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -NaNmul:+inf:NaN -NaNmul:-inf:NaN --inf:NaNmul:NaN -+inf:NaNmul:NaN -+inf:+inf:inf -+inf:-inf:-inf --inf:+inf:-inf --inf:-inf:inf -+0:+0:0 -+0:+1:0 -+1:+0:0 -+0:-1:0 --1:+0:0 -123456789123456789:0:0 -0:123456789123456789:0 --1:-1:1 --1:+1:-1 -+1:-1:-1 -+1:+1:1 -+2:+3:6 --2:+3:-6 -+2:-3:-6 --2:-3:6 -111:111:12321 -10101:10101:102030201 -1001001:1001001:1002003002001 -100010001:100010001:10002000300020001 -10000100001:10000100001:100002000030000200001 -11111111111:9:99999999999 -22222222222:9:199999999998 -33333333333:9:299999999997 -44444444444:9:399999999996 -55555555555:9:499999999995 -66666666666:9:599999999994 -77777777777:9:699999999993 -88888888888:9:799999999992 -99999999999:9:899999999991 -+25:+25:625 -+12345:+12345:152399025 -+99999:+11111:1111088889 -9999:10000:99990000 -99999:100000:9999900000 -999999:1000000:999999000000 -9999999:10000000:99999990000000 -99999999:100000000:9999999900000000 -999999999:1000000000:999999999000000000 -9999999999:10000000000:99999999990000000000 -99999999999:100000000000:9999999999900000000000 -999999999999:1000000000000:999999999999000000000000 -9999999999999:10000000000000:99999999999990000000000000 -99999999999999:100000000000000:9999999999999900000000000000 -999999999999999:1000000000000000:999999999999999000000000000000 -9999999999999999:10000000000000000:99999999999999990000000000000000 -99999999999999999:100000000000000000:9999999999999999900000000000000000 -999999999999999999:1000000000000000000:999999999999999999000000000000000000 -9999999999999999999:10000000000000000000:99999999999999999990000000000000000000 -3:3.5:10.5^ -3.5:3:10.5^ -&bdiv-list -100:20:5,0 -4095:4095:1,0 --4095:-4095:1,0 -4095:-4095:-1,0 --4095:4095:-1,0 -123:2:61,1 -9:5:1,4 -9:4:2,1 -# inf handling and general remainder -5:8:0,5 -0:8:0,0 -11:2:5,1 -11:-2:-6,-1 --11:2:-6,1 -# see table in documentation in MBI -0:inf:0,0 -0:-inf:0,0 -5:inf:0,5 -5:-inf:-1,-inf --5:inf:-1,inf --5:-inf:0,-5 -inf:5:inf,NaN --inf:5:-inf,NaN -inf:-5:-inf,NaN --inf:-5:inf,NaN -5:5:1,0 --5:-5:1,0 -inf:inf:NaN,NaN --inf:-inf:NaN,NaN --inf:inf:NaN,NaN -inf:-inf:NaN,NaN -8:0:inf,8 -inf:0:inf,inf -# exceptions to remainder rule --8:0:-inf,-8 --inf:0:-inf,-inf -0:0:NaN,0 -&bdiv -abc:abc:NaN -abc:1:NaN -1:abc:NaN -0:0:NaN -# inf handling (see table in doc) -0:inf:0 -0:-inf:0 -5:inf:0 -5:-inf:-1 --5:inf:-1 --5:-inf:0 -inf:5:inf --inf:5:-inf -inf:-5:-inf --inf:-5:inf -5:5:1 --5:-5:1 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:inf -inf:0:inf --8:0:-inf --inf:0:-inf -11:2:5.5^ --11:-2:5.5^ --11:2:-5.5^ -11:-2:-5.5^ -0:1:0 -0:-1:0 -1:1:1 --1:-1:1 -1:-1:-1 --1:1:-1 -1:2:0.5^ -2:1:2 -1000000000:9:111111111.1111111111111111111111111111111^ -2000000000:9:222222222.2222222222222222222222222222222^ -3000000000:9:333333333.3333333333333333333333333333333^ -4000000000:9:444444444.4444444444444444444444444444444^ -5000000000:9:555555555.5555555555555555555555555555556^ -6000000000:9:666666666.6666666666666666666666666666667^ -7000000000:9:777777777.7777777777777777777777777777778^ -8000000000:9:888888888.8888888888888888888888888888889^ -9000000000:9:1000000000 -35500000:113:314159.2920353982300884955752212389380531^ -71000000:226:314159.2920353982300884955752212389380531^ -106500000:339:314159.2920353982300884955752212389380531^ -1000000000:3:333333333.3333333333333333333333333333333^ -+10:+5:2 -+100:+4:25 -+1000:+8:125 -+10000:+16:625 -999999999999:9:111111111111 -999999999999:99:10101010101 -999999999999:999:1001001001 -999999999999:9999:100010001 -999999999999999:99999:10000100001 -+1111088889:99999:11111 --5:-3:1.666666666666666666666666666666666666667^ --5:3:-1.666666666666666666666666666666666666667^ -4:3:1.333333333333333333333333333333333333333^ -4:-3:-1.333333333333333333333333333333333333333^ -1:3:0.3333333333333333333333333333333333333333^ -1:-3:-0.3333333333333333333333333333333333333333^ --2:-3:0.6666666666666666666666666666666666666667^ --2:3:-0.6666666666666666666666666666666666666667^ -8:5:1.6^ --8:5:-1.6^ -14:-3:-4.666666666666666666666666666666666666667^ --14:3:-4.666666666666666666666666666666666666667^ --14:-3:4.666666666666666666666666666666666666667^ -14:3:4.666666666666666666666666666666666666667^ -# bug in Calc with '99999' vs $BASE-1 -#10000000000000000000000000000000000000000000000000000000000000000000000000000000000:10000000375084540248994272022843165711074:999999962491547381984643365663244474111576 -12:24:0.5^ -&bmod -# inf handling, see table in doc -0:inf:0 -0:-inf:0 -5:inf:5 -5:-inf:-inf --5:inf:inf --5:-inf:-5 -inf:5:NaN --inf:5:NaN -inf:-5:NaN --inf:-5:NaN -5:5:0 --5:-5:0 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:8 -inf:0:inf --inf:0:-inf --8:0:-8 -0:0:0 -abc:abc:NaN -abc:1:abc:NaN -1:abc:NaN -0:1:0 -1:0:1 -0:-1:0 --1:0:-1 -1:1:0 --1:-1:0 -1:-1:0 --1:1:0 -1:2:1 -2:1:0 -1000000000:9:1 -2000000000:9:2 -3000000000:9:3 -4000000000:9:4 -5000000000:9:5 -6000000000:9:6 -7000000000:9:7 -8000000000:9:8 -9000000000:9:0 -35500000:113:33 -71000000:226:66 -106500000:339:99 -1000000000:3:1 -10:5:0 -100:4:0 -1000:8:0 -10000:16:0 -999999999999:9:0 -999999999999:99:0 -999999999999:999:0 -999999999999:9999:0 -999999999999999:99999:0 --9:+5:1 -+9:-5:-1 --9:-5:-4 --5:3:1 --2:3:1 -4:3:1 -1:3:1 --5:-3:-2 --2:-3:-2 -4:-3:-2 -1:-3:-2 -4095:4095:0 -100041000510123:3:0 -152403346:12345:4321 -9:5:4 -&bgcd -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:0 -+0:+1:1 -+1:+0:1 -+1:+1:1 -+2:+3:1 -+3:+2:1 --3:+2:1 -100:625:25 -4096:81:1 -1034:804:2 -27:90:56:1 -27:90:54:9 -&blcm -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:NaN -+1:+0:0 -+0:+1:0 -+27:+90:270 -+1034:+804:415668 -&band -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:0 -3:2:2 -+8:+2:0 -+281474976710656:0:0 -+281474976710656:1:0 -+281474976710656:+281474976710656:281474976710656 --2:-3:-4 --1:-1:-1 --6:-6:-6 --7:-4:-8 --7:4:0 --4:7:4 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0x0xFFFF -0xFFFFFF:0xFFFFFF:0x0xFFFFFF -0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF -0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0x0xF0F0 -0x0F0F:0x0F0F:0x0x0F0F -0xF0F0F0:0xF0F0F0:0x0xF0F0F0 -0x0F0F0F:0x0F0F0F:0x0x0F0F0F -0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 -0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F -0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F -0x1F0F0F0F0F0F:0x3F0F0F0F0F0F:0x0x1F0F0F0F0F0F -&bior -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:3 -+8:+2:10 -+281474976710656:0:281474976710656 -+281474976710656:1:281474976710657 -+281474976710656:281474976710656:281474976710656 --2:-3:-1 --1:-1:-1 --6:-6:-6 --7:4:-3 --4:7:-1 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0x0xFFFF -0xFFFFFF:0xFFFFFF:0x0xFFFFFF -0xFFFFFFFF:0xFFFFFFFF:0x0xFFFFFFFF -0xFFFFFFFFFF:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0:0xFFFF:0x0xFFFF -0:0xFFFFFF:0x0xFFFFFF -0:0xFFFFFFFF:0x0xFFFFFFFF -0:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xFFFF:0:0x0xFFFF -0xFFFFFF:0:0x0xFFFFFF -0xFFFFFFFF:0:0x0xFFFFFFFF -0xFFFFFFFFFF:0:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0x0xF0F0 -0x0F0F:0x0F0F:0x0x0F0F -0xF0F0:0x0F0F:0x0xFFFF -0xF0F0F0:0xF0F0F0:0x0xF0F0F0 -0x0F0F0F:0x0F0F0F:0x0x0F0F0F -0x0F0F0F:0xF0F0F0:0x0xFFFFFF -0xF0F0F0F0:0xF0F0F0F0:0x0xF0F0F0F0 -0x0F0F0F0F:0x0F0F0F0F:0x0x0F0F0F0F -0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF -0xF0F0F0F0F0:0xF0F0F0F0F0:0x0xF0F0F0F0F0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0x0x0F0F0F0F0F -0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0x0xF0F0F0F0F0F0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0x0x0F0F0F0F0F0F -0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -0x1F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -&bxor -abc:abc:NaN -abc:0:NaN -0:abc:NaN -1:2:3 -+8:+2:10 -+281474976710656:0:281474976710656 -+281474976710656:1:281474976710657 -+281474976710656:281474976710656:0 --2:-3:3 --1:-1:0 --6:-6:0 --7:4:-3 --4:7:-5 -4:-7:-3 --4:-7:5 -# equal arguments are treated special, so also do some test with unequal ones -0xFFFF:0xFFFF:0 -0xFFFFFF:0xFFFFFF:0 -0xFFFFFFFF:0xFFFFFFFF:0 -0xFFFFFFFFFF:0xFFFFFFFFFF:0 -0xFFFFFFFFFFFF:0xFFFFFFFFFFFF:0 -0:0xFFFF:0x0xFFFF -0:0xFFFFFF:0x0xFFFFFF -0:0xFFFFFFFF:0x0xFFFFFFFF -0:0xFFFFFFFFFF:0x0xFFFFFFFFFF -0:0xFFFFFFFFFFFF:0x0xFFFFFFFFFFFF -0xFFFF:0:0x0xFFFF -0xFFFFFF:0:0x0xFFFFFF -0xFFFFFFFF:0:0x0xFFFFFFFF -0xFFFFFFFFFF:0:0x0xFFFFFFFFFF -0xFFFFFFFFFFFF:0:0x0xFFFFFFFFFFFF -0xF0F0:0xF0F0:0 -0x0F0F:0x0F0F:0 -0xF0F0:0x0F0F:0x0xFFFF -0xF0F0F0:0xF0F0F0:0 -0x0F0F0F:0x0F0F0F:0 -0x0F0F0F:0xF0F0F0:0x0xFFFFFF -0xF0F0F0F0:0xF0F0F0F0:0 -0x0F0F0F0F:0x0F0F0F0F:0 -0x0F0F0F0F:0xF0F0F0F0:0x0xFFFFFFFF -0xF0F0F0F0F0:0xF0F0F0F0F0:0 -0x0F0F0F0F0F:0x0F0F0F0F0F:0 -0x0F0F0F0F0F:0xF0F0F0F0F0:0x0xFFFFFFFFFF -0xF0F0F0F0F0F0:0xF0F0F0F0F0F0:0 -0x0F0F0F0F0F0F:0x0F0F0F0F0F0F:0 -0x0F0F0F0F0F0F:0xF0F0F0F0F0F0:0x0xFFFFFFFFFFFF -&bnot -abc:NaN -+0:-1 -+8:-9 -+281474976710656:-281474976710657 --1:0 --2:1 --12:11 -&digit -0:0:0 -12:0:2 -12:1:1 -123:0:3 -123:1:2 -123:2:1 -123:-1:1 -123:-2:2 -123:-3:3 -123456:0:6 -123456:1:5 -123456:2:4 -123456:3:3 -123456:4:2 -123456:5:1 -123456:-1:1 -123456:-2:2 -123456:-3:3 -100000:-3:0 -100000:0:0 -100000:1:0 -&mantissa -abc:NaN -1e4:1 -2e0:2 -123:123 --1:-1 --2:-2 -+inf:inf --inf:-inf -&exponent -abc:NaN -1e4:4 -2e0:0 -123:0 --1:0 --2:0 -0:1 -+inf:inf --inf:inf -&parts -abc:NaN,NaN -1e4:1,4 -2e0:2,0 -123:123,0 --1:-1,0 --2:-2,0 -0:0,1 -+inf:inf,inf --inf:-inf,inf -&bfac --1:NaN -NaNfac:NaN -+inf:inf --inf:NaN -0:1 -1:1 -2:2 -3:6 -4:24 -5:120 -6:720 -10:3628800 -11:39916800 -12:479001600 -&bpow -abc:12:NaN -12:abc:NaN -0:0:1 -0:1:0 -0:2:0 -0:-1:inf -0:-2:inf -1:0:1 -1:1:1 -1:2:1 -1:3:1 -1:-1:1 -1:-2:1 -1:-3:1 -2:0:1 -2:1:2 -2:2:4 -2:3:8 -3:3:27 -2:-1:0.5^ --2:-1:-0.5^ -2:-2:0.25^ -# Y is even => result positive --2:-2:0.25^ -# Y is odd => result negative --2:-3:-0.125^ -+inf:1234500012:inf --inf:1234500012:inf --inf:1234500013:-inf -+inf:-12345000123:inf --inf:-12345000123:-inf -# 1 ** -x => 1 / (1 ** x) --1:0:1 --2:0:1 --1:1:-1 --1:2:1 --1:3:-1 --1:4:1 --1:5:-1 --1:-1:-1 --1:-2:1 --1:-3:-1 --1:-4:1 --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 --3:2:9 --3:3:-27 --3:4:81 --3:5:-243 -10:2:100 -10:3:1000 -10:4:10000 -10:5:100000 -10:6:1000000 -10:7:10000000 -10:8:100000000 -10:9:1000000000 -10:20:100000000000000000000 -123456:2:15241383936 -#2:0.5:1.41^ -&length -100:3 -10:2 -1:1 -0:1 -12345:5 -10000000000000000:17 --123:3 -215960156869840440586892398248:30 -# broot always upgrades -&broot -144:2:12^ -123:2:11.09053650640941716205160010260993291846^ -# bsqrt always upgrades -&bsqrt -145:12.04159457879229548012824103037860805243^ -144:12^ -143:11.95826074310139802112984075619561661399^ -16:4 -170:13.03840481040529742916594311485836883306^ -169:13 -168:12.96148139681572046193193487217599331541^ -4:2 -3:1.732050807568877293527446341505872366943^ -2:1.41421356237309504880168872420969807857^ -9:3 -12:3.464101615137754587054892683011744733886^ -256:16 -100000000:10000 -4000000000000:2000000 -152399026:12345.00004050222755607815159966235881398^ -152399025:12345 -152399024:12344.99995949777231103967404745303741942^ -1:1 -0:0 --2:NaN --123:NaN -Nan:NaN -+inf:inf --inf:NaN -&bround -$round_mode('trunc') -0:12:0 -NaNbround:12:NaN -+inf:12:inf --inf:12:-inf -1234:0:1234 -1234:2:1200 -123456:4:123400 -123456:5:123450 -123456:6:123456 -+10123456789:5:10123000000 --10123456789:5:-10123000000 -+10123456789:9:10123456700 --10123456789:9:-10123456700 -+101234500:6:101234000 --101234500:6:-101234000 -#+101234500:-4:101234000 -#-101234500:-4:-101234000 -$round_mode('zero') -+20123456789:5:20123000000 --20123456789:5:-20123000000 -+20123456789:9:20123456800 --20123456789:9:-20123456800 -+201234500:6:201234000 --201234500:6:-201234000 -#+201234500:-4:201234000 -#-201234500:-4:-201234000 -+12345000:4:12340000 --12345000:4:-12340000 -$round_mode('+inf') -+30123456789:5:30123000000 --30123456789:5:-30123000000 -+30123456789:9:30123456800 --30123456789:9:-30123456800 -+301234500:6:301235000 --301234500:6:-301234000 -#+301234500:-4:301235000 -#-301234500:-4:-301234000 -+12345000:4:12350000 --12345000:4:-12340000 -$round_mode('-inf') -+40123456789:5:40123000000 --40123456789:5:-40123000000 -+40123456789:9:40123456800 --40123456789:9:-40123456800 -+401234500:6:401234000 -+401234500:6:401234000 -#-401234500:-4:-401235000 -#-401234500:-4:-401235000 -+12345000:4:12340000 --12345000:4:-12350000 -$round_mode('odd') -+50123456789:5:50123000000 --50123456789:5:-50123000000 -+50123456789:9:50123456800 --50123456789:9:-50123456800 -+501234500:6:501235000 --501234500:6:-501235000 -#+501234500:-4:501235000 -#-501234500:-4:-501235000 -+12345000:4:12350000 --12345000:4:-12350000 -$round_mode('even') -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601234000 --601234500:6:-601234000 -#+601234500:-4:601234000 -#-601234500:-4:-601234000 -#-601234500:-9:0 -#-501234500:-9:0 -#-601234500:-8:0 -#-501234500:-8:0 -+1234567:7:1234567 -+1234567:6:1234570 -+12345000:4:12340000 --12345000:4:-12340000 -&is_zero -0:1 -NaNzero:0 -+inf:0 --inf:0 -123:0 --1:0 -1:0 -&is_one -0:0 -NaNone:0 -+inf:0 --inf:0 -1:1 -2:0 --1:0 --2:0 -# floor and ceil are pretty pointless in integer space, but play safe -&bfloor -0:0 -NaNfloor:NaN -+inf:inf --inf:-inf --1:-1 --2:-2 -2:2 -3:3 -abc:NaN -&bceil -NaNceil:NaN -+inf:inf --inf:-inf -0:0 --1:-1 --2:-2 -2:2 -3:3 -abc:NaN -&bint -NaN:NaN -+inf:inf --inf:-inf -0:0 --1:-1 --2:-2 -2:2 -3:3 -&as_hex -128:0x80 --128:-0x80 -0:0x0 --0:0x0 -1:0x1 -0x123456789123456789:0x123456789123456789 -+inf:inf --inf:-inf -NaNas_hex:NaN -&as_bin -128:0b10000000 --128:-0b10000000 -0:0b0 --0:0b0 -1:0b1 -0b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101 -+inf:inf --inf:-inf -NaNas_bin:NaN diff --git a/dist/Math-BigInt/t/upgrade.t b/dist/Math-BigInt/t/upgrade.t deleted file mode 100644 index d209879a89..0000000000 --- a/dist/Math-BigInt/t/upgrade.t +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 2124 - + 2; # our own tests - -use Math::BigInt upgrade => 'Math::BigFloat'; -use Math::BigFloat; - -use vars qw ($scale $class $try $x $y $f @args $ans $ans1 $ans1_str $setup - $ECL $CL); -$class = "Math::BigInt"; -$CL = "Math::BigInt::Calc"; -$ECL = "Math::BigFloat"; - -is (Math::BigInt->upgrade(),'Math::BigFloat'); -is (Math::BigInt->downgrade()||'',''); - -require 't/upgrade.inc'; # all tests here for sharing diff --git a/dist/Math-BigInt/t/upgrade2.t b/dist/Math-BigInt/t/upgrade2.t deleted file mode 100644 index cdc8d0edf4..0000000000 --- a/dist/Math-BigInt/t/upgrade2.t +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl -w - -# Test 2 levels of upgrade classes. This used to cause a segv. - -use Test::More tests => 1; - -use Math::BigInt upgrade => 'Math::BigFloat'; -use Math::BigFloat upgrade => 'Math::BigMouse'; - -no warnings 'once'; -@Math::BigMouse::ISA = 'Math::BigFloat'; - -() = sqrt Math::BigInt->new(2); -pass('sqrt on a big int does not segv if there are 2 upgrade levels'); diff --git a/dist/Math-BigInt/t/upgradef.t b/dist/Math-BigInt/t/upgradef.t deleted file mode 100644 index 611d9fad27..0000000000 --- a/dist/Math-BigInt/t/upgradef.t +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 6; - -############################################################################### -package Math::BigFloat::Test; - -use Math::BigFloat; -require Exporter; -use vars qw/@ISA/; -@ISA = qw/Exporter Math::BigFloat/; - -use overload; - -sub isa - { - my ($self,$class) = @_; - return if $class =~ /^Math::Big(Int|Float)/; # we aren't one of these - UNIVERSAL::isa($self,$class); - } - -sub bmul - { - return __PACKAGE__->new(123); - } - -sub badd - { - return __PACKAGE__->new(321); - } - -############################################################################### -package main; - -# use Math::BigInt upgrade => 'Math::BigFloat'; -use Math::BigFloat upgrade => 'Math::BigFloat::Test'; - -use vars qw ($scale $class $try $x $y $z $f @args $ans $ans1 $ans1_str $setup - $ECL $CL); -$class = "Math::BigFloat"; -$CL = "Math::BigInt::Calc"; -$ECL = "Math::BigFloat::Test"; - -is (Math::BigFloat->upgrade(),$ECL); -is (Math::BigFloat->downgrade()||'',''); - -$x = $class->new(123); $y = $ECL->new(123); $z = $x->bmul($y); -is (ref($z),$ECL); is ($z,123); - -$x = $class->new(123); $y = $ECL->new(123); $z = $x->badd($y); -is (ref($z),$ECL); is ($z,321); - - - -# not yet: -# require 'upgrade.inc'; # all tests here for sharing diff --git a/dist/Math-BigInt/t/use.t b/dist/Math-BigInt/t/use.t deleted file mode 100644 index 3d0b9e2cd6..0000000000 --- a/dist/Math-BigInt/t/use.t +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/perl -w - -# use Module(); doesn't call import() - thanx for cpan testers David. M. Town -# and Andreas Marcel Riechert for spotting it. It is fixed by the same code -# that fixes require Math::BigInt, but we make a test to be sure it really -# works. - -use strict; -use Test::More tests => 1; - -my ($try,$ans,$x); - -use Math::BigInt(); $x = Math::BigInt->new(1); ++$x; - -is ($x,2); - -# all tests done - -1; diff --git a/dist/Math-BigInt/t/use_lib1.t b/dist/Math-BigInt/t/use_lib1.t deleted file mode 100644 index 2045af1833..0000000000 --- a/dist/Math-BigInt/t/use_lib1.t +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/perl -w - -# see if using Math::BigInt and Math::BigFloat works together nicely. -# all use_lib*.t should be equivalent - -use strict; -use Test::More tests => 2; - -BEGIN { unshift @INC, 't'; } - -use Math::BigFloat lib => 'BareCalc'; - -is (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); - -is (Math::BigFloat->new(123)->badd(123),246); diff --git a/dist/Math-BigInt/t/use_lib2.t b/dist/Math-BigInt/t/use_lib2.t deleted file mode 100644 index 23239e10e9..0000000000 --- a/dist/Math-BigInt/t/use_lib2.t +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/perl -w - -# see if using Math::BigInt and Math::BigFloat works together nicely. -# all use_lib*.t should be equivalent - -use strict; -use Test::More tests => 2; - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt; -use Math::BigFloat lib => 'BareCalc'; - -is (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); - -is (Math::BigFloat->new(123)->badd(123),246); diff --git a/dist/Math-BigInt/t/use_lib3.t b/dist/Math-BigInt/t/use_lib3.t deleted file mode 100644 index 95263a0dcd..0000000000 --- a/dist/Math-BigInt/t/use_lib3.t +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/perl -w - -# see if using Math::BigInt and Math::BigFloat works together nicely. -# all use_lib*.t should be equivalent - -use strict; -use Test::More tests => 2; - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt lib => 'BareCalc'; -use Math::BigFloat; - -is (Math::BigInt->config()->{lib},'Math::BigInt::BareCalc'); - -is (Math::BigFloat->new(123)->badd(123),246); diff --git a/dist/Math-BigInt/t/use_lib4.t b/dist/Math-BigInt/t/use_lib4.t deleted file mode 100644 index a0d0564a35..0000000000 --- a/dist/Math-BigInt/t/use_lib4.t +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/perl -w - -# see if using Math::BigInt and Math::BigFloat works together nicely. -# all use_lib*.t should be equivalent, except this, since the later overrides -# the former lib statement - -use strict; -use Test::More tests => 2; - -BEGIN { unshift @INC, 't'; } - -use Math::BigInt lib => 'BareCalc'; -use Math::BigFloat lib => 'Calc'; - -is (Math::BigInt->config()->{lib},'Math::BigInt::Calc'); - -is (Math::BigFloat->new(123)->badd(123),246); diff --git a/dist/Math-BigInt/t/use_mbfw.t b/dist/Math-BigInt/t/use_mbfw.t deleted file mode 100644 index afa3733250..0000000000 --- a/dist/Math-BigInt/t/use_mbfw.t +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/perl -w - -# check that using BigFloat with "with" and "lib" at the same time works -# broken in versions up to v1.63 - -use strict; -use Test::More tests => 2; - -BEGIN { unshift @INC, 't'; } - -# the replacement lib can handle the lib statement, but it could also ignore -# it completely, for instance, when it is a 100% replacement for BigInt, but -# doesn't know the concept of alternative libs. But it still needs to cope -# with "lib => ". SubClass does record it, so we test here essential if -# BigFloat hands the lib properly down, any more is outside out testing reach. - -use Math::BigFloat with => 'Math::BigInt::Subclass', lib => 'BareCalc'; - -is (Math::BigFloat->config()->{with}, 'Math::BigInt::BareCalc' ); - -# is ($Math::BigInt::Subclass::lib, 'BareCalc' ); - -# it never arrives here, but that is a design decision in SubClass -is (Math::BigInt->config->{lib}, 'Math::BigInt::BareCalc' ); - -# all tests done diff --git a/dist/Math-BigInt/t/with_sub.t b/dist/Math-BigInt/t/with_sub.t deleted file mode 100644 index d90bbbc7dd..0000000000 --- a/dist/Math-BigInt/t/with_sub.t +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/perl -w - -# Test use Math::BigFloat with => 'Math::BigInt::SomeSubclass'; - -use strict; -use Test::More tests => 2340 + 1; - -use Math::BigFloat with => 'Math::BigInt::Subclass', lib => 'Calc'; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigFloat"; -$CL = "Math::BigInt::Calc"; - -# the with argument is ignored -is (Math::BigFloat->config()->{with}, 'Math::BigInt::Calc'); - -require 't/bigfltpm.inc'; # all tests here for sharing diff --git a/dist/Math-BigRat/lib/Math/BigRat.pm b/dist/Math-BigRat/lib/Math/BigRat.pm deleted file mode 100644 index e0c12b31f3..0000000000 --- a/dist/Math-BigRat/lib/Math/BigRat.pm +++ /dev/null @@ -1,2202 +0,0 @@ - -# -# "Tax the rat farms." - Lord Vetinari -# - -# The following hash values are used: -# sign : +,-,NaN,+inf,-inf -# _d : denominator -# _n : numerator (value = _n/_d) -# _a : accuracy -# _p : precision -# You should not look at the innards of a BigRat - use the methods for this. - -package Math::BigRat; - -# anything older is untested, and unlikely to work -use 5.006; -use strict; -use Carp (); - -use Math::BigFloat; -use vars qw($VERSION @ISA $upgrade $downgrade - $accuracy $precision $round_mode $div_scale $_trap_nan $_trap_inf); - -@ISA = qw(Math::BigFloat); - -$VERSION = '0.260801'; -$VERSION = eval $VERSION; - -# inherit overload from Math::BigFloat, but disable the bitwise ops that don't -# make much sense for rationals unless they're truncated or something first - -use overload - map { - my $op = $_; - ($op => sub { - Carp::croak("bitwise operation $op not supported in Math::BigRat"); - }); - } qw(& | ^ ~ << >> &= |= ^= <<= >>=); - -BEGIN - { - *objectify = \&Math::BigInt::objectify; # inherit this from BigInt - *AUTOLOAD = \&Math::BigFloat::AUTOLOAD; # can't inherit AUTOLOAD - # we inherit these from BigFloat because currently it is not possible - # that MBF has a different $MBI variable than we, because MBF also uses - # Math::BigInt::config->('lib'); (there is always only one library loaded) - *_e_add = \&Math::BigFloat::_e_add; - *_e_sub = \&Math::BigFloat::_e_sub; - *as_int = \&as_number; - *is_pos = \&is_positive; - *is_neg = \&is_negative; - } - -############################################################################## -# Global constants and flags. Access these only via the accessor methods! - -$accuracy = $precision = undef; -$round_mode = 'even'; -$div_scale = 40; -$upgrade = undef; -$downgrade = undef; - -# These are internally, and not to be used from the outside at all! - -$_trap_nan = 0; # are NaNs ok? set w/ config() -$_trap_inf = 0; # are infs ok? set w/ config() - -# the package we are using for our private parts, defaults to: -# Math::BigInt->config()->{lib} -my $MBI = 'Math::BigInt::Calc'; - -my $nan = 'NaN'; -my $class = 'Math::BigRat'; - -sub isa - { - return 0 if $_[1] =~ /^Math::Big(Int|Float)/; # we aren't - UNIVERSAL::isa(@_); - } - -############################################################################## - -sub _new_from_float - { - # turn a single float input into a rational number (like '0.1') - my ($self,$f) = @_; - - return $self->bnan() if $f->is_nan(); - return $self->binf($f->{sign}) if $f->{sign} =~ /^[+-]inf$/; - - $self->{_n} = $MBI->_copy( $f->{_m} ); # mantissa - $self->{_d} = $MBI->_one(); - $self->{sign} = $f->{sign} || '+'; - if ($f->{_es} eq '-') - { - # something like Math::BigRat->new('0.1'); - # 1 / 1 => 1/10 - $MBI->_lsft ( $self->{_d}, $f->{_e} ,10); - } - else - { - # something like Math::BigRat->new('10'); - # 1 / 1 => 10/1 - $MBI->_lsft ( $self->{_n}, $f->{_e} ,10) unless - $MBI->_is_zero($f->{_e}); - } - $self; - } - -sub new - { - # create a Math::BigRat - my $class = shift; - - my ($n,$d) = @_; - - my $self = { }; bless $self,$class; - - # input like (BigInt) or (BigFloat): - if ((!defined $d) && (ref $n) && (!$n->isa('Math::BigRat'))) - { - if ($n->isa('Math::BigFloat')) - { - $self->_new_from_float($n); - } - if ($n->isa('Math::BigInt')) - { - # TODO: trap NaN, inf - $self->{_n} = $MBI->_copy($n->{value}); # "mantissa" = N - $self->{_d} = $MBI->_one(); # d => 1 - $self->{sign} = $n->{sign}; - } - if ($n->isa('Math::BigInt::Lite')) - { - # TODO: trap NaN, inf - $self->{sign} = '+'; $self->{sign} = '-' if $$n < 0; - $self->{_n} = $MBI->_new(abs($$n)); # "mantissa" = N - $self->{_d} = $MBI->_one(); # d => 1 - } - return $self->bnorm(); # normalize (120/1 => 12/10) - } - - # input like (BigInt,BigInt) or (BigLite,BigLite): - if (ref($d) && ref($n)) - { - # do N first (for $self->{sign}): - if ($n->isa('Math::BigInt')) - { - # TODO: trap NaN, inf - $self->{_n} = $MBI->_copy($n->{value}); # "mantissa" = N - $self->{sign} = $n->{sign}; - } - elsif ($n->isa('Math::BigInt::Lite')) - { - # TODO: trap NaN, inf - $self->{sign} = '+'; $self->{sign} = '-' if $$n < 0; - $self->{_n} = $MBI->_new(abs($$n)); # "mantissa" = $n - } - else - { - require Carp; - Carp::croak(ref($n) . " is not a recognized object format for Math::BigRat->new"); - } - # now D: - if ($d->isa('Math::BigInt')) - { - # TODO: trap NaN, inf - $self->{_d} = $MBI->_copy($d->{value}); # "mantissa" = D - # +/+ or -/- => +, +/- or -/+ => - - $self->{sign} = $d->{sign} ne $self->{sign} ? '-' : '+'; - } - elsif ($d->isa('Math::BigInt::Lite')) - { - # TODO: trap NaN, inf - $self->{_d} = $MBI->_new(abs($$d)); # "mantissa" = D - my $ds = '+'; $ds = '-' if $$d < 0; - # +/+ or -/- => +, +/- or -/+ => - - $self->{sign} = $ds ne $self->{sign} ? '-' : '+'; - } - else - { - require Carp; - Carp::croak(ref($d) . " is not a recognized object format for Math::BigRat->new"); - } - return $self->bnorm(); # normalize (120/1 => 12/10) - } - return $n->copy() if ref $n; # already a BigRat - - if (!defined $n) - { - $self->{_n} = $MBI->_zero(); # undef => 0 - $self->{_d} = $MBI->_one(); - $self->{sign} = '+'; - return $self; - } - - # string input with / delimiter - if ($n =~ /\s*\/\s*/) - { - return $class->bnan() if $n =~ /\/.*\//; # 1/2/3 isn't valid - return $class->bnan() if $n =~ /\/\s*$/; # 1/ isn't valid - ($n,$d) = split (/\//,$n); - # try as BigFloats first - if (($n =~ /[\.eE]/) || ($d =~ /[\.eE]/)) - { - local $Math::BigFloat::accuracy = undef; - local $Math::BigFloat::precision = undef; - - # one of them looks like a float - my $nf = Math::BigFloat->new($n,undef,undef); - $self->{sign} = '+'; - return $self->bnan() if $nf->is_nan(); - - $self->{_n} = $MBI->_copy( $nf->{_m} ); # get mantissa - - # now correct $self->{_n} due to $n - my $f = Math::BigFloat->new($d,undef,undef); - return $self->bnan() if $f->is_nan(); - $self->{_d} = $MBI->_copy( $f->{_m} ); - - # calculate the difference between nE and dE - my $diff_e = $nf->exponent()->bsub( $f->exponent); - if ($diff_e->is_negative()) - { - # < 0: mul d with it - $MBI->_lsft( $self->{_d}, $MBI->_new( $diff_e->babs()), 10); - } - elsif (!$diff_e->is_zero()) - { - # > 0: mul n with it - $MBI->_lsft( $self->{_n}, $MBI->_new( $diff_e), 10); - } - } - else - { - # both d and n look like (big)ints - - $self->{sign} = '+'; # no sign => '+' - $self->{_n} = undef; - $self->{_d} = undef; - if ($n =~ /^([+-]?)0*([0-9]+)\z/) # first part ok? - { - $self->{sign} = $1 || '+'; # no sign => '+' - $self->{_n} = $MBI->_new($2 || 0); - } - - if ($d =~ /^([+-]?)0*([0-9]+)\z/) # second part ok? - { - $self->{sign} =~ tr/+-/-+/ if ($1 || '') eq '-'; # negate if second part neg. - $self->{_d} = $MBI->_new($2 || 0); - } - - if (!defined $self->{_n} || !defined $self->{_d}) - { - $d = Math::BigInt->new($d,undef,undef) unless ref $d; - $n = Math::BigInt->new($n,undef,undef) unless ref $n; - - if ($n->{sign} =~ /^[+-]$/ && $d->{sign} =~ /^[+-]$/) - { - # both parts are ok as integers (weird things like ' 1e0' - $self->{_n} = $MBI->_copy($n->{value}); - $self->{_d} = $MBI->_copy($d->{value}); - $self->{sign} = $n->{sign}; - $self->{sign} =~ tr/+-/-+/ if $d->{sign} eq '-'; # -1/-2 => 1/2 - return $self->bnorm(); - } - - $self->{sign} = '+'; # a default sign - return $self->bnan() if $n->is_nan() || $d->is_nan(); - - # handle inf cases: - if ($n->is_inf() || $d->is_inf()) - { - if ($n->is_inf()) - { - return $self->bnan() if $d->is_inf(); # both are inf => NaN - my $s = '+'; # '+inf/+123' or '-inf/-123' - $s = '-' if substr($n->{sign},0,1) ne $d->{sign}; - # +-inf/123 => +-inf - return $self->binf($s); - } - # 123/inf => 0 - return $self->bzero(); - } - } - } - - return $self->bnorm(); - } - - # simple string input - if (($n =~ /[\.eE]/) && $n !~ /^0x/) - { - # looks like a float, quacks like a float, so probably is a float - $self->{sign} = 'NaN'; - local $Math::BigFloat::accuracy = undef; - local $Math::BigFloat::precision = undef; - $self->_new_from_float(Math::BigFloat->new($n,undef,undef)); - } - else - { - # for simple forms, use $MBI directly - if ($n =~ /^([+-]?)0*([0-9]+)\z/) - { - $self->{sign} = $1 || '+'; - $self->{_n} = $MBI->_new($2 || 0); - $self->{_d} = $MBI->_one(); - } - else - { - my $n = Math::BigInt->new($n,undef,undef); - $self->{_n} = $MBI->_copy($n->{value}); - $self->{_d} = $MBI->_one(); - $self->{sign} = $n->{sign}; - return $self->bnan() if $self->{sign} eq 'NaN'; - return $self->binf($self->{sign}) if $self->{sign} =~ /^[+-]inf$/; - } - } - $self->bnorm(); - } - -sub copy - { - # if two arguments, the first one is the class to "swallow" subclasses - my ($c,$x) = @_; - - if (scalar @_ == 1) - { - $x = $_[0]; - $c = ref($x); - } - return unless ref($x); # only for objects - - my $self = bless {}, $c; - - $self->{sign} = $x->{sign}; - $self->{_d} = $MBI->_copy($x->{_d}); - $self->{_n} = $MBI->_copy($x->{_n}); - $self->{_a} = $x->{_a} if defined $x->{_a}; - $self->{_p} = $x->{_p} if defined $x->{_p}; - $self; - } - -############################################################################## - -sub config - { - # return (later set?) configuration data as hash ref - my $class = shift || 'Math::BigRat'; - - if (@_ == 1 && ref($_[0]) ne 'HASH') - { - my $cfg = $class->SUPER::config(); - return $cfg->{$_[0]}; - } - - my $cfg = $class->SUPER::config(@_); - - # now we need only to override the ones that are different from our parent - $cfg->{class} = $class; - $cfg->{with} = $MBI; - $cfg; - } - -############################################################################## - -sub bstr - { - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc - { - my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf - return $s; - } - - my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # '+3/2' => '3/2' - - return $s . $MBI->_str($x->{_n}) if $MBI->_is_one($x->{_d}); - $s . $MBI->_str($x->{_n}) . '/' . $MBI->_str($x->{_d}); - } - -sub bsstr - { - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc - { - my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf - return $s; - } - - my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # +3 vs 3 - $s . $MBI->_str($x->{_n}) . '/' . $MBI->_str($x->{_d}); - } - -sub bnorm - { - # reduce the number to the shortest form - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - # Both parts must be objects of whatever we are using today. - if ( my $c = $MBI->_check($x->{_n}) ) - { - require Carp; Carp::croak( - "n did not pass the self-check ($c) in bnorm()"); - } - if ( my $c = $MBI->_check($x->{_d}) ) - { - require Carp; Carp::croak( - "d did not pass the self-check ($c) in bnorm()"); - } - - # no normalize for NaN, inf etc. - return $x if $x->{sign} !~ /^[+-]$/; - - # normalize zeros to 0/1 - if ($MBI->_is_zero($x->{_n})) - { - $x->{sign} = '+'; # never leave a -0 - $x->{_d} = $MBI->_one() unless $MBI->_is_one($x->{_d}); - return $x; - } - - return $x if $MBI->_is_one($x->{_d}); # no need to reduce - - # reduce other numbers - my $gcd = $MBI->_copy($x->{_n}); - $gcd = $MBI->_gcd($gcd,$x->{_d}); - - if (!$MBI->_is_one($gcd)) - { - $x->{_n} = $MBI->_div($x->{_n},$gcd); - $x->{_d} = $MBI->_div($x->{_d},$gcd); - } - $x; - } - -############################################################################## -# sign manipulation - -sub bneg - { - # (BRAT or num_str) return BRAT - # negate number or make a negated number from string - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x if $x->modify('bneg'); - - # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN' - $x->{sign} =~ tr/+-/-+/ - unless ($x->{sign} eq '+' && $MBI->_is_zero($x->{_n})); - $x; - } - -############################################################################## -# special values - -sub _bnan - { - # used by parent class bnan() to initialize number to NaN - my $self = shift; - - if ($_trap_nan) - { - require Carp; - my $class = ref($self); - # "$self" below will stringify the object, this blows up if $self is a - # partial object (happens under trap_nan), so fix it beforehand - $self->{_d} = $MBI->_zero() unless defined $self->{_d}; - $self->{_n} = $MBI->_zero() unless defined $self->{_n}; - Carp::croak ("Tried to set $self to NaN in $class\::_bnan()"); - } - $self->{_n} = $MBI->_zero(); - $self->{_d} = $MBI->_zero(); - } - -sub _binf - { - # used by parent class bone() to initialize number to +inf/-inf - my $self = shift; - - if ($_trap_inf) - { - require Carp; - my $class = ref($self); - # "$self" below will stringify the object, this blows up if $self is a - # partial object (happens under trap_nan), so fix it beforehand - $self->{_d} = $MBI->_zero() unless defined $self->{_d}; - $self->{_n} = $MBI->_zero() unless defined $self->{_n}; - Carp::croak ("Tried to set $self to inf in $class\::_binf()"); - } - $self->{_n} = $MBI->_zero(); - $self->{_d} = $MBI->_zero(); - } - -sub _bone - { - # used by parent class bone() to initialize number to +1/-1 - my $self = shift; - $self->{_n} = $MBI->_one(); - $self->{_d} = $MBI->_one(); - } - -sub _bzero - { - # used by parent class bzero() to initialize number to 0 - my $self = shift; - $self->{_n} = $MBI->_zero(); - $self->{_d} = $MBI->_one(); - } - -############################################################################## -# mul/add/div etc - -sub badd - { - # add two rational numbers - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - # +inf + +inf => +inf, -inf + -inf => -inf - return $x->binf(substr($x->{sign},0,1)) - if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; - - # +inf + -inf or -inf + +inf => NaN - return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/); - - # 1 1 gcd(3,4) = 1 1*3 + 1*4 7 - # - + - = --------- = -- - # 4 3 4*3 12 - - # we do not compute the gcd() here, but simple do: - # 5 7 5*3 + 7*4 43 - # - + - = --------- = -- - # 4 3 4*3 12 - - # and bnorm() will then take care of the rest - - # 5 * 3 - $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_d}); - - # 7 * 4 - my $m = $MBI->_mul( $MBI->_copy( $y->{_n} ), $x->{_d} ); - - # 5 * 3 + 7 * 4 - ($x->{_n}, $x->{sign}) = _e_add( $x->{_n}, $m, $x->{sign}, $y->{sign}); - - # 4 * 3 - $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_d}); - - # normalize result, and possible round - $x->bnorm()->round(@r); - } - -sub bsub - { - # subtract two rational numbers - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - # flip sign of $x, call badd(), then flip sign of result - $x->{sign} =~ tr/+-/-+/ - unless $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); # not -0 - $x->badd($y,@r); # does norm and round - $x->{sign} =~ tr/+-/-+/ - unless $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); # not -0 - $x; - } - -sub bmul - { - # multiply two rational numbers - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x->bnan() if ($x->{sign} eq 'NaN' || $y->{sign} eq 'NaN'); - - # inf handling - if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) - { - return $x->bnan() if $x->is_zero() || $y->is_zero(); - # result will always be +-inf: - # +inf * +/+inf => +inf, -inf * -/-inf => +inf - # +inf * -/-inf => -inf, -inf * +/+inf => -inf - return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/); - return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/); - return $x->binf('-'); - } - - # x== 0 # also: or y == 1 or y == -1 - return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); - - # XXX TODO: - # According to Knuth, this can be optimized by doing gcd twice (for d and n) - # and reducing in one step. This would save us the bnorm() at the end. - - # 1 2 1 * 2 2 1 - # - * - = ----- = - = - - # 4 3 4 * 3 12 6 - - $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_n}); - $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_d}); - - # compute new sign - $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; - - $x->bnorm()->round(@r); - } - -sub bdiv - { - # (dividend: BRAT or num_str, divisor: BRAT or num_str) return - # (BRAT,BRAT) (quo,rem) or BRAT (only rem) - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bdiv'); - - my $wantarray = wantarray; # call only once - - # At least one argument is NaN. This is handled the same way as in - # Math::BigInt -> bdiv(). See the comments in the code implementing that - # method. - - if ($x -> is_nan() || $y -> is_nan()) { - return $wantarray ? ($x -> bnan(), $self -> bnan()) : $x -> bnan(); - } - - # Divide by zero and modulo zero. This is handled the same way as in - # Math::BigInt -> bdiv(). See the comments in the code implementing that - # method. - - if ($y -> is_zero()) { - my ($quo, $rem); - if ($wantarray) { - $rem = $x -> copy(); - } - if ($x -> is_zero()) { - $quo = $x -> bnan(); - } else { - $quo = $x -> binf($x -> {sign}); - } - return $wantarray ? ($quo, $rem) : $quo; - } - - # Numerator (dividend) is +/-inf. This is handled the same way as in - # Math::BigInt -> bdiv(). See the comments in the code implementing that - # method. - - if ($x -> is_inf()) { - my ($quo, $rem); - $rem = $self -> bnan() if $wantarray; - if ($y -> is_inf()) { - $quo = $x -> bnan(); - } else { - my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-'; - $quo = $x -> binf($sign); - } - return $wantarray ? ($quo, $rem) : $quo; - } - - # Denominator (divisor) is +/-inf. This is handled the same way as in - # Math::BigFloat -> bdiv(). See the comments in the code implementing that - # method. - - if ($y -> is_inf()) { - my ($quo, $rem); - if ($wantarray) { - if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { - $rem = $x -> copy(); - $quo = $x -> bzero(); - } else { - $rem = $self -> binf($y -> {sign}); - $quo = $x -> bone('-'); - } - return ($quo, $rem); - } else { - if ($y -> is_inf()) { - if ($x -> is_nan() || $x -> is_inf()) { - return $x -> bnan(); - } else { - return $x -> bzero(); - } - } - } - } - - # At this point, both the numerator and denominator are finite numbers, and - # the denominator (divisor) is non-zero. - - # x == 0? - return wantarray ? ($x,$self->bzero()) : $x if $x->is_zero(); - - # XXX TODO: list context, upgrade - # According to Knuth, this can be optimized by doing gcd twice (for d and n) - # and reducing in one step. This would save us the bnorm() at the end. - - # 1 1 1 3 - # - / - == - * - - # 4 3 4 1 - - $x->{_n} = $MBI->_mul( $x->{_n}, $y->{_d}); - $x->{_d} = $MBI->_mul( $x->{_d}, $y->{_n}); - - # compute new sign - $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; - - $x -> bnorm(); - if (wantarray) { - my $rem = $x -> copy(); - $x -> bfloor(); - $x -> round(@r); - $rem -> bsub($x -> copy()) -> bmul($y); - return $x, $rem; - } else { - $x -> round(@r); - return $x; - } - } - -sub bmod - { - # compute "remainder" (in Perl way) of $x / $y - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->modify('bmod'); - - # At least one argument is NaN. This is handled the same way as in - # Math::BigInt -> bmod(). - - if ($x -> is_nan() || $y -> is_nan()) { - return $x -> bnan(); - } - - # Modulo zero. This is handled the same way as in Math::BigInt -> bmod(). - - if ($y -> is_zero()) { - return $x; - } - - # Numerator (dividend) is +/-inf. This is handled the same way as in - # Math::BigInt -> bmod(). - - if ($x -> is_inf()) { - return $x -> bnan(); - } - - # Denominator (divisor) is +/-inf. This is handled the same way as in - # Math::BigInt -> bmod(). - - if ($y -> is_inf()) { - if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) { - return $x; - } else { - return $x -> binf($y -> sign()); - } - } - - # At this point, both the numerator and denominator are finite numbers, and - # the denominator (divisor) is non-zero. - - return $x if $x->is_zero(); # 0 / 7 = 0, mod 0 - - # Compute $x - $y * floor($x/$y). This can probably be optimized by working - # on a lower level. - - $x -> bsub($x -> copy() -> bdiv($y) -> bfloor() -> bmul($y)); - return $x -> round(@r); - } - -############################################################################## -# bdec/binc - -sub bdec - { - # decrement value (subtract 1) - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf - - if ($x->{sign} eq '-') - { - $x->{_n} = $MBI->_add( $x->{_n}, $x->{_d}); # -5/2 => -7/2 - } - else - { - if ($MBI->_acmp($x->{_n},$x->{_d}) < 0) # n < d? - { - # 1/3 -- => -2/3 - $x->{_n} = $MBI->_sub( $MBI->_copy($x->{_d}), $x->{_n}); - $x->{sign} = '-'; - } - else - { - $x->{_n} = $MBI->_sub($x->{_n}, $x->{_d}); # 5/2 => 3/2 - } - } - $x->bnorm()->round(@r); - } - -sub binc - { - # increment value (add 1) - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf - - if ($x->{sign} eq '-') - { - if ($MBI->_acmp($x->{_n},$x->{_d}) < 0) - { - # -1/3 ++ => 2/3 (overflow at 0) - $x->{_n} = $MBI->_sub( $MBI->_copy($x->{_d}), $x->{_n}); - $x->{sign} = '+'; - } - else - { - $x->{_n} = $MBI->_sub($x->{_n}, $x->{_d}); # -5/2 => -3/2 - } - } - else - { - $x->{_n} = $MBI->_add($x->{_n},$x->{_d}); # 5/2 => 7/2 - } - $x->bnorm()->round(@r); - } - -############################################################################## -# is_foo methods (the rest is inherited) - -sub is_int - { - # return true if arg (BRAT or num_str) is an integer - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't - $MBI->_is_one($x->{_d}); # x/y && y != 1 => no integer - 0; - } - -sub is_zero - { - # return true if arg (BRAT or num_str) is zero - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return 1 if $x->{sign} eq '+' && $MBI->_is_zero($x->{_n}); - 0; - } - -sub is_one - { - # return true if arg (BRAT or num_str) is +1 or -1 if signis given - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - my $sign = $_[2] || ''; $sign = '+' if $sign ne '-'; - return 1 - if ($x->{sign} eq $sign && $MBI->_is_one($x->{_n}) && $MBI->_is_one($x->{_d})); - 0; - } - -sub is_odd - { - # return true if arg (BFLOAT or num_str) is odd or false if even - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't - ($MBI->_is_one($x->{_d}) && $MBI->_is_odd($x->{_n})); # x/2 is not, but 3/1 - 0; - } - -sub is_even - { - # return true if arg (BINT or num_str) is even or false if odd - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't - return 1 if ($MBI->_is_one($x->{_d}) # x/3 is never - && $MBI->_is_even($x->{_n})); # but 4/1 is - 0; - } - -############################################################################## -# parts() and friends - -sub numerator - { - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - # NaN, inf, -inf - return Math::BigInt->new($x->{sign}) if ($x->{sign} !~ /^[+-]$/); - - my $n = Math::BigInt->new($MBI->_str($x->{_n})); $n->{sign} = $x->{sign}; - $n; - } - -sub denominator - { - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - # NaN - return Math::BigInt->new($x->{sign}) if $x->{sign} eq 'NaN'; - # inf, -inf - return Math::BigInt->bone() if $x->{sign} !~ /^[+-]$/; - - Math::BigInt->new($MBI->_str($x->{_d})); - } - -sub parts - { - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - my $c = 'Math::BigInt'; - - return ($c->bnan(),$c->bnan()) if $x->{sign} eq 'NaN'; - return ($c->binf(),$c->binf()) if $x->{sign} eq '+inf'; - return ($c->binf('-'),$c->binf()) if $x->{sign} eq '-inf'; - - my $n = $c->new( $MBI->_str($x->{_n})); - $n->{sign} = $x->{sign}; - my $d = $c->new( $MBI->_str($x->{_d})); - ($n,$d); - } - -sub length - { - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $nan unless $x->is_int(); - $MBI->_len($x->{_n}); # length(-123/1) => length(123) - } - -sub digit - { - my ($self,$x,$n) = ref($_[0]) ? (undef,$_[0],$_[1]) : objectify(1,@_); - - return $nan unless $x->is_int(); - $MBI->_digit($x->{_n},$n || 0); # digit(-123/1,2) => digit(123,2) - } - -############################################################################## -# special calc routines - -sub bceil - { - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - return $x if $x->{sign} !~ /^[+-]$/ || # not for NaN, inf - $MBI->_is_one($x->{_d}); # 22/1 => 22, 0/1 => 0 - - $x->{_n} = $MBI->_div($x->{_n},$x->{_d}); # 22/7 => 3/1 w/ truncate - $x->{_d} = $MBI->_one(); # d => 1 - $x->{_n} = $MBI->_inc($x->{_n}) - if $x->{sign} eq '+'; # +22/7 => 4/1 - $x->{sign} = '+' if $MBI->_is_zero($x->{_n}); # -0 => 0 - $x; - } - -sub bfloor - { - my ($self,$x) = ref($_[0]) ? (ref($_[0]),$_[0]) : objectify(1,@_); - - return $x if $x->{sign} !~ /^[+-]$/ || # not for NaN, inf - $MBI->_is_one($x->{_d}); # 22/1 => 22, 0/1 => 0 - - $x->{_n} = $MBI->_div($x->{_n},$x->{_d}); # 22/7 => 3/1 w/ truncate - $x->{_d} = $MBI->_one(); # d => 1 - $x->{_n} = $MBI->_inc($x->{_n}) - if $x->{sign} eq '-'; # -22/7 => -4/1 - $x; - } - -sub bfac - { - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - # if $x is not an integer - if (($x->{sign} ne '+') || (!$MBI->_is_one($x->{_d}))) - { - return $x->bnan(); - } - - $x->{_n} = $MBI->_fac($x->{_n}); - # since _d is 1, we don't need to reduce/norm the result - $x->round(@r); - } - -sub bpow - { - # power ($x ** $y) - - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - return $x if $x->{sign} =~ /^[+-]inf$/; # -inf/+inf ** x - return $x->bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan; - return $x->bone(@r) if $y->is_zero(); - return $x->round(@r) if $x->is_one() || $y->is_one(); - - if ($x->{sign} eq '-' && $MBI->_is_one($x->{_n}) && $MBI->_is_one($x->{_d})) - { - # if $x == -1 and odd/even y => +1/-1 - return $y->is_odd() ? $x->round(@r) : $x->babs()->round(@r); - # my Casio FX-5500L has a bug here: -1 ** 2 is -1, but -1 * -1 is 1; - } - # 1 ** -y => 1 / (1 ** |y|) - # so do test for negative $y after above's clause - - return $x->round(@r) if $x->is_zero(); # 0**y => 0 (if not y <= 0) - - # shortcut if y == 1/N (is then sqrt() respective broot()) - if ($MBI->_is_one($y->{_n})) - { - return $x->bsqrt(@r) if $MBI->_is_two($y->{_d}); # 1/2 => sqrt - return $x->broot($MBI->_str($y->{_d}),@r); # 1/N => root(N) - } - - # shortcut y/1 (and/or x/1) - if ($MBI->_is_one($y->{_d})) - { - # shortcut for x/1 and y/1 - if ($MBI->_is_one($x->{_d})) - { - $x->{_n} = $MBI->_pow($x->{_n},$y->{_n}); # x/1 ** y/1 => (x ** y)/1 - if ($y->{sign} eq '-') - { - # 0.2 ** -3 => 1/(0.2 ** 3) - ($x->{_n},$x->{_d}) = ($x->{_d},$x->{_n}); # swap - } - # correct sign; + ** + => + - if ($x->{sign} eq '-') - { - # - * - => +, - * - * - => - - $x->{sign} = '+' if $MBI->_is_even($y->{_n}); - } - return $x->round(@r); - } - # x/z ** y/1 - $x->{_n} = $MBI->_pow($x->{_n},$y->{_n}); # 5/2 ** y/1 => 5 ** y / 2 ** y - $x->{_d} = $MBI->_pow($x->{_d},$y->{_n}); - if ($y->{sign} eq '-') - { - # 0.2 ** -3 => 1/(0.2 ** 3) - ($x->{_n},$x->{_d}) = ($x->{_d},$x->{_n}); # swap - } - # correct sign; + ** + => + - if ($x->{sign} eq '-') - { - # - * - => +, - * - * - => - - $x->{sign} = '+' if $MBI->_is_even($y->{_n}); - } - return $x->round(@r); - } - -# print STDERR "# $x $y\n"; - - # otherwise: - - # n/d n ______________ - # a/b = -\/ (a/b) ** d - - # (a/b) ** n == (a ** n) / (b ** n) - $MBI->_pow($x->{_n}, $y->{_n} ); - $MBI->_pow($x->{_d}, $y->{_n} ); - - return $x->broot($MBI->_str($y->{_d}),@r); # n/d => root(n) - } - -sub blog - { - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,$class,@_); - } - - # blog(1,Y) => 0 - return $x->bzero() if $x->is_one() && $y->{sign} eq '+'; - - # $x <= 0 => NaN - return $x->bnan() if $x->is_zero() || $x->{sign} ne '+' || $y->{sign} ne '+'; - - if ($x->is_int() && $y->is_int()) - { - return $self->new($x->as_number()->blog($y->as_number(),@r)); - } - - # do it with floats - $x->_new_from_float( $x->_as_float()->blog(Math::BigFloat->new("$y"),@r) ); - } - -sub bexp - { - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,$class,@_); - } - - return $x->binf(@r) if $x->{sign} eq '+inf'; - return $x->bzero(@r) if $x->{sign} eq '-inf'; - - # we need to limit the accuracy to protect against overflow - my $fallback = 0; - my ($scale,@params); - ($x,@params) = $x->_find_round_parameters(@r); - - # also takes care of the "error in _find_round_parameters?" case - return $x if $x->{sign} eq 'NaN'; - - # no rounding at all, so must use fallback - if (scalar @params == 0) - { - # simulate old behaviour - $params[0] = $self->div_scale(); # and round to it as accuracy - $params[1] = undef; # P = undef - $scale = $params[0]+4; # at least four more for proper round - $params[2] = $r[2]; # round mode by caller or undef - $fallback = 1; # to clear a/p afterwards - } - else - { - # the 4 below is empirical, and there might be cases where it's not enough... - $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined - } - - return $x->bone(@params) if $x->is_zero(); - - # See the comments in Math::BigFloat on how this algorithm works. - # Basically we calculate A and B (where B is faculty(N)) so that A/B = e - - my $x_org = $x->copy(); - if ($scale <= 75) - { - # set $x directly from a cached string form - $x->{_n} = - $MBI->_new("90933395208605785401971970164779391644753259799242"); - $x->{_d} = - $MBI->_new("33452526613163807108170062053440751665152000000000"); - $x->{sign} = '+'; - } - else - { - # compute A and B so that e = A / B. - - # After some terms we end up with this, so we use it as a starting point: - my $A = $MBI->_new("90933395208605785401971970164779391644753259799242"); - my $F = $MBI->_new(42); my $step = 42; - - # Compute how many steps we need to take to get $A and $B sufficiently big - my $steps = Math::BigFloat::_len_to_steps($scale - 4); -# print STDERR "# Doing $steps steps for ", $scale-4, " digits\n"; - while ($step++ <= $steps) - { - # calculate $a * $f + 1 - $A = $MBI->_mul($A, $F); - $A = $MBI->_inc($A); - # increment f - $F = $MBI->_inc($F); - } - # compute $B as factorial of $steps (this is faster than doing it manually) - my $B = $MBI->_fac($MBI->_new($steps)); - -# print "A ", $MBI->_str($A), "\nB ", $MBI->_str($B), "\n"; - - $x->{_n} = $A; - $x->{_d} = $B; - $x->{sign} = '+'; - } - - # $x contains now an estimate of e, with some surplus digits, so we can round - if (!$x_org->is_one()) - { - # raise $x to the wanted power and round it in one step: - $x->bpow($x_org, @params); - } - else - { - # else just round the already computed result - delete $x->{_a}; delete $x->{_p}; - # shortcut to not run through _find_round_parameters again - if (defined $params[0]) - { - $x->bround($params[0],$params[2]); # then round accordingly - } - else - { - $x->bfround($params[1],$params[2]); # then round accordingly - } - } - if ($fallback) - { - # clear a/p after round, since user did not request it - delete $x->{_a}; delete $x->{_p}; - } - - $x; - } - -sub bnok - { - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,$class,@_); - } - - # do it with floats - $x->_new_from_float( $x->_as_float()->bnok(Math::BigFloat->new("$y"),@r) ); - } - -sub _float_from_part - { - my $x = shift; - - my $f = Math::BigFloat->bzero(); - $f->{_m} = $MBI->_copy($x); - $f->{_e} = $MBI->_zero(); - - $f; - } - -sub _as_float - { - my $x = shift; - - local $Math::BigFloat::upgrade = undef; - local $Math::BigFloat::accuracy = undef; - local $Math::BigFloat::precision = undef; - # 22/7 => 3.142857143.. - - my $a = $x->accuracy() || 0; - if ($a != 0 || !$MBI->_is_one($x->{_d})) - { - # n/d - return scalar Math::BigFloat->new($x->{sign} . $MBI->_str($x->{_n}))->bdiv( $MBI->_str($x->{_d}), $x->accuracy()); - } - # just n - Math::BigFloat->new($x->{sign} . $MBI->_str($x->{_n})); - } - -sub broot - { - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - if ($x->is_int() && $y->is_int()) - { - return $self->new($x->as_number()->broot($y->as_number(),@r)); - } - - # do it with floats - $x->_new_from_float( $x->_as_float()->broot($y->_as_float(),@r) )->bnorm()->bround(@r); - } - -sub bmodpow - { - # set up parameters - my ($self,$x,$y,$m,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,$m,@r) = objectify(3,@_); - } - - # $x or $y or $m are NaN or +-inf => NaN - return $x->bnan() - if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/ || - $m->{sign} !~ /^[+-]$/; - - if ($x->is_int() && $y->is_int() && $m->is_int()) - { - return $self->new($x->as_number()->bmodpow($y->as_number(),$m,@r)); - } - - warn ("bmodpow() not fully implemented"); - $x->bnan(); - } - -sub bmodinv - { - # set up parameters - my ($self,$x,$y,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y,@r) = objectify(2,@_); - } - - # $x or $y are NaN or +-inf => NaN - return $x->bnan() - if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/; - - if ($x->is_int() && $y->is_int()) - { - return $self->new($x->as_number()->bmodinv($y->as_number(),@r)); - } - - warn ("bmodinv() not fully implemented"); - $x->bnan(); - } - -sub bsqrt - { - my ($self,$x,@r) = ref($_[0]) ? (ref($_[0]),@_) : objectify(1,@_); - - return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0 - return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf - return $x->round(@r) if $x->is_zero() || $x->is_one(); - - local $Math::BigFloat::upgrade = undef; - local $Math::BigFloat::downgrade = undef; - local $Math::BigFloat::precision = undef; - local $Math::BigFloat::accuracy = undef; - local $Math::BigInt::upgrade = undef; - local $Math::BigInt::precision = undef; - local $Math::BigInt::accuracy = undef; - - $x->{_n} = _float_from_part( $x->{_n} )->bsqrt(); - $x->{_d} = _float_from_part( $x->{_d} )->bsqrt(); - - # XXX TODO: we probably can optimize this: - - # if sqrt(D) was not integer - if ($x->{_d}->{_es} ne '+') - { - $x->{_n}->blsft($x->{_d}->exponent()->babs(),10); # 7.1/4.51 => 7.1/45.1 - $x->{_d} = $MBI->_copy( $x->{_d}->{_m} ); # 7.1/45.1 => 71/45.1 - } - # if sqrt(N) was not integer - if ($x->{_n}->{_es} ne '+') - { - $x->{_d}->blsft($x->{_n}->exponent()->babs(),10); # 71/45.1 => 710/45.1 - $x->{_n} = $MBI->_copy( $x->{_n}->{_m} ); # 710/45.1 => 710/451 - } - - # convert parts to $MBI again - $x->{_n} = $MBI->_lsft( $MBI->_copy( $x->{_n}->{_m} ), $x->{_n}->{_e}, 10) - if ref($x->{_n}) ne $MBI && ref($x->{_n}) ne 'ARRAY'; - $x->{_d} = $MBI->_lsft( $MBI->_copy( $x->{_d}->{_m} ), $x->{_d}->{_e}, 10) - if ref($x->{_d}) ne $MBI && ref($x->{_d}) ne 'ARRAY'; - - $x->bnorm()->round(@r); - } - -sub blsft - { - my ($self,$x,$y,$b,@r) = objectify(3,@_); - - $b = 2 unless defined $b; - $b = $self->new($b) unless ref ($b); - $x->bmul( $b->copy()->bpow($y), @r); - $x; - } - -sub brsft - { - my ($self,$x,$y,$b,@r) = objectify(3,@_); - - $b = 2 unless defined $b; - $b = $self->new($b) unless ref ($b); - $x->bdiv( $b->copy()->bpow($y), @r); - $x; - } - -############################################################################## -# round - -sub round - { - $_[0]; - } - -sub bround - { - $_[0]; - } - -sub bfround - { - $_[0]; - } - -############################################################################## -# comparing - -sub bcmp - { - # compare two signed numbers - - # set up parameters - my ($self,$x,$y) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y) = objectify(2,@_); - } - - if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) - { - # handle +-inf and NaN - return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/; - return +1 if $x->{sign} eq '+inf'; - return -1 if $x->{sign} eq '-inf'; - return -1 if $y->{sign} eq '+inf'; - return +1; - } - # check sign for speed first - return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y - return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0 - - # shortcut - my $xz = $MBI->_is_zero($x->{_n}); - my $yz = $MBI->_is_zero($y->{_n}); - return 0 if $xz && $yz; # 0 <=> 0 - return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y - return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0 - - my $t = $MBI->_mul( $MBI->_copy($x->{_n}), $y->{_d}); - my $u = $MBI->_mul( $MBI->_copy($y->{_n}), $x->{_d}); - - my $cmp = $MBI->_acmp($t,$u); # signs are equal - $cmp = -$cmp if $x->{sign} eq '-'; # both are '-' => reverse - $cmp; - } - -sub bacmp - { - # compare two numbers (as unsigned) - - # set up parameters - my ($self,$x,$y) = (ref($_[0]),@_); - # objectify is costly, so avoid it - if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) - { - ($self,$x,$y) = objectify(2,$class,@_); - } - - if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) - { - # handle +-inf and NaN - return undef if (($x->{sign} eq $nan) || ($y->{sign} eq $nan)); - return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/; - return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/; - return -1; - } - - my $t = $MBI->_mul( $MBI->_copy($x->{_n}), $y->{_d}); - my $u = $MBI->_mul( $MBI->_copy($y->{_n}), $x->{_d}); - $MBI->_acmp($t,$u); # ignore signs - } - -############################################################################## -# output conversation - -sub numify - { - # convert 17/8 => float (aka 2.125) - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, NaN, etc - - # N/1 => N - my $neg = ''; $neg = '-' if $x->{sign} eq '-'; - return $neg . $MBI->_num($x->{_n}) if $MBI->_is_one($x->{_d}); - - $x->_as_float()->numify() + 0.0; - } - -sub as_number - { - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - # NaN, inf etc - return Math::BigInt->new($x->{sign}) if $x->{sign} !~ /^[+-]$/; - - my $u = Math::BigInt->bzero(); - $u->{value} = $MBI->_div( $MBI->_copy($x->{_n}), $x->{_d}); # 22/7 => 3 - $u->bneg if $x->{sign} eq '-'; # no negative zero - $u; - } - -sub as_float - { - # return N/D as Math::BigFloat - - # set up parameters - my ($self,$x,@r) = (ref($_[0]),@_); - # objectify is costly, so avoid it - ($self,$x,@r) = objectify(1,$class,@_) unless ref $_[0]; - - # NaN, inf etc - return Math::BigFloat->new($x->{sign}) if $x->{sign} !~ /^[+-]$/; - - my $u = Math::BigFloat->bzero(); - $u->{sign} = $x->{sign}; - # n - $u->{_m} = $MBI->_copy($x->{_n}); - $u->{_e} = $MBI->_zero(); - $u->bdiv( $MBI->_str($x->{_d}), @r); - # return $u - $u; - } - -sub as_bin - { - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x unless $x->is_int(); - - my $s = $x->{sign}; $s = '' if $s eq '+'; - $s . $MBI->_as_bin($x->{_n}); - } - -sub as_hex - { - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x unless $x->is_int(); - - my $s = $x->{sign}; $s = '' if $s eq '+'; - $s . $MBI->_as_hex($x->{_n}); - } - -sub as_oct - { - my ($self,$x) = ref($_[0]) ? (undef,$_[0]) : objectify(1,@_); - - return $x unless $x->is_int(); - - my $s = $x->{sign}; $s = '' if $s eq '+'; - $s . $MBI->_as_oct($x->{_n}); - } - -############################################################################## - -sub from_hex - { - my $class = shift; - - $class->new(@_); - } - -sub from_bin - { - my $class = shift; - - $class->new(@_); - } - -sub from_oct - { - my $class = shift; - - my @parts; - for my $c (@_) - { - push @parts, Math::BigInt->from_oct($c); - } - $class->new ( @parts ); - } - -############################################################################## -# import - -sub import - { - my $self = shift; - my $l = scalar @_; - my $lib = ''; my @a; - my $try = 'try'; - - for ( my $i = 0; $i < $l ; $i++) - { - if ( $_[$i] eq ':constant' ) - { - # this rest causes overlord er load to step in - overload::constant float => sub { $self->new(shift); }; - } -# elsif ($_[$i] eq 'upgrade') -# { -# # this causes upgrading -# $upgrade = $_[$i+1]; # or undef to disable -# $i++; -# } - elsif ($_[$i] eq 'downgrade') - { - # this causes downgrading - $downgrade = $_[$i+1]; # or undef to disable - $i++; - } - elsif ($_[$i] =~ /^(lib|try|only)\z/) - { - $lib = $_[$i+1] || ''; # default Calc - $try = $1; # lib, try or only - $i++; - } - elsif ($_[$i] eq 'with') - { - # this argument is no longer used - #$MBI = $_[$i+1] || 'Math::BigInt::Calc'; # default Math::BigInt::Calc - $i++; - } - else - { - push @a, $_[$i]; - } - } - require Math::BigInt; - - # let use Math::BigInt lib => 'GMP'; use Math::BigRat; still have GMP - if ($lib ne '') - { - my @c = split /\s*,\s*/, $lib; - foreach (@c) - { - $_ =~ tr/a-zA-Z0-9://cd; # limit to sane characters - } - $lib = join(",", @c); - } - my @import = ('objectify'); - push @import, $try => $lib if $lib ne ''; - - # MBI already loaded, so feed it our lib arguments - Math::BigInt->import( @import ); - - $MBI = Math::BigFloat->config()->{lib}; - - # register us with MBI to get notified of future lib changes - Math::BigInt::_register_callback( $self, sub { $MBI = $_[0]; } ); - - # any non :constant stuff is handled by our parent, Exporter (loaded - # by Math::BigFloat, even if @_ is empty, to give it a chance - $self->SUPER::import(@a); # for subclasses - $self->export_to_level(1,$self,@a); # need this, too - } - -1; - -__END__ - -=pod - -=head1 NAME - -Math::BigRat - Arbitrary big rational numbers - -=head1 SYNOPSIS - - use Math::BigRat; - - my $x = Math::BigRat->new('3/7'); $x += '5/9'; - - print $x->bstr(),"\n"; - print $x ** 2,"\n"; - - my $y = Math::BigRat->new('inf'); - print "$y ", ($y->is_inf ? 'is' : 'is not') , " infinity\n"; - - my $z = Math::BigRat->new(144); $z->bsqrt(); - -=head1 DESCRIPTION - -Math::BigRat complements Math::BigInt and Math::BigFloat by providing support -for arbitrary big rational numbers. - -=head2 MATH LIBRARY - -You can change the underlying module that does the low-level -math operations by using: - - use Math::BigRat try => 'GMP'; - -Note: This needs Math::BigInt::GMP installed. - -The following would first try to find Math::BigInt::Foo, then -Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: - - use Math::BigRat try => 'Foo,Math::BigInt::Bar'; - -If you want to get warned when the fallback occurs, replace "try" with -"lib": - - use Math::BigRat lib => 'Foo,Math::BigInt::Bar'; - -If you want the code to die instead, replace "try" with -"only": - - use Math::BigRat only => 'Foo,Math::BigInt::Bar'; - -=head1 METHODS - -Any methods not listed here are derived from Math::BigFloat (or -Math::BigInt), so make sure you check these two modules for further -information. - -=head2 new() - - $x = Math::BigRat->new('1/3'); - -Create a new Math::BigRat object. Input can come in various forms: - - $x = Math::BigRat->new(123); # scalars - $x = Math::BigRat->new('inf'); # infinity - $x = Math::BigRat->new('123.3'); # float - $x = Math::BigRat->new('1/3'); # simple string - $x = Math::BigRat->new('1 / 3'); # spaced - $x = Math::BigRat->new('1 / 0.1'); # w/ floats - $x = Math::BigRat->new(Math::BigInt->new(3)); # BigInt - $x = Math::BigRat->new(Math::BigFloat->new('3.1')); # BigFloat - $x = Math::BigRat->new(Math::BigInt::Lite->new('2')); # BigLite - - # You can also give D and N as different objects: - $x = Math::BigRat->new( - Math::BigInt->new(-123), - Math::BigInt->new(7), - ); # => -123/7 - -=head2 numerator() - - $n = $x->numerator(); - -Returns a copy of the numerator (the part above the line) as signed BigInt. - -=head2 denominator() - - $d = $x->denominator(); - -Returns a copy of the denominator (the part under the line) as positive BigInt. - -=head2 parts() - - ($n,$d) = $x->parts(); - -Return a list consisting of (signed) numerator and (unsigned) denominator as -BigInts. - -=head2 numify() - - my $y = $x->numify(); - -Returns the object as a scalar. This will lose some data if the object -cannot be represented by a normal Perl scalar (integer or float), so -use Las_number()> or L instead. - -This routine is automatically used whenever a scalar is required: - - my $x = Math::BigRat->new('3/1'); - @array = (0,1,2,3); - $y = $array[$x]; # set $y to 3 - -=head2 as_int()/as_number() - - $x = Math::BigRat->new('13/7'); - print $x->as_int(),"\n"; # '1' - -Returns a copy of the object as BigInt, truncated to an integer. - -C is an alias for C. - -=head2 as_float() - - $x = Math::BigRat->new('13/7'); - print $x->as_float(),"\n"; # '1' - - $x = Math::BigRat->new('2/3'); - print $x->as_float(5),"\n"; # '0.66667' - -Returns a copy of the object as BigFloat, preserving the -accuracy as wanted, or the default of 40 digits. - -This method was added in v0.22 of Math::BigRat (April 2008). - -=head2 as_hex() - - $x = Math::BigRat->new('13'); - print $x->as_hex(),"\n"; # '0xd' - -Returns the BigRat as hexadecimal string. Works only for integers. - -=head2 as_bin() - - $x = Math::BigRat->new('13'); - print $x->as_bin(),"\n"; # '0x1101' - -Returns the BigRat as binary string. Works only for integers. - -=head2 as_oct() - - $x = Math::BigRat->new('13'); - print $x->as_oct(),"\n"; # '015' - -Returns the BigRat as octal string. Works only for integers. - -=head2 from_hex()/from_bin()/from_oct() - - my $h = Math::BigRat->from_hex('0x10'); - my $b = Math::BigRat->from_bin('0b10000000'); - my $o = Math::BigRat->from_oct('020'); - -Create a BigRat from an hexadecimal, binary or octal number -in string form. - -=head2 length() - - $len = $x->length(); - -Return the length of $x in digits for integer values. - -=head2 digit() - - print Math::BigRat->new('123/1')->digit(1); # 1 - print Math::BigRat->new('123/1')->digit(-1); # 3 - -Return the N'ths digit from X when X is an integer value. - -=head2 bnorm() - - $x->bnorm(); - -Reduce the number to the shortest form. This routine is called -automatically whenever it is needed. - -=head2 bfac() - - $x->bfac(); - -Calculates the factorial of $x. For instance: - - print Math::BigRat->new('3/1')->bfac(),"\n"; # 1*2*3 - print Math::BigRat->new('5/1')->bfac(),"\n"; # 1*2*3*4*5 - -Works currently only for integers. - -=head2 bround()/round()/bfround() - -Are not yet implemented. - -=head2 bmod() - - $x->bmod($y); - -Returns $x modulo $y. When $x is finite, and $y is finite and non-zero, the -result is identical to the remainder after floored division (F-division). If, -in addition, both $x and $y are integers, the result is identical to the result -from Perl's % operator. - -=head2 bneg() - - $x->bneg(); - -Used to negate the object in-place. - -=head2 is_one() - - print "$x is 1\n" if $x->is_one(); - -Return true if $x is exactly one, otherwise false. - -=head2 is_zero() - - print "$x is 0\n" if $x->is_zero(); - -Return true if $x is exactly zero, otherwise false. - -=head2 is_pos()/is_positive() - - print "$x is >= 0\n" if $x->is_positive(); - -Return true if $x is positive (greater than or equal to zero), otherwise -false. Please note that '+inf' is also positive, while 'NaN' and '-inf' aren't. - -C is an alias for C. - -=head2 is_neg()/is_negative() - - print "$x is < 0\n" if $x->is_negative(); - -Return true if $x is negative (smaller than zero), otherwise false. Please -note that '-inf' is also negative, while 'NaN' and '+inf' aren't. - -C is an alias for C. - -=head2 is_int() - - print "$x is an integer\n" if $x->is_int(); - -Return true if $x has a denominator of 1 (e.g. no fraction parts), otherwise -false. Please note that '-inf', 'inf' and 'NaN' aren't integer. - -=head2 is_odd() - - print "$x is odd\n" if $x->is_odd(); - -Return true if $x is odd, otherwise false. - -=head2 is_even() - - print "$x is even\n" if $x->is_even(); - -Return true if $x is even, otherwise false. - -=head2 bceil() - - $x->bceil(); - -Set $x to the next bigger integer value (e.g. truncate the number to integer -and then increment it by one). - -=head2 bfloor() - - $x->bfloor(); - -Truncate $x to an integer value. - -=head2 bsqrt() - - $x->bsqrt(); - -Calculate the square root of $x. - -=head2 broot() - - $x->broot($n); - -Calculate the N'th root of $x. - -=head2 badd() - - $x->badd($y); - -Adds $y to $x and returns the result. - -=head2 bmul() - - $x->bmul($y); - -Multiplies $y to $x and returns the result. - -=head2 bsub() - - $x->bsub($y); - -Subtracts $y from $x and returns the result. - -=head2 bdiv() - - $q = $x->bdiv($y); - ($q, $r) = $x->bdiv($y); - -In scalar context, divides $x by $y and returns the result. In list context, -does floored division (F-division), returning an integer $q and a remainder $r -so that $x = $q * $y + $r. The remainer (modulo) is equal to what is returned -by C<$x->bmod($y)>. - -=head2 bdec() - - $x->bdec(); - -Decrements $x by 1 and returns the result. - -=head2 binc() - - $x->binc(); - -Increments $x by 1 and returns the result. - -=head2 copy() - - my $z = $x->copy(); - -Makes a deep copy of the object. - -Please see the documentation in L for further details. - -=head2 bstr()/bsstr() - - my $x = Math::BigInt->new('8/4'); - print $x->bstr(),"\n"; # prints 1/2 - print $x->bsstr(),"\n"; # prints 1/2 - -Return a string representing this object. - -=head2 bacmp()/bcmp() - -Used to compare numbers. - -Please see the documentation in L for further details. - -=head2 blsft()/brsft() - -Used to shift numbers left/right. - -Please see the documentation in L for further details. - -=head2 bpow() - - $x->bpow($y); - -Compute $x ** $y. - -Please see the documentation in L for further details. - -=head2 bexp() - - $x->bexp($accuracy); # calculate e ** X - -Calculates two integers A and B so that A/B is equal to C, where C is -Euler's number. - -This method was added in v0.20 of Math::BigRat (May 2007). - -See also C. - -=head2 bnok() - - $x->bnok($y); # x over y (binomial coefficient n over k) - -Calculates the binomial coefficient n over k, also called the "choose" -function. The result is equivalent to: - - ( n ) n! - | - | = ------- - ( k ) k!(n-k)! - -This method was added in v0.20 of Math::BigRat (May 2007). - -=head2 config() - - use Data::Dumper; - - print Dumper ( Math::BigRat->config() ); - print Math::BigRat->config()->{lib},"\n"; - -Returns a hash containing the configuration, e.g. the version number, lib -loaded etc. The following hash keys are currently filled in with the -appropriate information. - - key RO/RW Description - Example - ============================================================ - lib RO Name of the Math library - Math::BigInt::Calc - lib_version RO Version of 'lib' - 0.30 - class RO The class of config you just called - Math::BigRat - version RO version number of the class you used - 0.10 - upgrade RW To which class numbers are upgraded - undef - downgrade RW To which class numbers are downgraded - undef - precision RW Global precision - undef - accuracy RW Global accuracy - undef - round_mode RW Global round mode - even - div_scale RW Fallback accuracy for div - 40 - trap_nan RW Trap creation of NaN (undef = no) - undef - trap_inf RW Trap creation of +inf/-inf (undef = no) - undef - -By passing a reference to a hash you may set the configuration values. This -works only for values that a marked with a C above, anything else is -read-only. - -=head2 objectify() - -This is an internal routine that turns scalars into objects. - -=head1 BUGS - -Please report any bugs or feature requests to -C, or through the web interface at -L -(requires login). -We will be notified, and then you'll automatically be notified of progress on -your bug as I make changes. - -=head1 SUPPORT - -You can find documentation for this module with the perldoc command. - - perldoc Math::BigRat - -You can also look for information at: - -=over 4 - -=item * RT: CPAN's request tracker - -L - -=item * AnnoCPAN: Annotated CPAN documentation - -L - -=item * CPAN Ratings - -L - -=item * Search CPAN - -L - -=item * CPAN Testers Matrix - -L - -=item * The Bignum mailing list - -=over 4 - -=item * Post to mailing list - -C - -=item * View mailing list - -L - -=item * Subscribe/Unsubscribe - -L - -=back - -=back - -=head1 LICENSE - -This program is free software; you may redistribute it and/or modify it under -the same terms as Perl itself. - -=head1 SEE ALSO - -L, L and L as well as the backends -L, L, and L. - -=head1 AUTHORS - -(C) by Tels L 2001 - 2009. - -Currently maintained by Peter John Acklam . - -=cut diff --git a/dist/Math-BigRat/t/Math/BigRat/Test.pm b/dist/Math-BigRat/t/Math/BigRat/Test.pm deleted file mode 100644 index 74f9f9d004..0000000000 --- a/dist/Math-BigRat/t/Math/BigRat/Test.pm +++ /dev/null @@ -1,122 +0,0 @@ -package Math::BigRat::Test; - -require 5.005_02; -use strict; - -use Exporter; -use Math::BigRat; -use Math::BigFloat; -use vars qw($VERSION @ISA - $accuracy $precision $round_mode $div_scale); - -@ISA = qw(Math::BigRat Exporter); -$VERSION = 0.04; - -use overload; # inherit overload from BigRat - -# Globals -$accuracy = $precision = undef; -$round_mode = 'even'; -$div_scale = 40; - -my $class = 'Math::BigRat::Test'; - -#ub new -#{ -# my $proto = shift; -# my $class = ref($proto) || $proto; -# -# my $value = shift; -# my $a = $accuracy; $a = $_[0] if defined $_[0]; -# my $p = $precision; $p = $_[1] if defined $_[1]; -# # Store the floating point value -# my $self = Math::BigFloat->new($value,$a,$p,$round_mode); -# bless $self, $class; -# $self->{'_custom'} = 1; # make sure this never goes away -# return $self; -#} - -BEGIN - { - *fstr = \&bstr; - *fsstr = \&bsstr; - *objectify = \&Math::BigInt::objectify; - *AUTOLOAD = \&Math::BigRat::AUTOLOAD; - no strict 'refs'; - foreach my $method ( qw/ div acmp floor ceil root sqrt log fac modpow modinv/) - { - *{'b' . $method} = \&{'Math::BigRat::b' . $method}; - } - } - -sub fround - { - my ($x,$a) = @_; - - #print "$a $accuracy $precision $round_mode\n"; - Math::BigFloat->round_mode($round_mode); - Math::BigFloat->accuracy($a || $accuracy); - Math::BigFloat->precision(undef); - my $y = Math::BigFloat->new($x->bsstr(),undef,undef); - $class->new($y->fround($a)); - } - -sub ffround - { - my ($x,$p) = @_; - - Math::BigFloat->round_mode($round_mode); - Math::BigFloat->accuracy(undef); - Math::BigFloat->precision($p || $precision); - my $y = Math::BigFloat->new($x->bsstr(),undef,undef); - $class->new($y->ffround($p)); - } - -sub bstr - { - # calculate a BigFloat compatible string output - my ($x) = @_; - - $x = $class->new($x) unless ref $x; - - if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc - { - my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf - return $s; - } - - my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # +3 vs 3 - -# print " bstr \$x ", $accuracy || $x->{_a} || 'notset', " ", $precision || $x->{_p} || 'notset', "\n"; - return $s.$x->{_n} if $x->{_d}->is_one(); - my $output = Math::BigFloat->new($x->{_n})->bdiv($x->{_d}); - local $Math::BigFloat::accuracy = $accuracy || $x->{_a}; - local $Math::BigFloat::precision = $precision || $x->{_p}; - $s.$output->bstr(); - } - -sub numify - { - $_[0]->bsstr(); - } - -sub bsstr - { - # calculate a BigFloat compatible string output - my ($x) = @_; - - $x = $class->new($x) unless ref $x; - - if ($x->{sign} !~ /^[+-]$/) # inf, NaN etc - { - my $s = $x->{sign}; $s =~ s/^\+//; # +inf => inf - return $s; - } - - my $s = ''; $s = $x->{sign} if $x->{sign} ne '+'; # +3 vs 3 - - my $output = Math::BigFloat->new($x->{_n})->bdiv($x->{_d}); - return $s.$output->bsstr(); - } - -1; diff --git a/dist/Math-BigRat/t/big_ap.t b/dist/Math-BigRat/t/big_ap.t deleted file mode 100644 index 1b45eddfe7..0000000000 --- a/dist/Math-BigRat/t/big_ap.t +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/perl -w - -# Test that accuracy() and precision() in BigInt/BigFloat do not disturb -# the rounding force in BigRat. - -use strict; -use Test::More tests => 17; - -use Math::BigInt; -use Math::BigFloat; -use Math::BigRat; - -my $r = 'Math::BigRat'; -my $proper = $r->new('12345678901234567890/2'); -my $proper_inc = $r->new('12345678901234567890/2')->binc(); -my $proper_dec = $r->new('12345678901234567890/2')->bdec(); -my $proper_int = Math::BigInt->new('12345678901234567890'); -my $proper_float = Math::BigFloat->new('12345678901234567890'); -my $proper2 = $r->new('12345678901234567890'); - -print "# Start\n"; - -Math::BigInt->accuracy(3); -Math::BigFloat->accuracy(5); - -my ($x,$y,$z); - -############################################################################## -# new() - -$z = $r->new('12345678901234567890/2'); -is ($z,$proper); - -$z = $r->new('1234567890123456789E1'); -is ($z,$proper2); - -$z = $r->new('12345678901234567890/1E0'); -is ($z,$proper2); -$z = $r->new('1234567890123456789e1/1'); -is ($z,$proper2); -$z = $r->new('1234567890123456789e1/1E0'); -is ($z,$proper2); - -$z = $r->new($proper_int); -is ($z,$proper2); - -$z = $r->new($proper_float); -is ($z,$proper2); - -############################################################################## -# bdiv - -$x = $r->new('12345678901234567890'); $y = Math::BigRat->new('2'); -$z = $x->copy->bdiv($y); -is ($z,$proper); - -############################################################################## -# bmul - -$x = $r->new("$proper"); $y = Math::BigRat->new('1'); -$z = $x->copy->bmul($y); -is ($z,$proper); -$z = $r->new('12345678901234567890/1E0'); -is ($z,$proper2); - -$z = $r->new($proper_int); -is ($z,$proper2); - -$z = $r->new($proper_float); -is ($z,$proper2); - -############################################################################## -# bdiv - -$x = $r->new('12345678901234567890'); $y = Math::BigRat->new('2'); -$z = $x->copy->bdiv($y); -is ($z,$proper); - -############################################################################## -# bmul - -$x = $r->new("$proper"); $y = Math::BigRat->new('1'); -$z = $x->copy->bmul($y); -is ($z,$proper); - -$x = $r->new("$proper"); $y = Math::BigRat->new('2'); -$z = $x->copy->bmul($y); -is ($z,$proper2); - -############################################################################## -# binc/bdec - -$x = $proper->copy()->binc(); is ($x,$proper_inc); -$x = $proper->copy()->bdec(); is ($x,$proper_dec); diff --git a/dist/Math-BigRat/t/bigfltpm.inc b/dist/Math-BigRat/t/bigfltpm.inc deleted file mode 100644 index 9c884b7d9e..0000000000 --- a/dist/Math-BigRat/t/bigfltpm.inc +++ /dev/null @@ -1,1673 +0,0 @@ -#include this file into another test for subclass testing... - -ok ($class->config()->{lib},$CL); - -use strict; - -my $z; - -while () - { - chomp; - $_ =~ s/#.*$//; # remove comments - $_ =~ s/\s+$//; # trailing spaces - next if /^$/; # skip empty lines & comments - if (s/^&//) - { - $f = $_; - } - elsif (/^\$/) - { - $setup = $_; $setup =~ s/\$/\$${class}::/g; # round_mode, div_scale - #print "\$setup== $setup\n"; - } - else - { - if (m|^(.*?):(/.+)$|) - { - $ans = $2; - @args = split(/:/,$1,99); - } - else - { - @args = split(/:/,$_,99); $ans = pop(@args); - } - $try = "\$x = $class->new(\"$args[0]\");"; - if ($f eq "fnorm") - { - $try .= "\$x;"; - } elsif ($f eq "finf") { - $try .= "\$x->finf('$args[1]');"; - } elsif ($f eq "is_inf") { - $try .= "\$x->is_inf('$args[1]');"; - } elsif ($f eq "fone") { - $try .= "\$x->bone('$args[1]');"; - } elsif ($f eq "fstr") { - $try .= "\$x->accuracy($args[1]); \$x->precision($args[2]);"; - $try .= '$x->fstr();'; - } elsif ($f eq "parts") { - # ->bstr() to see if an object is returned - $try .= '($a,$b) = $x->parts(); $a = $a->bstr(); $b = $b->bstr();'; - $try .= '"$a $b";'; - } elsif ($f eq "exponent") { - # ->bstr() to see if an object is returned - $try .= '$x->exponent()->bstr();'; - } elsif ($f eq "mantissa") { - # ->bstr() to see if an object is returned - $try .= '$x->mantissa()->bstr();'; - } elsif ($f =~ /^(numify|length|as_number|as_hex|as_bin)$/) { - $try .= "\$x->$f();"; - # some unary ops (test the fxxx form, since that is done by AUTOLOAD) - } elsif ($f =~ /^f(nan|sstr|neg|floor|ceil|abs)$/) { - $try .= "\$x->f$1();"; - # some is_xxx test function - } elsif ($f =~ /^is_(zero|one|negative|positive|odd|even|nan|int)$/) { - $try .= "\$x->$f();"; - } elsif ($f eq "finc") { - $try .= '++$x;'; - } elsif ($f eq "fdec") { - $try .= '--$x;'; - }elsif ($f eq "fround") { - $try .= "$setup; \$x->fround($args[1]);"; - } elsif ($f eq "ffround") { - $try .= "$setup; \$x->ffround($args[1]);"; - } elsif ($f eq "fsqrt") { - $try .= "$setup; \$x->fsqrt();"; - } elsif ($f eq "ffac") { - $try .= "$setup; \$x->ffac();"; - } elsif ($f eq "flog") { - if (defined $args[1] && $args[1] ne '') - { - $try .= "\$y = $class->new($args[1]);"; - $try .= "$setup; \$x->flog(\$y);"; - } - else - { - $try .= "$setup; \$x->flog();"; - } - } - else - { - $try .= "\$y = $class->new(\"$args[1]\");"; - - if ($f eq "bgcd") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new(\"$args[2]\"); "; - } - $try .= "$class\::bgcd(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } - elsif ($f eq "blcm") - { - if (defined $args[2]) - { - $try .= " \$z = $class->new(\"$args[2]\"); "; - } - $try .= "$class\::blcm(\$x, \$y"; - $try .= ", \$z" if (defined $args[2]); - $try .= " );"; - } elsif ($f eq "fcmp") { - $try .= '$x->fcmp($y);'; - } elsif ($f eq "facmp") { - $try .= '$x->facmp($y);'; - } elsif ($f eq "fpow") { - $try .= '$x ** $y;'; - } elsif ($f eq "bnok") { - $try .= '$x->bnok($y);'; - } elsif ($f eq "froot") { - $try .= "$setup; \$x->froot(\$y);"; - } elsif ($f eq "fadd") { - $try .= '$x + $y;'; - } elsif ($f eq "fsub") { - $try .= '$x - $y;'; - } elsif ($f eq "fmul") { - $try .= '$x * $y;'; - } elsif ($f eq "fdiv") { - $try .= "$setup; \$x / \$y;"; - } elsif ($f eq "fdiv-list") { - $try .= "$setup; join(',',\$x->fdiv(\$y));"; - } elsif ($f eq "frsft") { - $try .= '$x >> $y;'; - } elsif ($f eq "flsft") { - $try .= '$x << $y;'; - } elsif ($f eq "fmod") { - $try .= '$x % $y;'; - } else { warn "Unknown op '$f'"; } - } - # print "# Trying: '$try'\n"; - $ans1 = eval $try; - print "# Error: $@\n" if $@; - if ($ans =~ m|^/(.*)$|) - { - my $pat = $1; - if ($ans1 =~ /$pat/) - { - ok (1,1); - } - else - { - print "# '$try' expected: /$pat/ got: '$ans1'\n" if !ok(1,0); - } - } - else - { - if ($ans eq "") - { - ok_undef ($ans1); - } - else - { - print "# Tried: '$try'\n" if !ok ($ans1, $ans); - if (ref($ans1) eq "$class") - { - # float numbers are normalized (for now), so mantissa shouldn't have - # trailing zeros - #print $ans1->_trailing_zeros(),"\n"; - print "# Has trailing zeros after '$try'\n" - if !ok ($CL->_zeros( $ans1->{_m}), 0); - } - } - } # end pattern or string - } - } # end while - -# check whether $class->new( Math::BigInt->new()) destroys it -# ($y == 12 in this case) -$x = Math::BigInt->new(1200); $y = $class->new($x); -ok ($y,1200); ok ($x,1200); - -############################################################################### -# Really huge, big, ultra-mega-biggy-monster exponents -# Technically, the exponents should not be limited (they are BigInts), but -# practically there are a few places were they are limited to a Perl scalar. -# This is sometimes for speed, sometimes because otherwise the number wouldn't -# fit into your memory (just think of 1e123456789012345678901234567890 + 1!) -# anyway. We don't test everything here, but let's make sure it just basically -# works. - -my $monster = '1e1234567890123456789012345678901234567890'; - -# new and exponent -ok ($class->new($monster)->bsstr(), - '1e+1234567890123456789012345678901234567890'); -ok ($class->new($monster)->exponent(), - '1234567890123456789012345678901234567890'); -# cmp -ok ($class->new($monster) > 0,1); - -# sub/mul -ok ($class->new($monster)->bsub( $monster),0); -ok ($class->new($monster)->bmul(2)->bsstr(), - '2e+1234567890123456789012345678901234567890'); - -# mantissa -$monster = '1234567890123456789012345678901234567890e2'; -ok ($class->new($monster)->mantissa(), - '123456789012345678901234567890123456789'); - -############################################################################### -# zero,inf,one,nan - -$x = $class->new(2); $x->fzero(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); -$x = $class->new(2); $x->finf(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); -$x = $class->new(2); $x->fone(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); -$x = $class->new(2); $x->fnan(); ok_undef ($x->{_a}); ok_undef ($x->{_p}); - -############################################################################### -# bone/binf etc as plain calls (Lite failed them) - -ok ($class->fzero(),0); -ok ($class->fone(),1); -ok ($class->fone('+'),1); -ok ($class->fone('-'),-1); -ok ($class->fnan(),'NaN'); -ok ($class->finf(),'inf'); -ok ($class->finf('+'),'inf'); -ok ($class->finf('-'),'-inf'); -ok ($class->finf('-inf'),'-inf'); - -$class->accuracy(undef); $class->precision(undef); # reset - -############################################################################### -# bug in bsstr()/numify() showed up in after-rounding in bdiv() - -$x = $class->new('0.008'); $y = $class->new(2); -$x->bdiv(3,$y); -ok ($x,'0.0027'); - -############################################################################### -# fsqrt() with set global A/P or A/P enabled on $x, also a test whether fsqrt() -# correctly modifies $x - - -$x = $class->new(12); $class->precision(-2); $x->fsqrt(); ok ($x,'3.46'); - -$class->precision(undef); -$x = $class->new(12); $class->precision(0); $x->fsqrt(); ok ($x,'3'); - -$class->precision(-3); $x = $class->new(12); $x->fsqrt(); ok ($x,'3.464'); - -{ - no strict 'refs'; - # A and P set => NaN - ${${class}.'::accuracy'} = 4; $x = $class->new(12); - $x->fsqrt(3); ok ($x,'NaN'); - # supplied arg overrides set global - $class->precision(undef); $x = $class->new(12); $x->fsqrt(3); ok ($x,'3.46'); - $class->accuracy(undef); $class->precision(undef); # reset for further tests -} - -############################################################################# -# can we call objectify (broken until v1.52) - -{ - no strict; - $try = - '@args' . " = $class" . "::objectify(2,$class,4,5);".'join(" ",@args);'; - $ans = eval $try; - ok ($ans,"$class 4 5"); -} - -############################################################################# -# is_one('-') (broken until v1.64) - -ok ($class->new(-1)->is_one(),0); -ok ($class->new(-1)->is_one('-'),1); - -############################################################################# -# bug 1/0.5 leaving 2e-0 instead of 2e0 - -ok ($class->new(1)->fdiv('0.5')->bsstr(),'2e+0'); - -############################################################################### -# [perl #30609] bug with $x -= $x not being 0, but 2*$x - -$x = $class->new(3); $x -= $x; ok ($x, 0); -$x = $class->new(-3); $x -= $x; ok ($x, 0); -$x = $class->new(3); $x += $x; ok ($x, 6); -$x = $class->new(-3); $x += $x; ok ($x, -6); - -$x = $class->new('NaN'); $x -= $x; ok ($x->is_nan(), 1); -$x = $class->new('inf'); $x -= $x; ok ($x->is_nan(), 1); -$x = $class->new('-inf'); $x -= $x; ok ($x->is_nan(), 1); - -$x = $class->new('NaN'); $x += $x; ok ($x->is_nan(), 1); -$x = $class->new('inf'); $x += $x; ok ($x->is_inf(), 1); -$x = $class->new('-inf'); $x += $x; ok ($x->is_inf('-'), 1); - -$x = $class->new('3.14'); $x -= $x; ok ($x, 0); -$x = $class->new('-3.14'); $x -= $x; ok ($x, 0); -$x = $class->new('3.14'); $x += $x; ok ($x, '6.28'); -$x = $class->new('-3.14'); $x += $x; ok ($x, '-6.28'); - -$x = $class->new('3.14'); $x *= $x; ok ($x, '9.8596'); -$x = $class->new('-3.14'); $x *= $x; ok ($x, '9.8596'); -$x = $class->new('3.14'); $x /= $x; ok ($x, '1'); -$x = $class->new('-3.14'); $x /= $x; ok ($x, '1'); -$x = $class->new('3.14'); $x %= $x; ok ($x, '0'); -$x = $class->new('-3.14'); $x %= $x; ok ($x, '0'); - -############################################################################### -# the following two were reported by "kenny" via hotmail.com: - -#perl -MMath::BigFloat -wle 'print Math::BigFloat->new(0)->bpow(".1")' -#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. - -$x = $class->new(0); $y = $class->new('0.1'); -ok ($x ** $y, 0, 'no warnings and zero result'); - -#perl -MMath::BigFloat -lwe 'print Math::BigFloat->new(".222222222222222222222222222222222222222222")->bceil()' -#Use of uninitialized value in numeric le (<=) at BigFloat.pm line 1851. - -$x = $class->new(".222222222222222222222222222222222222222222"); -ok ($x->bceil(), 1, 'no warnings and one as result'); - -############################################################################### -# test **=, <<=, >>= - -# ((2^148)-1)/17 -$x = $class->new(2); $x **= 148; $x++; $x->bdiv(17, 60)->bfloor(); $x->accuracy(undef); -ok ($x,"20988936657440586486151264256610222593863921"); -ok ($x->length(),length "20988936657440586486151264256610222593863921"); - -$x = $class->new('2'); -my $y = $class->new('18'); -ok ($x <<= $y, 2 << 18); -ok ($x, 2 << 18); -ok ($x >>= $y, 2); -ok ($x, 2); - -$x = $class->new('2'); -$y = $class->new('18.2'); -$x <<= $y; # 2 * (2 ** 18.2); - -ok ($x->copy()->bfround(-9), '602248.763144685'); -ok ($x >>= $y, 2); # 2 * (2 ** 18.2) / (2 ** 18.2) => 2 -ok ($x, 2); - -1; # all done - -############################################################################### -# Perl 5.005 does not like ok ($x,undef) - -sub ok_undef - { - my $x = shift; - - ok (1,1) and return if !defined $x; - ok ($x,'undef'); - } - -__DATA__ -&bgcd -inf:12:NaN --inf:12:NaN -12:inf:NaN -12:-inf:NaN -inf:inf:NaN -inf:-inf:NaN --inf:-inf:NaN -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:0 -+0:+1:1 -+1:+0:1 -+1:+1:1 -+2:+3:1 -+3:+2:1 --3:+2:1 --3:-2:1 --144:-60:12 -144:-60:12 -144:60:12 -100:625:25 -4096:81:1 -1034:804:2 -27:90:56:1 -27:90:54:9 -&blcm -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+0:+0:NaN -+1:+0:0 -+0:+1:0 -+27:+90:270 -+1034:+804:415668 -$div_scale = 40; -&bnok -+inf:10:inf -NaN:NaN:NaN -NaN:1:NaN -1:NaN:NaN -1:1:1 -# k > n -1:2:0 -2:3:0 -# k < 0 -1:-2:0 -# 7 over 3 = 35 -7:3:35 -7:6:1 -100:90:17310309456440 -&flog -0::NaN --1::NaN --2::NaN -# base > 0, base != 1 -2:-1:NaN -2:0:NaN -2:1:NaN -# log(1) is always 1, regardless of $base -1::0 -1:1:0 -1:2:0 -2::0.6931471805599453094172321214581765680755 -2.718281828::0.9999999998311266953289851340574956564911 -$div_scale = 20; -2.718281828::0.99999999983112669533 -$div_scale = 15; -123::4.81218435537242 -10::2.30258509299405 -1000::6.90775527898214 -100::4.60517018598809 -2::0.693147180559945 -3.1415::1.14470039286086 -12345::9.42100640177928 -0.001::-6.90775527898214 -# bug until v1.71: -10:10:1 -100:100:1 -# reset for further tests -$div_scale = 40; -1::0 -&frsft -NaNfrsft:2:NaN -0:2:0 -1:1:0.5 -2:1:1 -4:1:2 -123:1:61.5 -32:3:4 -&flsft -NaNflsft:0:NaN -2:1:4 -4:3:32 -5:3:40 -1:2:4 -0:5:0 -&fnorm -1:1 --0:0 -fnormNaN:NaN -+inf:inf --inf:-inf -123:123 --123.4567:-123.4567 -# invalid inputs -1__2:NaN -1E1__2:NaN -11__2E2:NaN -.2E-3.:NaN -1e3e4:NaN -# strange, but valid -.2E2:20 -1.E3:1000 -# some inputs that result in zero -0e0:0 -+0e0:0 -+0e+0:0 --0e+0:0 -0e-0:0 --0e-0:0 -+0e-0:0 -000:0 -00e2:0 -00e02:0 -000e002:0 -000e1230:0 -00e-3:0 -00e+3:0 -00e-03:0 -00e+03:0 --000:0 --00e2:0 --00e02:0 --000e002:0 --000e1230:0 --00e-3:0 --00e+3:0 --00e-03:0 --00e+03:0 -&as_number -0:0 -1:1 -1.2:1 -2.345:2 --2:-2 --123.456:-123 --200:-200 -# test for bug in brsft() not handling cases that return 0 -0.000641:0 -0.0006412:0 -0.00064123:0 -0.000641234:0 -0.0006412345:0 -0.00064123456:0 -0.000641234567:0 -0.0006412345678:0 -0.00064123456789:0 -0.1:0 -0.01:0 -0.001:0 -0.0001:0 -0.00001:0 -0.000001:0 -0.0000001:0 -0.00000001:0 -0.000000001:0 -0.0000000001:0 -0.00000000001:0 -0.12345:0 -0.123456:0 -0.1234567:0 -0.12345678:0 -0.123456789:0 -&finf -1:+:inf -2:-:-inf -3:abc:inf -&as_hex -+inf:inf --inf:-inf -hexNaN:NaN -0:0x0 -5:0x5 --5:-0x5 -&as_bin -+inf:inf --inf:-inf -hexNaN:NaN -0:0b0 -5:0b101 --5:-0b101 -&numify -# uses bsstr() so 5 => 5e+0 to be compatible w/ Perls output -0:0e+1 -+1:1e+0 -1234:1234e+0 -NaN:NaN -+inf:inf --inf:-inf --5:-5e+0 -100:1e+2 --100:-1e+2 -&fnan -abc:NaN -2:NaN --2:NaN -0:NaN -&fone -2:+:1 --2:-:-1 --2:+:1 -2:-:-1 -0::1 --2::1 -abc::1 -2:abc:1 -&fsstr -+inf:inf --inf:-inf -abcfsstr:NaN --abcfsstr:NaN -1234.567:1234567e-3 -123:123e+0 --5:-5e+0 --100:-1e+2 -&fstr -+inf:::inf --inf:::-inf -abcfstr:::NaN -1234.567:9::1234.56700 -1234.567::-6:1234.567000 -12345:5::12345 -0.001234:6::0.00123400 -0.001234::-8:0.00123400 -0:4::0 -0::-4:0.0000 -&fnorm -inf:inf -+inf:inf --inf:-inf -+infinity:NaN -+-inf:NaN -abc:NaN - 1 a:NaN -1bcd2:NaN -11111b:NaN -+1z:NaN --1z:NaN -0e999:0 -0e-999:0 --0e999:0 --0e-999:0 -0:0 -+0:0 -+00:0 -+0_0_0:0 -000000_0000000_00000:0 --0:0 --0000:0 -+1:1 -+01:1 -+001:1 -+00000100000:100000 -123456789:123456789 --1:-1 --01:-1 --001:-1 --123456789:-123456789 --00000100000:-100000 -123.456a:NaN -123.456:123.456 -0.01:0.01 -.002:0.002 -+.2:0.2 --0.0003:-0.0003 --.0000000004:-0.0000000004 -123456E2:12345600 -123456E-2:1234.56 --123456E2:-12345600 --123456E-2:-1234.56 -1e1:10 -2e-11:0.00000000002 -# exercise _split - .02e-1:0.002 - 000001:1 - -00001:-1 - -1:-1 - 000.01:0.01 - -000.0023:-0.0023 - 1.1e1:11 --3e111:-3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 --4e-1111:-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004 -&fpow -NaN:1:NaN -1:NaN:NaN -NaN:-1:NaN --1:NaN:NaN -NaN:-21:NaN --21:NaN:NaN -NaN:21:NaN -21:NaN:NaN -0:0:1 -0:1:0 -0:9:0 -0:-2:inf -2:2:4 -1:2:1 -1:3:1 --1:2:1 --1:3:-1 -123.456:2:15241.383936 -2:-2:0.25 -2:-3:0.125 -128:-2:0.00006103515625 -abc:123.456:NaN -123.456:abc:NaN -+inf:123.45:inf --inf:123.45:-inf -+inf:-123.45:inf --inf:-123.45:-inf --2:2:4 --2:3:-8 --2:4:16 --2:5:-32 --3:2:9 --3:3:-27 --3:4:81 --3:5:-243 -# 2 ** 0.5 == sqrt(2) -# 1.41..7 and not 1.4170 since fallback (bsqrt(9) is '3', not 3.0...0) -2:0.5:1.41421356237309504880168872420969807857 -#2:0.2:1.148698354997035006798626946777927589444 -#6:1.5:14.6969384566990685891837044482353483518 -$div_scale = 20; -#62.5:12.5:26447206647554886213592.3959144 -$div_scale = 40; -&fneg -fnegNaN:NaN -+inf:-inf --inf:inf -+0:0 -+1:-1 --1:1 -+123456789:-123456789 --123456789:123456789 -+123.456789:-123.456789 --123456.789:123456.789 -&fabs -fabsNaN:NaN -+inf:inf --inf:inf -+0:0 -+1:1 --1:1 -+123456789:123456789 --123456789:123456789 -+123.456789:123.456789 --123456.789:123456.789 -&fround -$round_mode = "trunc" -+inf:5:inf --inf:5:-inf -0:5:0 -NaNfround:5:NaN -+10123456789:5:10123000000 --10123456789:5:-10123000000 -+10123456789.123:5:10123000000 --10123456789.123:5:-10123000000 -+10123456789:9:10123456700 --10123456789:9:-10123456700 -+101234500:6:101234000 --101234500:6:-101234000 -$round_mode = "zero" -+20123456789:5:20123000000 --20123456789:5:-20123000000 -+20123456789.123:5:20123000000 --20123456789.123:5:-20123000000 -+20123456789:9:20123456800 --20123456789:9:-20123456800 -+201234500:6:201234000 --201234500:6:-201234000 -$round_mode = "+inf" -+30123456789:5:30123000000 --30123456789:5:-30123000000 -+30123456789.123:5:30123000000 --30123456789.123:5:-30123000000 -+30123456789:9:30123456800 --30123456789:9:-30123456800 -+301234500:6:301235000 --301234500:6:-301234000 -$round_mode = "-inf" -+40123456789:5:40123000000 --40123456789:5:-40123000000 -+40123456789.123:5:40123000000 --40123456789.123:5:-40123000000 -+40123456789:9:40123456800 --40123456789:9:-40123456800 -+401234500:6:401234000 --401234500:6:-401235000 -$round_mode = "odd" -+50123456789:5:50123000000 --50123456789:5:-50123000000 -+50123456789.123:5:50123000000 --50123456789.123:5:-50123000000 -+50123456789:9:50123456800 --50123456789:9:-50123456800 -+501234500:6:501235000 --501234500:6:-501235000 -$round_mode = "even" -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601234000 --601234500:6:-601234000 -+60123456789.0123:5:60123000000 --60123456789.0123:5:-60123000000 -$round_mode = "common" -+60123456789:5:60123000000 --60123456789:5:-60123000000 -+60123456789:6:60123500000 --60123456789:6:-60123500000 -+60123456789:9:60123456800 --60123456789:9:-60123456800 -+601234500:6:601235000 --601234500:6:-601235000 -+601234400:6:601234000 --601234400:6:-601234000 -+601234600:6:601235000 --601234600:6:-601235000 -+601234300:6:601234000 -+60123456789.0123:5:60123000000 --60123456789.0123:5:-60123000000 -&ffround -$round_mode = "trunc" -+inf:5:inf --inf:5:-inf -0:5:0 -NaNffround:5:NaN -+1.23:-1:1.2 -+1.234:-1:1.2 -+1.2345:-1:1.2 -+1.23:-2:1.23 -+1.234:-2:1.23 -+1.2345:-2:1.23 -+1.23:-3:1.230 -+1.234:-3:1.234 -+1.2345:-3:1.234 --1.23:-1:-1.2 -+1.27:-1:1.2 --1.27:-1:-1.2 -+1.25:-1:1.2 --1.25:-1:-1.2 -+1.35:-1:1.3 --1.35:-1:-1.3 --0.0061234567890:-1:0.0 --0.0061:-1:0.0 --0.00612:-1:0.0 --0.00612:-2:0.00 --0.006:-1:0.0 --0.006:-2:0.00 --0.0006:-2:0.00 --0.0006:-3:0.000 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:0 -0.41:0:0 -$round_mode = "zero" -+2.23:-1:/2.2(?:0{5}\d+)? --2.23:-1:/-2.2(?:0{5}\d+)? -+2.27:-1:/2.(?:3|29{5}\d+) --2.27:-1:/-2.(?:3|29{5}\d+) -+2.25:-1:/2.2(?:0{5}\d+)? --2.25:-1:/-2.2(?:0{5}\d+)? -+2.35:-1:/2.(?:3|29{5}\d+) --2.35:-1:/-2.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -$round_mode = "+inf" -+3.23:-1:/3.2(?:0{5}\d+)? --3.23:-1:/-3.2(?:0{5}\d+)? -+3.27:-1:/3.(?:3|29{5}\d+) --3.27:-1:/-3.(?:3|29{5}\d+) -+3.25:-1:/3.(?:3|29{5}\d+) --3.25:-1:/-3.2(?:0{5}\d+)? -+3.35:-1:/3.(?:4|39{5}\d+) --3.35:-1:/-3.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-6e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:1 -0.51:0:1 -0.41:0:0 -$round_mode = "-inf" -+4.23:-1:/4.2(?:0{5}\d+)? --4.23:-1:/-4.2(?:0{5}\d+)? -+4.27:-1:/4.(?:3|29{5}\d+) --4.27:-1:/-4.(?:3|29{5}\d+) -+4.25:-1:/4.2(?:0{5}\d+)? --4.25:-1:/-4.(?:3|29{5}\d+) -+4.35:-1:/4.(?:3|29{5}\d+) --4.35:-1:/-4.(?:4|39{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.007|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -$round_mode = "odd" -+5.23:-1:/5.2(?:0{5}\d+)? --5.23:-1:/-5.2(?:0{5}\d+)? -+5.27:-1:/5.(?:3|29{5}\d+) --5.27:-1:/-5.(?:3|29{5}\d+) -+5.25:-1:/5.(?:3|29{5}\d+) --5.25:-1:/-5.(?:3|29{5}\d+) -+5.35:-1:/5.(?:3|29{5}\d+) --5.35:-1:/-5.(?:3|29{5}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.007|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:1 -0.51:0:1 -0.41:0:0 -$round_mode = "even" -+6.23:-1:/6.2(?:0{5}\d+)? --6.23:-1:/-6.2(?:0{5}\d+)? -+6.27:-1:/6.(?:3|29{5}\d+) --6.27:-1:/-6.(?:3|29{5}\d+) -+6.25:-1:/6.(?:2(?:0{5}\d+)?|29{5}\d+) --6.25:-1:/-6.(?:2(?:0{5}\d+)?|29{5}\d+) -+6.35:-1:/6.(?:4|39{5}\d+|29{8}\d+) --6.35:-1:/-6.(?:4|39{5}\d+|29{8}\d+) --0.0065:-1:0.0 --0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-7e-03 --0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 --0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.05:0:0 -0.5:0:0 -0.51:0:1 -0.41:0:0 -0.01234567:-3:0.012 -0.01234567:-4:0.0123 -0.01234567:-5:0.01235 -0.01234567:-6:0.012346 -0.01234567:-7:0.0123457 -0.01234567:-8:0.01234567 -0.01234567:-9:0.012345670 -0.01234567:-12:0.012345670000 -&fcmp -fcmpNaN:fcmpNaN: -fcmpNaN:+0: -+0:fcmpNaN: -+0:+0:0 --1:+0:-1 -+0:-1:1 -+1:+0:1 -+0:+1:-1 --1:+1:-1 -+1:-1:1 --1:-1:0 -+1:+1:0 --1.1:0:-1 -+0:-1.1:1 -+1.1:+0:1 -+0:+1.1:-1 -+123:+123:0 -+123:+12:1 -+12:+123:-1 --123:-123:0 --123:-12:-1 --12:-123:1 -+123:+124:-1 -+124:+123:1 --123:-124:1 --124:-123:-1 -0:0.01:-1 -0:0.0001:-1 -0:-0.0001:1 -0:-0.1:1 -0.1:0:1 -0.00001:0:1 --0.0001:0:-1 --0.1:0:-1 -0:0.0001234:-1 -0:-0.0001234:1 -0.0001234:0:1 --0.0001234:0:-1 -0.0001:0.0005:-1 -0.0005:0.0001:1 -0.005:0.0001:1 -0.001:0.0005:1 -0.000001:0.0005:-1 -0.00000123:0.0005:-1 -0.00512:0.0001:1 -0.005:0.000112:1 -0.00123:0.0005:1 -1.5:2:-1 -2:1.5:1 -1.54321:234:-1 -234:1.54321:1 -# infinity --inf:5432112345:-1 -+inf:5432112345:1 --inf:-5432112345:-1 -+inf:-5432112345:1 --inf:54321.12345:-1 -+inf:54321.12345:1 --inf:-54321.12345:-1 -+inf:-54321.12345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:1 --inf:+inf:-1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -&facmp -fcmpNaN:fcmpNaN: -fcmpNaN:+0: -+0:fcmpNaN: -+0:+0:0 --1:+0:1 -+0:-1:-1 -+1:+0:1 -+0:+1:-1 --1:+1:0 -+1:-1:0 --1:-1:0 -+1:+1:0 --1.1:0:1 -+0:-1.1:-1 -+1.1:+0:1 -+0:+1.1:-1 -+123:+123:0 -+123:+12:1 -+12:+123:-1 --123:-123:0 --123:-12:1 --12:-123:-1 -+123:+124:-1 -+124:+123:1 --123:-124:-1 --124:-123:1 -0:0.01:-1 -0:0.0001:-1 -0:-0.0001:-1 -0:-0.1:-1 -0.1:0:1 -0.00001:0:1 --0.0001:0:1 --0.1:0:1 -0:0.0001234:-1 -0:-0.0001234:-1 -0.0001234:0:1 --0.0001234:0:1 -0.0001:0.0005:-1 -0.0005:0.0001:1 -0.005:0.0001:1 -0.001:0.0005:1 -0.000001:0.0005:-1 -0.00000123:0.0005:-1 -0.00512:0.0001:1 -0.005:0.000112:1 -0.00123:0.0005:1 -1.5:2:-1 -2:1.5:1 -1.54321:234:-1 -234:1.54321:1 -# infinity --inf:5432112345:1 -+inf:5432112345:1 --inf:-5432112345:1 -+inf:-5432112345:1 --inf:54321.12345:1 -+inf:54321.12345:1 --inf:-54321.12345:1 -+inf:-54321.12345:1 -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:0 --inf:+inf:0 -5:inf:-1 --1:inf:-1 -5:-inf:-1 --1:-inf:-1 -# return undef -+inf:facmpNaN: -facmpNaN:inf: --inf:facmpNaN: -facmpNaN:-inf: -&fdec -fdecNaN:NaN -+inf:inf --inf:-inf -+0:-1 -+1:0 --1:-2 -1.23:0.23 --1.23:-2.23 -100:99 -101:100 --100:-101 --99:-100 --98:-99 -99:98 -&finc -fincNaN:NaN -+inf:inf --inf:-inf -+0:1 -+1:2 --1:0 -1.23:2.23 --1.23:-0.23 -100:101 --100:-99 --99:-98 --101:-100 -99:100 -&fadd -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:NaN --inf:+inf:NaN -+inf:+inf:inf --inf:-inf:-inf -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -+0:+0:0 -+1:+0:1 -+0:+1:1 -+1:+1:2 --1:+0:-1 -+0:-1:-1 --1:-1:-2 --1:+1:0 -+1:-1:0 -+9:+1:10 -+99:+1:100 -+999:+1:1000 -+9999:+1:10000 -+99999:+1:100000 -+999999:+1:1000000 -+9999999:+1:10000000 -+99999999:+1:100000000 -+999999999:+1:1000000000 -+9999999999:+1:10000000000 -+99999999999:+1:100000000000 -+10:-1:9 -+100:-1:99 -+1000:-1:999 -+10000:-1:9999 -+100000:-1:99999 -+1000000:-1:999999 -+10000000:-1:9999999 -+100000000:-1:99999999 -+1000000000:-1:999999999 -+10000000000:-1:9999999999 -+123456789:+987654321:1111111110 --123456789:+987654321:864197532 --123456789:-987654321:-1111111110 -+123456789:-987654321:-864197532 -0.001234:0.0001234:0.0013574 -&fsub -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:inf --inf:+inf:-inf -+inf:+inf:NaN --inf:-inf:NaN -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -+0:+0:0 -+1:+0:1 -+0:+1:-1 -+1:+1:0 --1:+0:-1 -+0:-1:1 --1:-1:0 --1:+1:-2 -+1:-1:2 -+9:+1:8 -+99:+1:98 -+999:+1:998 -+9999:+1:9998 -+99999:+1:99998 -+999999:+1:999998 -+9999999:+1:9999998 -+99999999:+1:99999998 -+999999999:+1:999999998 -+9999999999:+1:9999999998 -+99999999999:+1:99999999998 -+10:-1:11 -+100:-1:101 -+1000:-1:1001 -+10000:-1:10001 -+100000:-1:100001 -+1000000:-1:1000001 -+10000000:-1:10000001 -+100000000:-1:100000001 -+1000000000:-1:1000000001 -+10000000000:-1:10000000001 -+123456789:+987654321:-864197532 --123456789:+987654321:-1111111110 --123456789:-987654321:864197532 -+123456789:-987654321:1111111110 -&fmul -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:NaNmul:NaN -+inf:NaNmul:NaN -NaNmul:+inf:NaN -NaNmul:-inf:NaN -+inf:+inf:inf -+inf:-inf:-inf -+inf:-inf:-inf -+inf:+inf:inf -+inf:123.34:inf -+inf:-123.34:-inf --inf:123.34:-inf --inf:-123.34:inf -123.34:+inf:inf --123.34:+inf:-inf -123.34:-inf:-inf --123.34:-inf:inf -+0:+0:0 -+0:+1:0 -+1:+0:0 -+0:-1:0 --1:+0:0 -+123456789123456789:+0:0 -+0:+123456789123456789:0 --1:-1:1 --1:+1:-1 -+1:-1:-1 -+1:+1:1 -+2:+3:6 --2:+3:-6 -+2:-3:-6 --2:-3:6 -+111:+111:12321 -+10101:+10101:102030201 -+1001001:+1001001:1002003002001 -+100010001:+100010001:10002000300020001 -+10000100001:+10000100001:100002000030000200001 -+11111111111:+9:99999999999 -+22222222222:+9:199999999998 -+33333333333:+9:299999999997 -+44444444444:+9:399999999996 -+55555555555:+9:499999999995 -+66666666666:+9:599999999994 -+77777777777:+9:699999999993 -+88888888888:+9:799999999992 -+99999999999:+9:899999999991 -6:120:720 -10:10000:100000 -&fdiv-list -0:0:NaN,NaN -0:1:0,0 -9:4:2.25,1 -9:5:1.8,4 -# bug in v1.74 with bdiv in list context, when $y is 1 or -1 -2.1:-1:-2.1,0 -2.1:1:2.1,0 --2.1:-1:2.1,0 --2.1:1:-2.1,0 -&fdiv -$div_scale = 40; $round_mode = 'even' -abc:abc:NaN -abc:+1:abc:NaN -+1:abc:NaN --1:abc:NaN -0:abc:NaN -+0:+0:NaN -+0:+1:0 -+1:+0:inf -+3214:+0:inf -+0:-1:0 --1:+0:-inf --3214:+0:-inf -+1:+1:1 --1:-1:1 -+1:-1:-1 --1:+1:-1 -+1:+2:0.5 -+2:+1:2 -123:+inf:0 -123:-inf:0 -+10:+5:2 -+100:+4:25 -+1000:+8:125 -+10000:+16:625 -+10000:-16:-625 -+999999999999:+9:111111111111 -+999999999999:+99:10101010101 -+999999999999:+999:1001001001 -+999999999999:+9999:100010001 -+999999999999999:+99999:10000100001 -+1000000000:+9:111111111.1111111111111111111111111111111 -+2000000000:+9:222222222.2222222222222222222222222222222 -+3000000000:+9:333333333.3333333333333333333333333333333 -+4000000000:+9:444444444.4444444444444444444444444444444 -+5000000000:+9:555555555.5555555555555555555555555555556 -+6000000000:+9:666666666.6666666666666666666666666666667 -+7000000000:+9:777777777.7777777777777777777777777777778 -+8000000000:+9:888888888.8888888888888888888888888888889 -+9000000000:+9:1000000000 -+35500000:+113:314159.2920353982300884955752212389380531 -+71000000:+226:314159.2920353982300884955752212389380531 -+106500000:+339:314159.2920353982300884955752212389380531 -+1000000000:+3:333333333.3333333333333333333333333333333 -2:25.024996000799840031993601279744051189762:0.07992009269196593320152084692285869265447 -123456:1:123456 -$div_scale = 20 -+1000000000:+9:111111111.11111111111 -+2000000000:+9:222222222.22222222222 -+3000000000:+9:333333333.33333333333 -+4000000000:+9:444444444.44444444444 -+5000000000:+9:555555555.55555555556 -+6000000000:+9:666666666.66666666667 -+7000000000:+9:777777777.77777777778 -+8000000000:+9:888888888.88888888889 -+9000000000:+9:1000000000 -1:10:0.1 -1:100:0.01 -1:1000:0.001 -1:10000:0.0001 -1:504:0.001984126984126984127 -2:1.987654321:1.0062111801179738436 -123456789.123456789123456789123456789:1:123456789.12345678912 -# the next two cases are the "old" behaviour, but are now (>v0.01) different -#+35500000:+113:314159.292035398230088 -#+71000000:+226:314159.292035398230088 -+35500000:+113:314159.29203539823009 -+71000000:+226:314159.29203539823009 -+106500000:+339:314159.29203539823009 -+1000000000:+3:333333333.33333333333 -$div_scale = 1 -# round to accuracy 1 after bdiv -+124:+3:40 -123456789.1234:1:100000000 -# reset scale for further tests -$div_scale = 40 -&fmod -+9:4:1 -+9:5:4 -+9000:56:40 -+56:9000:56 -# inf handling, see table in doc -0:inf:0 -0:-inf:0 -5:inf:5 -5:-inf:5 --5:inf:-5 --5:-inf:-5 -inf:5:0 --inf:5:0 -inf:-5:0 --inf:-5:0 -5:5:0 --5:-5:0 -inf:inf:NaN --inf:-inf:NaN --inf:inf:NaN -inf:-inf:NaN -8:0:8 -inf:0:inf -# exceptions to reminder rule --inf:0:-inf --8:0:-8 -0:0:NaN -abc:abc:NaN -abc:1:abc:NaN -1:abc:NaN -0:0:NaN -0:1:0 -1:0:1 -0:-1:0 --1:0:-1 -1:1:0 --1:-1:0 -1:-1:0 --1:1:0 -1:2:1 -2:1:0 -1000000000:9:1 -2000000000:9:2 -3000000000:9:3 -4000000000:9:4 -5000000000:9:5 -6000000000:9:6 -7000000000:9:7 -8000000000:9:8 -9000000000:9:0 -35500000:113:33 -71000000:226:66 -106500000:339:99 -1000000000:3:1 -10:5:0 -100:4:0 -1000:8:0 -10000:16:0 -999999999999:9:0 -999999999999:99:0 -999999999999:999:0 -999999999999:9999:0 -999999999999999:99999:0 --9:+5:1 -+9:-5:-1 --9:-5:-4 --5:3:1 --2:3:1 -4:3:1 -1:3:1 --5:-3:-2 --2:-3:-2 -4:-3:-2 -1:-3:-2 -4095:4095:0 -100041000510123:3:0 -152403346:12345:4321 -87654321:87654321:0 -# now some floating point tests -123:2.5:0.5 -1230:2.5:0 -123.4:2.5:0.9 -123e1:25:5 --2.1:1:0.9 -2.1:1:0.1 --2.1:-1:-0.1 -2.1:-1:-0.9 --3:1:0 -3:1:0 --3:-1:0 -3:-1:0 -&ffac -Nanfac:NaN --1:NaN -+inf:inf --inf:NaN -0:1 -1:1 -2:2 -3:6 -4:24 -5:120 -6:720 -10:3628800 -11:39916800 -12:479001600 -&froot -# sqrt() -+0:2:0 -+1:2:1 --1:2:NaN -# -$x ** (1/2) => -$y, but not in froot() --123.456:2:NaN -+inf:2:inf --inf:2:NaN -2:2:1.41421356237309504880168872420969807857 --2:2:NaN -4:2:2 -9:2:3 -16:2:4 -100:2:10 -123.456:2:11.11107555549866648462149404118219234119 -15241.38393:2:123.4559999756998444766131352122991626468 -1.44:2:1.2 -12:2:3.464101615137754587054892683011744733886 -0.49:2:0.7 -0.0049:2:0.07 -# invalid ones -1:NaN:NaN --1:NaN:NaN -0:NaN:NaN --inf:NaN:NaN -+inf:NaN:NaN -NaN:0:NaN -NaN:2:NaN -NaN:inf:NaN -NaN:inf:NaN -12:-inf:NaN -12:inf:NaN -+0:0:NaN -+1:0:NaN --1:0:NaN --2:0:NaN --123.45:0:NaN -+inf:0:NaN -12:1:12 --12:1:NaN -8:-1:NaN --8:-1:NaN -# cubic root -8:3:2 --8:3:NaN -# fourths root -16:4:2 -81:4:3 -# see t/bigroot() for more tests -&fsqrt -+0:0 --1:NaN --2:NaN --16:NaN --123.45:NaN -nanfsqrt:NaN -+inf:inf --inf:NaN -1:1 -2:1.41421356237309504880168872420969807857 -4:2 -9:3 -16:4 -100:10 -123.456:11.11107555549866648462149404118219234119 -15241.38393:123.4559999756998444766131352122991626468 -1.44:1.2 -# sqrt(1.44) = 1.2, sqrt(e10) = e5 => 12e4 -1.44E10:120000 -2e10:141421.356237309504880168872420969807857 -144e20:120000000000 -# proved to be an endless loop under 7-9 -12:3.464101615137754587054892683011744733886 -0.49:0.7 -0.0049:0.07 -&is_nan -123:0 -abc:1 -NaN:1 --123:0 -&is_inf -+inf::1 --inf::1 -abc::0 -1::0 -NaN::0 --1::0 -+inf:-:0 -+inf:+:1 --inf:-:1 --inf:+:0 -# it must be exactly /^[+-]inf$/ -+infinity::0 --infinity::0 -&is_odd -abc:0 -0:0 --1:1 --3:1 -1:1 -3:1 -1000001:1 -1000002:0 -+inf:0 --inf:0 -123.45:0 --123.45:0 -2:0 -&is_int -NaNis_int:0 -0:1 -1:1 -2:1 --2:1 --1:1 --inf:0 -+inf:0 -123.4567:0 --0.1:0 --0.002:0 -&is_even -abc:0 -0:1 --1:0 --3:0 -1:0 -3:0 -1000001:0 -1000002:1 -2:1 -+inf:0 --inf:0 -123.456:0 --123.456:0 -0.01:0 --0.01:0 -120:1 -1200:1 --1200:1 -&is_positive -0:0 -1:1 --1:0 --123:0 -NaN:0 --inf:0 -+inf:1 -&is_negative -0:0 -1:0 --1:1 --123:1 -NaN:0 --inf:1 -+inf:0 -&parts -0:0 1 -1:1 0 -123:123 0 --123:-123 0 --1200:-12 2 -NaNparts:NaN NaN -+inf:inf inf --inf:-inf inf -&exponent -0:1 -1:0 -123:0 --123:0 --1200:2 -+inf:inf --inf:inf -NaNexponent:NaN -&mantissa -0:0 -1:1 -123:123 --123:-123 --1200:-12 -+inf:inf --inf:-inf -NaNmantissa:NaN -&length -123:3 --123:3 -0:1 -1:1 -12345678901234567890:20 -&is_zero -NaNzero:0 -+inf:0 --inf:0 -0:1 --1:0 -1:0 -&is_one -NaNone:0 -+inf:0 --inf:0 -0:0 -2:0 -1:1 --1:0 --2:0 -&ffloor -0:0 -abc:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-52 -12.2:12 -0.12345:0 -0.123456:0 -0.1234567:0 -0.12345678:0 -0.123456789:0 -&fceil -0:0 -abc:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-51 -12.2:13 diff --git a/dist/Math-BigRat/t/bigfltrt.t b/dist/Math-BigRat/t/bigfltrt.t deleted file mode 100644 index 325e6fa77e..0000000000 --- a/dist/Math-BigRat/t/bigfltrt.t +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 1; - -BEGIN { - unshift @INC, 't'; -} - -use Math::BigRat::Test lib => 'Calc'; # test via this Subclass - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigRat::Test"; -$CL = "Math::BigInt::Calc"; - -pass(); - -# fails still too many tests -#require 't/bigfltpm.inc'; # all tests here for sharing diff --git a/dist/Math-BigRat/t/biglog.t b/dist/Math-BigRat/t/biglog.t deleted file mode 100644 index 42e9ac8d64..0000000000 --- a/dist/Math-BigRat/t/biglog.t +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/perl -w - -# Test blog function (and bpow, since it uses blog), as well as bexp(). - -use strict; -use Test::More tests => 17; - -use Math::BigRat; - -my $cl = "Math::BigRat"; - -############################################################################# -# test log($n) - -# does not work yet -#is ($cl->new(2)->blog(), '0', "blog(2)"); -#is ($cl->new(288)->blog(), '5',"blog(288)"); -#is ($cl->new(2000)->blog(), '7', "blog(2000)"); - -############################################################################# -# test exp($n) - -is ($cl->new(1)->bexp()->as_int(), '2', "bexp(1)"); -is ($cl->new(2)->bexp()->as_int(), '7',"bexp(2)"); -is ($cl->new(3)->bexp()->as_int(), '20', "bexp(3)"); - -# rounding not implemented yet -#is ($cl->new(3)->bexp(10), '20', "bexp(3,10)"); - -# $x < 0 => NaN -ok ($cl->new(-2)->blog(), 'NaN'); -ok ($cl->new(-1)->blog(), 'NaN'); -ok ($cl->new(-10)->blog(), 'NaN'); -ok ($cl->new(-2,2)->blog(), 'NaN'); - -############################################################################# -# test bexp() with cached results - -is ($cl->new(1)->bexp(), - '90933395208605785401971970164779391644753259799242' . '/' . - '33452526613163807108170062053440751665152000000000', - 'bexp(1)'); -is ($cl->new(2)->bexp(1,40), $cl->new(1)->bexp(1,45)->bpow(2,40), 'bexp(2)'); - -is ($cl->new("12.5")->bexp(1,61), $cl->new(1)->bexp(1,65)->bpow(12.5,61), 'bexp(12.5)'); - -############################################################################# -# test bexp() with big values (non-cached) - -is ($cl->new(1)->bexp(1,100)->as_float(100), - '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427', - 'bexp(100)'); - -is ($cl->new("12.5")->bexp(1,91), $cl->new(1)->bexp(1,95)->bpow(12.5,91), - 'bexp(12.5) to 91 digits'); - -############################################################################# -# some integer results -is ($cl->new(2)->bpow(32)->blog(2), '32', "2 ** 32"); -is ($cl->new(3)->bpow(32)->blog(3), '32', "3 ** 32"); -is ($cl->new(2)->bpow(65)->blog(2), '65', "2 ** 65"); - -my $x = Math::BigInt->new( '777' ) ** 256; -my $base = Math::BigInt->new( '12345678901234' ); -is ($x->copy()->blog($base), 56, 'blog(777**256, 12345678901234)'); - -$x = Math::BigInt->new( '777' ) ** 777; -$base = Math::BigInt->new( '777' ); -is ($x->copy()->blog($base), 777, 'blog(777**777, 777)'); - -# all done -1; diff --git a/dist/Math-BigRat/t/bigrat.t b/dist/Math-BigRat/t/bigrat.t deleted file mode 100644 index a640e59244..0000000000 --- a/dist/Math-BigRat/t/bigrat.t +++ /dev/null @@ -1,332 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 202; - -# basic testing of Math::BigRat - -use Math::BigRat; -use Math::BigInt; -use Math::BigFloat; - -# shortcuts -my $cr = 'Math::BigRat'; -my $mbi = 'Math::BigInt'; -my $mbf = 'Math::BigFloat'; - -my ($x,$y,$z); - -$x = Math::BigRat->new(1234); is ($x,1234); -isa_ok ($x, 'Math::BigRat'); -is ($x->isa('Math::BigFloat'), 0); -is ($x->isa('Math::BigInt'), 0); - -############################################################################## -# new and bnorm() - -foreach my $func (qw/new bnorm/) - { - $x = $cr->$func(1234); is ($x,1234); - - $x = $cr->$func('1234/1'); is ($x,1234); - $x = $cr->$func('1234/2'); is ($x,617); - - $x = $cr->$func('100/1.0'); is ($x,100); - $x = $cr->$func('10.0/1.0'); is ($x,10); - $x = $cr->$func('0.1/10'); is ($x,'1/100'); - $x = $cr->$func('0.1/0.1'); is ($x,'1'); - $x = $cr->$func('1e2/10'); is ($x,10); - $x = $cr->$func('5/1e2'); is ($x,'1/20'); - $x = $cr->$func('1e2/1e1'); is ($x,10); - $x = $cr->$func('1 / 3'); is ($x,'1/3'); - $x = $cr->$func('-1 / 3'); is ($x,'-1/3'); - $x = $cr->$func('NaN'); is ($x,'NaN'); - $x = $cr->$func('inf'); is ($x,'inf'); - $x = $cr->$func('-inf'); is ($x,'-inf'); - $x = $cr->$func('1/'); is ($x,'NaN'); - - $x = $cr->$func("0x7e"); is ($x,126); - - # input ala '1+1/3' isn't parsed ok yet - $x = $cr->$func('1+1/3'); is ($x,'NaN'); - - $x = $cr->$func('1/1.2'); is ($x,'5/6'); - $x = $cr->$func('1.3/1.2'); is ($x,'13/12'); - $x = $cr->$func('1.2/1'); is ($x,'6/5'); - - ############################################################################ - # other classes as input - - $x = $cr->$func($mbi->new(1231)); is ($x,'1231'); - $x = $cr->$func($mbf->new(1232)); is ($x,'1232'); - $x = $cr->$func($mbf->new(1232.3)); is ($x,'12323/10'); - } - -my $n = 'numerator'; -my $d = 'denominator'; - -$x = $cr->new('-0'); is ($x,'0'); is ($x->$n(), '0'); is ($x->$d(),'1'); -$x = $cr->new('NaN'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); -$x = $cr->new('-NaN'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); -$x = $cr->new('-1r4'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); - -$x = $cr->new('+inf'); is ($x,'inf'); is ($x->$n(), 'inf'); is ($x->$d(),'1'); -$x = $cr->new('-inf'); is ($x,'-inf'); is ($x->$n(), '-inf'); is ($x->$d(),'1'); -$x = $cr->new('123a4'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); - -# wrong inputs -$x = $cr->new('1e2e2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); -$x = $cr->new('1+2+2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); -# failed due to BigFloat bug -$x = $cr->new('1.2.2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN'); - -is ($cr->new('123a4'),'NaN'); -is ($cr->new('123e4'),'1230000'); -is ($cr->new('-NaN'),'NaN'); -is ($cr->new('NaN'),'NaN'); -is ($cr->new('+inf'),'inf'); -is ($cr->new('-inf'),'-inf'); - -############################################################################## -# two Bigints - -is ($cr->new($mbi->new(3),$mbi->new(7))->badd(1),'10/7'); -is ($cr->new($mbi->new(-13),$mbi->new(7)),'-13/7'); -is ($cr->new($mbi->new(13),$mbi->new(-7)),'-13/7'); -is ($cr->new($mbi->new(-13),$mbi->new(-7)),'13/7'); - -############################################################################## -# mixed arguments - -is ($cr->new('3/7')->badd(1),'10/7'); -is ($cr->new('3/10')->badd(1.1),'7/5'); -is ($cr->new('3/7')->badd($mbi->new(1)),'10/7'); -is ($cr->new('3/10')->badd($mbf->new('1.1')),'7/5'); - -is ($cr->new('3/7')->bsub(1),'-4/7'); -is ($cr->new('3/10')->bsub(1.1),'-4/5'); -is ($cr->new('3/7')->bsub($mbi->new(1)),'-4/7'); -is ($cr->new('3/10')->bsub($mbf->new('1.1')),'-4/5'); - -is ($cr->new('3/7')->bmul(1),'3/7'); -is ($cr->new('3/10')->bmul(1.1),'33/100'); -is ($cr->new('3/7')->bmul($mbi->new(1)),'3/7'); -is ($cr->new('3/10')->bmul($mbf->new('1.1')),'33/100'); - -is ($cr->new('3/7')->bdiv(1),'3/7'); -is ($cr->new('3/10')->bdiv(1.1),'3/11'); -is ($cr->new('3/7')->bdiv($mbi->new(1)),'3/7'); -is ($cr->new('3/10')->bdiv($mbf->new('1.1')),'3/11'); - -############################################################################## -$x = $cr->new('1/4'); $y = $cr->new('1/3'); - -is ($x + $y, '7/12'); -is ($x * $y, '1/12'); -is ($x / $y, '3/4'); - -$x = $cr->new('7/5'); $x *= '3/2'; -is ($x,'21/10'); -$x -= '0.1'; -is ($x,'2'); # not 21/10 - -$x = $cr->new('2/3'); $y = $cr->new('3/2'); -is ($x > $y,''); -is ($x < $y,1); -is ($x == $y,''); - -$x = $cr->new('-2/3'); $y = $cr->new('3/2'); -is ($x > $y,''); -is ($x < $y,'1'); -is ($x == $y,''); - -$x = $cr->new('-2/3'); $y = $cr->new('-2/3'); -is ($x > $y,''); -is ($x < $y,''); -is ($x == $y,'1'); - -$x = $cr->new('-2/3'); $y = $cr->new('-1/3'); -is ($x > $y,''); -is ($x < $y,'1'); -is ($x == $y,''); - -$x = $cr->new('-124'); $y = $cr->new('-122'); -is ($x->bacmp($y),1); - -$x = $cr->new('-124'); $y = $cr->new('-122'); -is ($x->bcmp($y),-1); - -$x = $cr->new('3/7'); $y = $cr->new('5/7'); -is ($x+$y,'8/7'); - -$x = $cr->new('3/7'); $y = $cr->new('5/7'); -is ($x*$y,'15/49'); - -$x = $cr->new('3/5'); $y = $cr->new('5/7'); -is ($x*$y,'3/7'); - -$x = $cr->new('3/5'); $y = $cr->new('5/7'); -is ($x/$y,'21/25'); - -$x = $cr->new('7/4'); $y = $cr->new('1'); -is ($x % $y,'3/4'); - -$x = $cr->new('7/4'); $y = $cr->new('5/13'); -is ($x % $y,'11/52'); - -$x = $cr->new('7/4'); $y = $cr->new('5/9'); -is ($x % $y,'1/12'); - -$x = $cr->new('-144/9')->bsqrt(); is ($x,'NaN'); -$x = $cr->new('144/9')->bsqrt(); is ($x,'4'); -$x = $cr->new('3/4')->bsqrt(); is ($x, - '1732050807568877293527446341505872366943/' - .'2000000000000000000000000000000000000000'); - -############################################################################## -# bpow - -$x = $cr->new('2/1'); $z = $x->bpow('3/1'); is ($x,'8'); -$x = $cr->new('1/2'); $z = $x->bpow('3/1'); is ($x,'1/8'); -$x = $cr->new('1/3'); $z = $x->bpow('4/1'); is ($x,'1/81'); -$x = $cr->new('2/3'); $z = $x->bpow('4/1'); is ($x,'16/81'); - -$x = $cr->new('2/3'); $z = $x->bpow('5/3'); -is ($x, '31797617848703662994667839220546583581/62500000000000000000000000000000000000'); - -############################################################################## -# bfac - -$x = $cr->new('1'); $x->bfac(); is ($x,'1'); -for (my $i = 0; $i < 8; $i++) - { - $x = $cr->new("$i/1")->bfac(); is ($x,$mbi->new($i)->bfac()); - } - -# test for $self->bnan() vs. $x->bnan(); -$x = $cr->new('-1'); $x->bfac(); is ($x,'NaN'); - -############################################################################## -# binc/bdec - -$x = $cr->new('3/2'); is ($x->binc(),'5/2'); -$x = $cr->new('15/6'); is ($x->bdec(),'3/2'); - -############################################################################## -# bfloor/bceil - -$x = $cr->new('-7/7'); is ($x->$n(), '-1'); is ($x->$d(), '1'); -$x = $cr->new('-7/7')->bfloor(); is ($x->$n(), '-1'); is ($x->$d(), '1'); - -############################################################################## -# bsstr - -$x = $cr->new('7/5')->bsstr(); is ($x,'7/5'); -$x = $cr->new('-7/5')->bsstr(); is ($x,'-7/5'); - -############################################################################## -# numify() - -my @array = qw/1 2 3 4 5 6 7 8 9/; -$x = $cr->new('8/8'); is ($array[$x],2); -$x = $cr->new('16/8'); is ($array[$x],3); -$x = $cr->new('17/8'); is ($array[$x],3); -$x = $cr->new('33/8'); is ($array[$x],5); -$x = $cr->new('-33/8'); is ($array[$x],6); -$x = $cr->new('-8/1'); is ($array[$x],2); # -8 => 2 - -$x = $cr->new('33/8'); is ($x->numify() * 1000, 4125); -$x = $cr->new('-33/8'); is ($x->numify() * 1000, -4125); -$x = $cr->new('inf'); is ($x->numify(), 'inf'); -$x = $cr->new('-inf'); is ($x->numify(), '-inf'); -$x = $cr->new('NaN'); is ($x->numify(), 'NaN'); - -$x = $cr->new('4/3'); is ($x->numify(), 4/3); - -############################################################################## -# as_hex(), as_bin(), as_oct() - -$x = $cr->new('8/8'); -is ($x->as_hex(), '0x1'); is ($x->as_bin(), '0b1'); is ($x->as_oct(), '01'); -$x = $cr->new('80/8'); -is ($x->as_hex(), '0xa'); is ($x->as_bin(), '0b1010'); is ($x->as_oct(), '012'); - -############################################################################## -# broot(), blog(), bmodpow() and bmodinv() - -$x = $cr->new(2) ** 32; -$y = $cr->new(4); -$z = $cr->new(3); - -is ($x->copy()->broot($y), 2 ** 8); -is (ref($x->copy()->broot($y)), $cr); - -is ($x->copy()->bmodpow($y,$z), 1); -is (ref($x->copy()->bmodpow($y,$z)), $cr); - -$x = $cr->new(8); -$y = $cr->new(5033); -$z = $cr->new(4404); - -is ($x->copy()->bmodinv($y), $z); -is (ref($x->copy()->bmodinv($y)), $cr); - -# square root with exact result -$x = $cr->new('1.44'); -is ($x->copy()->broot(2), '6/5'); -is (ref($x->copy()->broot(2)), $cr); - -# log with exact result -$x = $cr->new('256.1'); -is ($x->copy()->blog(2), '8000563442710106079310294693803606983661/1000000000000000000000000000000000000000'); -is (ref($x->copy()->blog(2)), $cr); - -$x = $cr->new(144); -is ($x->copy()->broot('2'), 12, 'v/144 = 12'); - -$x = $cr->new(12*12*12); -is ($x->copy()->broot('3'), 12, '(12*12*12) ** 1/3 = 12'); - -############################################################################## -# from_hex(), from_bin(), from_oct() - -$x = Math::BigRat->from_hex('0x100'); -is ($x, '256', 'from_hex'); -$x = $cr->from_hex('0x100'); -is ($x, '256', 'from_hex'); - -$x = Math::BigRat->from_bin('0b100'); -is ($x, '4', 'from_bin'); -$x = $cr->from_bin('0b100'); -is ($x, '4', 'from_bin'); - -$x = Math::BigRat->from_oct('0100'); -is ($x, '64', 'from_oct'); -$x = $cr->from_oct('0100'); -is ($x, '64', 'from_oct'); - -############################################################################## -# as_float() - -$x = Math::BigRat->new('1/2'); my $f = $x->as_float(); - -is ($x, '1/2', '$x unmodified'); -is ($f, '0.5', 'as_float(0.5)'); - -$x = Math::BigRat->new('2/3'); $f = $x->as_float(5); - -is ($x, '2/3', '$x unmodified'); -is ($f, '0.66667', 'as_float(2/3,5)'); - -############################################################################## -# int() - -$x = Math::BigRat->new('5/2'); -is int($x), '2', '5/2 converted to integer'; -$x = Math::BigRat->new('-1/2'); -is int($x), '0', '-1/2 converted to integer'; - -############################################################################## -# done - -1; diff --git a/dist/Math-BigRat/t/bigratpm.inc b/dist/Math-BigRat/t/bigratpm.inc deleted file mode 100644 index b2f507fee9..0000000000 --- a/dist/Math-BigRat/t/bigratpm.inc +++ /dev/null @@ -1,916 +0,0 @@ -#include this file into another test for subclass testing... - -is ($class->config()->{lib},$CL); - -$setup = ''; - -while () - { - chomp; - $_ =~ s/#.*$//; # remove comments - $_ =~ s/\s+$//; # trailing spaces - next if /^$/; # skip empty lines & comments - if (s/^&//) - { - $f = $_; - } - elsif (/^\$/) - { - $setup = $_; $setup =~ s/\$/\$${class}::/g; # round_mode, div_scale - #print "\$setup== $setup\n"; - } - else - { - if (m|^(.*?):(/.+)$|) - { - $ans = $2; - @args = split(/:/,$1,99); - } - else - { - @args = split(/:/,$_,99); $ans = pop(@args); - } - $try = "\$x = new $class \"$args[0]\";"; - if ($f eq "bnorm") - { - $try .= "\$x;"; - } elsif ($f eq "finf") { - my $a = $args[1] || ''; - $try .= "\$x->binf('$a');"; - } elsif ($f eq "is_inf") { - $try .= "\$x->is_inf('$args[1]');"; - } elsif ($f eq "fone") { - $try .= "\$x->bone('$args[1]');"; - } elsif ($f eq "fstr") { - $try .= "\$x->accuracy($args[1]); \$x->precision($args[2]);"; - $try .= '$x->bstr();'; - } elsif ($f eq "parts") { - # ->bstr() to see if an object is returned - $try .= '($a,$b) = $x->parts(); $a = $a->bstr(); $b = $b->bstr();'; - $try .= '"$a $b";'; - } elsif ($f eq "numerator") { - # ->bstr() to see if an object is returned - $try .= '$x->numerator()->bstr();'; - } elsif ($f eq "denominator") { - # ->bstr() to see if an object is returned - $try .= '$x->denominator()->bstr();'; - } elsif ($f =~ /^(length|numify)$/) { - $try .= "\$x->$f();"; - # some unary ops (can't test the fxxx form, since no AUTOLOAD in BigRat) - } elsif ($f =~ /^f(nan|sstr|neg|floor|ceil|abs)$/) { - $try .= "\$x->b$1();"; - # some is_xxx test function - } elsif ($f =~ /^is_(zero|one|pos|neg|negative|positive|odd|even|nan|int)\z/) { - $try .= "\$x->$f();"; - } elsif ($f =~ /^(as_number|as_int)\z/){ - $try .= "\$x->$1();"; - } elsif ($f eq "finc") { - $try .= '++$x;'; - } elsif ($f eq "fdec") { - $try .= '--$x;'; - } elsif ($f eq "digit") { - $try .= "\$x->digit($args[1]);"; - } elsif ($f eq "fround") { - $try .= "$setup; \$x->bround($args[1]);"; - } elsif ($f eq "ffround") { - $try .= "$setup; \$x->bfround($args[1]);"; - } elsif ($f eq "fsqrt") { - $try .= "$setup; \$x->bsqrt();"; - } elsif ($f eq "flog") { - $try .= "$setup; \$x->blog();"; - } elsif ($f eq "ffac") { - $try .= "$setup; \$x->bfac();"; - } - else - { - $try .= "\$y = new $class \"$args[1]\";"; - if ($f eq "bcmp") { - $try .= '$x <=> $y;'; - } elsif ($f eq "bacmp") { - $try .= '$x->bacmp($y);'; - } elsif ($f eq "bpow") { - $try .= '$x ** $y;'; - } elsif ($f eq "fpow") { - $try .= '$x->bpow($y);'; - } elsif ($f eq "badd") { - $try .= '$x + $y;'; - } elsif ($f eq "bsub") { - $try .= '$x - $y;'; - } elsif ($f eq "bmul") { - $try .= '$x * $y;'; - } elsif ($f eq "bdiv") { - $try .= "$setup; \$x / \$y;"; - } elsif ($f eq "bdiv-list") { - $try .= "$setup; join(',',\$x->bdiv(\$y));"; - } elsif ($f eq "brsft") { - $try .= '$x >> $y;'; - } elsif ($f eq "blsft") { - $try .= '$x << $y;'; - } elsif ($f eq "bmod") { - $try .= '$x % $y;'; - } elsif( $f eq "bmodinv") { - $try .= "\$x->bmodinv(\$y);"; - } elsif( $f eq "blog") { - $try .= "\$x->blog(\$y);"; - } else { - $try .= "\$z = $class->new(\"$args[2]\");"; - - # Functions with three arguments - if( $f eq "bmodpow") { - $try .= "\$x->bmodpow(\$y,\$z);"; - } else { warn "Unknown op '$f'"; } - } - } - # print "# Trying: '$try'\n"; - $ans1 = eval $try; - if ($ans =~ m|^/(.*)$|) - { - my $pat = $1; - like ($ans1, qr/$pat/); - } - else - { - if ($ans eq "") - { - is ($ans1, undef); - } - else - { - is ($ans1, $ans) or diag("Tried: '$try'"); -# if (ref($ans1) eq "$class") -# { -# # float numbers are normalized (for now), so mantissa shouldn't have -# # trailing zeros -# #print $ans1->_trailing_zeros(),"\n"; -# print "# Has trailing zeros after '$try'\n" -# if !is ($ans1->{_m}->_trailing_zeros(), 0); -# } - } - } # end pattern or string - } - } # end while - -# check whether $class->new( Math::BigInt->new()) destroys it -# ($y == 12 in this case) -$x = Math::BigInt->new(1200); $y = $class->new($x); -is ($y,1200); is ($x,1200); - -############################################################################### -# zero,inf,one,nan - -$x = $class->new(2); $x->bzero(); is ($x->{_a}, undef); is ($x->{_p}, undef); -$x = $class->new(2); $x->binf(); is ($x->{_a}, undef); is ($x->{_p}, undef); -$x = $class->new(2); $x->bone(); is ($x->{_a}, undef); is ($x->{_p}, undef); -$x = $class->new(2); $x->bnan(); is ($x->{_a}, undef); is ($x->{_p}, undef); - -__DATA__ -&digit -123:2:1 -1234:0:4 -1234:1:3 -1234:2:2 -1234:3:1 -1234:-1:1 -1234:-2:2 -1234:-3:3 -1234:-4:4 -0:0:0 -0:1:0 -&bmodinv -# format: number:modulus:result -# bmodinv Data errors -abc:abc:NaN -abc:5:NaN -5:abc:NaN -# bmodinv Expected Results from normal use -1:5:1 -3:5:2 -3:-5:-3 --2:5:2 -8:5033:4404 -1234567891:13:6 --1234567891:13:7 -324958749843759385732954874325984357439658735983745:2348249874968739:1741662881064902 -## bmodinv Error cases / useless use of function -inf:5:NaN -5:inf:NaN --inf:5:NaN -5:-inf:NaN -&as_number -144/7:20 -12/1:12 --12/1:-12 --12/3:-4 -NaN:NaN -+inf:inf --inf:-inf -&as_int -144/7:20 -12/1:12 --12/1:-12 --12/3:-4 -NaN:NaN -+inf:inf --inf:-inf -&bmodpow -# format: number:exponent:modulus:result -# bmodpow Data errors -abc:abc:abc:NaN -5:abc:abc:NaN -abc:5:abc:NaN -abc:abc:5:NaN -5:5:abc:NaN -5:abc:5:NaN -abc:5:5:NaN -# bmodpow Expected results -0:0:2:1 -1:0:2:1 -0:0:1:0 -8:7:5032:3840 -8:-1:5033:4404 -8:8:-5:-4 -98436739867439843769485798542749827593285729587325:43698764986460981048259837659386739857456983759328457:6943857329857295827698367:3104744730915914415259518 -# bmodpow Error cases -8:-1:16:NaN -inf:5:13:NaN -5:inf:13:NaN -&bmod -NaN:1:NaN -1:NaN:NaN -1:1:0 -2:2:0 -12:6:0 -7/4:4/14:1/28 -7/4:4/16:0 --7/4:4/16:0 --7/4:-4/16:0 -7/4:-4/16:0 -7/4:4/32:0 --7/4:4/32:0 --7/4:-4/32:0 -7/4:-4/32:0 -7/4:4/28:1/28 --7/4:4/28:3/28 -7/4:-4/28:-3/28 --7/4:-4/28:-1/28 -&fsqrt -1:1 -0:0 -NaN:NaN -+inf:inf --inf:NaN -144:12 -# sqrt(144) / sqrt(4) = 12/2 = 6/1 -144/4:6 -25/16:5/4 --3:NaN -&flog -NaN:NaN -0:NaN --2:NaN -&blog -NaN:NaN:NaN -0:NaN:NaN -NaN:0:NaN -NaN:1:NaN -1:NaN:NaN -0:2:NaN -0:-2:NaN -3:-2:NaN -&finf -1:+:inf -2:-:-inf -3:abc:inf -&numify -0:0 -+1:1 -1234:1234 -3/4:0.75 -5/2:2.5 -3/2:1.5 -5/4:1.25 -NaN:NaN -+inf:inf --inf:-inf -&fnan -abc:NaN -2:NaN --2:NaN -0:NaN -&fone -2:+:1 --2:-:-1 --2:+:1 -2:-:-1 -0::1 --2::1 -abc::1 -2:abc:1 -&fsstr -+inf:inf --inf:-inf -abcfsstr:NaN -1:1/1 -3/1:3/1 -0.1:1/10 -&bnorm -1:1 --0:0 -bnormNaN:NaN -+inf:inf --inf:-inf -inf/inf:NaN -5/inf:0 -5/-inf:0 -inf/5:inf --inf/5:-inf -inf/-5:-inf --inf/-5:inf -123:123 --123.4567:-1234567/10000 -# invalid inputs -1__2:NaN -1E1__2:NaN -11__2E2:NaN -#1.E3:NaN -.2E-3.:NaN -#1e3e4:NaN -.2E2:20 -inf:inf -+inf:inf --inf:-inf -+infinity:NaN -+-inf:NaN -abc:NaN - 1 a:NaN -1bcd2:NaN -11111b:NaN -+1z:NaN --1z:NaN -0:0 -+0:0 -+00:0 -+0_0_0:0 -000000_0000000_00000:0 --0:0 --0000:0 -+1:1 -+01:1 -+001:1 -+00000100000:100000 -+00000800/00000010:80 --00000800/00000010:-80 -+00000800/-00000010:-80 --00000800/-00000010:80 -123456789:123456789 --1:-1 --01:-1 --001:-1 --123456789:-123456789 --00000100000:-100000 -123.456a:NaN -123.456:15432/125 -0.01:1/100 -.002:1/500 -+.2:1/5 --0.0003:-3/10000 --.0000000004:-1/2500000000 -123456E2:12345600 -123456E-2:30864/25 --123456E2:-12345600 --123456E-2:-30864/25 -1e1:10 -2e-11:1/50000000000 -12/10:6/5 -0.1/0.1:1 -100/0.1:1000 -0.1/10:1/100 -1 / 3:1/3 -1/ 3:1/3 -1 /3:1/3 -&fneg -fnegNaN:NaN -+inf:-inf --inf:inf -+0:0 -+1:-1 --1:1 -+123456789:-123456789 --123456789:123456789 -+123.456789:-123456789/1000000 --123456.789:123456789/1000 -123/7:-123/7 --123/7:123/7 -123/-7:123/7 -&fabs -fabsNaN:NaN -+inf:inf --inf:inf -+0:0 -+1:1 --1:1 -+123456789:123456789 --123456789:123456789 -+123.456789:123456789/1000000 --123456.789:123456789/1000 -&badd -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:NaN --inf:+inf:NaN -+inf:+inf:inf --inf:-inf:-inf -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -+0:+0:0 -+1:+0:1 -+0:+1:1 -+1:+1:2 --1:+0:-1 -+0:-1:-1 --1:-1:-2 --1:+1:0 -+1:-1:0 -+9:+1:10 -+99:+1:100 -+999:+1:1000 -+9999:+1:10000 -+99999:+1:100000 -+999999:+1:1000000 -+9999999:+1:10000000 -+99999999:+1:100000000 -+999999999:+1:1000000000 -+9999999999:+1:10000000000 -+99999999999:+1:100000000000 -+10:-1:9 -+100:-1:99 -+1000:-1:999 -+10000:-1:9999 -+100000:-1:99999 -+1000000:-1:999999 -+10000000:-1:9999999 -+100000000:-1:99999999 -+1000000000:-1:999999999 -+10000000000:-1:9999999999 -+123456789:+987654321:1111111110 --123456789:+987654321:864197532 --123456789:-987654321:-1111111110 -+123456789:-987654321:-864197532 -1/3:1/3:2/3 -2/3:-1/3:1/3 -&bsub -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:-inf:inf --inf:+inf:-inf -+inf:+inf:NaN --inf:-inf:NaN -baddNaN:+inf:NaN -baddNaN:+inf:NaN -+inf:baddNaN:NaN --inf:baddNaN:NaN -+0:+0:0 -+1:+0:1 -+0:+1:-1 -+1:+1:0 --1:+0:-1 -+0:-1:1 --1:-1:0 --1:+1:-2 -+1:-1:2 -+9:+1:8 -+99:+1:98 -+999:+1:998 -+9999:+1:9998 -+99999:+1:99998 -+999999:+1:999998 -+9999999:+1:9999998 -+99999999:+1:99999998 -+999999999:+1:999999998 -+9999999999:+1:9999999998 -+99999999999:+1:99999999998 -+10:-1:11 -+100:-1:101 -+1000:-1:1001 -+10000:-1:10001 -+100000:-1:100001 -+1000000:-1:1000001 -+10000000:-1:10000001 -+100000000:-1:100000001 -+1000000000:-1:1000000001 -+10000000000:-1:10000000001 -+123456789:+987654321:-864197532 --123456789:+987654321:-1111111110 --123456789:-987654321:864197532 -+123456789:-987654321:1111111110 -2/3:1/3:1/3 -7/27:3/54:11/54 --2/3:+2/3:-4/3 --2/3:-2/3:0 -0:-123:123 -0:123:-123 -&bmul -abc:abc:NaN -abc:+0:NaN -+0:abc:NaN -+inf:NaNmul:NaN -+inf:NaNmul:NaN -NaNmul:+inf:NaN -NaNmul:-inf:NaN -+inf:+inf:inf -+inf:-inf:-inf -+inf:-inf:-inf -+inf:+inf:inf -+inf:123.34:inf -+inf:-123.34:-inf --inf:123.34:-inf --inf:-123.34:inf -123.34:+inf:inf --123.34:+inf:-inf -123.34:-inf:-inf --123.34:-inf:inf -+0:+0:0 -+0:+1:0 -+1:+0:0 -+0:-1:0 --1:+0:0 -+123456789123456789:+0:0 -+0:+123456789123456789:0 --1:-1:1 --1:+1:-1 -+1:-1:-1 -+1:+1:1 -+2:+3:6 --2:+3:-6 -+2:-3:-6 --2:-3:6 -+111:+111:12321 -+10101:+10101:102030201 -+1001001:+1001001:1002003002001 -+100010001:+100010001:10002000300020001 -+10000100001:+10000100001:100002000030000200001 -+11111111111:+9:99999999999 -+22222222222:+9:199999999998 -+33333333333:+9:299999999997 -+44444444444:+9:399999999996 -+55555555555:+9:499999999995 -+66666666666:+9:599999999994 -+77777777777:+9:699999999993 -+88888888888:+9:799999999992 -+99999999999:+9:899999999991 -6:120:720 -10:10000:100000 -1/4:1/3:1/12 -&bdiv-list -0:0:NaN,0 -0:1:0,0 -1:0:inf,1 --1:0:-inf,-1 -9:4:2,1 --9:4:-3,3 -9:-4:-3,-3 --9:-4:2,-1 -11/7:2/3:2,5/21 --11/7:2/3:-3,3/7 -&bdiv -$div_scale = 40; $round_mode = 'even' -abc:abc:NaN -abc:+1:abc:NaN -+1:abc:NaN --1:abc:NaN -0:abc:NaN -+0:+0:NaN -+0:+1:0 -+1:+0:inf -+3214:+0:inf -+0:-1:0 --1:+0:-inf --3214:+0:-inf -+1:+1:1 --1:-1:1 -+1:-1:-1 --1:+1:-1 -+1:+2:1/2 -+2:+1:2 -123:+inf:0 -123:-inf:0 -+10:+5:2 -+100:+4:25 -+1000:+8:125 -+10000:+16:625 -+10000:-16:-625 -+999999999999:+9:111111111111 -+999999999999:+99:10101010101 -+999999999999:+999:1001001001 -+999999999999:+9999:100010001 -+999999999999999:+99999:10000100001 -+1000000000:+9:1000000000/9 -+2000000000:+9:2000000000/9 -+3000000000:+9:1000000000/3 -+4000000000:+9:4000000000/9 -+5000000000:+9:5000000000/9 -+6000000000:+9:2000000000/3 -+7000000000:+9:7000000000/9 -+8000000000:+9:8000000000/9 -+9000000000:+9:1000000000 -+35500000:+113:35500000/113 -+71000000:+226:35500000/113 -+106500000:+339:35500000/113 -+1000000000:+3:1000000000/3 -2:25.024996000799840031993601279744051189762:1000000000000000000000000000000000000000/12512498000399920015996800639872025594881 -123456:1:123456 -1/4:1/3:3/4 -# reset scale for further tests -$div_scale = 40 -&is_nan -123:0 -abc:1 -NaN:1 --123:0 -&is_inf -+inf::1 --inf::1 -abc::0 -1::0 -NaN::0 --1::0 -+inf:-:0 -+inf:+:1 --inf:-:1 --inf:+:0 -# it must be exactly /^[+-]inf$/ -+infinity::0 --infinity::0 -&is_odd -abc:0 -0:0 --1:1 --3:1 -1:1 -3:1 -1000001:1 -1000002:0 -+inf:0 --inf:0 -123.45:0 --123.45:0 -2:0 -&is_int -NaNis_int:0 -0:1 -1:1 -2:1 --2:1 --1:1 --inf:0 -+inf:0 -123.4567:0 --0.1:0 --0.002:0 -1/3:0 -3/1:1 -&is_even -abc:0 -0:1 --1:0 --3:0 -1:0 -3:0 -1000001:0 -1000002:1 -2:1 -+inf:0 --inf:0 -123.456:0 --123.456:0 -0.01:0 --0.01:0 -120:1 -1200:1 --1200:1 -&is_pos -0:0 -1:1 --1:0 --123:0 -NaN:0 --inf:0 -+inf:1 -&is_positive -0:0 -1:1 --1:0 --123:0 -NaN:0 --inf:0 -+inf:1 -&is_neg -0:0 -1:0 --1:1 --123:1 -NaN:0 --inf:1 -+inf:0 -&is_negative -0:0 -1:0 --1:1 --123:1 -NaN:0 --inf:1 -+inf:0 -&parts -0:0 1 -1:1 1 -123:123 1 --123:-123 1 --1200:-1200 1 -5/7:5 7 --5/7:-5 7 -NaNparts:NaN NaN -+inf:inf inf --inf:-inf inf -&length -123:3 --123:3 -0:1 -1:1 -12345678901234567890:20 -&is_zero -NaNzero:0 -+inf:0 --inf:0 -0:1 --1:0 -1:0 -0/3:1 -1/3:0 --0/3:1 -5/inf:1 -&is_one -NaNone:0 -+inf:0 --inf:0 -0:0 -2:0 -1:1 --1:0 --2:0 -1/3:0 -100/100:1 -0.1/0.1:1 -5/inf:0 -&ffloor -0:0 -abc:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-52 -12.2:12 -3/7:0 -6/7:0 -7/7:1 -8/7:1 -13/7:1 -14/7:2 -15/7:2 --3/7:-1 --6/7:-1 --7/1:-7 --8/7:-2 --13/7:-2 --14/7:-2 --15/7:-3 -&fceil -0:0 -abc:NaN -+inf:inf --inf:-inf -1:1 --51:-51 --51.2:-51 -12.2:13 -3/7:1 -6/7:1 -8/7:2 -13/7:2 -14/7:2 -15/7:3 --3/7:0 --6/7:0 --8/7:-1 --13/7:-1 --14/7:-2 --15/7:-2 -&ffac -NaN:NaN -1:1 --1:NaN -&bpow -# bpow test for overload of ** -2:2:4 -3:3:27 -&bacmp -+0:-0:0 -+0:+1:-1 --1:+1:0 -+1:-1:0 --1:+2:-1 -+2:-1:1 --123456789:+987654321:-1 -+123456789:-987654321:-1 -+987654321:+123456789:1 --987654321:+123456789:1 --123:+4567889:-1 -# NaNs -acmpNaN:123: -123:acmpNaN: -acmpNaN:acmpNaN: -# infinity -+inf:+inf:0 --inf:-inf:0 -+inf:-inf:0 --inf:+inf:0 -+inf:123:1 --inf:123:1 -+inf:-123:1 --inf:-123:1 -+inf:1/23:1 --inf:1/23:1 -+inf:-1/23:1 --inf:-1/23:1 -+inf:12/3:1 --inf:12/3:1 -+inf:-12/3:1 --inf:-12/3:1 -123:inf:-1 --123:inf:-1 -123:-inf:-1 --123:-inf:-1 -1/23:inf:-1 --1/23:inf:-1 -1/23:-inf:-1 --1/23:-inf:-1 -12/3:inf:-1 --12/3:inf:-1 -12/3:-inf:-1 --12/3:-inf:-1 -# return undef -+inf:NaN: -NaN:inf: --inf:NaN: -NaN:-inf: -1/3:2/3:-1 -2/3:1/3:1 -2/3:2/3:0 -&fpow -2/1:3/1:8 -3/1:3/1:27 -5/2:3/1:125/8 --2/1:3/1:-8 --3/1:3/1:-27 --5/2:3/1:-125/8 --2/1:4/1:16 --3/1:4/1:81 --5/2:4/1:625/16 --5/2:-4/1:16/625 -1/5:-3:125 --1/5:-3:-125 -&numerator -NaN:NaN -inf:inf --inf:-inf -3/7:3 --3/7:-3 -0:0 -1:1 -5/-3:-5 -&denominator -NaN:NaN -inf:1 --inf:1 -3/7:7 -0:1 -1/1:1 --1/1:1 --3/7:7 -4/-5:5 -&finc -3/2:5/2 --15/6:-3/2 -NaN:NaN --1/3:2/3 --2/7:5/7 -&fdec -15/6:3/2 --3/2:-5/2 -1/3:-2/3 -2/7:-5/7 -NaN:NaN diff --git a/dist/Math-BigRat/t/bigratpm.t b/dist/Math-BigRat/t/bigratpm.t deleted file mode 100644 index b3f550e30c..0000000000 --- a/dist/Math-BigRat/t/bigratpm.t +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/perl -w - -use strict; -use Test::More tests => 696; - -use Math::BigRat lib => 'Calc'; - -use vars qw ($class $try $x $y $f @args $ans $ans1 $ans1_str $setup $CL); -$class = "Math::BigRat"; -$CL = "Math::BigInt::Calc"; - -require 't/bigratpm.inc'; # all tests here for sharing diff --git a/dist/Math-BigRat/t/bigratup.t b/dist/Math-BigRat/t/bigratup.t deleted file mode 100644 index a55cbb59ae..0000000000 --- a/dist/Math-BigRat/t/bigratup.t +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/perl -w - -# Test whether $Math::BigInt::upgrade breaks our neck - -use strict; -use Test::More tests => 5; - -use Math::BigInt upgrade => 'Math::BigRat'; -use Math::BigRat; - -my $rat = 'Math::BigRat'; -my ($x,$y,$z); - -############################################################################## -# bceil/bfloor - -$x = $rat->new('49/4'); is ($x->bfloor(),'12', 'floor(49/4)'); -$x = $rat->new('49/4'); is ($x->bceil(),'13', 'ceil(49/4)'); - -############################################################################## -# bsqrt - -$x = $rat->new('144'); is ($x->bsqrt(),'12', 'bsqrt(144)'); -$x = $rat->new('144/16'); is ($x->bsqrt(),'3', 'bsqrt(144/16)'); -$x = $rat->new('1/3'); is ($x->bsqrt(), - '1000000000000000000000000000000000000000/1732050807568877293527446341505872366943', - 'bsqrt(1/3)'); - -# all tests successful - -1; diff --git a/dist/Math-BigRat/t/bigroot.t b/dist/Math-BigRat/t/bigroot.t deleted file mode 100644 index 24599482e1..0000000000 --- a/dist/Math-BigRat/t/bigroot.t +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/perl -w - -# Test broot function (and bsqrt() function, since it is used by broot()). - -# It is too slow to be simple included in bigfltpm.inc, where it would get -# executed 3 times. - -# But it is better to test the numerical functionality, instead of not testing -# it at all. - -use strict; -use Test::More tests => 8 * 2; - -use Math::BigFloat; -use Math::BigInt; - -my $cl = "Math::BigFloat"; -my $c = "Math::BigInt"; - -# 2 ** 240 = -# 1766847064778384329583297500742918515827483896875618958121606201292619776 - -test_broot ('2','240', 8, undef, '1073741824'); -test_broot ('2','240', 9, undef, '106528681.3099908308759836475139583940127'); -test_broot ('2','120', 9, undef, '10321.27324073880096577298929482324664787'); -test_broot ('2','120', 17, undef, '133.3268493632747279600707813049418888729'); - -test_broot ('2','120', 8, undef, '32768'); -test_broot ('2','60', 8, undef, '181.0193359837561662466161566988413540569'); -test_broot ('2','60', 9, undef, '101.5936673259647663841091609134277286651'); -test_broot ('2','60', 17, undef, '11.54672461623965153271017217302844672562'); - -sub test_broot - { - my ($x,$n,$y,$scale,$result) = @_; - - my $s = $scale || 'undef'; - is ($cl->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $cl $x->bpow($n)->broot($y,$s) == $result"); - $result =~ s/\..*//; - is ($c->new($x)->bpow($n)->broot($y,$scale),$result, "Try: $c $x->bpow($n)->broot($y,$s) == $result"); - } diff --git a/dist/Math-BigRat/t/bitwise.t b/dist/Math-BigRat/t/bitwise.t deleted file mode 100644 index be9aa4ce38..0000000000 --- a/dist/Math-BigRat/t/bitwise.t +++ /dev/null @@ -1,15 +0,0 @@ -use strict; -use warnings; -use Test::More tests => 22; - -use Math::BigRat; - -my $x = Math::BigRat->new('3/7'); - -for my $op (qw(& | ^ << >> &= |= ^= <<= >>=)) { - ok !eval "my \$y = \$x $op 42; 1"; - like $@, qr/^bitwise operation \Q$op\E not supported in Math::BigRat/; -} - -ok !eval "my \$y = ~\$x; 1"; -like $@, qr/^bitwise operation ~ not supported in Math::BigRat/; diff --git a/dist/Math-BigRat/t/hang.t b/dist/Math-BigRat/t/hang.t deleted file mode 100644 index b2b94a0011..0000000000 --- a/dist/Math-BigRat/t/hang.t +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/perl -w - -# test for bug #34584: hang in exp(1/2) - -use strict; -use Test::More tests => 1; - -use Math::BigRat; - -my $result = Math::BigRat->new('1/2')->bexp(); - -is ("$result", "9535900335500879457687887524133067574481/5783815921445270815783609372070483523265", - "exp(1/2) worked"); - -############################################################################## -# done - -1; diff --git a/dist/Math-BigRat/t/requirer.t b/dist/Math-BigRat/t/requirer.t deleted file mode 100644 index 06ce1f4faa..0000000000 --- a/dist/Math-BigRat/t/requirer.t +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl -w - -# check that simple requiring BigRat works - -use strict; -use Test::More tests => 1; - -my ($x); - -require Math::BigRat; $x = Math::BigRat->new(1); ++$x; - -is ($x, 2, '$x got successfully modified'); - -# all tests done diff --git a/dist/Math-BigRat/t/trap.t b/dist/Math-BigRat/t/trap.t deleted file mode 100644 index 2811524a18..0000000000 --- a/dist/Math-BigRat/t/trap.t +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/perl -w - -# test that config ( trap_nan => 1, trap_inf => 1) really works/dies - -use strict; -use Test::More tests => 29; - -use Math::BigRat; - -my $mbi = 'Math::BigRat'; -my ($cfg,$x); - -foreach my $class ($mbi) - { - # can do and defaults are okay? - can_ok ($class, 'config'); - is ($class->config()->{trap_nan}, 0); - is ($class->config()->{trap_inf}, 0); - - # can set? - $cfg = $class->config( trap_nan => 1 ); is ($cfg->{trap_nan},1); - - # can set via hash ref? - $cfg = $class->config( { trap_nan => 1 } ); is ($cfg->{trap_nan},1); - - # also test that new() still works normally - eval ("\$x = \$class->new('42'); \$x->bnan();"); - like ($@, qr/^Tried to set/); - is ($x,42); # after new() never modified - - # can reset? - $cfg = $class->config( trap_nan => 0 ); is ($cfg->{trap_nan},0); - - # can set? - $cfg = $class->config( trap_inf => 1 ); is ($cfg->{trap_inf},1); - eval ("\$x = \$class->new('4711'); \$x->binf();"); - like ($@, qr/^Tried to set/); - is ($x,4711); # after new() never modified - - # +$x/0 => +inf - eval ("\$x = \$class->new('4711'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/); - is ($x,4711); # after new() never modified - - # -$x/0 => -inf - eval ("\$x = \$class->new('-0815'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/); - is ($x,-815); # after new() never modified - - $cfg = $class->config( trap_nan => 1 ); - # 0/0 => NaN - eval ("\$x = \$class->new('0'); \$x->bdiv(0);"); - like ($@, qr/^Tried to set/); - is ($x,0); # after new() never modified - } - -############################################################################## -# BigRat - -$cfg = Math::BigRat->config( trap_nan => 1 ); - -for my $trap (qw/0.1a +inf inf -inf/) - { - my $x = Math::BigRat->new('7/4'); - - eval ("\$x = \$mbi->new('$trap');"); - is ($x,'7/4'); # never modified since it dies - eval ("\$x = \$mbi->new('$trap');"); - is ($x,'7/4'); # never modified since it dies - eval ("\$x = \$mbi->new('$trap/7');"); - is ($x,'7/4'); # never modified since it dies - } - -# all tests done diff --git a/t/porting/known_pod_issues.dat b/t/porting/known_pod_issues.dat index 4ddc3c0934..a0c51356d3 100644 --- a/t/porting/known_pod_issues.dat +++ b/t/porting/known_pod_issues.dat @@ -241,8 +241,6 @@ YAML YAML::Syck YAML::Tiny dist/data-dumper/dumper.pm ? Should you be using L<...> instead of 1 -dist/math-bigint/lib/math/bigint.pm Verbatim line length including indents exceeds 79 by 71 -dist/math-bigrat/lib/math/bigrat.pm Verbatim line length including indents exceeds 79 by 7 dist/module-corelist/lib/module/corelist.pod Verbatim line length including indents exceeds 79 by 4 dist/module-corelist/lib/module/corelist/utils.pm Verbatim line length including indents exceeds 79 by 2 dist/pathtools/lib/file/spec/amigaos.pm Verbatim line length including indents exceeds 79 by 1 -- cgit v1.2.1