view release on metacpan or search on metacpan
#define DOUBLESIZE $doublesize
#$d_longdbl HAS_LONG_DOUBLE
#ifdef HAS_LONG_DOUBLE
#define LONG_DOUBLESIZE $longdblsize
#define LONG_DOUBLEKIND $longdblkind
#endif
#include <math.h>
#include <string.h>
#include <stdio.h>
/* Note that whether the sign bit is on or off
* for NaN depends on the CPU/FPU, and possibly
* can be affected by the build toolchain.
*
* For example for older MIPS and HP-PA 2.0 the quiet NaN is:
* 0x7f, 0xf7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
* 0x7f, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
* (respectively) as opposed to the more usual
* 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
*
* Pre-IEEE-754 floating point format do not even have inf/nan support
* at all. They might have a "max" value (DBL_MAX), which may be deadly
* to even mention, causing immediate SIGFPE or equivalent: this is
* the case with VAX floating point, for example.
*/
cpan/Math-BigInt/t/bitwise-mbr.t Math-BigInt
cpan/Math-BigInt/t/calling-class-methods.t Test Math::BigInt
cpan/Math-BigInt/t/calling-constant.t Test file related to Math::BigInt
cpan/Math-BigInt/t/calling-instance-methods.t Test Math::BigInt
cpan/Math-BigInt/t/calling-lib1.t Test file related to Math::BigInt
cpan/Math-BigInt/t/calling-lib2.t Test file related to Math::BigInt
cpan/Math-BigInt/t/config.t Test Math::BigInt->config()
cpan/Math-BigInt/t/downgrade-mbi-mbf.t Math-BigInt
cpan/Math-BigInt/t/downgrade-mbi-mbr.t Math-BigInt
cpan/Math-BigInt/t/hang-mbr.t Math-BigInt
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/Math/BigFloat/BareSubclass.pm Math-BigInt
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/Lib/Minimal.pm Module related to Math::BigInt
cpan/Math-BigInt/t/Math/BigInt/Lib/TestUtil.pm Test Math::BigInt
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/Math/BigRat/Subclass.pm Math-BigInt
cpan/Math-BigInt/t/mbf_ali.t Tests for BigFloat
cpan/Math-BigInt/t/mbi_ali.t Tests for BigInt
cpan/Math-BigInt/t/mbi_rand.t Test Math::BigInt randomly
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/mbr_ali.t Math-BigInt
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/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/rt121139.t Math-BigInt
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_mbr.t Math-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
Porting/Glossary view on Meta::CPAN
This variable conditionally defines HAS_MUNMAP if munmap() is
available to unmap a region mapped by mmap().
d_mymalloc (mallocsrc.U):
This variable conditionally defines MYMALLOC in case other parts
of the source want to take special action if MYMALLOC is used.
This may include different sorts of profiling or error detection.
d_nan (d_nan.U):
This variable conditionally defines HAS_NAN if nan() is
available to generate NaN.
d_nanosleep (d_nanosleep.U):
This variable conditionally defines HAS_NANOSLEEP
if nanosleep() is available to sleep with 1E-9 sec accuracy.
d_ndbm (i_ndbm.U):
This variable conditionally defines the HAS_NDBM symbol, which
indicates that both the ndbm.h include file and an appropriate ndbm
library exist. Consult the different i_*ndbm variables
to find out the actual include location. Sometimes, a system has the
Porting/config_H view on Meta::CPAN
* The returned values are defined in <float.h> and are:
*
* FP_PLUS_NORM Positive normalized, nonzero
* FP_MINUS_NORM Negative normalized, nonzero
* FP_PLUS_DENORM Positive denormalized, nonzero
* FP_MINUS_DENORM Negative denormalized, nonzero
* FP_PLUS_ZERO +0.0
* FP_MINUS_ZERO -0.0
* FP_PLUS_INF +INF
* FP_MINUS_INF -INF
* FP_NANS Signaling Not a Number (NaNS)
* FP_NANQ Quiet Not a Number (NaNQ)
*/
/*#define HAS_CLASS / **/
/* HAS_CLEARENV:
* This symbol, if defined, indicates that the clearenv () routine is
* available for use.
*/
#define HAS_CLEARENV /**/
/* HAS_STRUCT_CMSGHDR:
Porting/config_H view on Meta::CPAN
/* HAS_FEGETROUND:
* This symbol, if defined, indicates that the fegetround routine is
* available to return the macro corresponding to the current rounding
* mode.
*/
#define HAS_FEGETROUND /**/
/* HAS_FINITE:
* This symbol, if defined, indicates that the finite routine is
* available to check whether a double is finite (non-infinity non-NaN).
*/
#define HAS_FINITE /**/
/* HAS_FINITEL:
* This symbol, if defined, indicates that the finitel routine is
* available to check whether a long double is finite
* (non-infinity non-NaN).
*/
#define HAS_FINITEL /**/
/* HAS_FLOCK_PROTO:
* This symbol, if defined, indicates that the system provides
* a prototype for the flock() function. Otherwise, it is up
* to the program to supply one. A good guess is
* extern int flock(int, int);
*/
#define HAS_FLOCK_PROTO /**/
Porting/config_H view on Meta::CPAN
* This symbol, if defined, indicates that the fmin routine is
* available to do the minimum function.
*/
#define HAS_FMIN /**/
/* HAS_FP_CLASS:
* This symbol, if defined, indicates that the fp_class routine is
* available to classify doubles. Available for example in Digital UNIX.
* The returned values are defined in <math.h> and are:
*
* FP_SNAN Signaling NaN (Not-a-Number)
* FP_QNAN Quiet NaN (Not-a-Number)
* FP_POS_INF +infinity
* FP_NEG_INF -infinity
* FP_POS_NORM Positive normalized
* FP_NEG_NORM Negative normalized
* FP_POS_DENORM Positive denormalized
* FP_NEG_DENORM Negative denormalized
* FP_POS_ZERO +0.0 (positive zero)
* FP_NEG_ZERO -0.0 (negative zero)
*/
/*#define HAS_FP_CLASS / **/
Porting/config_H view on Meta::CPAN
* available to classify long doubles. Available for example in
* Digital UNIX. See for possible values HAS_FP_CLASS.
*/
/*#define HAS_FP_CLASSL / **/
/* HAS_FPCLASS:
* This symbol, if defined, indicates that the fpclass routine is
* available to classify doubles. Available for example in Solaris/SVR4.
* The returned values are defined in <ieeefp.h> and are:
*
* FP_SNAN signaling NaN
* FP_QNAN quiet NaN
* FP_NINF negative infinity
* FP_PINF positive infinity
* FP_NDENORM negative denormalized non-zero
* FP_PDENORM positive denormalized non-zero
* FP_NZERO negative zero
* FP_PZERO positive zero
* FP_NNORM negative normalized non-zero
* FP_PNORM positive normalized non-zero
*/
/*#define HAS_FPCLASS / **/
/* HAS_FPCLASSIFY:
* This symbol, if defined, indicates that the fpclassify routine is
* available to classify doubles. Available for example in HP-UX.
* The returned values are defined in <math.h> and are
*
* FP_NORMAL Normalized
* FP_ZERO Zero
* FP_INFINITE Infinity
* FP_SUBNORMAL Denormalized
* FP_NAN NaN
*
*/
/* HAS_FP_CLASSIFY:
* This symbol, if defined, indicates that the fp_classify routine is
* available to classify doubles. The values are defined in <math.h>
*
* FP_NORMAL Normalized
* FP_ZERO Zero
* FP_INFINITE Infinity
* FP_SUBNORMAL Denormalized
* FP_NAN NaN
*
*/
#define HAS_FPCLASSIFY /**/
/*#define HAS_FP_CLASSIFY / **/
/* HAS_FPCLASSL:
* This symbol, if defined, indicates that the fpclassl routine is
* available to classify long doubles. Available for example in IRIX.
* The returned values are defined in <ieeefp.h> and are:
*
* FP_SNAN signaling NaN
* FP_QNAN quiet NaN
* FP_NINF negative infinity
* FP_PINF positive infinity
* FP_NDENORM negative denormalized non-zero
* FP_PDENORM positive denormalized non-zero
* FP_NZERO negative zero
* FP_PZERO positive zero
* FP_NNORM negative normalized non-zero
* FP_PNORM positive normalized non-zero
*/
/*#define HAS_FPCLASSL / **/
Porting/config_H view on Meta::CPAN
#define HAS_INT64_T /**/
/* HAS_ISBLANK:
* This manifest constant lets the C program know that isblank
* is available.
*/
#define HAS_ISBLANK /**/
/* HAS_ISFINITE:
* This symbol, if defined, indicates that the isfinite routine is
* available to check whether a double is finite (non-infinity non-NaN).
*/
#define HAS_ISFINITE /**/
/* HAS_ISFINITEL:
* This symbol, if defined, indicates that the isfinitel routine is
* available to check whether a long double is finite.
* (non-infinity non-NaN).
*/
/*#define HAS_ISFINITEL / **/
/* HAS_ISINF:
* This symbol, if defined, indicates that the isinf routine is
* available to check whether a double is an infinity.
*/
#define HAS_ISINF /**/
/* HAS_ISINFL:
Porting/config_H view on Meta::CPAN
#define HAS_ISINFL /**/
/* HAS_ISLESS:
* This symbol, if defined, indicates that the isless routine is
* available to do the isless function.
*/
/*#define HAS_ISLESS / **/
/* HAS_ISNAN:
* This symbol, if defined, indicates that the isnan routine is
* available to check whether a double is a NaN.
*/
#define HAS_ISNAN /**/
/* HAS_ISNANL:
* This symbol, if defined, indicates that the isnanl routine is
* available to check whether a long double is a NaN.
*/
#define HAS_ISNANL /**/
/* HAS_ISNORMAL:
* This symbol, if defined, indicates that the isnormal routine is
* available to check whether a double is normal (non-zero normalized).
*/
#define HAS_ISNORMAL /**/
/* HAS_J0:
Porting/config_H view on Meta::CPAN
#define HAS_MPROTECT /**/
/* HAS_STRUCT_MSGHDR:
* This symbol, if defined, indicates that the struct msghdr
* is supported.
*/
#define HAS_STRUCT_MSGHDR /**/
/* HAS_NAN:
* This symbol, if defined, indicates that the nan routine is
* available to generate NaN.
*/
#define HAS_NAN /**/
/* HAS_NANOSLEEP:
* This symbol, if defined, indicates that the nanosleep
* system call is available to sleep with 1E-9 sec accuracy.
*/
#define HAS_NANOSLEEP /**/
/* HAS_NEARBYINT:
Porting/config_H view on Meta::CPAN
/* HAS_UALARM:
* This symbol, if defined, indicates that the ualarm routine is
* available to do alarms with microsecond granularity.
*/
#define HAS_UALARM /**/
/* HAS_UNORDERED:
* This symbol, if defined, indicates that the unordered routine is
* available to check whether two doubles are unordered
* (effectively: whether either of them is NaN)
*/
/*#define HAS_UNORDERED / **/
/* HAS_UNSETENV:
* This symbol, if defined, indicates that the unsetenv () routine is
* available for use.
*/
#define HAS_UNSETENV /**/
/* HAS_USLEEP_PROTO:
the IEEE 128-bit ("quadruple precision") which would give 116 bit of
mantissa (nor it is implemented in hardware), instead it's a special
software implementation called "double-double", which gives 106 bits
of mantissa.
There seem to be various problems in this long double implementation.
If Configure detects this brokenness, it will disable the long double support.
This can be overridden with explicit C<-Duselongdouble> (or C<-Dusemorebits>,
which enables both long doubles and 64 bit integers). If you decide to
enable long doubles, for most of the broken things Perl has implemented
workarounds, but the handling of the special values infinity and NaN
remains badly broken: for example infinity plus zero results in NaN.
=head2 Recommended Options AIX 5.1/5.2/5.3/6.1 and 7.1 (threaded/32-bit)
With the following options you get a threaded Perl version which
passes all make tests in threaded 32-bit mode, which is the default
configuration for the Perl builds that AIX ships with.
rm config.sh
./Configure \
-d \
Prior to 5.8.0, Perl simply accepted the default floating point options of the
C compiler, namely representing doubles with G_FLOAT on Alpha. Single
precision floating point values are represented in F_FLOAT format when either
D_FLOAT or G_FLOAT is in use for doubles. Beginning with 5.8.0, Alpha builds
now use IEEE floating point formats by default, which in VMS parlance are S_FLOAT
for singles and T_FLOAT for doubles. Itanium builds have always used IEEE by
default. The available non-default options are D_FLOAT or G_FLOAT on Alpha
or Itanium.
The use of IEEE introduces NaN, infinity, and denormalization capabilities not
available with D_FLOAT and G_FLOAT. When using one of those non-IEEE formats,
silent underflow and overflow are emulated in the conversion of strings to
numbers, but it is preferable to get the real thing by using IEEE where possible.
You are likely to see quite a few test failures when not using IEEE floating point.
Regardless of what floating point format you consider preferable, be aware
that the choice may have an impact on compatibility with external libraries,
such as database interfaces, and with existing data, such as data created with
the C<pack> function and written to disk, or data stored via the Storable
extension. For example, a C<pack("d", $foo)")> will create a D_FLOAT,
charclass_invlists.h view on Meta::CPAN
"600000",
"700000",
"800000",
"900000",
"1000000",
"10000000",
"20000000",
"100000000",
"10000000000",
"1000000000000",
"NaN",
"nan",
NULL
};
static const char * const UNI_sb_values[] = {
"AT",
"at",
"ATerm",
"aterm",
"CL",
config_h.SH view on Meta::CPAN
* The returned values are defined in <float.h> and are:
*
* FP_PLUS_NORM Positive normalized, nonzero
* FP_MINUS_NORM Negative normalized, nonzero
* FP_PLUS_DENORM Positive denormalized, nonzero
* FP_MINUS_DENORM Negative denormalized, nonzero
* FP_PLUS_ZERO +0.0
* FP_MINUS_ZERO -0.0
* FP_PLUS_INF +INF
* FP_MINUS_INF -INF
* FP_NANS Signaling Not a Number (NaNS)
* FP_NANQ Quiet Not a Number (NaNQ)
*/
#$d_class HAS_CLASS /**/
/* HAS_CLEARENV:
* This symbol, if defined, indicates that the clearenv () routine is
* available for use.
*/
#$d_clearenv HAS_CLEARENV /**/
/* HAS_STRUCT_CMSGHDR:
config_h.SH view on Meta::CPAN
/* HAS_FFSL:
* This symbol, if defined, indicates that the ffsl routine is available
* to find the first bit set in its argument. If it's not available,
* roll your own.
*/
#$d_ffs HAS_FFS /**/
#$d_ffsl HAS_FFSL /**/
/* HAS_FINITE:
* This symbol, if defined, indicates that the finite routine is
* available to check whether a double is finite (non-infinity non-NaN).
*/
#$d_finite HAS_FINITE /**/
/* HAS_FINITEL:
* This symbol, if defined, indicates that the finitel routine is
* available to check whether a long double is finite
* (non-infinity non-NaN).
*/
#$d_finitel HAS_FINITEL /**/
/* HAS_FLOCK_PROTO:
* This symbol, if defined, indicates that the system provides
* a prototype for the flock() function. Otherwise, it is up
* to the program to supply one. A good guess is
* extern int flock(int, int);
*/
#$d_flockproto HAS_FLOCK_PROTO /**/
config_h.SH view on Meta::CPAN
* This symbol, if defined, indicates that the fmin routine is
* available to do the minimum function.
*/
#$d_fmin HAS_FMIN /**/
/* HAS_FP_CLASS:
* This symbol, if defined, indicates that the fp_class routine is
* available to classify doubles. Available for example in Digital UNIX.
* The returned values are defined in <math.h> and are:
*
* FP_SNAN Signaling NaN (Not-a-Number)
* FP_QNAN Quiet NaN (Not-a-Number)
* FP_POS_INF +infinity
* FP_NEG_INF -infinity
* FP_POS_NORM Positive normalized
* FP_NEG_NORM Negative normalized
* FP_POS_DENORM Positive denormalized
* FP_NEG_DENORM Negative denormalized
* FP_POS_ZERO +0.0 (positive zero)
* FP_NEG_ZERO -0.0 (negative zero)
*/
#$d_fp_class HAS_FP_CLASS /**/
config_h.SH view on Meta::CPAN
* available to classify long doubles. Available for example in
* Digital UNIX. See for possible values HAS_FP_CLASS.
*/
#$d_fp_classl HAS_FP_CLASSL /**/
/* HAS_FPCLASS:
* This symbol, if defined, indicates that the fpclass routine is
* available to classify doubles. Available for example in Solaris/SVR4.
* The returned values are defined in <ieeefp.h> and are:
*
* FP_SNAN signaling NaN
* FP_QNAN quiet NaN
* FP_NINF negative infinity
* FP_PINF positive infinity
* FP_NDENORM negative denormalized non-zero
* FP_PDENORM positive denormalized non-zero
* FP_NZERO negative zero
* FP_PZERO positive zero
* FP_NNORM negative normalized non-zero
* FP_PNORM positive normalized non-zero
*/
#$d_fpclass HAS_FPCLASS /**/
/* HAS_FPCLASSIFY:
* This symbol, if defined, indicates that the fpclassify routine is
* available to classify doubles. Available for example in HP-UX.
* The returned values are defined in <math.h> and are
*
* FP_NORMAL Normalized
* FP_ZERO Zero
* FP_INFINITE Infinity
* FP_SUBNORMAL Denormalized
* FP_NAN NaN
*
*/
/* HAS_FP_CLASSIFY:
* This symbol, if defined, indicates that the fp_classify routine is
* available to classify doubles. The values are defined in <math.h>
*
* FP_NORMAL Normalized
* FP_ZERO Zero
* FP_INFINITE Infinity
* FP_SUBNORMAL Denormalized
* FP_NAN NaN
*
*/
#$d_fpclassify HAS_FPCLASSIFY /**/
#$d_fp_classify HAS_FP_CLASSIFY /**/
/* HAS_FPCLASSL:
* This symbol, if defined, indicates that the fpclassl routine is
* available to classify long doubles. Available for example in IRIX.
* The returned values are defined in <ieeefp.h> and are:
*
* FP_SNAN signaling NaN
* FP_QNAN quiet NaN
* FP_NINF negative infinity
* FP_PINF positive infinity
* FP_NDENORM negative denormalized non-zero
* FP_PDENORM positive denormalized non-zero
* FP_NZERO negative zero
* FP_PZERO positive zero
* FP_NNORM negative normalized non-zero
* FP_PNORM positive normalized non-zero
*/
#$d_fpclassl HAS_FPCLASSL /**/
config_h.SH view on Meta::CPAN
#$d_int64_t HAS_INT64_T /**/
/* HAS_ISBLANK:
* This manifest constant lets the C program know that isblank
* is available.
*/
#$d_isblank HAS_ISBLANK /**/
/* HAS_ISFINITE:
* This symbol, if defined, indicates that the isfinite routine is
* available to check whether a double is finite (non-infinity non-NaN).
*/
#$d_isfinite HAS_ISFINITE /**/
/* HAS_ISFINITEL:
* This symbol, if defined, indicates that the isfinitel routine is
* available to check whether a long double is finite.
* (non-infinity non-NaN).
*/
#$d_isfinitel HAS_ISFINITEL /**/
/* HAS_ISINF:
* This symbol, if defined, indicates that the isinf routine is
* available to check whether a double is an infinity.
*/
#$d_isinf HAS_ISINF /**/
/* HAS_ISINFL:
config_h.SH view on Meta::CPAN
#$d_isinfl HAS_ISINFL /**/
/* HAS_ISLESS:
* This symbol, if defined, indicates that the isless routine is
* available to do the isless function.
*/
#$d_isless HAS_ISLESS /**/
/* HAS_ISNAN:
* This symbol, if defined, indicates that the isnan routine is
* available to check whether a double is a NaN.
*/
#$d_isnan HAS_ISNAN /**/
/* HAS_ISNANL:
* This symbol, if defined, indicates that the isnanl routine is
* available to check whether a long double is a NaN.
*/
#$d_isnanl HAS_ISNANL /**/
/* HAS_ISNORMAL:
* This symbol, if defined, indicates that the isnormal routine is
* available to check whether a double is normal (non-zero normalized).
*/
#$d_isnormal HAS_ISNORMAL /**/
/* HAS_J0:
config_h.SH view on Meta::CPAN
#$d_mprotect HAS_MPROTECT /**/
/* HAS_STRUCT_MSGHDR:
* This symbol, if defined, indicates that the struct msghdr
* is supported.
*/
#$d_msghdr_s HAS_STRUCT_MSGHDR /**/
/* HAS_NAN:
* This symbol, if defined, indicates that the nan routine is
* available to generate NaN.
*/
#$d_nan HAS_NAN /**/
/* HAS_NANOSLEEP:
* This symbol, if defined, indicates that the nanosleep
* system call is available to sleep with 1E-9 sec accuracy.
*/
#$d_nanosleep HAS_NANOSLEEP /**/
/* HAS_NEARBYINT:
config_h.SH view on Meta::CPAN
/* HAS_UALARM:
* This symbol, if defined, indicates that the ualarm routine is
* available to do alarms with microsecond granularity.
*/
#$d_ualarm HAS_UALARM /**/
/* HAS_UNORDERED:
* This symbol, if defined, indicates that the unordered routine is
* available to check whether two doubles are unordered
* (effectively: whether either of them is NaN)
*/
#$d_unordered HAS_UNORDERED /**/
/* HAS_UNSETENV:
* This symbol, if defined, indicates that the unsetenv () routine is
* available for use.
*/
#$d_unsetenv HAS_UNSETENV /**/
/* HAS_USLEEP_PROTO:
configure.com view on Meta::CPAN
$ GOSUB inlibc
$ d_isless = tmp
$!
$! Check to see if pre-C99 isnanl exists
$!
$ OS
$ WS "#include <stdlib.h>"
$ WS "#include <math.h>"
$ WS "int main()"
$ WS "{"
$ WS " long double x = NaN;"
$ WS " return isnanl(x) ? EXIT_SUCCESS : EXIT_FAILURE;"
$ WS "}"
$ CS
$ tmp = "isnanl"
$ GOSUB inlibc
$ d_isnanl = tmp
$!
$! Check to see if llrint exists
$!
$ OS
cpan/JSON-PP/lib/JSON/PP.pm view on Meta::CPAN
results in a different JSON text from the one JSON::XS encodes (and
thus may break tests that compare entire JSON texts). If you do
need the previous behavior for compatibility or for finer control,
set PERL_JSON_PP_USE_B environmental variable to true before you
C<use> JSON::PP (or JSON.pm).
Note that numerical precision has the same meaning as under Perl (so
binary to decimal conversion follows the same rules as in Perl, which
can differ to other languages). Also, your perl interpreter might expose
extensions to the floating point numbers of your platform, such as
infinities or NaN's - these cannot be represented in JSON, and it is an
error to pass those in.
JSON::PP (and JSON::XS) trusts what you pass to C<encode> method
(or C<encode_json> function) is a clean, validated data structure with
values that can be represented as valid JSON values only, because it's
not from an external data source (as opposed to JSON texts you pass to
C<decode> or C<decode_json>, which JSON::PP considers tainted and
doesn't trust). As JSON::PP doesn't know exactly what you and consumers
of your JSON texts want the unexpected values to be (you may want to
convert them into null, or to stringify them with or without
normalisation (string representation of infinities/NaN may vary
depending on platforms), or to croak without conversion), you're advised
to do what you and your consumers need before you encode, and also not
to numify values that may start with values that look like a number
(including infinities/NaN), without validating.
=back
=head2 OBJECT SERIALISATION
As JSON cannot directly represent Perl objects, you have to choose between
a pure JSON representation (without the ability to deserialise the object
automatically again), and a nonstandard extension to the JSON syntax,
tagged values.
cpan/Math-BigInt-FastCalc/t/biglog.t view on Meta::CPAN
is($class->new(21)->blog(undef, 71),
'3.0445224377234229965005979803657054342845752874046106401940844835750742',
"$class->new(21)->blog(undef, 71)");
###############################################################################
# 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($class->new(-2)->blog(), 'NaN', "$class->new(-2)->blog()");
is($class->new(-1)->blog(), 'NaN', "$class->new(-1)->blog()");
is($class->new(-10)->blog(), 'NaN', "$class->new(-10)->blog()");
is($class->new(-2, 2)->blog(), 'NaN', "$class->new(-2, 2)->blog()");
my $ten = $class->new(10)->blog();
# 10 is cached (up to 75 digits)
is($class->new(10)->blog(),
'2.302585092994045684017991454684364207601',
qq|$class->new(10)->blog()|);
# 0.1 is using the cached value for log(10), too
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
package Math::BigFloat;
#
# Mike grinned. 'Two down, infinity to go' - Mike Nostrus in 'Before and After'
#
# The following hash values are used internally:
#
# sign : "+", "-", "+inf", "-inf", or "NaN"
# _m : absolute value of mantissa ($LIB thingy)
# _es : sign of exponent ("+" or "-")
# _e : absolute value of exponent ($LIB thingy)
# accuracy : accuracy (scalar)
# precision : precision (scalar)
use 5.006001;
use strict;
use warnings;
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
$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 $LIB = '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';
# Has import() been called yet? This variable is needed to make "require" work.
my $IMPORT = 0;
# 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)
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
###############################################################################
# Constructor methods
###############################################################################
sub new {
# Create a new Math::BigFloat object from a string or another bigfloat
# object.
# _e: exponent
# _m: mantissa
# sign => ("+", "-", "+inf", "-inf", or "NaN")
my $self = shift;
my $selfref = ref $self;
my $class = $selfref || $self;
# Make "require" work.
$class -> import() if $IMPORT == 0;
# Although this use has been discouraged for more than 10 years, people
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
( [+-]? )
inf (?: inity )?
\s*
\z
/ix)
{
my $sgn = $1 || '+';
return $class -> binf($sgn, @r);
}
# Handle explicit NaNs (not the ones returned due to invalid input).
if ($wanted =~ / ^
\s*
( [+-]? )
nan
\s*
\z
/ix)
{
return $class -> bnan(@r);
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
$self = $self->round(@r)
unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
return $downgrade -> new($self -> bdstr(), @r)
if defined($downgrade) && $self -> is_int();
return $self;
}
# If we get here, the value is neither a valid decimal, binary, octal, or
# hexadecimal number. It is not an explicit Inf or a NaN either.
return $class -> bnan(@r);
}
sub from_dec {
my $self = shift;
my $selfref = ref $self;
my $class = $selfref || $self;
# Make "require" work.
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
unless($selfref) {
$self->{accuracy} = $class -> accuracy();
$self->{precision} = $class -> precision();
}
}
return $self;
}
sub bnan {
# create/assign a 'NaN'
# Class::method(...) -> Class->method(...)
unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
$_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
{
#carp "Using ", (caller(0))[3], "() as a function is deprecated;",
# " use is as a method instead";
unshift @_, __PACKAGE__;
}
my $self = shift;
my $selfref = ref $self;
my $class = $selfref || $self;
{
no strict 'refs';
if (${"${class}::_trap_nan"}) {
croak("Tried to create NaN in $class->bnan()");
}
}
# Make "require" work.
$class -> import() if $IMPORT == 0;
# Don't modify constant (read-only) objects.
return $self if $selfref && $self->modify('bnan');
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
($x->{sign} eq $sign &&
$LIB->_is_zero($x->{_e}) &&
$LIB->_is_one($x->{_m})) ? 1 : 0;
}
sub is_odd {
# return true if arg (BFLOAT or num_str) is odd or false if even
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
(($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
($LIB->_is_zero($x->{_e})) &&
($LIB->_is_odd($x->{_m}))) ? 1 : 0;
}
sub is_even {
# return true if arg (BINT or num_str) is even or false if odd
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
(($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
($x->{_es} eq '+') && # 123.45 isn't
($LIB->_is_even($x->{_m}))) ? 1 : 0; # but 1200 is
}
sub is_int {
# return true if arg (BFLOAT or num_str) is an integer
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
(($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't
($x->{_es} eq '+')) ? 1 : 0; # 1e-1 => no integer
}
###############################################################################
# Comparison methods
###############################################################################
sub bcmp {
# Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
# Compares 2 values, ignoring their signs.
# Returns one of undef, <0, =0, >0. (suitable for sort)
# set up parameters
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# handle +-inf and NaN's
if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
return 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();
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
if defined($downgrade) && $x->is_int();
return $x;
}
sub binc {
# increment arg by one
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
return $x if $x->modify('binc');
# Inf and NaN
return $x -> bnan(@r) if $x -> is_nan();
return $x -> binf($x->{sign}, @r) if $x -> is_inf();
# Non-integer
if ($x->{_es} eq '-') {
return $x->badd($class->bone(), @r);
}
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
if defined($downgrade) && $x -> is_int();
return $x;
}
sub bdec {
# decrement arg by one
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
return $x if $x->modify('bdec');
# Inf and NaN
return $x -> bnan(@r) if $x -> is_nan();
return $x -> binf($x->{sign}, @r) if $x -> is_inf();
# Non-integer
if ($x->{_es} eq '-') {
return $x->badd($class->bone('-'), @r);
}
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
}
sub badd {
# set up parameters
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
return $x if $x->modify('badd');
# inf and NaN handling
if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
# $x is NaN and/or $y is NaN
if ($x->{sign} eq $nan || $y->{sign} eq $nan) {
$x = $x->bnan();
}
# $x is Inf and $y is Inf
elsif ($x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/) {
# +Inf + +Inf or -Inf + -Inf => same, rest is NaN
$x = $x->bnan() if $x->{sign} ne $y->{sign};
}
# +-inf + something => +-inf; something +-inf => +-inf
elsif ($y->{sign} =~ /^[+-]inf$/) {
$x->{sign} = $y->{sign};
}
return $downgrade -> new($x -> bdstr(), @r) if defined $downgrade;
return $x -> round(@r);
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
if ($y -> is_zero()) {
$x = $x -> round(@r);
} else {
# To correctly handle the special case $x -> bsub($x), we note the sign
# of $x, then flip the sign of $y, and if the sign of $x changed too,
# then we know that $x and $y are the same object.
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
if ($xsign =~ /^[+-]$/) {
$x = $x -> bzero(@r);
} else {
$x = $x -> bnan(); # NaN, -inf, +inf
}
return $downgrade -> new($x -> bdstr(), @r) if defined $downgrade;
return $x -> round(@r);
}
$x = $x -> badd($y, @r); # badd does not leave internal zeros
$y -> {sign} =~ tr/+-/-+/; # reset $y (does nothing for NaN)
}
return $downgrade -> new($x -> bdstr(), @r)
if defined($downgrade) && ($x->is_int() || $x->is_inf() || $x->is_nan());
$x; # already rounded by badd() or no rounding
}
sub bmul {
# multiply two numbers
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
# aEb * cEd = (a*c)E(b+d)
$x->{_m} = $LIB->_mul($x->{_m}, $y->{_m});
($x->{_e}, $x->{_es})
= $LIB -> _sadd($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
$r[3] = $y; # no push!
# adjust sign:
$x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
# z=inf handling (z=NaN handled above)
if ($z->{sign} =~ /^[+-]inf$/) {
$x->{sign} = $z->{sign};
return $downgrade -> new($x -> bdstr(), @r) if defined $downgrade;
return $x -> round(@r);
}
# take lower of the two e's and adapt m1 to it to match m2
my $e = $z->{_e};
$e = $LIB->_zero() if !defined $e; # if no BFLOAT?
$e = $LIB->_copy($e); # make copy (didn't do it yet)
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
my ($class, $x, $y, @r) = (ref($_[0]), @_);
# objectify is costly, so avoid it
if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
($class, $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().
if ($x -> is_nan() || $y -> is_nan()) {
return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
: $x -> bnan(@r);
}
# 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.
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
sub bmod {
# (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return remainder
# set up parameters
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: 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().
return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
# Modulo zero. This is handled the same way as in Math::BigInt -> bmod().
if ($y -> is_zero()) {
return $x -> round(@r);
}
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
return $num if $num->modify('bmodpow');
return $num -> bnan(@r)
if $mod->is_nan() || $exp->is_nan() || $mod->is_nan();
# check modulus for valid values
return $num->bnan(@r) if $mod->{sign} ne '+' || $mod->is_zero();
# check exponent for valid values
if ($exp->{sign} =~ /\w/) {
# i.e., if it's NaN, +inf, or -inf...
return $num->bnan(@r);
}
$num = $num->bmodinv($mod, @r) if $exp->{sign} eq '-';
# check num for valid values (also NaN if there was no inverse but $exp < 0)
return $num->bnan(@r) 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 = $num->bpow($exp)->bmod($mod);
return $downgrade -> new($num -> bdstr(), @r) if defined($downgrade)
&& ($num->is_int() || $num->is_inf() || $num->is_nan());
return $num -> round(@r);
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
# set up parameters
my ($class, $x, $y, $a, $p, $r) = (ref($_[0]), @_);
# objectify is costly, so avoid it
if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
($class, $x, $y, $a, $p, $r) = objectify(2, @_);
}
return $x if $x -> modify('bpow');
# $x and/or $y is a NaN
return $x -> bnan() if $x -> is_nan() || $y -> is_nan();
# $x and/or $y is a +/-Inf
if ($x -> is_inf("-")) {
return $x -> bzero() if $y -> is_negative();
return $x -> bnan() if $y -> is_zero();
return $x if $y -> is_odd();
return $x -> bneg();
} elsif ($x -> is_inf("+")) {
return $x -> bzero() if $y -> is_negative();
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
return $x -> bone() if $x -> is_one("+");
return $x -> binf("+");
}
if ($x -> is_zero()) {
return $x -> bone() if $y -> is_zero();
return $x -> binf() if $y -> is_negative();
return $x;
}
# We don't support complex numbers, so upgrade or return NaN.
if ($x -> is_negative() && !$y -> is_int()) {
return $upgrade -> bpow($x, $y, $a, $p, $r) if defined $upgrade;
return $x -> bnan();
}
if ($x -> is_one("+") || $y -> is_one()) {
return $x;
}
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
return $x -> bzero(@r) if $x->{sign} eq '-inf';
# Get the rounding parameters, if any.
my $fallback = 0;
my ($scale, @params);
($x, @params) = $x -> _find_round_parameters(@r);
# Error in _find_round_parameters?
return $x -> bnan(@r) if $x->{sign} eq 'NaN';
return $x -> bone(@r) if $x -> is_zero();
# If no rounding parameters are give, use fallback.
if (!@params) {
$params[0] = $class -> div_scale(); # fallback accuracy
$params[1] = undef; # no precision
$params[2] = $r[2]; # rounding mode
$scale = $params[0];
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
# $y -> batan2($x) returns the arcus tangens of $y / $x.
# Set up parameters.
my ($class, $y, $x, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
# Quick exit if $y is read-only.
return $y if $y -> modify('batan2');
# Handle all NaN cases.
return $y -> bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan;
# 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();
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
return $x -> bnan(@r) if $x->is_nan() || $y->is_nan();
if ($x -> is_neg()) {
# -27 ** (1/3) = -3
return $x -> broot($y -> copy() -> bneg(), @r) -> bneg()
if $x -> is_int() && $y -> is_int() && $y -> is_neg();
return $upgrade -> broot($x, $y, @r) if defined $upgrade;
return $x -> bnan(@r);
}
# 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(@r);
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
# round a number with A=5 to 5 digits afterwards again
if (defined $x->{accuracy} && $x->{accuracy} < $scale) {
return $downgrade -> new($x) if defined($downgrade)
&& ($x->is_int() || $x->is_inf() || $x->is_nan());
return $x;
}
# scale < 0 makes no sense
# scale == 0 => keep all digits
# never round a +-inf, NaN
if ($scale <= 0 || $x->{sign} !~ /^[+-]$/) {
return $downgrade -> new($x) if defined($downgrade)
&& ($x->is_int() || $x->is_inf() || $x->is_nan());
return $x;
}
# 1: never round a 0
# 2: if we should keep more digits than the mantissa has, do nothing
if ($x->is_zero() || $LIB->_len($x->{_m}) <= $scale) {
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
return $x if $x->modify('bfround'); # no-op
my ($scale, $mode) = $x->_scale_p(@p);
if (!defined $scale) {
return $downgrade -> new($x) if defined($downgrade)
&& ($x->is_int() || $x->is_inf() || $x->is_nan());
return $x;
}
# never round a 0, +-inf, NaN
if ($x->is_zero()) {
$x->{precision} = $scale if !defined $x->{precision} || $x->{precision} < $scale; # -3 < -2
return $downgrade -> new($x) if defined($downgrade)
&& ($x->is_int() || $x->is_inf() || $x->is_nan());
return $x;
}
if ($x->{sign} !~ /^[+-]$/) {
return $downgrade -> new($x) if defined($downgrade)
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
#carp "Using ", (caller(0))[3], "() as a function is deprecated;",
# " use is as a method instead";
unshift @_, __PACKAGE__;
}
my ($class, @args) = objectify(0, @_);
my $x = shift @args;
$x = defined(blessed($x)) && $x -> isa(__PACKAGE__) ? $x -> copy()
: $class -> new($x);
return $class->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN?
while (@args) {
my $y = shift @args;
$y = $class -> new($y)
unless defined(blessed($y)) && $y -> isa(__PACKAGE__);
return $x->bnan() unless $y -> is_int();
my $gcd = $x -> bgcd($y);
$x = $x -> bdiv($gcd) -> bmul($y);
}
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
sub dparts {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Not-a-number.
if ($x -> is_nan()) {
my $int = $class -> bnan();
return $int unless wantarray;
my $frc = $class -> bzero(); # or NaN?
return ($int, $frc);
}
# Infinity.
if ($x -> is_inf()) {
my $int = $class -> binf($x->{sign});
return $int unless wantarray;
my $frc = $class -> bzero();
return ($int, $frc);
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
}
# Fractional parts with the numerator and denominator as integers. E.g.,
# "123.4375" is returned as "1975" and "16".
sub fparts {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# NaN => NaN/NaN
if ($x -> is_nan()) {
return $class -> bnan() unless wantarray;
return $class -> bnan(), $class -> bnan();
}
# ±Inf => ±Inf/1
if ($x -> is_inf()) {
my $numer = $class -> binf($x->{sign});
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
###############################################################################
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 ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Finite number
my $es = '0';
my $len = 1;
my $cad = 0;
my $dot = '.';
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
$es;
}
# Decimal notation, e.g., "12345.6789" (no exponent).
sub bdstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bdstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
}
# Scientific notation with significand/mantissa and exponent as integers, e.g.,
# "12345.6789" is written as "123456789e-4".
sub bsstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bsstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
. 'e' . $x->{_es} . $LIB->_str($x->{_e});
}
# Normalized notation, e.g., "12345.6789" is written as "1.23456789e+4".
sub bnstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bnstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
return $str;
}
# Engineering notation, e.g., "12345.6789" is written as "12.3456789e+3".
sub bestr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bestr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
return $str;
}
# Fractional notation, e.g., "123.4375" is written as "1975/16".
sub bfstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bfstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
return $str;
}
sub to_hex {
# return number as hexadecimal string (only for integers defined)
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> to_hex($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
my $str = $LIB->_to_hex($z);
return $x->{sign} eq '-' ? "-$str" : $str;
}
sub to_oct {
# return number as octal digit string (only for integers defined)
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> to_oct($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
my $str = $LIB->_to_oct($z);
return $x->{sign} eq '-' ? "-$str" : $str;
}
sub to_bin {
# return number as binary digit string (only for integers defined)
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> to_bin($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
# Boolean methods (these don't modify the invocand)
$x->is_zero(); # if $x is 0
$x->is_one(); # if $x is +1
$x->is_one("+"); # ditto
$x->is_one("-"); # if $x is -1
$x->is_inf(); # if $x is +inf or -inf
$x->is_inf("+"); # if $x is +inf
$x->is_inf("-"); # if $x is -inf
$x->is_nan(); # if $x is NaN
$x->is_positive(); # if $x > 0
$x->is_pos(); # ditto
$x->is_negative(); # if $x < 0
$x->is_neg(); # ditto
$x->is_odd(); # if $x is odd
$x->is_even(); # if $x is even
$x->is_int(); # if $x is an integer
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
$x->bne($y); # true if and only if $x != $y
$x->blt($y); # true if and only if $x < $y
$x->ble($y); # true if and only if $x <= $y
$x->bgt($y); # true if and only if $x > $y
$x->bge($y); # true if and only if $x >= $y
# Arithmetic methods
$x->bneg(); # negation
$x->babs(); # absolute value
$x->bsgn(); # sign function (-1, 0, 1, or NaN)
$x->bnorm(); # normalize (no-op)
$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->bmuladd($y,$z); # $x = $x * $y + $z
$x->bdiv($y); # division (floored), set $x to quotient
# return (quo,rem) or quo if scalar
$x->btdiv($y); # division (truncated), set $x to quotient
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
$x->bceil(); # round towards plus infinity
$x->bint(); # round towards zero
# Other mathematical methods
$x->bgcd($y); # greatest common divisor
$x->blcm($y); # least common multiple
# Object property methods (do not modify the invocand)
$x->sign(); # the sign, either +, - or NaN
$x->digit($n); # the nth digit, counting from the right
$x->digit(-$n); # the nth digit, counting from the left
$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 Math::BigInt objects
$x->mantissa(); # return (signed) mantissa as BigInt
$x->exponent(); # return exponent as BigInt
$x->parts(); # return (mantissa,exponent) as BigInt
$x->sparts(); # mantissa and exponent (as integers)
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
=item *
Leading and trailing whitespace is ignored.
=item *
Leading zeros are ignored, except for floating point numbers with a binary
exponent, in which case the number is interpreted as an octal floating point
number. For example, "01.4p+0" gives 1.5, "00.4p+0" gives 0.5, but "0.4p+0"
gives a NaN. And while "0377" gives 255, "0377p0" gives 255.
=item *
If the string has a "0x" or "0X" prefix, it is interpreted as a hexadecimal
number.
=item *
If the string has a "0o" or "0O" prefix, it is interpreted as an octal number. A
floating point literal with a "0" prefix is also interpreted as an octal number.
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
If the string has a "0b" or "0B" prefix, it is interpreted as a binary number.
=item *
Underline characters are allowed in the same way as they are allowed in literal
numerical constants.
=item *
If the string can not be interpreted, NaN is returned.
=item *
For hexadecimal, octal, and binary floating point numbers, the exponent must be
separated from the significand (mantissa) by the letter "p" or "P", not "e" or
"E" as with decimal numbers.
=back
Some examples of valid string input
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
=over
=item from_hex()
$x -> from_hex("0x1.921fb54442d18p+1");
$x = Math::BigFloat -> from_hex("0x1.921fb54442d18p+1");
Interpret input as a hexadecimal string.A prefix ("0x", "x", ignoring case) is
optional. A single underscore character ("_") may be placed between any two
digits. If the input is invalid, a NaN is returned. The exponent is in base 2
using decimal digits.
If called as an instance method, the value is assigned to the invocand.
=item from_oct()
$x -> from_oct("1.3267p-4");
$x = Math::BigFloat -> from_oct("1.3267p-4");
Interpret input as an octal string. A single underscore character ("_") may be
placed between any two digits. If the input is invalid, a NaN is returned. The
exponent is in base 2 using decimal digits.
If called as an instance method, the value is assigned to the invocand.
=item from_bin()
$x -> from_bin("0b1.1001p-4");
$x = Math::BigFloat -> from_bin("0b1.1001p-4");
Interpret input as a hexadecimal string. A prefix ("0b" or "b", ignoring case)
is optional. A single underscore character ("_") may be placed between any two
digits. If the input is invalid, a NaN is returned. The exponent is in base 2
using decimal digits.
If called as an instance method, the value is assigned to the invocand.
=item from_ieee754()
Interpret the input as a value encoded as described in IEEE754-2008. The input
can be given as a byte string, hex string or binary string. The input is
assumed to be in big-endian byte-order.
cpan/Math-BigInt/lib/Math/BigFloat.pm view on Meta::CPAN
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</precision()> with L</accuracy()> 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
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
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:
#
# sign : "+", "-", "+inf", "-inf", or "NaN"
# value : unsigned int with actual value ($LIB thingy)
# accuracy : accuracy (scalar)
# precision : precision (scalar)
# Remember not to take shortcuts ala $xs = $x->{value}; $LIB->foo($xs); since
# underlying lib might change the reference!
use 5.006001;
use strict;
use warnings;
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# $round_mode is 'even', 'odd', '+inf', '-inf', 'zero', 'trunc', or 'common'.
our $round_mode = 'even';
our $accuracy = undef;
our $precision = undef;
our $div_scale = 40;
our $upgrade = undef; # default is no upgrade
our $downgrade = undef; # default is no downgrade
# These are internally, and not to be used from the outside at all
our $_trap_nan = 0; # are NaNs ok? set w/ config()
our $_trap_inf = 0; # are infs ok? set w/ config()
my $nan = 'NaN'; # constants for easier life
# Module to do the low level math.
my $DEFAULT_LIB = 'Math::BigInt::Calc';
my $LIB;
# Has import() been called yet? This variable is needed to make "require" work.
my $IMPORT = 0;
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
( [+-]? )
inf (?: inity )?
\s*
\z
/ix)
{
my $sgn = $1 || '+';
return $class -> binf($sgn, @r);
}
# Handle explicit NaNs (not the ones returned due to invalid input).
if ($wanted =~ / ^
\s*
( [+-]? )
nan
\s*
\z
/ix)
{
return $class -> bnan(@r);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
return $self;
}
# The value is not an integer, so upgrade if upgrading is enabled.
return $upgrade -> new($wanted, @r) if defined $upgrade;
}
# If we get here, the value is neither a valid decimal, binary, octal, or
# hexadecimal number. It is not explicit an Inf or a NaN either.
return $class -> bnan(@r);
}
# Create a Math::BigInt from a decimal string. This is an equivalent to
# from_hex(), from_oct(), and from_bin(). It is like new() except that it does
# not accept anything but a string representing a finite decimal number.
sub from_dec {
my $self = shift;
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
$self->{precision} = $_[1];
} elsif (!$selfref) {
$self->{accuracy} = $class -> accuracy();
$self->{precision} = $class -> precision();
}
return $self;
}
sub bnan {
# create/assign a 'NaN'
# Class::method(...) -> Class->method(...)
unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
$_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
{
#carp "Using ", (caller(0))[3], "() as a function is deprecated;",
# " use is as a method instead";
unshift @_, __PACKAGE__;
}
my $self = shift;
my $selfref = ref($self);
my $class = $selfref || $self;
{
no strict 'refs';
if (${"${class}::_trap_nan"}) {
croak("Tried to create NaN in $class->bnan()");
}
}
# Make "require" work.
$class -> import() if $IMPORT == 0;
# Don't modify constant (read-only) objects.
return $self if $selfref && $self->modify('bnan');
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
}
###############################################################################
# Boolean methods
###############################################################################
sub is_zero {
# return true if arg (BINT or num_str) is zero (array '+', '0')
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
return 0 if $x->{sign} !~ /^\+$/; # -, NaN & +-inf aren't
$LIB->_is_zero($x->{value});
}
sub is_one {
# return true if arg (BINT or num_str) is +1, or -1 if sign is given
my (undef, $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
$LIB->_is_one($x->{value});
}
sub is_finite {
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
return $x->{sign} eq '+' || $x->{sign} eq '-';
}
sub is_inf {
# return true if arg (BINT or num_str) is +-inf
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
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_nan {
# return true if arg (BINT or num_str) is NaN
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
$x->{sign} eq $nan ? 1 : 0;
}
sub is_positive {
# return true when arg (BINT or num_str) is positive (> 0)
my (undef, $x) = ref($_[0]) ? (undef, @_) : 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 (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
$x->{sign} =~ /^-/ ? 1 : 0; # -inf is negative, but NaN is not
}
sub is_non_negative {
# Return true if argument is non-negative (>= 0).
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
return 1 if $x->{sign} =~ /^\+/;
return 1 if $x -> is_zero();
return 0;
}
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
return 1 if $x->{sign} =~ /^\-/;
return 1 if $x -> is_zero();
return 0;
}
sub is_odd {
# return true when arg (BINT or num_str) is odd, false for even
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't
$LIB->_is_odd($x->{value});
}
sub is_even {
# return true when arg (BINT or num_str) is even, false for odd
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't
$LIB->_is_even($x->{value});
}
sub is_int {
# return true when arg (BINT or num_str) is an integer
my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
$x->{sign} =~ /^[+-]$/ ? 1 : 0; # inf/-inf/NaN aren't
}
###############################################################################
# Comparison methods
###############################################################################
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
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
return $upgrade->bcmp($x, $y)
if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) {
# handle +-inf and NaN
return 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
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
return $upgrade->bacmp($x, $y)
if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) {
# handle +-inf and NaN
return 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;
}
$LIB->_acmp($x->{value}, $y->{value}); # lib does only 0, 1, -1
}
sub beq {
my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# (BINT or num_str) return BINT
# negate number or make a negated number from string
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
return $x if $x->modify('bneg');
return $upgrade -> bneg($x, @r)
if defined($upgrade) && !$x->isa(__PACKAGE__);
# Don't negate +0 so we always have the normalized form +0. Does nothing for
# 'NaN'.
$x->{sign} =~ tr/+-/-+/
unless $x->{sign} eq '+' && $LIB->_is_zero($x->{value});
$x -> round(@r);
}
sub babs {
# (BINT or num_str) return BINT
# make number absolute, or return absolute BINT from string
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
? (ref($_[0]), @_)
: objectify(2, @_);
return $x if $x->modify('badd');
$r[3] = $y; # no push!
return $upgrade->badd($x, $y, @r)
if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
# Inf and NaN handling
if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
# NaN first
return $x->bnan(@r) 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->round(@r) if $x->{sign} eq $y->{sign};
return $x->bnan(@r);
}
# ±Inf + something => ±Inf
# something + ±Inf => ±Inf
if ($y->{sign} =~ /^[+-]inf$/) {
$x->{sign} = $y->{sign};
}
return $x -> round(@r);
}
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
return $upgrade -> bsub($x, $y, @r)
if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
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(@r); # NaN, -inf, +inf
}
$x = $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 rounding
}
sub bmul {
# multiply the first number by the second number
# (BINT or num_str, BINT or num_str) return BINT
# set up parameters
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
$x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => +
$x->{value} = $LIB->_mul($x->{value}, $y->{value}); # do actual math
$x->{sign} = '+' if $LIB->_is_zero($x->{value}); # no -0
($x->{value}, $x->{sign})
= $LIB -> _sadd($x->{value}, $x->{sign}, $zv, $zs);
return $x->round(@r);
}
# At least one of x, y, and z is a NaN
return $x->bnan(@r) if (($x->{sign} eq $nan) ||
($y->{sign} eq $nan) ||
($z->{sign} eq $nan));
# At least one of x, y, and z is an Inf
if ($x->{sign} eq "-inf") {
if ($y -> is_neg()) { # x = -inf, y < 0
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# Set up parameters.
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: 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(@r), $class -> bnan(@r))
: $x -> bnan(@r);
}
# 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
# -5 / 0 = -inf -5 % 0 = -5
# -inf / 0 = -inf -inf % 0 = -inf
if ($y -> is_zero()) {
my $rem;
if ($wantarray) {
$rem = $x -> copy() -> round(@r);
}
if ($x -> is_zero()) {
$x = $x -> bnan(@r);
} else {
$x = $x -> binf($x -> {sign}, @r);
}
return $wantarray ? ($x, $rem) : $x;
}
# 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
# inf / 5 = inf inf % 5 = NaN
# inf / inf = NaN inf % inf = NaN
#
# -inf / -inf = NaN -inf % -inf = NaN
# -inf / -5 = inf -inf % -5 = NaN
# -inf / 5 = -inf -inf % 5 = NaN
# -inf / inf = NaN -inf % inf = NaN
if ($x -> is_inf()) {
my $rem;
$rem = $class -> bnan(@r) if $wantarray;
if ($y -> is_inf()) {
$x = $x -> bnan(@r);
} else {
my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
$x = $x -> binf($sign, @r);
}
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# Set up parameters
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
return $x if $x -> modify('btdiv');
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(@r), $class -> bnan(@r))
: $x -> bnan(@r);
}
# 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
# -5 / 0 = -inf -5 % 0 = -5
# -inf / 0 = -inf -inf % 0 = -inf
if ($y -> is_zero()) {
my $rem;
if ($wantarray) {
$rem = $x -> copy(@r);
}
if ($x -> is_zero()) {
$x = $x -> bnan(@r);
} else {
$x = $x -> binf($x -> {sign}, @r);
}
return $wantarray ? ($x, $rem) : $x;
}
# 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
# inf / 5 = inf inf % 5 = NaN
# inf / inf = NaN inf % inf = NaN
#
# -inf / -inf = NaN -inf % -inf = NaN
# -inf / -5 = inf -inf % -5 = NaN
# -inf / 5 = -inf -inf % 5 = NaN
# -inf / inf = NaN -inf % inf = NaN
if ($x -> is_inf()) {
my $rem;
$rem = $class -> bnan(@r) if $wantarray;
if ($y -> is_inf()) {
$x = $x -> bnan(@r);
} else {
my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
$x = $x -> binf($sign,@r );
}
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# Set up parameters.
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: 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(@r);
}
# Modulo zero. See documentation for bdiv().
if ($y -> is_zero()) {
return $x -> round(@r);
}
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
sub btmod {
# Remainder after truncated division.
# set up parameters
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
return $x if $x -> modify('btmod');
# At least one argument is NaN.
if ($x -> is_nan() || $y -> is_nan()) {
return $x -> bnan(@r);
}
# Modulo zero. See documentation for btdiv().
if ($y -> is_zero()) {
return $x -> round(@r);
}
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
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 ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
return $x if $x->modify('bmodinv');
# Return NaN if one or both arguments is +inf, -inf, or nan.
return $x->bnan(@r) if ($y->{sign} !~ /^[+-]$/ ||
$x->{sign} !~ /^[+-]$/);
# Return NaN if $y is zero; 1 % 0 makes no sense.
return $x->bnan(@r) 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(@r) if ($y->is_one('+') ||
$y->is_one('-'));
return $upgrade -> bmodinv($x, $y, @r)
if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
!$y -> isa(__PACKAGE__));
# 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
# $LIB->_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 = $x->bmod($y);
return $x->bnan(@r) 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}) = $LIB->_modinv($x->{value}, $y->{value});
return $x->bnan(@r) 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
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# 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 = $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(@r) 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 $class -> bnan(@r);
} else {
return $num -> copy(@r);
}
}
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# compute power of two numbers -- stolen from Knuth Vol 2 pg 233
# modifies first argument
# set up parameters
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
return $x if $x -> modify('bpow');
# $x and/or $y is a NaN
return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
# $x and/or $y is a +/-Inf
if ($x -> is_inf("-")) {
return $x -> bzero(@r) if $y -> is_negative();
return $x -> bnan(@r) if $y -> is_zero();
return $x -> round(@r) if $y -> is_odd();
return $x -> bneg(@r);
} elsif ($x -> is_inf("+")) {
return $x -> bzero(@r) if $y -> is_negative();
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
}
if ($x -> is_one("-")) {
return $x -> round(@r) if $y -> is_odd();
return $x -> bneg(@r);
}
return $upgrade -> bpow($x, $y, @r) if defined $upgrade;
# We don't support finite non-integers, so return zero. The reason for
# returning zero, not NaN, is that all output is in the open interval (0,1),
# and truncating that to integer gives zero.
if ($y->{sign} eq '-' || !$y -> isa(__PACKAGE__)) {
return $x -> bzero(@r);
}
$r[3] = $y; # no push!
$x->{value} = $LIB -> _pow($x->{value}, $y->{value});
$x->{sign} = $x -> is_negative() && $y -> is_odd() ? '-' : '+';
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
$x = $x -> round(@r);
}
sub bexp {
# Calculate e ** $x (Euler's number to the power of X), truncated to
# an integer value.
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
return $x if $x->modify('bexp');
# inf, -inf, NaN, <0 => NaN
return $x -> bnan(@r) if $x->{sign} eq 'NaN';
return $x -> bone(@r) if $x->is_zero();
return $x -> round(@r) if $x->{sign} eq '+inf';
return $x -> bzero(@r) if $x->{sign} eq '-inf';
return $upgrade -> bexp($x, @r) if defined $upgrade;
require Math::BigFloat;
my $tmp = Math::BigFloat -> bexp($x, @r) -> as_int();
$x->{value} = $tmp->{value};
return $x -> round(@r);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# Set up parameters.
my ($class, $n, $k, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_)
: objectify(2, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
return $n if $n->modify('bnok');
# All cases where at least one argument is NaN.
return $n->bnan(@r) if $n->{sign} eq 'NaN' || $k->{sign} eq 'NaN';
# All cases where at least one argument is +/-inf.
if ($n -> is_inf()) {
if ($k -> is_inf()) { # bnok(+/-inf,+/-inf)
return $n -> bnan(@r);
} elsif ($k -> is_neg()) { # bnok(+/-inf,k), k < 0
return $n -> bzero(@r);
} elsif ($k -> is_zero()) { # bnok(+/-inf,k), k = 0
return $n -> bone(@r);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# set up parameters
my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
? (ref($_[0]), @_) : objectify(2, @_);
$y = $class->new(2) unless defined $y;
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(@r) 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->broot($x, $y, @r) if defined $upgrade;
$x->{value} = $LIB->_root($x->{value}, $y->{value});
$x->round(@r);
}
sub bfac {
# (BINT or num_str, BINT or num_str) return BINT
# compute factorial number from $x, modify $x in place
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
return $x if $x->modify('bfac') || $x->{sign} eq '+inf'; # inf => inf
return $x->bnan(@r) if $x->{sign} ne '+'; # NaN, <0 => NaN
return $upgrade -> bfac($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
$x->{value} = $LIB->_fac($x->{value});
$x->round(@r);
}
sub bdfac {
# compute double factorial, modify $x in place
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
$b = 2 unless defined $b;
$b = $class -> new($b) unless defined(blessed($b));
# Handle "foreign" objects.
return $upgrade -> blsft($x, $y, $b, @r)
if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
!$y -> isa(__PACKAGE__) ||
!$b -> isa(__PACKAGE__));
# Handle NaN cases.
return $x -> bnan(@r)
if $x -> is_nan() || $y -> is_nan() || $b -> is_nan();
# blsft($x, -$y, $b) = brsft($x, $y, $b)
return $x -> brsft($y -> copy() -> bneg(), $b, @r) if $y -> is_neg();
# Now handle all cases where at least one operand is ±Inf or the result
# will be ±Inf or NaN.
if ($y -> is_inf("+")) {
if ($b -> is_one("-")) {
return $x -> bnan(@r);
} elsif ($b -> is_one("+")) {
return $x -> round(@r);
} elsif ($b -> is_zero()) {
return $x -> bnan(@r) if $x -> is_inf();
return $x -> bzero(@r);
} else {
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
$b = 2 unless defined $b;
$b = $class -> new($b) unless defined(blessed($b));
# Handle "foreign" objects.
return $upgrade -> brsft($x, $y, $b, @r)
if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
!$y -> isa(__PACKAGE__) ||
!$b -> isa(__PACKAGE__));
# Handle NaN cases.
return $x -> bnan(@r)
if $x -> is_nan() || $y -> is_nan() || $b -> is_nan();
# brsft($x, -$y, $b) = blsft($x, $y, $b)
return $x -> blsft($y -> copy() -> bneg(), $b, @r) if $y -> is_neg();
# Now handle all cases where at least one operand is ±Inf or the result
# will be ±Inf or NaN.
if ($b -> is_inf()) {
return $x -> bnan(@r) if $x -> is_inf() || $y -> is_zero();
if ($b -> is_inf("+")) {
if ($x -> is_negative()) {
return $x -> bone("-", @r);
} else {
return $x -> bzero(@r);
}
} else {
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
my ($class, $x, @a) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
my ($scale, $mode) = $x->_scale_a(@a);
return $x if !defined $scale || $x->modify('bround'); # no-op
if ($x->is_zero() || $scale == 0) {
$x->{accuracy} = $scale if !defined $x->{accuracy} || $x->{accuracy} > $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)) {
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
last;
}
}
return $upgrade -> bgcd(@args) if $do_upgrade;
}
my $x = shift @args;
$x = defined(blessed($x)) && $x -> isa(__PACKAGE__) ? $x -> copy()
: $class -> new($x);
return $class->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN?
while (@args) {
my $y = shift @args;
$y = $class->new($y)
unless defined(blessed($y)) && $y -> isa(__PACKAGE__);
return $class->bnan() if $y->{sign} !~ /^[+-]$/; # y NaN?
$x->{value} = $LIB->_gcd($x->{value}, $y->{value});
last if $LIB->_is_one($x->{value});
}
return $x -> babs();
}
sub blcm {
# (BINT or num_str, BINT or num_str) return BINT
# does not modify arguments, but returns new object
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
$do_upgrade = 1;
last;
}
}
return $upgrade -> blcm(@args) if $do_upgrade;
}
my $x = shift @args;
$x = defined(blessed($x)) && $x -> isa(__PACKAGE__) ? $x -> copy()
: $class -> new($x);
return $class->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN?
while (@args) {
my $y = shift @args;
$y = $class -> new($y)
unless defined(blessed($y)) && $y -> isa(__PACKAGE__);
return $x->bnan() if $y->{sign} !~ /^[+-]$/; # y not integer
$x -> {value} = $LIB->_lcm($x -> {value}, $y -> {value});
}
return $x -> babs();
}
###############################################################################
# Object property methods
###############################################################################
sub sign {
# return the sign of the number: +/-/-inf/+inf/NaN
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
$x->{sign};
}
sub digit {
# return the nth decimal digit, negative values count backward, 0 is right
my (undef, $x, $n, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
sub length {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
my $e = $LIB->_len($x->{value});
wantarray ? ($e, 0) : $e;
}
sub exponent {
# return a copy of the exponent (here always 0, NaN or 1 for $m == 0)
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Upgrade?
return $upgrade -> exponent($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
if ($x->{sign} !~ /^[+-]$/) {
my $s = $x->{sign};
$s =~ s/^[+-]//; # NaN, -inf, +inf => NaN or inf
return $class->new($s, @r);
}
return $class->bzero(@r) if $x->is_zero();
# 12300 => 2 trailing zeros => exponent is 2
$class->new($LIB->_zeros($x->{value}), @r);
}
sub mantissa {
# return the mantissa (compatible to Math::BigFloat, e.g. reduced)
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Upgrade?
return $upgrade -> mantissa($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
if ($x->{sign} !~ /^[+-]$/) {
# for NaN, +inf, -inf: keep the sign
return $class->new($x->{sign}, @r);
}
my $m = $x->copy();
$m -> precision(undef);
$m -> accuracy(undef);
# that's a bit inefficient:
my $zeros = $LIB->_zeros($m->{value});
$m = $m->brsft($zeros, 10) if $zeros != 0;
$m -> round(@r);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
sub dparts {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Not-a-number.
if ($x -> is_nan()) {
my $int = $class -> bnan(@r);
return $int unless wantarray;
my $frc = $class -> bzero(@r); # or NaN?
return ($int, $frc);
}
# Infinity.
if ($x -> is_inf()) {
my $int = $class -> binf($x->{sign}, @r);
return $int unless wantarray;
my $frc = $class -> bzero(@r);
return ($int, $frc);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
}
# Fractional parts with the numerator and denominator as integers. E.g.,
# "123.4375" is returned as "1975" and "16".
sub fparts {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# NaN => NaN/NaN
if ($x -> is_nan()) {
return $class -> bnan(@r) unless wantarray;
return $class -> bnan(@r), $class -> bnan(@r);
}
# ±Inf => ±Inf/1
if ($x -> is_inf()) {
my $numer = $class -> binf($x->{sign}, @r);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
###############################################################################
# String conversion methods
###############################################################################
sub bstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
}
# Scientific notation with significand/mantissa as an integer, e.g., "12345" is
# written as "1.2345e+4".
sub bsstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bsstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
}
# Normalized notation, e.g., "12345" is written as "1.2345e+4".
sub bnstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bnstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
}
# Engineering notation, e.g., "12345" is written as "12.345e+3".
sub bestr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bestr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
}
# Decimal notation, e.g., "12345" (no exponent).
sub bdstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bdstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
}
# Fraction notation, e.g., "123.4375" is written as "1975/16", but "123" is
# written as "123", not "123/1".
sub bfstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bfstr($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
($x->{sign} eq '-' ? '-' : '') . $LIB->_str($x->{value});
}
sub to_hex {
# return as hex string with no prefix
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> to_hex($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
return $x->{sign} eq '-' ? "-$hex" : $hex;
}
sub to_oct {
# return as octal string with no prefix
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> to_oct($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
return $x->{sign} eq '-' ? "-$oct" : $oct;
}
sub to_bin {
# return as binary string with no prefix
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> to_bin($x, @r)
if defined($upgrade) && !$x -> isa(__PACKAGE__);
# Finite number
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
}
# import done
}
sub _trailing_zeros {
# return the amount of trailing zeros in $x (as scalar)
my $x = shift;
$x = __PACKAGE__->new($x) unless ref $x;
return 0 if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf etc
$LIB->_zeros($x->{value}); # must handle odd values, 0 etc
}
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;
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
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 bdiv().
# 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 $class = ref($self); # find out class of argument(s)
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
shift; # class name
my $value = shift;
no warnings 'numeric';
# detect numbers
# string & "" -> ""
# number & "" -> 0 (with warning)
# nan and inf can detect as numbers, so check with * 0
return unless CORE::length((my $dummy = "") & $value);
return unless 0 + $value eq $value;
return 1 if $value * 0 == 0;
return -1; # Inf/NaN
}
# Trims the sign of the significand, the (absolute value of the) significand,
# the sign of the exponent, and the (absolute value of the) exponent. The
# returned values have no underscores ("_") or unnecessary leading or trailing
# zeros.
sub _trim_split_parts {
shift; # class name
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
Math::BigInt->accuracy(); # get class accuracy
Math::BigInt->accuracy($n); # set class accuracy
Math::BigInt->precision(); # get class precision
Math::BigInt->precision($n); # set class precision
Math::BigInt->round_mode(); # get class rounding mode
Math::BigInt->round_mode($m); # set global round mode, must be one of
# 'even', 'odd', '+inf', '-inf', 'zero',
# 'trunc', or 'common'
Math::BigInt->div_scale($n); # set fallback accuracy
Math::BigInt->trap_inf($b); # trap infinities or not
Math::BigInt->trap_nan($b); # trap NaNs or not
Math::BigInt->config(); # return hash with configuration
# Constructor methods (when the class methods below are used as instance
# methods, the value is assigned the invocand)
$x = Math::BigInt->new($str); # defaults to 0
$x = Math::BigInt->new('0x123'); # from hexadecimal
$x = Math::BigInt->new('0b101'); # from binary
$x = Math::BigInt->from_hex('cafe'); # from hexadecimal
$x = Math::BigInt->from_oct('377'); # from octal
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
# Boolean methods (these don't modify the invocand)
$x->is_zero(); # if $x is 0
$x->is_one(); # if $x is +1
$x->is_one("+"); # ditto
$x->is_one("-"); # if $x is -1
$x->is_inf(); # if $x is +inf or -inf
$x->is_inf("+"); # if $x is +inf
$x->is_inf("-"); # if $x is -inf
$x->is_nan(); # if $x is NaN
$x->is_positive(); # if $x > 0
$x->is_pos(); # ditto
$x->is_negative(); # if $x < 0
$x->is_neg(); # ditto
$x->is_odd(); # if $x is odd
$x->is_even(); # if $x is even
$x->is_int(); # if $x is an integer
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
$x->bne($y); # true if and only if $x != $y
$x->blt($y); # true if and only if $x < $y
$x->ble($y); # true if and only if $x <= $y
$x->bgt($y); # true if and only if $x > $y
$x->bge($y); # true if and only if $x >= $y
# Arithmetic methods
$x->bneg(); # negation
$x->babs(); # absolute value
$x->bsgn(); # sign function (-1, 0, 1, or NaN)
$x->bnorm(); # normalize (no-op)
$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->bmuladd($y,$z); # $x = $x * $y + $z
$x->bdiv($y); # division (floored), set $x to quotient
# return (quo,rem) or quo if scalar
$x->btdiv($y); # division (truncated), set $x to quotient
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
$x->bceil(); # round towards plus infinity
$x->bint(); # round towards zero
# Other mathematical methods
$x->bgcd($y); # greatest common divisor
$x->blcm($y); # least common multiple
# Object property methods (do not modify the invocand)
$x->sign(); # the sign, either +, - or NaN
$x->digit($n); # the nth digit, counting from the right
$x->digit(-$n); # the nth digit, counting from the left
$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 Math::BigInt objects
$x->mantissa(); # return (signed) mantissa as a Math::BigInt
$x->exponent(); # return exponent as a Math::BigInt
$x->parts(); # return (mantissa,exponent) as a Math::BigInt
$x->sparts(); # mantissa and exponent (as integers)
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
=head1 DESCRIPTION
Math::BigInt provides support for arbitrary precision integers. Overloading is
also provided for Perl operators.
=head2 Input
Input values to these routines may be any scalar number or string that looks
like a number and represents an integer. Anything that is accepted by Perl as a
literal numeric constant should be accepted by this module, except that finite
non-integers return NaN.
=over
=item *
Leading and trailing whitespace is ignored.
=item *
Leading zeros are ignored, except for floating point numbers with a binary
exponent, in which case the number is interpreted as an octal floating point
number. For example, "01.4p+0" gives 1.5, "00.4p+0" gives 0.5, but "0.4p+0"
gives a NaN. And while "0377" gives 255, "0377p0" gives 255.
=item *
If the string has a "0x" or "0X" prefix, it is interpreted as a hexadecimal
number.
=item *
If the string has a "0o" or "0O" prefix, it is interpreted as an octal number. A
floating point literal with a "0" prefix is also interpreted as an octal number.
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
If the string has a "0b" or "0B" prefix, it is interpreted as a binary number.
=item *
Underline characters are allowed in the same way as they are allowed in literal
numerical constants.
=item *
If the string can not be interpreted, or does not represent a finite integer,
NaN is returned.
=item *
For hexadecimal, octal, and binary floating point numbers, the exponent must be
separated from the significand (mantissa) by the letter "p" or "P", not "e" or
"E" as with decimal numbers.
=back
Some examples of valid string input
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
Set/get the rounding mode.
=item trap_inf()
Set/get the value determining whether infinities should cause a fatal error or
not.
=item trap_nan()
Set/get the value determining whether NaNs should cause a fatal error or not.
=item upgrade()
Set/get the class for upgrading. When a computation might result in a
non-integer, the operands are upgraded to this class. This is used for instance
by L<bignum>. The default is C<undef>, i.e., no upgrading.
# with no upgrading
$x = Math::BigInt->new(12);
$y = Math::BigInt->new(5);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
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 division etc.
40
trap_nan RW Trap NaNs
undef
trap_inf RW Trap +inf/-inf
undef
=back
=head2 Constructor methods
=over
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
Interpret input as a decimal. It is equivalent to new(), but does not accept
anything but strings representing finite, decimal numbers.
=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_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_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 from_bytes()
$x = Math::BigInt->from_bytes("\xf3\x6b"); # $x = 62315
Interpret the input as a byte string, assuming big endian byte order. The
output is always a non-negative, finite integer.
In some special cases, from_bytes() matches the conversion done by unpack():
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
is either '-' or '+', indicating whether you want infinity or minus infinity.
If used as an instance method, assigns the value to the invocand.
$x->binf();
$x->binf('-');
=item bnan()
$x = Math::BigInt->bnan();
Creates a new Math::BigInt object representing NaN (Not A Number). If used as
an instance method, assigns the value to the invocand.
$x->bnan();
=item bpi()
$x = Math::BigInt->bpi(100); # 3
$x->bpi(100); # 3
Creates a new Math::BigInt object representing PI. If used as an instance
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
=item is_one( [ SIGN ])
$x->is_one(); # true if $x is +1
$x->is_one("+"); # ditto
$x->is_one("-"); # true if $x is -1
Returns true if the invocand is one and false otherwise.
=item is_finite()
$x->is_finite(); # true if $x is not +inf, -inf or NaN
Returns true if the invocand is a finite number, i.e., it is neither +inf,
-inf, nor NaN.
=item is_inf( [ SIGN ] )
$x->is_inf(); # true if $x is +inf
$x->is_inf("+"); # ditto
$x->is_inf("-"); # true if $x is -inf
Returns true if the invocand is infinite and false otherwise.
=item is_nan()
$x->is_nan(); # true if $x is NaN
=item is_positive()
=item is_pos()
$x->is_positive(); # true if > 0
$x->is_pos(); # ditto
Returns true if the invocand is positive and false otherwise. A C<NaN> is
neither positive nor negative.
=item is_negative()
=item is_neg()
$x->is_negative(); # true if < 0
$x->is_neg(); # ditto
Returns true if the invocand is negative and false otherwise. A C<NaN> is
neither positive nor negative.
=item is_non_positive()
$x->is_non_positive(); # true if <= 0
Returns true if the invocand is negative or zero.
=item is_non_negative()
$x->is_non_negative(); # true if >= 0
Returns true if the invocand is positive or zero.
=item is_odd()
$x->is_odd(); # true if odd, false for even
Returns true if the invocand is odd and false otherwise. C<NaN>, C<+inf>, and
C<-inf> are neither odd nor even.
=item is_even()
$x->is_even(); # true if $x is even
Returns true if the invocand is even and false otherwise. C<NaN>, C<+inf>,
C<-inf> are not integers and are neither odd nor even.
=item is_int()
$x->is_int(); # true if $x is an integer
Returns true if the invocand is an integer and false otherwise. C<NaN>,
C<+inf>, C<-inf> are not integers.
=back
=head2 Comparison methods
None of these methods modify the invocand object. Note that a C<NaN> is neither
less than, greater than, or equal to anything else, even a C<NaN>.
=over
=item bcmp()
$x->bcmp($y);
Returns -1, 0, 1 depending on whether $x is less than, equal to, or grater than
$y. Returns undef if any operand is a NaN.
=item bacmp()
$x->bacmp($y);
Returns -1, 0, 1 depending on whether the absolute value of $x is less than,
equal to, or grater than the absolute value of $y. Returns undef if any operand
is a NaN.
=item beq()
$x -> beq($y);
Returns true if and only if $x is equal to $y, and false otherwise.
=item bne()
$x -> bne($y);
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
These methods modify the invocand object and returns it.
=over
=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)
Normalize the number. This is a no-op and is provided only for backwards
compatibility.
=item binc()
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
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<bgcd($y, $mod)==1>. 'C<NaN>' 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<bmodpow> is far superior to
writing
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
=back
=head2 Object property methods
=over
=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 digitsum()
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
In Math::BigInt, this method is identical to C<parts()>.
=item nparts()
Returns the significand (mantissa) and exponent corresponding to normalized
notation. In scalar context, only the significand is returned. For finite
non-zero numbers, the significand's absolute value is greater than or equal to
1 and less than 10. The output of C<nparts()> corresponds to the output from
C<bnstr()>. In Math::BigInt, if the significand can not be represented as an
integer, upgrading is performed or NaN is returned.
=item eparts()
Returns the significand (mantissa) and exponent corresponding to engineering
notation. In scalar context, only the significand is returned. For finite
non-zero numbers, the significand's absolute value is greater than or equal to
1 and less than 1000, and the exponent is a multiple of 3. The output of
C<eparts()> corresponds to the output from C<bestr()>. In Math::BigInt, if the
significand can not be represented as an integer, upgrading is performed or NaN
is returned.
=item dparts()
Returns the integer part and the fraction part. If the fraction part can not be
represented as an integer, upgrading is performed or NaN is returned. The
output of C<dparts()> corresponds to the output from C<bdstr()>.
=item fparts()
Returns the smallest possible numerator and denominator so that the numerator
divided by the denominator gives back the original value. For finite numbers,
both values are integers. Mnemonic: fraction.
=item numerator()
Together with L</denominator()>, returns the smallest integers so that the
numerator divided by the denominator reproduces the original value. With
Math::BigInt, numerator() simply returns a copy of the invocand.
=item denominator()
Together with L</numerator()>, returns the smallest integers so that the
numerator divided by the denominator reproduces the original value. With
Math::BigInt, denominator() always returns either a 1 or a NaN.
=back
=head2 String conversion methods
=over
=item bstr()
Returns a string representing the number using decimal notation. In
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
* 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):
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
* Math::BigFloat uses Math::BigInt internally, but setting A or P inside
Math::BigInt as globals does not tamper with the parts of a Math::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 bdiv/bsqrt) and will not
be rounded.
* There is another setting for bdiv() (and thus for bsqrt()). If neither of
A or P is defined, bdiv() 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 :-)
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
* The defaults are set up so that the new code gives the same results as
the old code (except in a few cases on bdiv):
+ 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 Math::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
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
The first library that is successfully loaded is the one that will be used. Any
further attempts at loading a different module will be ignored. This is to avoid
the situation where module A requires math library X, and module B requires math
library Y, causing modules A and B to be incompatible. For example,
use Math::BigInt; # loads default "Calc"
use Math::BigFloat only => "GMP"; # ignores "GMP"
=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 get '+inf' when dividing a positive number by 0, and '-inf'
when dividing any negative number by 0.
=head1 EXAMPLES
use Math::BigInt;
sub bigint { Math::BigInt->new(shift); }
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
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 NUMERIC LITERALS
After C<use Math::BigInt ':constant'> all numeric literals in the given scope
are converted to C<Math::BigInt> objects. This conversion happens at compile
time. Every non-integer is convert to a NaN.
For example,
perl -MMath::BigInt=:constant -le 'print 2**150'
prints the exact value of C<2**150>. Note that without conversion of constants
to objects the expression C<2**150> is calculated using Perl scalars, which
leads to an inaccurate result.
Please note that strings are not affected, so that
cpan/Math-BigInt/lib/Math/BigInt.pm view on Meta::CPAN
is($x,$y); # fails
is($x->bsstr(), $y); # okay
$y = Math::BigInt->new($y);
is($x, $y); # okay
Alternatively, simply use C<< <=> >> for comparisons, this always gets it
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<Infinity and Not a Number> for problems in
comparing NaNs.
=item int()
C<int()> returns (at least for Perl v5.7.1 and up) another Math::BigInt, not a
Perl scalar:
$x = Math::BigInt->new(123);
$y = int($x); # 123 as a Math::BigInt
$x = Math::BigFloat->new(123.45);
$y = int($x); # 123 as a Math::BigFloat
cpan/Math-BigInt/lib/Math/BigInt/Calc.pm view on Meta::CPAN
# 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 @$x == 1 && $x->[0] == 0;
# BASE 0 or 1 => NaN
return if @$base == 1 && $base->[0] < 2;
# X == 1 => 0 (is exact)
if (@$x == 1 && $x->[0] == 1) {
@$x = 0;
return $x, 1;
}
my $cmp = $c->_acmp($x, $base);
cpan/Math-BigInt/lib/Math/BigInt/Calc.pm view on Meta::CPAN
my ($c, $x) = @_;
($x, my $is_exact) = $c -> _log_int($x, $c -> _two());
return wantarray ? ($x, $is_exact) : $x;
}
sub _ilog10 {
# calculate int(log10($x))
my ($c, $x) = @_;
# X == 0 => NaN
return if @$x == 1 && $x->[0] == 0;
# X == 1 => 0 (is exact)
if (@$x == 1 && $x->[0] == 1) {
@$x = 0;
return wantarray ? ($x, 1) : $x;
}
my $x_orig = $c -> _copy($x);
my $nm1 = $c -> _len($x) - 1;
cpan/Math-BigInt/lib/Math/BigInt/Calc.pm view on Meta::CPAN
return wantarray ? ($x, 1) : $x if $is_pow10;
return wantarray ? ($x, 0) : $x;
}
sub _clog2 {
# calculate ceil(log2($x))
my ($c, $x) = @_;
# X == 0 => NaN
return if @$x == 1 && $x->[0] == 0;
# X == 1 => 0 (is exact)
if (@$x == 1 && $x->[0] == 1) {
@$x = 0;
return wantarray ? ($x, 1) : $x;
}
cpan/Math-BigInt/lib/Math/BigInt/Calc.pm view on Meta::CPAN
}
return wantarray ? ($x, 1) : $x if $acmp == 0; # result is exact
return wantarray ? ($x, 0) : $x; # result is too small
}
sub _clog10 {
# calculate ceil(log2($x))
my ($c, $x) = @_;
# X == 0 => NaN
return if @$x == 1 && $x->[0] == 0;
# X == 1 => 0 (is exact)
if (@$x == 1 && $x->[0] == 1) {
@$x = 0;
return wantarray ? ($x, 1) : $x;
}
# Get the number of base 10 digits. $n is the desired output, except when
# $x is an exact power of 10, in which case $n is 1 too big.
cpan/Math-BigInt/lib/Math/BigInt/Calc.pm view on Meta::CPAN
my $t = $c->_add( # step 2:
$c->_mul($c->_copy($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 unless $c->_is_one($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
cpan/Math-BigInt/lib/Math/BigInt/Lib.pm view on Meta::CPAN
return $x;
}
sub _log_int {
# calculate integer log of $x to base $base
# calculate integer log of $x to base $base
# ref to array, ref to array - return ref to array
my ($class, $x, $base) = @_;
# X == 0 => NaN
return if $class -> _is_zero($x);
$base = $class -> _new(2) unless defined($base);
$base = $class -> _new($base) unless ref($base);
# BASE 0 or 1 => NaN
return if $class -> _is_zero($base) || $class -> _is_one($base);
# X == 1 => 0 (is exact)
if ($class -> _is_one($x)) {
return $class -> _zero(), 1 if wantarray;
return $class -> _zero();
}
my $cmp = $class -> _acmp($x, $base);
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
#
# "Tax the rat farms." - Lord Vetinari
#
# The following hash values are used:
# sign : "+", "-", "+inf", "-inf", or "NaN"
# _d : denominator
# _n : numerator (value = _n/_d)
# accuracy : accuracy
# precision : precision
# You should not look at the innards of a BigRat - use the methods for this.
package Math::BigRat;
use 5.006;
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
# 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 math backend library
my $LIB = 'Math::BigInt::Calc';
my $nan = 'NaN';
# Has import() been called yet? This variable is needed to make "require" work.
my $IMPORT = 0;
# Compare the following function with @ISA above. This inheritance mess needs a
# clean up. When doing so, also consider the BEGIN block and the AUTOLOAD code.
# Fixme!
sub isa {
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
unless ref($d) && ($d -> isa('Math::BigRat') ||
$d -> isa('Math::BigInt') ||
$d -> isa('Math::BigFloat'));
# At this point both $n and $d are objects.
if ($n -> is_nan() || $d -> is_nan()) {
return $class -> bnan();
}
# At this point neither $n nor $d is a NaN.
if ($n -> is_zero()) {
if ($d -> is_zero()) { # 0/0 = NaN
return $class -> bnan();
}
return $class -> bzero();
}
if ($d -> is_zero()) {
return $class -> binf($d -> sign());
}
# At this point, neither $n nor $d is a NaN or a zero.
# Copy them now before manipulating them.
$n = $n -> copy();
$d = $d -> copy();
if ($d < 0) { # make sure denominator is positive
$n -> bneg();
$d -> bneg();
}
if ($n -> is_inf()) {
return $class -> bnan() if $d -> is_inf(); # Inf/Inf = NaN
return $class -> binf($n -> sign());
}
# At this point $n is finite.
return $class -> bzero() if $d -> is_inf();
return $class -> binf($d -> sign()) if $d -> is_zero();
# At this point both $n and $d are finite and non-zero.
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
my $selfref = ref $self;
my $class = $selfref || $self;
# Make "require" work.
$class -> import() if $IMPORT == 0;
$self = bless {}, $class unless $selfref;
if ($_trap_nan) {
croak ("Tried to set a variable to NaN in $class->bnan()");
}
return $downgrade -> bnan() if defined $downgrade;
$self -> {sign} = $nan;
$self -> {_n} = $LIB -> _zero();
$self -> {_d} = $LIB -> _one();
($self, $self->{accuracy}, $self->{precision})
= $self->_find_round_parameters(@_);
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
###############################################################################
# String conversion methods
###############################################################################
sub bstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bstr($x, @r)
if defined($upgrade) && !$x -> isa($class);
# Finite number
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
$str .= $LIB->_str($x->{_n});
$str .= '/' . $LIB->_str($x->{_d}) unless $LIB -> _is_one($x->{_d});
return $str;
}
sub bsstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bsstr($x, @r)
if defined($upgrade) && !$x -> isa($class);
# Finite number
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
$str .= $LIB->_str($x->{_n});
$str .= '/' . $LIB->_str($x->{_d}) unless $LIB -> _is_one($x->{_d});
return $str;
}
sub bfstr {
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
# Upgrade?
return $upgrade -> bfstr($x, @r)
if defined($upgrade) && !$x -> isa($class);
# Finite number
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
# Both parts must be objects of whatever we are using today.
if (my $c = $LIB->_check($x->{_n})) {
croak("n did not pass the self-check ($c) in bnorm()");
}
if (my $c = $LIB->_check($x->{_d})) {
croak("d did not pass the self-check ($c) in bnorm()");
}
# no normalize for NaN, inf etc.
if ($x->{sign} !~ /^[+-]$/) {
return $downgrade -> new($x) if defined $downgrade;
return $x;
}
# normalize zeros to 0/1
if ($LIB->_is_zero($x->{_n})) {
return $downgrade -> bzero() if defined($downgrade);
$x->{sign} = '+'; # never leave a -0
$x->{_d} = $LIB->_one() unless $LIB->_is_one($x->{_d});
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
##############################################################################
# sign manipulation
sub bneg {
# (BRAT or num_str) return BRAT
# negate number or make a negated number from string
my ($class, $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 '+' && $LIB->_is_zero($x->{_n}));
return $downgrade -> new($x)
if defined($downgrade) && $LIB -> _is_one($x->{_d});
$x;
}
##############################################################################
# mul/add/div etc
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
sub bmul {
# multiply two rational numbers
# set up parameters
my ($class, $x, $y, @r) = (ref($_[0]), @_);
# objectify is costly, so avoid it
if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
($class, $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('-');
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
my ($class, $x, $y, @r) = (ref($_[0]), @_);
# objectify is costly, so avoid it
if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
($class, $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()) {
if ($wantarray) {
return $downgrade -> bnan(), $downgrade -> bnan()
if defined($downgrade);
return $x -> bnan(), $class -> bnan();
} else {
return $downgrade -> bnan()
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
# set up parameters
my ($class, $x, $y, @r) = (ref($_[0]), @_);
# objectify is costly, so avoid it
if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
($class, $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 $downgrade -> bzero() if defined $downgrade;
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
return $x -> round(@r);
}
##############################################################################
# bdec/binc
sub bdec {
# decrement value (subtract 1)
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
if ($x->{sign} !~ /^[+-]$/) { # NaN, inf, -inf
return $downgrade -> new($x) if defined $downgrade;
return $x;
}
if ($x->{sign} eq '-') {
$x->{_n} = $LIB->_add($x->{_n}, $x->{_d}); # -5/2 => -7/2
} else {
if ($LIB->_acmp($x->{_n}, $x->{_d}) < 0) # n < d?
{
# 1/3 -- => -2/3
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
$x->{_n} = $LIB->_sub($x->{_n}, $x->{_d}); # 5/2 => 3/2
}
}
$x->bnorm()->round(@r);
}
sub binc {
# increment value (add 1)
my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
if ($x->{sign} !~ /^[+-]$/) { # NaN, inf, -inf
return $downgrade -> new($x) if defined $downgrade;
return $x;
}
if ($x->{sign} eq '-') {
if ($LIB->_acmp($x->{_n}, $x->{_d}) < 0) {
# -1/3 ++ => 2/3 (overflow at 0)
$x->{_n} = $LIB->_sub($LIB->_copy($x->{_d}), $x->{_n});
$x->{sign} = '+';
} else {
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
$x -> round(@r);
}
##############################################################################
# is_foo methods (the rest is inherited)
sub is_int {
# return true if arg (BRAT or num_str) is an integer
my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't
$LIB->_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 ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
return 1 if $x->{sign} eq '+' && $LIB->_is_zero($x->{_n});
0;
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
$sign = '+' if $sign ne '-';
return 1 if ($x->{sign} eq $sign &&
$LIB->_is_one($x->{_n}) && $LIB->_is_one($x->{_d}));
0;
}
sub is_odd {
# return true if arg (BFLOAT or num_str) is odd or false if even
my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
($LIB->_is_one($x->{_d}) && $LIB->_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 ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't
return 1 if ($LIB->_is_one($x->{_d}) # x/3 is never
&& $LIB->_is_even($x->{_n})); # but 4/1 is
0;
}
##############################################################################
# parts() and friends
sub numerator {
my ($class, $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($LIB->_str($x->{_n}));
$n->{sign} = $x->{sign};
$n;
}
sub denominator {
my ($class, $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($LIB->_str($x->{_d}));
}
sub parts {
my ($class, $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($LIB->_str($x->{_n}));
$n->{sign} = $x->{sign};
my $d = $c->new($LIB->_str($x->{_d}));
($n, $d);
}
sub dparts {
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
return $nan unless $x->is_int();
$LIB->_digit($x->{_n}, $n || 0); # digit(-123/1, 2) => digit(123, 2)
}
##############################################################################
# special calc routines
sub bceil {
my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
if ($x->{sign} !~ /^[+-]$/ || # NaN or inf or
$LIB->_is_one($x->{_d})) # integer
{
return $downgrade -> new($x) if defined $downgrade;
return $x;
}
$x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
$x->{_d} = $LIB->_one(); # d => 1
$x->{_n} = $LIB->_inc($x->{_n}) if $x->{sign} eq '+'; # +22/7 => 4/1
$x->{sign} = '+' if $x->{sign} eq '-' && $LIB->_is_zero($x->{_n}); # -0 => 0
return $downgrade -> new($x) if defined $downgrade;
$x;
}
sub bfloor {
my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
if ($x->{sign} !~ /^[+-]$/ || # NaN or inf or
$LIB->_is_one($x->{_d})) # integer
{
return $downgrade -> new($x) if defined $downgrade;
return $x;
}
$x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
$x->{_d} = $LIB->_one(); # d => 1
$x->{_n} = $LIB->_inc($x->{_n}) if $x->{sign} eq '-'; # -22/7 => -4/1
return $downgrade -> new($x) if defined $downgrade;
$x;
}
sub bint {
my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
if ($x->{sign} !~ /^[+-]$/ || # NaN or inf or
$LIB->_is_one($x->{_d})) # integer
{
return $downgrade -> new($x) if defined $downgrade;
return $x;
}
$x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
$x->{_d} = $LIB->_one(); # d => 1
$x->{sign} = '+' if $x->{sign} eq '-' && $LIB -> _is_zero($x->{_n});
return $downgrade -> new($x) if defined $downgrade;
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
# set up parameters
my ($class, $x, $y, @r) = (ref($_[0]), @_);
# objectify is costly, so avoid it
if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
($class, $x, $y, @r) = objectify(2, @_);
}
return $x if $x->modify('bpow');
# $x and/or $y is a NaN
return $x->bnan() if $x->is_nan() || $y->is_nan();
# $x and/or $y is a +/-Inf
if ($x->is_inf("-")) {
return $x->bzero() if $y->is_negative();
return $x->bnan() if $y->is_zero();
return $x if $y->is_odd();
return $x->bneg();
} elsif ($x->is_inf("+")) {
return $x->bzero() if $y->is_negative();
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
return $x->bone() if $x -> is_one("+");
return $x->binf("+");
}
if ($x -> is_zero()) {
return $x -> bone() if $y -> is_zero();
return $x -> binf() if $y -> is_negative();
return $x;
}
# We don't support complex numbers, so upgrade or return NaN.
if ($x -> is_negative() && !$y -> is_int()) {
return $upgrade -> bpow($upgrade -> new($x), $y, @r)
if defined $upgrade;
return $x -> bnan();
}
if ($x -> is_one("+") || $y -> is_one()) {
return $x;
}
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
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] = $class->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 {
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
$x -> {sign} = $xtmp -> {sign};
$x -> {_n} = $xtmp -> {_n};
$x -> {_d} = $xtmp -> {_d};
return $x;
}
sub bsqrt {
my ($class, $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();
my $n = $x -> {_n};
my $d = $x -> {_d};
# Look for an exact solution. For the numerator and the denominator, take
# the square root and square it and see if we got the original value. If we
# did, for both the numerator and the denominator, we have an exact
# solution.
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
# set up parameters
my ($class, $x, $y) = (ref($_[0]), @_);
# objectify is costly, so avoid it
if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
($class, $x, $y) = objectify(2, @_);
}
if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
# $x is NaN and/or $y is NaN
return if $x->{sign} eq $nan || $y->{sign} eq $nan;
# $x and $y are both either +inf or -inf
return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/;
# $x = +inf and $y < +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
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
# compare two numbers (as unsigned)
# set up parameters
my ($class, $x, $y) = (ref($_[0]), @_);
# objectify is costly, so avoid it
if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
($class, $x, $y) = objectify(2, @_);
}
if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) {
# handle +-inf and NaN
return 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 = $LIB->_mul($LIB->_copy($x->{_n}), $y->{_d});
my $u = $LIB->_mul($LIB->_copy($y->{_n}), $x->{_d});
$LIB->_acmp($t, $u); # ignore signs
}
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
Math::BigFloat -> upgrade($upg);
Math::BigFloat -> downgrade($dng);
return $y;
}
sub to_hex {
my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
return $nan unless $x->is_int();
my $str = $LIB->_to_hex($x->{_n});
return $x->{sign} eq "-" ? "-$str" : $str;
}
sub to_oct {
my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
return $nan unless $x->is_int();
my $str = $LIB->_to_oct($x->{_n});
return $x->{sign} eq "-" ? "-$str" : $str;
}
sub to_bin {
my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
# Inf and NaN
if ($x->{sign} ne '+' && $x->{sign} ne '-') {
return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
return 'inf'; # +inf
}
return $nan unless $x->is_int();
my $str = $LIB->_to_bin($x->{_n});
return $x->{sign} eq "-" ? "-$str" : $str;
}
sub as_bin {
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
=item from_bin()
my $b = Math::BigRat->from_bin('0b10000000');
Create a BigRat from an binary number in string form.
=item bnan()
$x = Math::BigRat->bnan();
Creates a new BigRat object representing NaN (Not A Number).
If used on an object, it will set it to NaN:
$x->bnan();
=item bzero()
$x = Math::BigRat->bzero();
Creates a new BigRat object representing zero.
If used on an object, it will set it to zero:
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
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<bgcd($y, $mod)==1>. 'C<NaN>' 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<bmodpow> is far superior to
writing
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
print "$x is 0\n" if $x->is_zero();
Return true if $x is exactly zero, otherwise false.
=item 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_positive()> is an alias for C<is_pos()>.
=item 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_negative()> is an alias for C<is_neg()>.
=item 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.
=item is_odd()
print "$x is odd\n" if $x->is_odd();
Return true if $x is odd, otherwise false.
=item is_even()
print "$x is even\n" if $x->is_even();
cpan/Math-BigInt/lib/Math/BigRat.pm view on Meta::CPAN
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, sqrt etc.
40
trap_nan RW Trap NaNs
undef
trap_inf RW Trap +inf/-inf
undef
=back
=head1 NUMERIC LITERALS
After C<use Math::BigRat ':constant'> all numeric literals in the given scope
are converted to C<Math::BigRat> objects. This conversion happens at compile
time. Every non-integer is convert to a NaN.
For example,
perl -MMath::BigRat=:constant -le 'print 2**150'
prints the exact value of C<2**150>. Note that without conversion of constants
to objects the expression C<2**150> is calculated using Perl scalars, which
leads to an inaccurate result.
Please note that strings are not affected, so that
cpan/Math-BigInt/t/Math/BigInt/Scalar.pm view on Meta::CPAN
require Exporter;
our @ISA = qw(Exporter);
our $VERSION = '0.13';
##############################################################################
# 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;
}
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
is($x->{accuracy}, undef, qq|\$x = $CLASS->new(2); \$x->bnan(); \$x->{accuracy}|);
is($x->{precision}, undef, qq|\$x = $CLASS->new(2); \$x->bnan(); \$x->{precision}|);
###############################################################################
# bone/binf etc as plain calls (Lite failed them)
is($CLASS->bzero(), 0, qq|$CLASS->bzero()|);
is($CLASS->bone(), 1, qq|$CLASS->bone()|);
is($CLASS->bone("+"), 1, qq|$CLASS->bone("+")|);
is($CLASS->bone("-"), -1, qq|$CLASS->bone("-")|);
is($CLASS->bnan(), "NaN", qq|$CLASS->bnan()|);
is($CLASS->binf(), "inf", qq|$CLASS->binf()|);
is($CLASS->binf("+"), "inf", qq|$CLASS->binf("+")|);
is($CLASS->binf("-"), "-inf", qq|$CLASS->binf("-")|);
is($CLASS->binf("-inf"), "-inf", qq|$CLASS->binf("-inf")|);
$CLASS->accuracy(undef); # reset
$CLASS->precision(undef); # reset
###############################################################################
# bsqrt() with set global A/P or A/P enabled on $x, also a test whether bsqrt()
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
qq| $CLASS->precision(0); \$x->bsqrt();|);
$CLASS->precision(-3);
$x = $CLASS->new(12);
$x->bsqrt();
is($x, '3.464',
qq|$CLASS->precision(-3); \$x = $CLASS->new(12); \$x->bsqrt();|);
{
no strict 'refs';
# A and P set => NaN
${${CLASS}.'::accuracy'} = 4;
$x = $CLASS->new(12);
$x->bsqrt(3);
is($x, 'NaN', "A and P set => NaN");
# supplied arg overrides set global
$CLASS->precision(undef);
$x = $CLASS->new(12);
$x->bsqrt(3);
is($x, '3.46', "supplied arg overrides set global");
# reset for further tests
$CLASS->accuracy(undef);
$CLASS->precision(undef);
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
is($x, 0, qq|\$x = $CLASS->new(-3); \$x -= \$x;|);
$x = $CLASS->new(3);
$x += $x;
is($x, 6, qq|\$x = $CLASS->new(3); \$x += \$x;|);
$x = $CLASS->new(-3);
$x += $x;
is($x, -6, qq|\$x = $CLASS->new(-3); \$x += \$x;|);
$x = $CLASS->new("NaN");
$x -= $x;
is($x->is_nan(), 1, qq|\$x = $CLASS->new("NaN"); \$x -= \$x;|);
$x = $CLASS->new("inf");
$x -= $x;
is($x->is_nan(), 1, qq|\$x = $CLASS->new("inf"); \$x -= \$x;|);
$x = $CLASS->new("-inf");
$x -= $x;
is($x->is_nan(), 1, qq|\$x = $CLASS->new("-inf"); \$x -= \$x;|);
$x = $CLASS->new("NaN");
$x += $x;
is($x->is_nan(), 1, qq|\$x = $CLASS->new("NaN"); \$x += \$x;|);
$x = $CLASS->new("inf");
$x += $x;
is($x->is_inf(), 1, qq|\$x = $CLASS->new("inf"); \$x += \$x;|);
$x = $CLASS->new("-inf");
$x += $x;
is($x->is_inf("-"), 1, qq|\$x = $CLASS->new("-inf"); \$x += \$x;|);
$x = $CLASS->new("3.14");
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
is($x >>= $y, 2,
qq|\$x = $CLASS->new("2"); \$y = $CLASS->new("18.2");| .
q| $x <<= $y; $x >>= $y|);
is($x, 2,
qq|\$x = $CLASS->new("2"); \$y = $CLASS->new("18.2");| .
q| $x <<= $y; $x >>= $y; $x|);
__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
NaN:10:NaN
+inf:10:NaN
-inf:10:NaN
1.2:10:0.3623577545
2.4:12:-0.737393715541
0:10:1.000000000
0:20:1.0000000000000000000
1:10:0.5403023059
1:12:0.540302305868
&bsin
NaN:10:NaN
+inf:10:NaN
-inf:10:NaN
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:14:0
0:10:0
0.1:14:0.099668652491162
0.2:13:0.1973955598499
0.2:14:0.19739555984988
0.5:14:0.46364760900081
1:14:0.78539816339744
-1:14:-0.78539816339744
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
2.0:14:1.1071487177941
2.5:14:1.1902899496825
3.0:14:1.2490457723982
6.0:14:1.4056476493803
12:14:1.4876550949064
24:14:1.5291537476963
48:14:1.5499660067587
&batan2
NaN:1:10:NaN
NaN:NaN:10:NaN
1:NaN:10:NaN
-inf:-inf:14:-2.3561944901923
-inf:-1:14:-1.5707963267949
-inf:0:14:-1.5707963267949
-inf:+1:14:-1.5707963267949
-inf:+inf:14:-0.78539816339745
-1:-inf:14:-3.1415926535898
-1:-1:14:-2.3561944901923
-1:0:14:-1.5707963267949
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
-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
&blog
0::-inf
-1::NaN
-2::NaN
# base > 0, base != 1
2:-1:NaN
2:0:0
2:1:NaN
# log(1)
1::0
1:1:NaN
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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
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
&brsft
invalid:2:NaN
0:2:0
1:1:0.5
2:1:1
4:1:2
123:1:61.5
32:3:4
&blsft
invalid:0:NaN
2:1:4
4:3:32
5:3:40
1:2:4
0:5:0
&bnorm
1:1
-0:0
invalid:NaN
+inf:inf
-inf:-inf
123:123
-123.4567:-123.4567
# invalid inputs
.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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
&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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
&as_hex
128:0x80
-128:-0x80
0:0x0
-0:0x0
1:0x1
0x123456789123456789:0x123456789123456789
+inf:inf
-inf:-inf
invalid:NaN
&as_bin
128:0b10000000
-128:-0b10000000
0:0b0
-0:0b0
1:0b1
0b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101
0x123456789123456789:0b100100011010001010110011110001001000100100011010001010110011110001001
+inf:inf
-inf:-inf
invalid:NaN
&as_oct
128:0200
-128:-0200
0:00
-0:00
1:01
0b1010111101010101010110110110110110101:01275252666665
0x123456789123456789:044321263611044321263611
+inf:inf
-inf:-inf
invalid:NaN
&to_hex
128:80
-128:-80
0:0
-0:0
1:1
0x123456789123456789:123456789123456789
+inf:inf
-inf:-inf
invalid:NaN
&to_bin
128:10000000
-128:-10000000
0:0
-0:0
1:1
0b1010111101010101010110110110110110101:1010111101010101010110110110110110101
0x123456789123456789:100100011010001010110011110001001000100100011010001010110011110001001
+inf:inf
-inf:-inf
invalid:NaN
&to_oct
128:200
-128:-200
0:0
-0:0
1:1
0b1010111101010101010110110110110110101:1275252666665
0x123456789123456789:44321263611044321263611
+inf:inf
-inf:-inf
invalid:NaN
&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
&bnan
abc:NaN
2:NaN
-2:NaN
0:NaN
&bone
2:+:1
-2:-:-1
-2:+:1
2:-:-1
0::1
-2::1
abc::1
2::1
&bsstr
+inf:inf
-inf:-inf
abcfsstr:NaN
-abcfsstr:NaN
1234.567:1234567e-3
123:123e+0
-5:-5e+0
-100:-1e+2
&bstr
+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
&bnorm
inf:inf
+inf:inf
-inf:-inf
+infinity:inf
+-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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
+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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
-00001:-1
-1:-1
000.01:0.01
-000.0023:-0.0023
1.1e1:11
-3e111:-3000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-4e-1111:-0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
&bpow
#
abc:123.456:NaN
123.456:abc:NaN
#
-inf:-inf:0
-inf:-3:0
-inf:-2:0
-inf:-1.5:0
-inf:-1:0
-inf:-0.5:0
-inf:0:NaN
-inf:0.5:inf # directed infinity
-inf:1:-inf
-inf:1.5:inf # directed infinity
-inf:2:inf
-inf:3:-inf
-inf:inf:inf # complex infinity
-inf:NaN:NaN
#
-3:-inf:0
-3:-3:-0.03703703703703703703703703703703703703704
-3:-2:0.1111111111111111111111111111111111111111
-3:-1.5:NaN
-3:-1:-0.3333333333333333333333333333333333333333
-3:-0.5:NaN
-3:0:1
-3:0.5:NaN
-3:1:-3
-3:1.5:NaN
-3:2:9
-3:3:-27
-3:inf:inf # complex infinity
-3:NaN:NaN
#
-2:-inf:0
-2:-3:-0.125
-2:-2:0.25
-2:-1.5:NaN
-2:-1:-0.5
-2:-0.5:NaN
-2:0:1
-2:0.5:NaN
-2:1:-2
-2:1.5:NaN
-2:2:4
-2:3:-8
-2:inf:inf # complex infinity
-2:NaN:NaN
#
-1.5:-inf:0
-1.5:-3:-0.2962962962962962962962962962962962962963
-1.5:-2:0.4444444444444444444444444444444444444444
-1.5:-1.5:NaN
-1.5:-1:-0.6666666666666666666666666666666666666667
-1.5:-0.5:NaN
-1.5:0:1
-1.5:0.5:NaN
-1.5:1:-1.5
-1.5:1.5:NaN
-1.5:2:2.25
-1.5:3:-3.375
-1.5:inf:inf # complex infinity
-1.5:NaN:NaN
#
-1:-inf:NaN
-1:-3:-1
-1:-2:1
-1:-1.5:NaN
-1:-1:-1
-1:-0.5:NaN
-1:0:1
-1:0.5:NaN
-1:1:-1
-1:1.5:NaN
-1:2:1
-1:3:-1
-1:inf:NaN
-1:NaN:NaN
#
-0.5:-inf:inf # complex infinity
-0.5:-3:-8
-0.5:-2:4
-0.5:-1.5:NaN
-0.5:-1:-2
-0.5:-0.5:NaN
-0.5:0:1
-0.5:0.5:NaN
-0.5:1:-0.5
-0.5:1.5:NaN
-0.5:2:0.25
-0.5:3:-0.125
-0.5:inf:0
-0.5:NaN:NaN
#
0:-inf:inf # complex infinity
0:-3:inf # complex infinity
0:-2:inf # complex infinity
0:-1.5:inf # complex infinity
0:-1:inf # complex infinity
0:-0.5:inf # complex infinity
0:0:1
0:0.5:0
0:1:0
0:1.5:0
0:2:0
0:3:0
0:inf:0
0:NaN:NaN
#
0.5:-inf:inf
0.5:-3:8
0.5:-2:4
0.5:-1.5:2.828427124746190097603377448419396157139
0.5:-1:2
0.5:-0.5:1.41421356237309504880168872420969807857
0.5:0:1
0.5:0.5:0.7071067811865475244008443621048490392848
0.5:1:0.5
0.5:1.5:0.3535533905932737622004221810524245196424
0.5:2:0.25
0.5:3:0.125
0.5:inf:0
0.5:NaN:NaN
#
1:-inf:1
1:-3:1
1:-2:1
1:-1.5:1
1:-1:1
1:-0.5:1
1:0:1
1:0.5:1
1:1:1
1:1.5:1
1:2:1
1:3:1
1:inf:1
1:NaN:NaN
#
1.5:-inf:0
1.5:-3:0.2962962962962962962962962962962962962963
1.5:-2:0.4444444444444444444444444444444444444444
1.5:-1.5:0.5443310539518173551549520166013091982147
1.5:-1:0.6666666666666666666666666666666666666667
1.5:-0.5:0.816496580927726032732428024901963797322
1.5:0:1
1.5:0.5:1.224744871391589049098642037352945695983
1.5:1:1.5
1.5:1.5:1.837117307087383573647963056029418543974
1.5:2:2.25
1.5:3:3.375
1.5:inf:inf
1.5:NaN:NaN
#
2:-inf:0
2:-3:0.125
2:-2:0.25
2:-1.5:0.3535533905932737622004221810524245196424
2:-1:0.5
2:-0.5:0.7071067811865475244008443621048490392848
2:0:1
2:0.5:1.41421356237309504880168872420969807857
2:1:2
2:1.5:2.828427124746190097603377448419396157139
2:2:4
2:3:8
2:inf:inf
2:NaN:NaN
#
3:-inf:0
3:-3:0.03703703703703703703703703703703703703704
3:-2:0.1111111111111111111111111111111111111111
3:-1.5:0.1924500897298752548363829268339858185492
3:-1:0.3333333333333333333333333333333333333333
3:-0.5:0.5773502691896257645091487805019574556476
3:0:1
3:0.5:1.732050807568877293527446341505872366943
3:1:3
3:1.5:5.196152422706631880582339024517617100828
3:2:9
3:3:27
3:inf:inf
3:NaN:NaN
#
inf:-inf:0
inf:-3:0
inf:-2:0
inf:-1.5:0
inf:-1:0
inf:-0.5:0
inf:0:NaN
inf:0.5:inf
inf:1:inf
inf:1.5:inf
inf:2:inf
inf:3:inf
inf:inf:inf
inf:NaN:NaN
#
NaN:-inf:NaN
NaN:-3:NaN
NaN:-2:NaN
NaN:-1.5:NaN
NaN:-1:NaN
NaN:-0.5:NaN
NaN:0:NaN
NaN:0.5:NaN
NaN:1:NaN
NaN:1.5:NaN
NaN:2:NaN
NaN:3:NaN
NaN:inf:NaN
NaN:NaN:NaN
#
123.456:2:15241.383936
128:-2:0.00006103515625
#
+inf:123.45:inf
-inf:123.45:inf # directed infinity
+inf:-123.45:0
-inf:-123.45:0
#2:0.2:1.148698354997035006798626946777927589444
#6:1.5:14.6969384566990685891837044482353483518
$div_scale = 20
#62.5:12.5:26447206647554886213592.3959144
$div_scale = 40
&bneg
invalid:NaN
+inf:-inf
-inf:inf
+0:0
+1:-1
-1:1
+123456789:-123456789
-123456789:123456789
+123.456789:-123.456789
-123456.789:123456.789
&babs
invalid:NaN
+inf:inf
-inf:inf
+0:0
+1:1
-1:1
+123456789:123456789
-123456789:123456789
+123.456789:123.456789
-123456.789:123456.789
&bround
$round_mode = "trunc"
+inf:5:inf
-inf:5:-inf
0:5:0
invalid: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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
-601234600:6:-601235000
+601234300:6:601234000
+60123456789.0123:5:60123000000
-60123456789.0123:5:-60123000000
&bfround
$round_mode = "trunc"
+inf:5:inf
-inf:5:-inf
0:5:0
invalid: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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
+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:
&bacmp
invalid:invalid:
invalid:+0:
+0:invalid:
+0:+0:0
-1:+0:1
+0:-1:-1
+1:+0:1
+0:+1:-1
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
-1:inf:-1
5:-inf:-1
-1:-inf:-1
# return undef
+inf:invalid:
invalid:inf:
-inf:invalid:
invalid:-inf:
&bdec
invalid: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
&binc
invalid: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
&badd
abc:abc:NaN
abc:+0:NaN
+0:abc:NaN
+inf:-inf:NaN
-inf:+inf:NaN
+inf:+inf:inf
-inf:-inf:-inf
invalid:+inf:NaN
invalid:+inf:NaN
+inf:invalid:NaN
-inf:invalid: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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
+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
&bsub
abc:abc:NaN
abc:+0:NaN
+0:abc:NaN
+inf:-inf:inf
-inf:+inf:-inf
+inf:+inf:NaN
-inf:-inf:NaN
invalid:+inf:NaN
invalid:+inf:NaN
+inf:invalid:NaN
-inf:invalid: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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
+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
invalid:+inf:0:NaN
invalid:-inf:0:NaN
-inf:invalid:0:NaN
+inf:invalid: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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
-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
&bmul
abc:abc:NaN
abc:+0:NaN
+0:abc:NaN
+inf:invalid:NaN
+inf:invalid:NaN
invalid:+inf:NaN
invalid:-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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
+44444444444:+9:399999999996
+55555555555:+9:499999999995
+66666666666:+9:599999999994
+77777777777:+9:699999999993
+88888888888:+9:799999999992
+99999999999:+9:899999999991
6:120:720
10:10000:100000
&bdiv-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
&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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
+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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
-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
&bfac
invalid:NaN
+inf:inf
-inf:NaN
-1: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
&bdfac
NaN:NaN
+inf:inf
-inf:NaN
-2:NaN
-1:1
0:1
1:1
2:2
3:3
4:8
5:15
6:48
7:105
8:384
9:945
10:3840
11:10395
12:46080
&btfac
NaN:NaN
+inf:inf
-inf:NaN
-3:NaN
-2:1
-1:1
0:1
1:1
2:2
3:3
4:4
5:10
6:18
7:28
8:80
9:162
10:280
11:880
12:1944
&bmfac
7:-inf:NaN
7:-1:NaN
7:0:NaN
7:2.5:NaN
7:inf:7
7:NaN:NaN
NaN:1:NaN
+inf:1:inf
-inf:1:NaN
-1:1:NaN
0:1:1
1:1:1
2:1:2
3:1:6
4:1:24
5:1:120
6:1:720
7:1:5040
8:1:40320
9:1:362880
10:1:3628800
NaN:2:NaN
+inf:2:inf
-inf:2:NaN
-2:2:NaN
-1:2:1
0:2:1
1:2:1
2:2:2
3:2:3
4:2:8
5:2:15
6:2:48
7:2:105
8:2:384
9:2:945
10:2:3840
NaN:3:NaN
+inf:3:inf
-inf:3:NaN
-3:3:NaN
-2:3:1
-1:3:1
0:3:1
1:3:1
2:3:2
3:3:3
4:3:4
5:3:10
6:3:18
7:3:28
8:3:80
9:3:162
10:3:280
NaN:4:NaN
+inf:4:inf
-inf:4:NaN
-4:4:NaN
-3:4:1
-2:4:1
-1:4:1
0:4:1
1:4:1
2:4:2
3:4:3
4:4:4
5:4:5
6:4:12
7:4:21
8:4:32
9:4:45
10:4:120
NaN:5:NaN
+inf:5:inf
-inf:5:NaN
-5:5:NaN
-4:5:1
-3:5:1
-2:5:1
-1:5:1
0:5:1
1:5:1
2:5:2
3:5:3
4:5:4
5:5:5
6:5:6
7:5:14
8:5:24
9:5:36
10:5:50
&broot
# sqrt()
+0:2:0
+1:2:1
-1:2:NaN
# -$x ** (1/2) => -$y, but not in broot()
-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
&bsqrt
+0:0
-1:NaN
-2:NaN
-16:NaN
-123.45:NaN
nanbsqrt: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
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
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
-inf:-inf:1
-inf:+inf:0
+inf:-inf:0
+inf:+inf:1
+iNfInItY::1
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
-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
&is_non_positive
0:1
1:0
-1:1
NaN:0
-inf:1
+inf:0
&is_non_negative
0:1
1:1
-1:0
NaN:0
-inf:0
+inf:1
&parts
0:0 0
1:1 0
123:123 0
-123:-123 0
-1200:-12 2
invalid:NaN NaN
+inf:inf inf
-inf:-inf inf
&exponent
0:0
1:0
123:0
-123:0
-1200:2
+inf:inf
-inf:inf
invalid:NaN
&mantissa
0:0
1:1
123:123
-123:-123
-1200:-12
+inf:inf
-inf:-inf
invalid:NaN
&length
123:3
-123:3
0:1
1:1
12345678901234567890:20
&is_zero
invalid:0
cpan/Math-BigInt/t/bigfltpm.inc view on Meta::CPAN
+inf:0
-inf:0
0:0
2:0
1:1
-1:0
-2:0
&bfloor
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
&bceil
0:0
abc:NaN
+inf:inf
-inf:-inf
1:1
-51:-51
-51.2:-51
12.2:13
-0.4:0
&bint
0:0
NaN:NaN
+inf:inf
-inf:-inf
1:1
-51:-51
-51.2:-51
12.2:12
-0.4:0
# overloaded functions
&log
-1:NaN
0:-inf
1:0
2:0.6931471805599453094172321214581765680755
3:1.098612288668109691395245236922525704647
123456789:18.63140176616801803319393334796320420971
1234567890987654321:41.657252696908474880343847955484513481
-inf:inf
inf:inf
NaN:NaN
&exp
&sin
&cos
&atan2
&int
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
} elsif ($f =~ /^(numify|length|stringify)$/) {
$try .= " \$x->$f();";
} elsif ($f =~ /^(to|as)_(hex|oct|bin)$/) {
$try .= " \$x->$f();";
# overloaded functions
} elsif ($f =~ /^(log|exp|sin|cos|atan2|int|neg|abs|sqrt)$/) {
$try .= " \$x = $f(\$x);";
} 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 "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 "bpi") {
$try .= " $CLASS\->bpi(\$x);";
} else {
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
is($CLASS->new("$c"), $m, qq|$CLASS->new("$c") = $m|);
is($CLASS->new(" $c"), $m, qq|$CLASS->new(" $c") = $m|);
is($CLASS->new("$c "), $m, qq|$CLASS->new("$c ") = $m|);
is($CLASS->new(" $c "), $m, qq|$CLASS->new(" $c ") = $m|);
is($CLASS->new("\n$c"), $m, qq|$CLASS->new("\\n$c") = $m|);
is($CLASS->new("$c\n"), $m, qq|$CLASS->new("$c\\n") = $m|);
is($CLASS->new("\n$c\n"), $m, qq|$CLASS->new("\\n$c\\n") = $m|);
is($CLASS->new(" \n$c\n"), $m, qq|$CLASS->new(" \\n$c\\n") = $m|);
is($CLASS->new(" \n$c \n"), $m, qq|$CLASS->new(" \\n$c \\n") = $m|);
is($CLASS->new(" \n$c\n "), $m, qq|$CLASS->new(" \\n$c\\n ") = $m|);
is($CLASS->new(" \n$c\n1"), 'NaN', qq|$CLASS->new(" \\n$c\\n1") = 'NaN'|);
is($CLASS->new("1 \n$c\n1"), 'NaN', qq|$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++;
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
$x = $CLASS->new('-629');
is($x->bmod(5033), 4404, q|$x->bmod(5033) = 4404|);
###########################################################################
# bone/binf etc as plain calls (Lite failed them)
is($CLASS->bzero(), 0, qq|$CLASS->bzero() = 0|);
is($CLASS->bone(), 1, qq|$CLASS->bone() = 1|);
is($CLASS->bone("+"), 1, qq|$CLASS->bone("+") = 1|);
is($CLASS->bone("-"), -1, qq|$CLASS->bone("-") = -1|);
is($CLASS->bnan(), "NaN", qq|$CLASS->bnan() = "NaN"|);
is($CLASS->binf(), "inf", qq|$CLASS->binf() = "inf"|);
is($CLASS->binf("+"), "inf", qq|$CLASS->binf("+") = "inf"|);
is($CLASS->binf("-"), "-inf", qq|$CLASS->binf("-") = "-inf"|);
is($CLASS->binf("-inf"), "-inf", qq|$CLASS->binf("-inf") = "-inf"|);
###########################################################################
# is_one("-")
is($CLASS->new(1)->is_one("-"), 0, qq|$CLASS->new(1)->is_one("-") = 0|);
is($CLASS->new(-1)->is_one("-"), 1, qq|$CLASS->new(-1)->is_one("-") = 1|);
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
# [perl #30609] bug with $x -= $x not being 0, but 2*$x
$x = $CLASS->new(3);
$x -= $x;
is($x, 0, qq|\$x = $CLASS->new(3); \$x -= \$x; = 0|);
$x = $CLASS->new(-3);
$x -= $x;
is($x, 0, qq|\$x = $CLASS->new(-3); \$x -= \$x; = 0|);
$x = $CLASS->new("NaN");
$x -= $x;
is($x->is_nan(), 1,
qq|\$x = $CLASS->new("NaN"); \$x -= \$x; \$x->is_nan() = 1|);
$x = $CLASS->new("inf");
$x -= $x;
is($x->is_nan(), 1,
qq|\$x = $CLASS->new("inf"); \$x -= \$x; \$x->is_nan() = 1|);
$x = $CLASS->new("-inf");
$x -= $x;
is($x->is_nan(), 1,
qq|\$x = $CLASS->new("-inf"); \$x -= \$x; \$x->is_nan() = 1|);
$x = $CLASS->new("NaN");
$x += $x;
is($x->is_nan(), 1,
qq|\$x = $CLASS->new("NaN"); \$x += \$x; \$x->is_nan() = 1|);
$x = $CLASS->new("inf");
$x += $x;
is($x->is_inf(), 1,
qq|\$x = $CLASS->new("inf"); \$x += \$x; \$x->is_inf() = 1|);
$x = $CLASS->new("-inf");
$x += $x;
is($x->is_inf("-"), 1,
qq|\$x = $CLASS->new("-inf"); \$x += \$x; \$x->is_inf("-") = 1|);
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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 = qq|Illegal sign $x->{sign}|
. qq| (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 = $LIB->_check($x->{value}) if $e eq '0';
}
# test done, see if error did crop up
if ($e eq '0') {
pass('is a valid object');
return;
}
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
2:1:3
&&=
5:7:5
&^=
5:7:2
&blog
#
invalid:2:NaN
122:invalid:NaN
invalid:invalid:NaN
#
122:inf:0
inf:122:inf
122:-inf:0
-inf:122:inf
-inf:-inf:NaN
0:4:-inf
-21:4:NaN
21:-21:NaN
#
0:-inf:NaN
0:-1:NaN
0:0:NaN
0:1:NaN
0:inf:NaN
#
1:-inf:0
1:-1:0
1:0:0
1:1:NaN
1:4:0
1:inf:0
#
inf:-inf:NaN
inf:-1:NaN
inf:0:NaN
inf:1:NaN
inf:4:inf
inf:inf:NaN
#
# normal results
1024:2:10
81:3:4
# 3.01.. truncate
82:3:4
# 3.9... truncate
80:3:3
4096:2:12
15625:5:6
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
+inf:1
-inf:0
invalid:0
&is_non_negative
0:1
-1:0
1:1
+inf:1
-inf:0
NaN:0
&is_non_positive
0:1
-1:1
1:0
+inf:0
-inf:1
NaN:0
&is_int
-inf:0
+inf:0
invalid:0
1:1
0:1
123e12:1
&is_odd
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
+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
invalid:123:
123:invalid:
invalid:invalid:
# 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
123.000:123
123e0:123
123e+0:123
123e-0:123
123.000e0:123
123.000e+0:123
123.000e-0: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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
0b100000000000000000000000000001:536870913
0b1000000000000000000000000000001:1073741825
0b10000000000000000000000000000001:2147483649
0b100000000000000000000000000000001:4294967297
0b1000000000000000000000000000000001:8589934593
0b10000000000000000000000000000000001:17179869185
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
0x9:9
0x11:17
0x21:33
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
invalid:-:-1
invalid:+:1
3::1
&binf
1:+:inf
2:-:-inf
3:+inf: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
+iNfInItY::1
-InFiNiTy::1
&blsft
abc:abc:NaN
+2:+2:8
+1:+32:4294967296
+1:+48:281474976710656
+8:-2:2
# 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:1
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:8
# exercise base 10
-1234:0:10:-1234
+1234:0:10:1234
+200:2:10:2
+1234:3:10:1
+1234:2:10:12
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
-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
5:5
-5:-5
100:100
-100:-100
&bneg
invalid:NaN
+inf:-inf
-inf:inf
abd:NaN
0:0
1:-1
-1:1
+123456789:-123456789
-123456789:123456789
&babs
invalid: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
invalid:invalid:
invalid:0:
0:invalid:
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
+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
invalid:+inf:NaN
invalid:+inf:NaN
+inf:invalid:NaN
-inf:invalid: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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
-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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
invalid:+inf:0:NaN
invalid:-inf:0:NaN
-inf:invalid:0:NaN
+inf:invalid: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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
-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
invalid:+inf:NaN
invalid:-inf:NaN
-inf:invalid:NaN
+inf:invalid: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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
99999999999999999:100000000000000000:9999999999999999900000000000000000
999999999999999999:1000000000000000000:999999999999999999000000000000000000
9999999999999999999:10000000000000000000:99999999999999999990000000000000000000
&bdiv-list
# Divide by zero and modulo zero.
inf:0:inf,inf
5:0:inf,5
0:0:NaN,0
-5:0:-inf,-5
-inf:0:-inf,-inf
# Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
inf:-inf:NaN,NaN
inf:-5:-inf,NaN
inf:5:inf,NaN
inf:inf:NaN,NaN
-inf:-inf:NaN,NaN
-inf:-5:inf,NaN
-inf:5:-inf,NaN
-inf:inf:NaN,NaN
# Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
# are covered above.
-5:inf:-1,inf
0:inf:0,0
5:inf:0,5
-5:-inf:0,-5
0:-inf:0,0
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
# Divide by zero and modulo zero.
inf:0:inf
5:0:inf
0:0:NaN
-5:0:-inf
-inf:0:-inf
# Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
inf:-inf:NaN
inf:-5:-inf
inf:5:inf
inf:inf:NaN
-inf:-inf:NaN
-inf:-5:inf
-inf:5:-inf
-inf:inf:NaN
# Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
# are covered above.
-5:inf:-1
0:inf:0
5:inf:0
-5:-inf:0
0:-inf:0
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
9999999_9999999_9999999_9999999:1_0000000_0000000_0000000:9999999
# bug with shortcut in Calc 0.44
949418181818187070707070707070707070:181818181853535353535353535353535353:5
&btdiv-list
# Divide by zero and modulo zero.
inf:0:inf,inf
5:0:inf,5
0:0:NaN,0
-5:0:-inf,-5
-inf:0:-inf,-inf
# Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
inf:-inf:NaN,NaN
inf:-5:-inf,NaN
inf:5:inf,NaN
inf:inf:NaN,NaN
-inf:-inf:NaN,NaN
-inf:-5:inf,NaN
-inf:5:-inf,NaN
-inf:inf:NaN,NaN
# Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
# are covered above.
-5:inf:0,-5
0:inf:0,0
5:inf:0,5
-5:-inf:0,-5
0:-inf:0,0
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
5:1:5,0
5:2:2,1
5:5:1,0
&btdiv
# Divide by zero and modulo zero.
inf:0:inf
5:0:inf
0:0:NaN
-5:0:-inf
-inf:0:-inf
# Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
inf:-inf:NaN
inf:-5:-inf
inf:5:inf
inf:inf:NaN
-inf:-inf:NaN
-inf:-5:inf
-inf:5:-inf
-inf:inf:NaN
# Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
# are covered above.
-5:inf:0
0:inf:0
5:inf:0
-5:-inf:0
0:-inf:0
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
5:-1:-5
5:1:5
5:2:2
5:5:1
###############################################################################
&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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
# Divide by zero and modulo zero.
inf:0:inf
5:0:5
0:0:0
-5:0:-5
-inf:0:-inf
# Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
inf:-inf:NaN
inf:-5:NaN
inf:5:NaN
inf:inf:NaN
-inf:-inf:NaN
-inf:-5:NaN
-inf:5:NaN
-inf:inf:NaN
# Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
# are covered above.
-5:inf:inf
0:inf:0
5:inf:5
-5:-inf:-5
0:-inf:0
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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:0
+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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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:0
+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,0
+inf:inf,inf
-inf:-inf,inf
&bfac
NaN:NaN
+inf:inf
-inf:NaN
-1: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
&bdfac
NaN:NaN
+inf:inf
-inf:NaN
-2:NaN
-1:1
0:1
1:1
2:2
3:3
4:8
5:15
6:48
7:105
8:384
9:945
10:3840
11:10395
12:46080
&btfac
NaN:NaN
+inf:inf
-inf:NaN
-3:NaN
-2:1
-1:1
0:1
1:1
2:2
3:3
4:4
5:10
6:18
7:28
8:80
9:162
10:280
11:880
12:1944
&bmfac
7:-inf:NaN
7:-1:NaN
7:0:NaN
7:inf:7
7:NaN:NaN
NaN:1:NaN
+inf:1:inf
-inf:1:NaN
-1:1:NaN
0:1:1
1:1:1
2:1:2
3:1:6
4:1:24
5:1:120
6:1:720
7:1:5040
8:1:40320
9:1:362880
10:1:3628800
NaN:2:NaN
+inf:2:inf
-inf:2:NaN
-2:2:NaN
-1:2:1
0:2:1
1:2:1
2:2:2
3:2:3
4:2:8
5:2:15
6:2:48
7:2:105
8:2:384
9:2:945
10:2:3840
NaN:3:NaN
+inf:3:inf
-inf:3:NaN
-3:3:NaN
-2:3:1
-1:3:1
0:3:1
1:3:1
2:3:2
3:3:3
4:3:4
5:3:10
6:3:18
7:3:28
8:3:80
9:3:162
10:3:280
NaN:4:NaN
+inf:4:inf
-inf:4:NaN
-4:4:NaN
-3:4:1
-2:4:1
-1:4:1
0:4:1
1:4:1
2:4:2
3:4:3
4:4:4
5:4:5
6:4:12
7:4:21
8:4:32
9:4:45
10:4:120
NaN:5:NaN
+inf:5:inf
-inf:5:NaN
-5:5:NaN
-4:5:1
-3:5:1
-2:5:1
-1:5:1
0:5:1
1:5:1
2:5:2
3:5:3
4:5:4
5:5:5
6:5:6
7:5:14
8:5:24
9:5:36
10:5:50
&bpow
#
abc:12:NaN
12:abc:NaN
#
#
-inf:-inf:0
-inf:-3:0
-inf:-2:0
-inf:-1:0
-inf:0:NaN
-inf:1:-inf
-inf:2:inf
-inf:3:-inf
-inf:inf:inf # complex infinity
-inf:NaN:NaN
#
-3:-inf:0
-3:-3:0
-3:-2:0
-3:-1:0
-3:0:1
-3:1:-3
-3:2:9
-3:3:-27
-3:inf:inf # complex infinity
-3:NaN:NaN
#
-2:-inf:0
-2:-3:0
-2:-2:0
-2:-1:0
-2:0:1
-2:1:-2
-2:2:4
-2:3:-8
-2:inf:inf # complex infinity
-2:NaN:NaN
#
-1:-inf:NaN
-1:-3:-1
-1:-2:1
-1:-1:-1
-1:0:1
-1:1:-1
-1:2:1
-1:3:-1
-1:inf:NaN
-1:NaN:NaN
#
0:-inf:inf # complex infinity
0:-3:inf # complex infinity
0:-2:inf # complex infinity
0:-1:inf # complex infinity
0:0:1
0:1:0
0:2:0
0:3:0
0:inf:0
0:NaN:NaN
#
1:-inf:1
1:-3:1
1:-2:1
1:-1:1
1:0:1
1:1:1
1:2:1
1:3:1
1:inf:1
1:NaN:NaN
#
2:-inf:0
2:-3:0
2:-2:0
2:-1:0
2:0:1
2:1:2
2:2:4
2:3:8
2:inf:inf
2:NaN:NaN
#
3:-inf:0
3:-3:0
3:-2:0
3:-1:0
3:0:1
3:1:3
3:2:9
3:3:27
3:inf:inf
3:NaN:NaN
#
inf:-inf:0
inf:-3:0
inf:-2:0
inf:-1:0
inf:0:NaN
inf:1:inf
inf:2:inf
inf:3:inf
inf:inf:inf
inf:NaN:NaN
#
NaN:-inf:NaN
NaN:-3:NaN
NaN:-2:NaN
NaN:-1:NaN
NaN:0:NaN
NaN:1:NaN
NaN:2:NaN
NaN:3:NaN
NaN:inf:NaN
NaN:NaN:NaN
#
+inf:1234500012:inf
-inf:1234500012:inf
-inf:1234500013:-inf
+inf:-12345000123:0
-inf:-12345000123:0
#
10:2:100
10:3:1000
10:4:10000
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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 broot()
-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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
-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
invalid: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
cpan/Math-BigInt/t/bigintpm.inc view on Meta::CPAN
+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
invalid:NaN
+inf:inf
-inf:-inf
-1:-1
-2:-2
2:2
3:3
abc:NaN
&bceil
invalid: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
invalid:NaN
&as_bin
128:0b10000000
-128:-0b10000000
0:0b0
-0:0b0
1:0b1
0b1010111101010101010110110110110110101:0b1010111101010101010110110110110110101
0x123456789123456789:0b100100011010001010110011110001001000100100011010001010110011110001001
+inf:inf
-inf:-inf
invalid:NaN
&as_oct
128:0200
-128:-0200
0:00
-0:00
1:01
0b1010111101010101010110110110110110101:01275252666665
0x123456789123456789:044321263611044321263611
+inf:inf
-inf:-inf
invalid:NaN
&to_hex
128:80
-128:-80
0:0
-0:0
1:1
0x123456789123456789:123456789123456789
+inf:inf
-inf:-inf
invalid:NaN
&to_bin
128:10000000
-128:-10000000
0:0
-0:0
1:1
0b1010111101010101010110110110110110101:1010111101010101010110110110110110101
0x123456789123456789:100100011010001010110011110001001000100100011010001010110011110001001
+inf:inf
-inf:-inf
invalid:NaN
&to_oct
128:200
-128:-200
0:0
-0:0
1:1
0b1010111101010101010110110110110110101:1275252666665
0x123456789123456789:44321263611044321263611
+inf:inf
-inf:-inf
invalid:NaN
# overloaded functions
&log
-1:NaN
0:-inf
1:0
2:0
3:1
123456789:18
1234567890987654321:41
-inf:inf
inf:inf
NaN:NaN
&exp
&sin
&cos
&atan2
&int
cpan/Math-BigInt/t/biglog.t view on Meta::CPAN
is($class->new(21)->blog(undef, 71),
'3.0445224377234229965005979803657054342845752874046106401940844835750742',
"$class->new(21)->blog(undef, 71)");
###############################################################################
# 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($class->new(-2)->blog(), 'NaN', "$class->new(-2)->blog()");
is($class->new(-1)->blog(), 'NaN', "$class->new(-1)->blog()");
is($class->new(-10)->blog(), 'NaN', "$class->new(-10)->blog()");
is($class->new(-2, 2)->blog(), 'NaN', "$class->new(-2, 2)->blog()");
my $ten = $class->new(10)->blog();
# 10 is cached (up to 75 digits)
is($class->new(10)->blog(),
'2.302585092994045684017991454684364207601',
qq|$class->new(10)->blog()|);
# 0.1 is using the cached value for log(10), too
cpan/Math-BigInt/t/bigrat.t view on Meta::CPAN
$x = $mbr->$method("1e2/1e1");
is($x, 10, qq|\$x = $mbr->$method("1e2/1e1")|);
$x = $mbr->$method("1 / 3");
is($x, "1/3", qq|\$x = $mbr->$method("1 / 3")|);
$x = $mbr->$method("-1 / 3");
is($x, "-1/3", qq|\$x = $mbr->$method("-1 / 3")|);
$x = $mbr->$method("NaN");
is($x, "NaN", qq|\$x = $mbr->$method("NaN")|);
$x = $mbr->$method("inf");
is($x, "inf", qq|\$x = $mbr->$method("inf")|);
$x = $mbr->$method("-inf");
is($x, "-inf", qq|\$x = $mbr->$method("-inf")|);
$x = $mbr->$method("1/");
is($x, "NaN", qq|\$x = $mbr->$method("1/")|);
$x = $mbr->$method("0x7e");
is($x, 126, qq|\$x = $mbr->$method("0x7e")|);
# input ala "1+1/3" isn"t parsed ok yet
$x = $mbr->$method("1+1/3");
is($x, "NaN", qq|\$x = $mbr->$method("1+1/3")|);
$x = $mbr->$method("1/1.2");
is($x, "5/6", qq|\$x = $mbr->$method("1/1.2")|);
$x = $mbr->$method("1.3/1.2");
is($x, "13/12", qq|\$x = $mbr->$method("1.3/1.2")|);
$x = $mbr->$method("1.2/1");
is($x, "6/5", qq|\$x = $mbr->$method("1.2/1")|);
cpan/Math-BigInt/t/bigrat.t view on Meta::CPAN
}
my $n = 'numerator';
my $d = 'denominator';
$x = $mbr->new('-0');
is($x, '0');
is($x->$n(), '0');
is($x->$d(), '1');
$x = $mbr->new('NaN');
is($x, 'NaN'); is($x->$n(), 'NaN');
is($x->$d(), 'NaN');
$x = $mbr->new('-NaN');
is($x, 'NaN'); is($x->$n(), 'NaN');
is($x->$d(), 'NaN');
$x = $mbr->new('-1r4');
is($x, 'NaN'); is($x->$n(), 'NaN');
is($x->$d(), 'NaN');
$x = $mbr->new('+inf');
is($x, 'inf'); is($x->$n(), 'inf');
is($x->$d(), '1');
$x = $mbr->new('-inf');
is($x, '-inf');
is($x->$n(), '-inf');
is($x->$d(), '1');
$x = $mbr->new('123a4');
is($x, 'NaN');
is($x->$n(), 'NaN');
is($x->$d(), 'NaN');
# wrong inputs
$x = $mbr->new('1e2e2');
is($x, 'NaN');
is($x->$n(), 'NaN');
is($x->$d(), 'NaN');
$x = $mbr->new('1+2+2');
is($x, 'NaN');
is($x->$n(), 'NaN');
is($x->$d(), 'NaN');
# failed due to BigFloat bug
$x = $mbr->new('1.2.2');
is($x, 'NaN');
is($x->$n(), 'NaN');
is($x->$d(), 'NaN');
is($mbr->new('123a4'), 'NaN');
is($mbr->new('123e4'), '1230000');
is($mbr->new('-NaN'), 'NaN');
is($mbr->new('NaN'), 'NaN');
is($mbr->new('+inf'), 'inf');
is($mbr->new('-inf'), '-inf');
##############################################################################
# two Bigints
is($mbr->new($mbi->new(3), $mbi->new(7))->badd(1), '10/7');
is($mbr->new($mbi->new(-13), $mbi->new(7)), '-13/7');
is($mbr->new($mbi->new(13), $mbi->new(-7)), '-13/7');
is($mbr->new($mbi->new(-13), $mbi->new(-7)), '13/7');
cpan/Math-BigInt/t/bigrat.t view on Meta::CPAN
$x = $mbr->new('7/4');
$y = $mbr->new('5/13');
is($x % $y, '11/52');
$x = $mbr->new('7/4');
$y = $mbr->new('5/9');
is($x % $y, '1/12');
$x = $mbr->new('-144/9')->bsqrt();
is($x, 'NaN');
$x = $mbr->new('144/9')->bsqrt();
is($x, '4');
$x = $mbr->new('3/4')->bsqrt();
is($x,
'4330127018922193233818615853764680917357/' .
'5000000000000000000000000000000000000000');
##############################################################################
cpan/Math-BigInt/t/bigrat.t view on Meta::CPAN
is($x, '1');
for (my $i = 0; $i < 8; $i++) {
$x = $mbr->new("$i/1")->bfac();
is($x, $mbi->new($i)->bfac());
}
# test for $self->bnan() vs. $x->bnan();
$x = $mbr->new('-1');
$x->bfac();
is($x, 'NaN');
##############################################################################
# binc/bdec
note("binc()");
$x = $mbr->new('3/2');
is($x->binc(), '5/2');
note("bdec()");
cpan/Math-BigInt/t/bigrat.t view on Meta::CPAN
subtest qq|$mbr -> new("-inf") -> numify()| => sub {
plan tests => 3;
$x = $mbr -> new("-inf") -> numify();
is(ref($x), "", '$x is a scalar');
ok(isnumeric($x), '$x is numeric');
cmp_ok($x, "==", -$inf, '$x has the right value');
};
subtest qq|$mbr -> new("NaN") -> numify()| => sub {
plan tests => 3;
$x = $mbr -> new("NaN") -> numify();
is(ref($x), "", '$x is a scalar');
ok(isnumeric($x), '$x is numeric');
cmp_ok($x, "!=", $nan, '$x has the right value'); # Note: NaN != NaN
};
##############################################################################
# as_hex(), as_bin(), as_oct()
note("as_hex(), as_bin(), as_oct()");
$x = $mbr->new('8/8');
is($x->as_hex(), '0x1');
is($x->as_bin(), '0b1');
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
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
4/9:2/3
36/49:6/7
49/121:7/11
999966000289/99999820000081:999983/9999991
&flog
NaN:NaN
0:-inf
-2:NaN
&blog
NaN:NaN:NaN
0:NaN:NaN
NaN:0:NaN
NaN:1:NaN
1:NaN:NaN
0:2:-inf
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
&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
3/1:3
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
#1.E3:NaN
.2E-3.:NaN
#1e3e4:NaN
.2E2:20
inf:inf
+inf:inf
-inf:-inf
+InFiNiTy:inf
+-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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
+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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
+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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
-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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
+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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
+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
# without sign argument
abc:0
NaN:0
+inf:1
-inf:1
-1:0
0:0
1:0
# with sign argument "+"
abc:+:0
NaN:+:0
+inf:+:1
-inf:+:0
-1:+:0
0:+:0
1:+:0
# with sign argument "-"
abc:-:0
NaN:-:0
+inf:-:0
-inf:-:1
-1:-:0
0:-:0
1:-:0
+infinity:1
-infinity:1
&is_odd
abc:0
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
-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
# with no sign argument
invalid:0
NaN:0
+inf:0
-inf:0
-2:0
-1:0
0:0
1:1
-2:0
# with sign argument "+"
invalid:+:0
NaN:+:0
+inf:+:0
-inf:+:0
-2:+:0
-1:+:0
0:+:0
1:+:1
-2:+:0
# with sign argument "-"
invalid:-:0
NaN:-:0
+inf:-:0
-inf:-:0
-2:-:0
-1:-:1
0:-:0
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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
-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
#
abc:123:NaN
123:abc:NaN
#
-inf:-inf:0
-inf:-3:0
-inf:-2:0
-inf:-3/2:0
-inf:-1:0
-inf:-1/2:0
-inf:0:NaN
-inf:1/2:inf # directed infinity
-inf:1:-inf
-inf:3/2:inf # directed infinity
-inf:2:inf
-inf:3:-inf
-inf:inf:inf # complex infinity
-inf:NaN:NaN
#
-3:-inf:0
-3:-3:-1/27
-3:-2:1/9
-3:-3/2:NaN
-3:-1:-1/3
-3:-1/2:NaN
-3:0:1
-3:1/2:NaN
-3:1:-3
-3:3/2:NaN
-3:2:9
-3:3:-27
-3:inf:inf # complex infinity
-3:NaN:NaN
#
-2:-inf:0
-2:-3:-1/8
-2:-2:1/4
-2:-3/2:NaN
-2:-1:-1/2
-2:-1/2:NaN
-2:0:1
-2:1/2:NaN
-2:1:-2
-2:3/2:NaN
-2:2:4
-2:3:-8
-2:inf:inf # complex infinity
-2:NaN:NaN
#
-3/2:-inf:0
-3/2:-3:-8/27
-3/2:-2:4/9
-3/2:-3/2:NaN
-3/2:-1:-2/3
-3/2:-1/2:NaN
-3/2:0:1
-3/2:1/2:NaN
-3/2:1:-3/2
-3/2:3/2:NaN
-3/2:2:9/4
-3/2:3:-27/8
-3/2:inf:inf # complex infinity
-3/2:NaN:NaN
#
-1:-inf:NaN
-1:-3:-1
-1:-2:1
-1:-3/2:NaN
-1:-1:-1
-1:-1/2:NaN
-1:0:1
-1:1/2:NaN
-1:1:-1
-1:3/2:NaN
-1:2:1
-1:3:-1
-1:inf:NaN
-1:NaN:NaN
#
-1/2:-inf:inf # complex infinity
-1/2:-3:-8
-1/2:-2:4
-1/2:-3/2:NaN
-1/2:-1:-2
-1/2:-1/2:NaN
-1/2:0:1
-1/2:1/2:NaN
-1/2:1:-1/2
-1/2:3/2:NaN
-1/2:2:1/4
-1/2:3:-1/8
-1/2:inf:0
-1/2:NaN:NaN
#
0:-inf:inf # complex infinity
0:-3:inf # complex infinity
0:-2:inf # complex infinity
0:-3/2:inf # complex infinity
0:-1:inf # complex infinity
0:-1/2:inf # complex infinity
0:0:1
0:1/2:0
0:1:0
0:3/2:0
0:2:0
0:3:0
0:inf:0
0:NaN:NaN
#
1/2:-inf:inf
1/2:-3:8
1/2:-2:4
#1/2:-3/2:2.828427124746190097603377448419396157139
1/2:-1:2
#1/2:-1/2:1.41421356237309504880168872420969807857
1/2:0:1
#1/2:1/2:0.7071067811865475244008443621048490392848
1/2:1:1/2
#1/2:3/2:0.3535533905932737622004221810524245196424
1/2:2:1/4
1/2:3:1/8
1/2:inf:0
1/2:NaN:NaN
#
1:-inf:1
1:-3:1
1:-2:1
1:-3/2:1
1:-1:1
1:-1/2:1
1:0:1
1:1/2:1
1:1:1
1:3/2:1
1:2:1
1:3:1
1:inf:1
1:NaN:NaN
#
3/2:-inf:0
3/2:-3:8/27
3/2:-2:4/9
#3/2:-3/2:0.5443310539518173551549520166013091982147
3/2:-1:2/3
#3/2:-1/2:0.816496580927726032732428024901963797322
3/2:0:1
#3/2:1/2:1.224744871391589049098642037352945695983
3/2:1:3/2
#3/2:3/2:1.837117307087383573647963056029418543974
3/2:2:9/4
3/2:3:27/8
3/2:inf:inf
3/2:NaN:NaN
#
2:-inf:0
2:-3:1/8
2:-2:1/4
#2:-3/2:0.3535533905932737622004221810524245196424
2:-1:1/2
#2:-1/2:0.7071067811865475244008443621048490392848
2:0:1
#2:1/2:1.41421356237309504880168872420969807857
2:1:2
#2:3/2:2.828427124746190097603377448419396157139
2:2:4
2:3:8
2:inf:inf
2:NaN:NaN
#
3:-inf:0
3:-3:1/27
3:-2:1/9
#3:-3/2:0.1924500897298752548363829268339858185492
3:-1:1/3
#3:-1/2:0.5773502691896257645091487805019574556476
3:0:1
#3:1/2:1.732050807568877293527446341505872366943
3:1:3
#3:3/2:5.196152422706631880582339024517617100828
3:2:9
3:3:27
3:inf:inf
3:NaN:NaN
#
inf:-inf:0
inf:-3:0
inf:-2:0
inf:-3/2:0
inf:-1:0
inf:-1/2:0
inf:0:NaN
inf:1/2:inf
inf:1:inf
inf:3/2:inf
inf:2:inf
inf:3:inf
inf:inf:inf
inf:NaN:NaN
#
NaN:-inf:NaN
NaN:-3:NaN
NaN:-2:NaN
NaN:-3/2:NaN
NaN:-1:NaN
NaN:-1/2:NaN
NaN:0:NaN
NaN:1/2:NaN
NaN:1:NaN
NaN:3/2:NaN
NaN:2:NaN
NaN:3:NaN
NaN:inf:NaN
NaN:NaN:NaN
&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
cpan/Math-BigInt/t/bigratpm.inc view on Meta::CPAN
-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
&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
&log
-1:NaN
0:-inf
1:0
34803:3267955896544848894312057422508991/312500000000000000000000000000000
-inf:inf
inf:inf
NaN:NaN
&exp
&sin
&cos
&atan2
&int
cpan/Math-BigInt/t/calling-class-methods.t view on Meta::CPAN
&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+0
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
cpan/Math-BigInt/t/calling-instance-methods.t view on Meta::CPAN
&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+0
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
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
is(ref $x, "Math::BigInt", "downgrades to Math::BigInt");
};
$x = Math::BigFloat -> new("Inf");
subtest '$x = Math::BigFloat -> new("inf")' => sub {
plan tests => 2;
cmp_ok($x, "==", "Inf", 'value of $x');
is(ref $x, "Math::BigInt", "downgrades to Math::BigInt");
};
$x = Math::BigFloat -> new("NaN");
subtest '$x = Math::BigFloat -> new("NaN")' => sub {
plan tests => 2;
is($x, "NaN", );
is(ref $x, "Math::BigInt", "downgrades to Math::BigInt");
};
note("testing bzero()");
$x = Math::BigFloat -> bzero();
subtest '$x = Math::BigFloat -> bzero()' => sub {
plan tests => 2;
cmp_ok($x, '==', 0, 'value of $x');
is(ref $x, 'Math::BigInt', 'downgrades to Math::BigInt');
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
plan tests => 2;
cmp_ok($x, '==', 'Inf', 'value of $x');
is(ref $x, 'Math::BigInt', 'downgrades to Math::BigInt');
};
note("testing bnan()");
$x = Math::BigFloat -> bnan();
subtest '$x = Math::BigFloat -> bnan()' => sub {
plan tests => 2;
is($x, 'NaN', 'value of $x');
is(ref $x, 'Math::BigInt', 'downgrades to Math::BigInt');
};
note("testing from_dec()");
$x = Math::BigFloat -> from_dec('3.14e2');
subtest '$x = Math::BigFloat -> from_dec("3.14e2")' => sub {
plan tests => 2;
cmp_ok($x, '==', 314, 'value of $x');
is(ref $x, 'Math::BigInt', 'downgrades to Math::BigInt');
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
my $one = Math::BigFloat -> bone();
my $four = Math::BigFloat -> new("4");
my $inf = Math::BigFloat -> binf();
my $nan = Math::BigFloat -> bnan();
is(ref $zero, "Math::BigFloat", "Creating a 0 does not downgrade");
is(ref $half, "Math::BigFloat", "Creating a 0.5 does not downgrade");
is(ref $one, "Math::BigFloat", "Creating a 1 does not downgrade");
is(ref $four, "Math::BigFloat", "Creating a 4 does not downgrade");
is(ref $inf, "Math::BigFloat", "Creating an Inf does not downgrade");
is(ref $nan, "Math::BigFloat", "Creating a NaN does not downgrade");
################################################################################
# Verify that other methods downgrade when they should.
Math::BigFloat -> downgrade("Math::BigInt");
note("testing bneg()");
$x = $zero -> copy() -> bneg();
subtest '$x = $zero -> copy() -> bneg();' => sub {
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bneg();
subtest '$x = $inf -> copy() -> bneg();' => sub {
plan tests => 2;
cmp_ok($x, '==', '-inf', '-(Inf) = -Inf');
is(ref($x), 'Math::BigInt', '-(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> bneg();
subtest '$x = $nan -> copy() -> bneg();' => sub {
plan tests => 2;
is($x, 'NaN', '-(NaN) = NaN');
is(ref($x), 'Math::BigInt', '-(NaN) => Math::BigInt');
};
note("testing bnorm()");
$x = $zero -> copy() -> bnorm();
subtest '$x = $zero -> copy() -> bnorm();' => sub {
plan tests => 2;
cmp_ok($x, '==', 0, 'value of $x');
is(ref($x), 'Math::BigInt', 'bnorm(0) => Math::BigInt');
};
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bnorm();
subtest '$x = $inf -> copy() -> bnorm();' => sub {
plan tests => 2;
cmp_ok($x, '==', 'inf', 'value of $x');
is(ref($x), 'Math::BigInt', 'bnorm(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> bnorm();
subtest '$x = $nan -> copy() -> bnorm();' => sub {
plan tests => 2;
is($x, 'NaN', 'bnorm(NaN)');
is(ref($x), 'Math::BigInt', 'bnorm(NaN) => Math::BigInt');
};
note("testing binc()");
$x = $zero -> copy() -> binc();
subtest '$x = $zero -> copy() -> binc();' => sub {
plan tests => 2;
cmp_ok($x, '==', 1, 'binc(0)');
is(ref($x), 'Math::BigInt', 'binc(0) => Math::BigInt');
};
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> binc();
subtest '$x = $inf -> copy() -> binc();' => sub {
plan tests => 2;
cmp_ok($x, '==', 'inf', 'binc(Inf)');
is(ref($x), 'Math::BigInt', 'binc(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> binc();
subtest '$x = $nan -> copy() -> binc();' => sub {
plan tests => 2;
is($x, 'NaN', 'binc(NaN)');
is(ref($x), 'Math::BigInt', 'binc(NaN) => Math::BigInt');
};
note("testing bdec()");
$x = $zero -> copy() -> bdec();
subtest '$x = $zero -> copy() -> bdec();' => sub {
plan tests => 2;
cmp_ok($x, '==', -1, 'bdec(0)');
is(ref($x), 'Math::BigInt', 'bdec(0) => Math::BigInt');
};
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bdec();
subtest '$x = $inf -> copy() -> bdec();' => sub {
plan tests => 2;
cmp_ok($x, '==', 'inf', 'bdec(Inf)');
is(ref($x), 'Math::BigInt', 'bdec(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> bdec();
subtest '' => sub {
plan tests => 2;
is($x, 'NaN', 'bdec(NaN)');
is(ref($x), 'Math::BigInt', 'bdec(NaN) => Math::BigInt');
};
note("testing badd()");
$x = $half -> copy() -> badd($nan);
subtest '$x = $half -> copy() -> badd($nan);' => sub {
plan tests => 2;
is($x, 'NaN', '0.5 + NaN = NaN');
is(ref($x), 'Math::BigInt', '0.5 + NaN => Math::BigInt');
};
$x = $half -> copy() -> badd($inf);
subtest '$x = $half -> copy() -> badd($inf);' => sub {
plan tests => 2;
cmp_ok($x, '==', '+Inf', '0.5 + Inf = Inf');
is(ref($x), 'Math::BigInt', '2.5 + Inf => Math::BigInt');
};
$x = $half -> copy() -> badd($half);
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> badd($four);
subtest '$x = $inf -> copy() -> badd($four);' => sub {
plan tests => 2;
cmp_ok($x, '==', '+Inf', 'Inf + 4 = Inf');
is(ref($x), 'Math::BigInt', 'Inf + 4 => Math::BigInt');
};
$x = $nan -> copy() -> badd($four);
subtest '$x = $nan -> copy() -> badd($four);' => sub {
plan tests => 2;
is($x, 'NaN', 'NaN + 4 = NaN');
is(ref($x), 'Math::BigInt', 'NaN + 4 => Math::BigInt');
};
note("testing bsub()");
$x = $half -> copy() -> bsub($nan);
subtest '$x = $half -> copy() -> bsub($nan);' => sub {
plan tests => 2;
is($x, 'NaN', '0.5 - NaN = NaN');
is(ref($x), 'Math::BigInt', '0.5 - NaN => Math::BigInt');
};
$x = $half -> copy() -> bsub($inf);
subtest '$x = $half -> copy() -> bsub($inf);' => sub {
plan tests => 2;
cmp_ok($x, '==', '-Inf', '2.5 - Inf = -Inf');
is(ref($x), 'Math::BigInt', '2.5 - Inf => Math::BigInt');
};
$x = $half -> copy() -> bsub($half);
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bsub($four);
subtest '$x = $inf -> copy() -> bsub($four);' => sub {
plan tests => 2;
cmp_ok($x, '==', 'Inf', 'Inf - 4 = Inf');
is(ref($x), 'Math::BigInt', 'Inf - 4 => Math::BigInt');
};
$x = $nan -> copy() -> bsub($four);
subtest '$x = $nan -> copy() -> bsub($four);' => sub {
plan tests => 2;
is($x, 'NaN', 'NaN - 4 = NaN');
is(ref($x), 'Math::BigInt', 'NaN - 4 => Math::BigInt');
};
note("testing bmul()");
$x = $zero -> copy() -> bmul($four);
subtest '$x = $zero -> copy() -> bmul($four);' => sub {
plan tests => 2;
cmp_ok($x, '==', 0, 'bmul(0, 4) = 0');
is(ref($x), 'Math::BigInt', 'bmul(0, 4) => Math::BigInt');
};
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bmul($four);
subtest '$x = $inf -> copy() -> bmul($four);' => sub {
plan tests => 2;
cmp_ok($x, '==', 'inf', 'bmul(Inf, 4) = Inf');
is(ref($x), 'Math::BigInt', 'bmul(Inf, 4) => Math::BigInt');
};
$x = $nan -> copy() -> bmul($four);
subtest '$x = $nan -> copy() -> bmul($four);' => sub {
plan tests => 2;
is($x, 'NaN', 'bmul(NaN, 4) = NaN');
is(ref($x), 'Math::BigInt', 'bmul(NaN, 4) => Math::BigInt');
};
$x = $four -> copy() -> bmul('0.5');
subtest '' => sub {
plan tests => 2;
cmp_ok($x, '==', 2, 'bmul(4, 0.5) = 2');
is(ref($x), 'Math::BigInt', 'bmul(4, 0.5) => Math::BigInt');
};
note("testing bmuladd()");
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bmuladd($four, $four);
subtest '$x = $inf -> copy() -> bmuladd($four, $four);' => sub {
plan tests => 2;
cmp_ok($x, '==', 'inf', 'bmuladd(Inf, 4, 4) = Inf');
is(ref($x), 'Math::BigInt', 'bmuladd(Inf, 4, 4) => Math::BigInt');
};
$x = $nan -> copy() -> bmuladd($four, $four);
subtest '$x = $nan -> copy() -> bmuladd($four, $four);' => sub {
plan tests => 2;
is($x, 'NaN', 'bmuladd(NaN, 4, 4) = NaN');
is(ref($x), 'Math::BigInt', 'bmuladd(NaN, 4, 4) => Math::BigInt');
};
$x = $four -> copy() -> bmuladd("0.5", $four);
subtest '$x = $four -> copy() -> bmuladd("0.5", $four);' => sub {
plan tests => 2;
cmp_ok($x, '==', 6, 'bmuladd(4, 0.5, 4) = 6');
is(ref($x), 'Math::BigInt', 'bmuladd(4, 0.5, 4) => Math::BigInt');
};
note("testing bdiv()");
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bround();
subtest '$x = $inf -> copy() -> bround();' => sub {
plan tests => 2;
cmp_ok($x, '==', 'inf', 'bround(Inf)');
is(ref($x), 'Math::BigInt', 'bround(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> bround();
subtest '$x = $nan -> copy() -> bround();' => sub {
plan tests => 2;
is($x, 'NaN', 'bround(NaN)');
is(ref($x), 'Math::BigInt', 'bround(NaN) => Math::BigInt');
};
note("testing bfround()");
note("testing Add tests for rounding a non-integer to an integer. Fixme!");
$x = $zero -> copy() -> bfround();
subtest '$x = $zero -> copy() -> bfround();' => sub {
plan tests => 2;
cmp_ok($x, '==', 0, 'bfround(0)');
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bfround();
subtest '$x = $inf -> copy() -> bfround();' => sub {
plan tests => 2;
cmp_ok($x, '==', 'inf', 'bfround(Inf)');
is(ref($x), 'Math::BigInt', 'bfround(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> bfround();
subtest '$x = $nan -> copy() -> bfround();' => sub {
plan tests => 2;
is($x, 'NaN', 'bfround(NaN)');
is(ref($x), 'Math::BigInt', 'bfround(NaN) => Math::BigInt');
};
note("testing bfloor()");
$x = $half -> copy() -> bfloor();
subtest '$x = $half -> copy() -> bfloor();' => sub {
plan tests => 2;
cmp_ok($x, '==', 0, 'bfloor(0)');
is(ref($x), 'Math::BigInt', 'bfloor(0) => Math::BigInt');
};
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bfloor();
subtest '$x = $inf -> copy() -> bfloor();' => sub {
plan tests => 2;
cmp_ok($x, '==', 'Inf', 'bfloor(Inf)');
is(ref($x), 'Math::BigInt', 'bfloor(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> bfloor();
subtest '$x = $nan -> copy() -> bfloor();' => sub {
plan tests => 2;
is($x, 'NaN', 'bfloor(NaN)');
is(ref($x), 'Math::BigInt', 'bfloor(NaN) => Math::BigInt');
};
note("testing bceil()");
$x = $half -> copy() -> bceil();
subtest '$x = $half -> copy() -> bceil();' => sub {
plan tests => 2;
cmp_ok($x, '==', 1, 'bceil(0)');
is(ref($x), 'Math::BigInt', 'bceil(0) => Math::BigInt');
};
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bceil();
subtest '$x = $inf -> copy() -> bceil();' => sub {
plan tests => 2;
cmp_ok($x, '==', 'Inf', 'bceil(Inf)');
is(ref($x), 'Math::BigInt', 'bceil(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> bceil();
subtest '$x = $nan -> copy() -> bceil();' => sub {
plan tests => 2;
is($x, 'NaN', 'bceil(NaN)');
is(ref($x), 'Math::BigInt', 'bceil(NaN) => Math::BigInt');
};
note("testing bint()");
$x = $half -> copy() -> bint();
subtest '$x = $half -> copy() -> bint();' => sub {
plan tests => 2;
cmp_ok($x, '==', 0, 'bint(0)');
is(ref($x), 'Math::BigInt', 'bint(0) => Math::BigInt');
};
cpan/Math-BigInt/t/downgrade-mbi-mbf.t view on Meta::CPAN
$x = $inf -> copy() -> bint();
subtest '$x = $inf -> copy() -> bint();' => sub {
plan tests => 2;
cmp_ok($x, '==', 'Inf', 'bint(Inf)');
is(ref($x), 'Math::BigInt', 'bint(Inf) => Math::BigInt');
};
$x = $nan -> copy() -> bint();
subtest '$x = $nan -> copy() -> bint();' => sub {
plan tests => 2;
is($x, 'NaN', 'bint(NaN)');
is(ref($x), 'Math::BigInt', 'bint(NaN) => Math::BigInt');
};
note("testing bgcd()");
note("testing blcm()");
note("testing mantissa()");
note("testing exponent()");
cpan/Math-BigInt/t/downgrade-mbi-mbr.t view on Meta::CPAN
is(ref $x, "Math::BigInt", "Creating a 0 downgrades to Math::BigInt");
$x = Math::BigRat -> new("1");
cmp_ok($x, "==", 1, 'new("1")');
is(ref $x, "Math::BigInt", "Creating a 1 downgrades to Math::BigInt");
$x = Math::BigRat -> new("Inf");
cmp_ok($x, "==", "Inf", 'new("inf")');
is(ref $x, "Math::BigInt", "Creating an Inf downgrades to Math::BigInt");
$x = Math::BigRat -> new("NaN");
is($x, "NaN", 'new("NaN")');
is(ref $x, "Math::BigInt", "Creating a NaN downgrades to Math::BigInt");
# bzero()
$x = Math::BigRat -> bzero();
cmp_ok($x, "==", 0, "bzero()");
is(ref $x, "Math::BigInt", "Creating a 0 downgrades to Math::BigInt");
# bone()
$x = Math::BigRat -> bone();
cpan/Math-BigInt/t/downgrade-mbi-mbr.t view on Meta::CPAN
# binf()
$x = Math::BigRat -> binf();
cmp_ok($x, "==", "Inf", "binf()");
is(ref $x, "Math::BigInt", "Creating an Inf downgrades to Math::BigInt");
# bnan()
$x = Math::BigRat -> bnan();
is($x, "NaN", "bnan()");
is(ref $x, "Math::BigInt", "Creating a NaN downgrades to Math::BigInt");
# from_hex()
$x = Math::BigRat -> from_hex("13a");
cmp_ok($x, "==", 314, 'from_hex("13a")');
is(ref $x, "Math::BigInt", 'from_hex("13a") downgrades to Math::BigInt');
# from_oct()
$x = Math::BigRat -> from_oct("472");
cpan/Math-BigInt/t/downgrade-mbi-mbr.t view on Meta::CPAN
my $half = Math::BigRat -> new("1/2");
my $four = Math::BigRat -> new("4");
my $zero = Math::BigRat -> bzero();
my $inf = Math::BigRat -> binf();
my $nan = Math::BigRat -> bnan();
is(ref $half, "Math::BigRat", "Creating a 0.5 does not downgrade");
is(ref $four, "Math::BigRat", "Creating a 4 does not downgrade");
is(ref $zero, "Math::BigRat", "Creating a 0 does not downgrade");
is(ref $inf, "Math::BigRat", "Creating an Inf does not downgrade");
is(ref $nan, "Math::BigRat", "Creating a NaN does not downgrade");
################################################################################
# Verify that other methods downgrade when they should.
Math::BigRat -> downgrade("Math::BigInt");
note("bneg()");
$x = $zero -> copy() -> bneg();
cmp_ok($x, "==", 0, "-(0) = 0");
cpan/Math-BigInt/t/downgrade-mbi-mbr.t view on Meta::CPAN
$x = $four -> copy() -> bneg();
cmp_ok($x, "==", -4, "-(4) = -4");
is(ref($x), "Math::BigInt", "-(4) => Math::BigInt");
$x = $inf -> copy() -> bneg();
cmp_ok($x, "==", "-inf", "-(Inf) = -Inf");
is(ref($x), "Math::BigInt", "-(Inf) => Math::BigInt");
$x = $nan -> copy() -> bneg();
is($x, "NaN", "-(NaN) = NaN");
is(ref($x), "Math::BigInt", "-(NaN) => Math::BigInt");
note("bnorm()");
$x = $zero -> copy() -> bnorm();
cmp_ok($x, "==", 0, "bnorm(0)");
is(ref($x), "Math::BigInt", "bnorm(0) => Math::BigInt");
$x = $four -> copy() -> bnorm();
cmp_ok($x, "==", 4, "bnorm(4)");
is(ref($x), "Math::BigInt", "bnorm(4) => Math::BigInt");
$x = $inf -> copy() -> bnorm();
cmp_ok($x, "==", "inf", "bnorm(Inf)");
is(ref($x), "Math::BigInt", "bnorm(Inf) => Math::BigInt");
$x = $nan -> copy() -> bnorm();
is($x, "NaN", "bnorm(NaN)");
is(ref($x), "Math::BigInt", "bnorm(NaN) => Math::BigInt");
note("binc()");
$x = $zero -> copy() -> binc();
cmp_ok($x, "==", 1, "binc(0)");
is(ref($x), "Math::BigInt", "binc(0) => Math::BigInt");
$x = $four -> copy() -> binc();
cmp_ok($x, "==", 5, "binc(4)");
is(ref($x), "Math::BigInt", "binc(4) => Math::BigInt");
$x = $inf -> copy() -> binc();
cmp_ok($x, "==", "inf", "binc(Inf)");
is(ref($x), "Math::BigInt", "binc(Inf) => Math::BigInt");
$x = $nan -> copy() -> binc();
is($x, "NaN", "binc(NaN)");
is(ref($x), "Math::BigInt", "binc(NaN) => Math::BigInt");
note("bdec()");
$x = $zero -> copy() -> bdec();
cmp_ok($x, "==", -1, "bdec(0)");
is(ref($x), "Math::BigInt", "bdec(0) => Math::BigInt");
$x = $four -> copy() -> bdec();
cmp_ok($x, "==", 3, "bdec(4)");
is(ref($x), "Math::BigInt", "bdec(4) => Math::BigInt");
$x = $inf -> copy() -> bdec();
cmp_ok($x, "==", "inf", "bdec(Inf)");
is(ref($x), "Math::BigInt", "bdec(Inf) => Math::BigInt");
$x = $nan -> copy() -> bdec();
is($x, "NaN", "bdec(NaN)");
is(ref($x), "Math::BigInt", "bdec(NaN) => Math::BigInt");
note("badd()");
$x = $half -> copy() -> badd($nan);
is($x, "NaN", "0.5 + NaN = NaN");
is(ref($x), "Math::BigInt", "0.5 + NaN => Math::BigInt");
$x = $half -> copy() -> badd($inf);
cmp_ok($x, "==", "+Inf", "0.5 + Inf = Inf");
is(ref($x), "Math::BigInt", "2.5 + Inf => Math::BigInt");
$x = $half -> copy() -> badd($half);
cmp_ok($x, "==", 1, "0.5 + 0.5 = 1");
is(ref($x), "Math::BigInt", "0.5 + 0.5 => Math::BigInt");
$x = $half -> copy() -> badd($half -> copy() -> bneg());
cpan/Math-BigInt/t/downgrade-mbi-mbr.t view on Meta::CPAN
$x = $zero -> copy() -> badd($four);
cmp_ok($x, "==", 4, "0 + 4 = 4");
is(ref($x), "Math::BigInt", "0 + 4 => Math::BigInt");
$x = $inf -> copy() -> badd($four);
cmp_ok($x, "==", "+Inf", "Inf + 4 = Inf");
is(ref($x), "Math::BigInt", "Inf + 4 => Math::BigInt");
$x = $nan -> copy() -> badd($four);
is($x, "NaN", "NaN + 4 = NaN");
is(ref($x), "Math::BigInt", "NaN + 4 => Math::BigInt");
note("bsub()");
$x = $half -> copy() -> bsub($nan);
is($x, "NaN", "0.5 - NaN = NaN");
is(ref($x), "Math::BigInt", "0.5 - NaN => Math::BigInt");
$x = $half -> copy() -> bsub($inf);
cmp_ok($x, "==", "-Inf", "2.5 - Inf = -Inf");
is(ref($x), "Math::BigInt", "2.5 - Inf => Math::BigInt");
$x = $half -> copy() -> bsub($half);
cmp_ok($x, "==", 0, "0.5 - 0.5 = 0");
is(ref($x), "Math::BigInt", "0.5 - 0.5 => Math::BigInt");
$x = $half -> copy() -> bsub($half -> copy() -> bneg());
cpan/Math-BigInt/t/downgrade-mbi-mbr.t view on Meta::CPAN
$x = $zero -> copy() -> bsub($four);
cmp_ok($x, "==", -4, "0 - 4 = -4");
is(ref($x), "Math::BigInt", "0 - 4 => Math::BigInt");
$x = $inf -> copy() -> bsub($four);
cmp_ok($x, "==", "Inf", "Inf - 4 = Inf");
is(ref($x), "Math::BigInt", "Inf - 4 => Math::BigInt");
$x = $nan -> copy() -> bsub($four);
is($x, "NaN", "NaN - 4 = NaN");
is(ref($x), "Math::BigInt", "NaN - 4 => Math::BigInt");
note("bmul()");
$x = $zero -> copy() -> bmul($four);
cmp_ok($x, "==", 0, "bmul(0, 4) = 0");
is(ref($x), "Math::BigInt", "bmul(0, 4) => Math::BigInt");
$x = $four -> copy() -> bmul($four);
cmp_ok($x, "==", 16, "bmul(4, 4) = 16");
is(ref($x), "Math::BigInt", "bmul(4, 4) => Math::BigInt");
$x = $inf -> copy() -> bmul($four);
cmp_ok($x, "==", "inf", "bmul(Inf, 4) = Inf");
is(ref($x), "Math::BigInt", "bmul(Inf, 4) => Math::BigInt");
$x = $nan -> copy() -> bmul($four);
is($x, "NaN", "bmul(NaN, 4) = NaN");
is(ref($x), "Math::BigInt", "bmul(NaN, 4) => Math::BigInt");
$x = $four -> copy() -> bmul("0.5");
cmp_ok($x, "==", 2, "bmul(4, 0.5) = 2");
is(ref($x), "Math::BigInt", "bmul(4, 0.5) => Math::BigInt");
# bmuladd()
note("bdiv()");
note("bmod()");
cpan/Math-BigInt/t/downgrade-mbi-mbr.t view on Meta::CPAN
$x = $four -> copy() -> bround();
cmp_ok($x, "==", 4, "bround(4)");
is(ref($x), "Math::BigInt", "bround(4) => Math::BigInt");
$x = $inf -> copy() -> bround();
cmp_ok($x, "==", "inf", "bround(Inf)");
is(ref($x), "Math::BigInt", "bround(Inf) => Math::BigInt");
$x = $nan -> copy() -> bround();
is($x, "NaN", "bround(NaN)");
is(ref($x), "Math::BigInt", "bround(NaN) => Math::BigInt");
note("bfround()");
# Add tests for rounding a non-integer to an integer. Fixme!
$x = $zero -> copy() -> bfround();
cmp_ok($x, "==", 0, "bfround(0)");
is(ref($x), "Math::BigInt", "bfround(0) => Math::BigInt");
$x = $four -> copy() -> bfround();
cmp_ok($x, "==", 4, "bfround(4)");
is(ref($x), "Math::BigInt", "bfround(4) => Math::BigInt");
$x = $inf -> copy() -> bfround();
cmp_ok($x, "==", "inf", "bfround(Inf)");
is(ref($x), "Math::BigInt", "bfround(Inf) => Math::BigInt");
$x = $nan -> copy() -> bfround();
is($x, "NaN", "bfround(NaN)");
is(ref($x), "Math::BigInt", "bfround(NaN) => Math::BigInt");
note("bfloor()");
$x = $half -> copy() -> bfloor();
cmp_ok($x, "==", 0, "bfloor(0)");
is(ref($x), "Math::BigInt", "bfloor(0) => Math::BigInt");
$x = $inf -> copy() -> bfloor();
cmp_ok($x, "==", "Inf", "bfloor(Inf)");
is(ref($x), "Math::BigInt", "bfloor(Inf) => Math::BigInt");
$x = $nan -> copy() -> bfloor();
is($x, "NaN", "bfloor(NaN)");
is(ref($x), "Math::BigInt", "bfloor(NaN) => Math::BigInt");
note("bceil()");
$x = $half -> copy() -> bceil();
cmp_ok($x, "==", 1, "bceil(0)");
is(ref($x), "Math::BigInt", "bceil(0) => Math::BigInt");
$x = $inf -> copy() -> bceil();
cmp_ok($x, "==", "Inf", "bceil(Inf)");
is(ref($x), "Math::BigInt", "bceil(Inf) => Math::BigInt");
$x = $nan -> copy() -> bceil();
is($x, "NaN", "bceil(NaN)");
is(ref($x), "Math::BigInt", "bceil(NaN) => Math::BigInt");
note("bint()");
$x = $half -> copy() -> bint();
cmp_ok($x, "==", 0, "bint(0)");
is(ref($x), "Math::BigInt", "bint(0) => Math::BigInt");
$x = $inf -> copy() -> bint();
cmp_ok($x, "==", "Inf", "bint(Inf)");
is(ref($x), "Math::BigInt", "bint(Inf) => Math::BigInt");
$x = $nan -> copy() -> bint();
is($x, "NaN", "bint(NaN)");
is(ref($x), "Math::BigInt", "bint(NaN) => Math::BigInt");
note("bgcd()");
note("blcm()");
# mantissa() ?
# exponent() ?
# parts() ?
cpan/Math-BigInt/t/inf_nan.t view on Meta::CPAN
# -*- mode: perl; -*-
# test inf/NaN handling all in one place
use strict;
use warnings;
use lib 't';
use Test::More tests => 1044;
use Math::BigInt;
use Math::BigFloat;
use Math::BigInt::Subclass;
cpan/Math-BigInt/t/inf_nan.t view on Meta::CPAN
# +
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) {
$args[2] = '0' if $args[2] eq '-0'; # Math::Big* has no -0
$x = $class->new($args[0]);
$y = $class->new($args[1]);
$z = $x->badd($y);
cpan/Math-BigInt/t/inf_nan.t view on Meta::CPAN
is($y->bstr(), $args[1], 'value of $y');
is($z->bstr(), $args[2], 'value of $z');
};
}
}
# -
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) {
$args[2] = '0' if $args[2] eq '-0'; # Math::Big* has no -0
$x = $class->new($args[0]);
$y = $class->new($args[1]);
$z = $x->bsub($y);
cpan/Math-BigInt/t/inf_nan.t view on Meta::CPAN
};
}
}
# *
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) {
$args[2] = '0' if $args[2] eq '-0'; # Math::Big* has no -0
$x = $class->new($args[0]);
$y = $class->new($args[1]);
$z = $x->bmul($y);
cpan/Math-BigInt/t/inf_nan.t view on Meta::CPAN
is($y->bstr(), $args[1], 'value of $y');
is($z->bstr(), $args[2], 'value of $z');
};
}
}
# /
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) {
$args[2] = '0' if $args[2] eq '-0'; # Math::Big* has no -0
my ($q, $r);
# bdiv in scalar context
cpan/Math-BigInt/t/inf_nan.t view on Meta::CPAN
is($y->bstr(), $args[1], 'value of $y');
is($m->bstr(), $r->bstr(), 'value of $m');
};
}
}
# /
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) {
$args[2] = '0' if $args[2] eq '-0'; # Math::Big* has no -0
$x = $class->new($args[0]);
$y = $class->new($args[1]);
$z = $x->bdiv($y);
cpan/Math-BigInt/t/inf_nan.t view on Meta::CPAN
}
#############################################################################
# overloaded comparisons
foreach my $c (@biclasses, @bfclasses) {
$x = $c->bnan();
$y = $c->bnan(); # test with two different objects, too
$z = $c->bzero();
is($x == $y, '', 'NaN == NaN: ""');
is($x != $y, 1, 'NaN != NaN: 1');
is($x == $x, '', 'NaN == NaN: ""');
is($x != $x, 1, 'NaN != NaN: 1');
is($z != $x, 1, '0 != NaN: 1');
is($z == $x, '', '0 == NaN: ""');
is($z < $x, '', '0 < NaN: ""');
is($z <= $x, '', '0 <= NaN: ""');
is($z >= $x, '', '0 >= NaN: ""');
#is($z > $x, '', '0 > NaN: ""'); # Bug! Todo: fix it!
}
# All done.
cpan/Math-BigInt/t/mbimbf.inc view on Meta::CPAN
is($x, 123500, qq|\$x = $mbi->new(123456); \$x->precision(2)|);
###############################################################################
# test actual rounding via round()
$x = $mbf->new("123.456");
is($x->copy()->round(5), "123.46",
qq|\$x = $mbf->new("123.456"); \$x->copy()->round(5)|);
is($x->copy()->round(4), "123.5",
qq|\$x = $mbf->new("123.456"); \$x->copy()->round(4)|);
is($x->copy()->round(5, 2), "NaN",
qq|\$x = $mbf->new("123.456"); \$x->copy()->round(5, 2)|);
is($x->copy()->round(undef, -2), "123.46",
qq|\$x = $mbf->new("123.456"); \$x->copy()->round(undef, -2)|);
is($x->copy()->round(undef, 2), 120,
qq|\$x = $mbf->new("123.456"); \$x->copy()->round(undef, 2)|);
$x = $mbi->new("123");
is($x->round(5, 2), "NaN",
qq|\$x = $mbi->new("123"); \$x->round(5, 2)|);
$x = $mbf->new("123.45000");
is($x->copy()->round(undef, -1, "odd"), "123.5",
qq|\$x = $mbf->new("123.45000"); \$x->copy()->round(undef, -1, "odd")|);
# see if rounding is 'sticky'
$x = $mbf->new("123.4567");
$y = $x->copy()->bround(); # no-op since nowhere A or P defined
cpan/Math-BigInt/t/mbimbf.inc view on Meta::CPAN
$z = $u->copy()->bmul($y, undef, 3, 'odd');
is($z, 30900, q|$z = 30900|);
$z = $u->copy()->bmul($y, undef, -1, 'odd');
is($z, 30862.5, q|$z = 30862.5|);
my $warn = '';
$SIG{__WARN__} = sub { $warn = shift; };
# These should no longer warn, even though '3.17' is a NaN in Math::BigInt
# (>= returns now false, bug until v1.80).
$warn = '';
eval '$z = 3.17 <= $y';
is($z, '', q|$z = ""|);
unlike($warn, qr/^Use of uninitialized value (\$y )?(in numeric le \(<=\) |)at/,
q|"$z = $y >= 3.17" gives warning as expected|);
$warn = '';
eval '$z = $y >= 3.17';
cpan/Math-BigInt/t/mbimbf.inc view on Meta::CPAN
@params = $x->_find_round_parameters(2, -2, "+inf");
is(scalar(@params), 1, q|scalar(@params) = 1|); # error, A and P defined
is($params[0], $x, q|$params[0] = $x|); # self
{
no strict 'refs';
${"$mbi\::accuracy"} = 1;
@params = $x->_find_round_parameters(undef, -2);
is(scalar(@params), 1, q|scalar(@params) = 1|); # error, A and P defined
is($params[0], $x, q|$params[0] = $x|); # self
is($x->is_nan(), 1, q|$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, q|scalar(@params) = 1|); # error, A and P defined
is($params[0], $x, q|$params[0] = $x|); # self
is($x->is_nan(), 1, q|$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 $class ($mbi, $mbf) {
$x = $class->new(2)->bzero();
is($x->{accuracy}, undef, qq|\$x = $class->new(2)->bzero(); \$x->{accuracy}|);
cpan/Math-BigInt/t/mbimbf.inc view on Meta::CPAN
$x = $class->new(2, undef, undef);
is($x->precision(), undef,
qq|$class->precision(-2); \$x = $class->new(2, undef, undef); \$x->precision()|);
}
$class->precision(undef); # reset
}
###############################################################################
# check whether mixing A and P creates a NaN
# new with set accuracy/precision and with parameters
{
no strict 'refs';
foreach my $class ($mbi, $mbf) {
is($class->new(123, 4, -3), 'NaN', # with parameters
"mixing A and P creates a NaN");
${"$class\::accuracy"} = 42;
${"$class\::precision"} = 2;
is($class->new(123), "NaN", # with globals
q|$class->new(123) = "NaN"|);
${"$class\::accuracy"} = undef;
${"$class\::precision"} = undef;
}
}
# binary ops
foreach my $class ($mbi, $mbf) {
#foreach (qw/add sub mul div pow mod/) {
foreach my $method (qw/add sub mul pow mod/) {
my $try = "my \$x = $class->new(1234); \$x->accuracy(5);";
$try .= " my \$y = $class->new(12); \$y->precision(-3);";
$try .= " \$x->b$method(\$y);";
$rc = eval $try;
is($rc, "NaN", $try);
}
}
# unary ops
foreach my $method (qw/new bsqrt/) {
my $try = "my \$x = $mbi->$method(1234, 5, -3);";
$rc = eval $try;
is($rc, "NaN", $try);
}
# 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, q|$x = 120|);
$class->accuracy(undef); # reset
cpan/Math-BigInt/t/mbimbf.inc view on Meta::CPAN
sub is_valid {
my ($x, $f) = @_;
my $e = 0; # error?
# ok as reference?
$e = 'Not a reference' if !ref($x);
# has ok sign?
$e = qq|Illegal sign $x->{sign}|
. q| (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 = $LIB->_check($x->{value}) if $e eq '0';
# test done, see if error did crop up
if ($e eq '0') {
pass('is a valid object');
return;
}
cpan/Math-BigInt/t/nan_cmp.t view on Meta::CPAN
# -*- mode: perl; -*-
# test that overloaded compare works when NaN are involved
use strict;
use warnings;
use Test::More tests => 26;
use Math::BigInt;
use Math::BigFloat;
compare('Math::BigInt');
cpan/Math-BigInt/t/trap.t view on Meta::CPAN
###########################################################################
# Default values.
###########################################################################
# defaults are okay?
is($class->config("trap_nan"), 0, 'trap_nan defaults to 0');
is($class->config("trap_inf"), 0, 'trap_inf defaults to 0');
###########################################################################
# Trap NaN.
###########################################################################
# can set?
$class->config( trap_nan => 1 );
is($class->config("trap_nan"), 1, qq|$class->config( trap_nan => 1 );|);
# can reset?
$class->config( trap_nan => 0 );
is($class->config("trap_nan"), 0, qq|$class->config( trap_nan => 0 );|);
# can set via hash ref?
$class->config( { trap_nan => 1 } );
is($class->config("trap_nan"), 1, qq|$class->config( { trap_nan => 1 } );|);
# 0/0 => NaN
$x = $class->new("0");
eval { $x->bdiv(0); };
like($@, qr/^Tried to /, qq|\$x = $class->new("0"); \$x->bdiv(0);|);
# new() didn't modify $x
is($x, 0, qq|\$x = $class->new("0"); \$x->bdiv(0);|);
# also test that new() still works normally
eval { $x = $class->new('42'); $x->bnan(); };
like($@, qr/^Tried to /, 'died');
cpan/Math-BigInt/t/trap.t view on Meta::CPAN
eval { $x = $class->new('4711'); $x->bdiv(0); };
like($@, qr/^Tried to /, 'died');
is($x, 4711, '$x after new() never modified');
# -$x/0 => -inf
eval { $x = $class->new('-0815'); $x->bdiv(0); };
like($@, qr/^Tried to /, 'died');
is($x, '-815', '$x after new not modified');
$class->config( trap_nan => 1 );
# 0/0 => NaN
eval { $x = $class->new('0'); $x->bdiv(0); };
like($@, qr/^Tried to /, 'died');
is($x, '0', '$x after new not modified');
}
##############################################################################
# Math::BigInt
$x = Math::BigInt->new(2);
eval { $x = $mbi->new('0.1'); };