Commit 4483dcf3 authored by Lucas Kanashiro's avatar Lucas Kanashiro

Imported Upstream version 1.999704

parent 51e3bd98
File mode changed from 100644 to 100755
BUGS 100644 → 100755
File mode changed from 100644 to 100755
......@@ -1418,6 +1418,21 @@ The changes are now grouped by distribution.
* Add test files t/blog-mbf.t and t/blog-mbi.t for better testing of the
blog() methods.
2015-09-25 v1.999704 pjacklam
* objectify() in lib/Math/BigInt.pm now uses 'isa' not 'eq' to check object
relationship. This is necessary for correct handling of subclasses.
* objectify() in lib/Math/BigInt.pm no longer expects as_int(), as_number()
and as_float() to return objects, but allows them to return numbers
formatted as strings. This is used by some other modules on CPAN.
* Better documentation of as_int() and as_number() in lib/Math/BigInt.pm.
* Add documentation for as_float() in lib/Math/BigFloat.pm
* Added test files t/objectify_mbf.t and t/objectify_mbi.t.
Please send us test-reports, your experiences with this and your ideas - we love
to hear about our work!
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -65,6 +65,8 @@ t/mbimbf.inc
t/mbimbf.t
t/nan_cmp.t
t/new_overloaded.t
t/objectify_mbf.t
t/objectify_mbi.t
t/req_mbf0.t
t/req_mbf1.t
t/req_mbfa.t
......
File mode changed from 100644 to 100755
......@@ -38,6 +38,6 @@
}
},
"release_status" : "stable",
"version" : "1.999703",
"version" : "1.999704",
"x_serialization_backend" : "JSON::PP version 2.27300"
}
......@@ -20,5 +20,5 @@ no_index:
- inc
requires:
perl: '5.006002'
version: '1.999703'
version: '1.999704'
x_serialization_backend: 'CPAN::Meta::YAML version 0.016'
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -16,16 +16,16 @@ Hash: SHA1
SHA1 f6ef98e8f3f6a4f670d569f279ef5a3c05c16d0c BENCHMARK
SHA1 d681b992c9e027633a59fbd52530805ee4e1621c BUGS
SHA1 6b566f5d8c537a75d173e1845d330a020b06ddab CHANGES
SHA1 8ea50abf4bd86783767a277d9578f06e78fc7e56 CHANGES
SHA1 f27c8bd98e754f96c9825fa6ce9f1244c93bdbe6 CREDITS
SHA1 bc2db74538d3acd53f71b5512a144fe18c253ecd GOALS
SHA1 fe61299e80a1dca48fbaa33f0869e3238ec07c70 HISTORY
SHA1 c42565ad26cdf91502bacf19a4f7080e63c51fe2 INSTALL
SHA1 d6a6c30ee6d9ba6b9afab8bbf6a25e1b23c744e0 LICENSE
SHA1 8f6a77cdcfede79a3fcf27549b2b8739eaefbfe8 MANIFEST
SHA1 183f44987a5dfba10182b1b89f1f92647e388a5f MANIFEST
SHA1 86df44fbe1ea7a68897113f0e327d0f0c25b91e3 MANIFEST.SKIP
SHA1 43cf33ae05d036694ebfe2987e31217140278c9e META.json
SHA1 3cec127e47b24f6b3b0a21c8a6fc67aec2f3916a META.yml
SHA1 27f71484021cedc3ac0c8d0776279f88610ba004 META.json
SHA1 926b03441e37ae13d3dc24a081fda22d2990b7c5 META.yml
SHA1 ae1e2767f1988a537d4a64fc84a07587cbca2ef5 Makefile.PL
SHA1 4e2191d1724e699862efc5350f480c8e02755112 NEW
SHA1 481f5827752d2100914db1eaeb60bf0bbd13529e README
......@@ -41,10 +41,10 @@ SHA1 76efdca3603159e0ae0e18f19fe72a0211a69529 inc/Module/Install/Makefile.pm
SHA1 2e33e87882d60db3913da6284dd5295e5315e18a inc/Module/Install/Metadata.pm
SHA1 c830b819e61bda5eca077c6291293bba61b3b9f2 inc/Module/Install/Win32.pm
SHA1 cb52b9d6f88d512d448a6f35ed0af0d1201a134b inc/Module/Install/WriteAll.pm
SHA1 d69e58dac458d7bd76c7fb6182d50e7005fcca6d lib/Math/BigFloat.pm
SHA1 7bc1a3b537261121d93d275431bd86c45c0c6e5e lib/Math/BigInt.pm
SHA1 92b2144f0032d651cf1e7d3b37a82b01010febac lib/Math/BigInt/Calc.pm
SHA1 193c0c6d9b83e783b8071b8bbeee3f5bc0dcfddd lib/Math/BigInt/CalcEmu.pm
SHA1 2b4a6499b7f55d6bb33cb942bdf83853d094b404 lib/Math/BigFloat.pm
SHA1 b11a534087024e20f724475d2648d259fc03eb1b lib/Math/BigInt.pm
SHA1 964d54429022e659b55e1b236041bccaa0fc74ba lib/Math/BigInt/Calc.pm
SHA1 70d46d848ba40caa86372df20efd3fac47d0e29c lib/Math/BigInt/CalcEmu.pm
SHA1 385cc7ed3bf3e6290640b9880445ca9a3dea629e t/00sig.t
SHA1 4463fdcebb8421cc2e63f87d678adb47856bf58c t/01load.t
SHA1 b2ed9206ca5b69ec3b8bbf850bf4b6dfde518e7e t/02pod.t
......@@ -84,6 +84,8 @@ SHA1 38d000d99cb2492ab518786e62ab1fae710e0f8d t/mbimbf.inc
SHA1 b13701babf6c3952a0b8de3803de838d52a13ddc t/mbimbf.t
SHA1 a8e152beb9c0583cca8ba397ca2952cf456f107c t/nan_cmp.t
SHA1 852bfc6c25fa982e0c48e270277f091732d062e2 t/new_overloaded.t
SHA1 7b8ad0d8e8bb1afb985d3af119bcd284a2ae105f t/objectify_mbf.t
SHA1 2a4e13c9a6a4f1c155d223e29b162e95c2722198 t/objectify_mbi.t
SHA1 4c28c03a8e373c03a0246548394588855e4173b9 t/req_mbf0.t
SHA1 1317f0500ac902cff77d78ea92b2b72dbf188331 t/req_mbf1.t
SHA1 4ce690af449cd6382b1372d4bacacb2781e91574 t/req_mbfa.t
......@@ -112,11 +114,11 @@ SHA1 34e18efc281d6538ba91742afa82d50c3b4434ad t/with_sub.t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
iQEcBAEBAgAGBQJWAEP5AAoJEGcmPl2fr166vegH/0Qm1Z/66sNEhc2jGhRjhlVv
aC2Y76G29bA7TkYhvVNxKZQ0imPBsLINr84jvAPynuwzdKa13EJX46sA1MUW3fmp
ciT4BNoAqv48WUmukh67cm2p/lwNsmA4h4Tz8u7jgDH2r4Ilsh1pUZg6eKTUHLtJ
3Hg3EYAqHuJKtrZQLzECmPpArNDYbHhcQbVI/mhqyIvKTne+qWDbxcvu9YKsAFHp
XwwgOH+PVmqsFiosM6W9FeR8bLnHmTntFFvKKk+Wns4Qo2y0Gxxcq5KwxNgfgoz2
dzWZtdzPqpXEWeqCEDYOMYC/lWu/fCcVamZfyBhTHaTubvari63DsB2hViwSZRc=
=G3gV
iQEcBAEBAgAGBQJWBUoXAAoJEGcmPl2fr166VgEIANnEsYBpwPSRz2gQhWKYyzAs
2c23b3RmyMA3VmnapfjRCpIZkFEmqHo0Pl/QwDBlNN00RN00cZHdwJ9s8dhNSZyJ
rE+Xdi0kc/I6D4EVXSf25AqH7VymZRoJzkX+IPqj7xFw7HmdIKraMwXY7CLsN+Fm
WhcnX6Pbr6D5+C6tNX6yLmjZyTnl7CaqO5Beq49OH23nHTzY2247rowumoH1PpTu
XIOArzhazregnPcb4sV6boqnZtFwosOGZmr8cWT82HGGyFhYhh1rokpYr/08RecI
RBdipu8M2qsbsKTHl0mAhc0AYXp0p8gZW1zGnMX55JH21h9uXZlJw3iLnhYvGTY=
=w70R
-----END PGP SIGNATURE-----
TODO 100644 → 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -12,7 +12,7 @@ package Math::BigFloat;
# _a : accuracy
# _p : precision
$VERSION = '1.999703';
$VERSION = '1.999704';
require 5.006002;
require Exporter;
......@@ -945,17 +945,17 @@ sub blog
if ($done) {
if ($fallback) {
# clear a/p after round, since user did not request it
# clear a/p after round, since user did not request it
delete $x->{_a};
delete $x->{_p};
}
}
return $x;
}
}
# when user set globals, they would interfere with our calculation, so
# disable them and later re-enable them
no strict 'refs';
my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
my $abr = "$self\::accuracy"; my $ab = $$abr; $$abr = undef;
my $pbr = "$self\::precision"; my $pb = $$pbr; $$pbr = undef;
# we also need to disable any set A or P on $x (_find_round_parameters took
# them already into account), since these would interfere, too
......@@ -971,7 +971,7 @@ sub blog
$x = Math::BigFloat->new($x);
$self = ref($x);
}
$done = 0;
# If the base is defined and an integer, try to calculate integer result
......@@ -4435,6 +4435,24 @@ Multiply $x by $y, and then add $z to the result.
This method was added in v1.87 of Math::BigInt (June 2007).
=item as_float()
This method is called when Math::BigFloat encounters an object it doesn't know
how to handle. For instance, assume $x is a Math::BigFloat, or subclass
thereof, and $y is defined, but not a Math::BigFloat, or subclass thereof. If
you do
$x -> badd($y);
$y needs to be converted into an object that $x can deal with. This is done by
first checking if $y is something that $x might be upgraded to. If that is the
case, no further attempts are made. The next is to see if $y supports the
method C<as_float()>. The method C<as_float()> is expected to return either an
object that has the same class as $x, a subclass thereof, or a string that
C<ref($x)-E<gt>new()> can parse to create an object.
In Math::BigFloat, C<as_float()> has the same effect as C<copy()>.
=back
=head1 Autocreating constants
......
......@@ -18,7 +18,7 @@ package Math::BigInt;
my $class = "Math::BigInt";
use 5.006002;
$VERSION = '1.999703';
$VERSION = '1.999704';
@ISA = qw(Exporter);
@EXPORT_OK = qw(objectify bgcd blcm);
......@@ -2826,9 +2826,7 @@ sub objectify {
# If it is an object of the right class, all is fine.
if ($ref -> isa($a[0])) {
next;
}
next if $ref -> isa($a[0]);
# Upgrading is OK, so skip further tests if the argument is upgraded.
......@@ -2836,27 +2834,44 @@ sub objectify {
next;
}
# If we want a Math::BigInt, see if the object can become one.
# Support the old misnomer as_number().
# See if we can call one of the as_xxx() methods. We don't know whether
# the as_xxx() method returns an object or a scalar, so re-check
# afterwards.
my $recheck = 0;
if ($a[0] eq 'Math::BigInt') {
if ($a[0] -> isa('Math::BigInt')) {
if ($a[$i] -> can('as_int')) {
$a[$i] = $a[$i] -> as_int();
next;
}
if ($a[$i] -> can('as_number')) {
$recheck = 1;
} elsif ($a[$i] -> can('as_number')) {
$a[$i] = $a[$i] -> as_number();
next;
$recheck = 1;
}
}
# If we want a Math::BigFloat, see if the object can become one.
if ($a[0] eq 'Math::BigFloat') {
elsif ($a[0] -> isa('Math::BigFloat')) {
if ($a[$i] -> can('as_float')) {
$a[$i] = $a[$i] -> as_float();
$recheck = $1;
}
}
# If we called one of the as_xxx() methods, recheck.
if ($recheck) {
$ref = ref($a[$i]);
# Perl scalars are fed to the appropriate constructor.
unless ($ref) {
$a[$i] = $a[0] -> new($a[$i]);
next;
}
# If it is an object of the right class, all is fine.
next if $ref -> isa($a[0]);
}
# Last resort.
......@@ -4351,15 +4366,29 @@ Return the signed mantissa of $x as BigInt.
$x->copy(); # make a true copy of $x (unlike $y = $x;)
=item as_int()/as_number()
=item as_int()
=item as_number()
These methods are called when Math::BigInt encounters an object it doesn't know
how to handle. For instance, assume $x is a Math::BigInt, or subclass thereof,
and $y is defined, but not a Math::BigInt, or subclass thereof. If you do
$x->as_int();
$x -> badd($y);
Returns $x as a BigInt (truncated towards zero). In BigInt this is the same as
C<copy()>.
$y needs to be converted into an object that $x can deal with. This is done by
first checking if $y is something that $x might be upgraded to. If that is the
case, no further attempts are made. The next is to see if $y supports the
method C<as_int()>. If it does, C<as_int()> is called, but if it doesn't, the
next thing is to see if $y supports the method C<as_number()>. If it does,
C<as_number()> is called. The method C<as_int()> (and C<as_number()>) is
expected to return either an object that has the same class as $x, a subclass
thereof, or a string that C<ref($x)-E<gt>new()> can parse to create an object.
C<as_number()> is an alias to this method. C<as_number> was introduced in
v1.22, while C<as_int()> was only introduced in v1.68.
C<as_number()> is an alias to C<as_int()>. C<as_number> was introduced in
v1.22, while C<as_int()> was introduced in v1.68.
In Math::BigInt, C<as_int()> has the same effect as C<copy()>.
=item bstr()
......@@ -4390,7 +4419,7 @@ Returns a normalized string representation of C<$x>.
This returns a normal Perl scalar from $x. It is used automatically
whenever a scalar is needed, for instance in array index operations.
This loses precision, to avoid this use L<as_int()|/"as_int()/as_number()"> instead.
This loses precision, to avoid this use L</as_int()> instead.
=item modify()
......@@ -5415,43 +5444,34 @@ This makes a copy of $x and takes O(N), but $x->bneg() is O(1).
=item Mixing different object types
In Perl you will get a floating point value if you do one of the following:
$float = 5.0 + 2;
$float = 2 + 5.0;
$float = 5 / 2;
With overloaded math, only the first two variants will result in a BigFloat:
With overloaded operators, it is the first (dominating) operand that determines
which method is called. Here are some examples showing what actually gets
called in various cases.
use Math::BigInt;
use Math::BigFloat;
$mbf = Math::BigFloat->new(5);
$mbi2 = Math::BigInteger->new(5);
$mbi = Math::BigInteger->new(2);
$mbf = Math::BigFloat->new(5);
$mbi2 = Math::BigInt->new(5);
$mbi = Math::BigInt->new(2);
# what actually gets called:
$float = $mbf + $mbi; # $mbf->badd()
$float = $mbf / $mbi; # $mbf->bdiv()
$integer = $mbi + $mbf; # $mbi->badd()
$integer = $mbi2 / $mbi; # $mbi2->bdiv()
$integer = $mbi2 / $mbf; # $mbi2->bdiv()
This is because math with overloaded operators follows the first (dominating)
operand, and the operation of that is called and returns thus the result. So,
Math::BigInt::bdiv() will always return a Math::BigInt, regardless whether
the result should be a Math::BigFloat or the second operant is one.
$float = $mbf + $mbi; # $mbf->badd($mbi)
$float = $mbf / $mbi; # $mbf->bdiv($mbi)
$integer = $mbi + $mbf; # $mbi->badd($mbf)
$integer = $mbi2 / $mbi; # $mbi2->bdiv($mbi)
$integer = $mbi2 / $mbf; # $mbi2->bdiv($mbf)
To get a Math::BigFloat you either need to call the operation manually,
make sure the operands are already of the proper type or casted to that type
via Math::BigFloat->new():
For instance, Math::BigInt->bdiv() will always return a Math::BigInt, regardless of
whether the second operant is a Math::BigFloat. To get a Math::BigFloat you
either need to call the operation manually, make sure each operand already is a
Math::BigFloat, or cast to that type via Math::BigFloat->new():
$float = Math::BigFloat->new($mbi2) / $mbi; # = 2.5
Beware of simple "casting" the entire expression, this would only convert
the already computed result:
Beware of casting the entire expression, as this would cast the
result, at which point it is too late:
$float = Math::BigFloat->new($mbi2 / $mbi); # = 2.0 thus wrong!
$float = Math::BigFloat->new($mbi2 / $mbi); # = 2
Beware also of the order of more complicated expressions like:
......
......@@ -4,7 +4,7 @@ use 5.006002;
use strict;
# use warnings; # do not use warnings for older Perls
our $VERSION = '1.999703';
our $VERSION = '1.999704';
# Package to store unsigned big integers in decimal and do math with them
......
......@@ -5,7 +5,7 @@ use strict;
# use warnings; # do not use warnings for older Perls
use vars qw/$VERSION/;
$VERSION = '1.999703';
$VERSION = '1.999704';
package Math::BigInt;
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
#!perl
#
# Verify that objectify() is able to convert a "foreign" object into what we
# want, when what we want is Math::BigFloat or subclass thereof.
use strict;
use warnings;
package main;
use Test::More tests => 6;
use Math::BigFloat;
###############################################################################
for my $class ('Math::BigFloat', 'Math::BigFloat::Subclass') {
# This object defines what we want.
my $float = $class -> new(10);
# Create various objects that should work with the object above after
# objectify() has done its thing.
my $float_percent1 = My::Percent::Float1 -> new(100);
is($float * $float_percent1, 10);
my $float_percent2 = My::Percent::Float2 -> new(100);
is($float * $float_percent2, 10);
my $float_percent3 = My::Percent::Float3 -> new(100);
is($float * $float_percent3, 10);
}
###############################################################################
# Class supports as_float(), which returns a Math::BigFloat.
package My::Percent::Float1;
sub new {
my $class = shift;
my $num = shift;
return bless \$num, $class;
}
sub as_float {
my $self = shift;
return Math::BigFloat -> new($$self / 100);
}
###############################################################################
# Class supports as_float(), which returns a scalar.
package My::Percent::Float2;
sub new {
my $class = shift;
my $num = shift;
return bless \$num, $class;
}
sub as_float {
my $self = shift;
return $$self / 100;
}
###############################################################################
# Class does not support as_float().
package My::Percent::Float3;
use overload '""' => sub { $_[0] -> as_string(); };
sub new {
my $class = shift;
my $num = shift;
return bless \$num, $class;
}
sub as_string {
my $self = shift;
return $$self / 100;
}
###############################################################################
package Math::BigFloat::Subclass;
use base 'Math::BigFloat';
#!perl
#
# Verify that objectify() is able to convert a "foreign" object into what we
# want, when what we want is Math::BigInt or subclass thereof.
use strict;
use warnings;
package main;
use Test::More tests => 10;
use Math::BigInt;
###############################################################################
for my $class ('Math::BigInt', 'Math::BigInt::Subclass') {
# This object defines what we want.
my $int = $class -> new(10);
# Create various objects that should work with the object above after
# objectify() has done its thing.
my $int_percent1 = My::Percent::Int1 -> new(100);
is($int * $int_percent1, 10);
my $int_percent2 = My::Percent::Int2 -> new(100);
is($int * $int_percent2, 10);
my $int_percent3 = My::Percent::Int3 -> new(100);
is($int * $int_percent3, 10);
my $int_percent4 = My::Percent::Int4 -> new(100);
is($int * $int_percent4, 10);
my $int_percent5 = My::Percent::Int5 -> new(100);
is($int * $int_percent5, 10);
}
###############################################################################
# Class supports as_int(), which returns a Math::BigInt.
package My::Percent::Int1;
sub new {
my $class = shift;
my $num = shift;
return bless \$num, $class;
}
sub as_int {
my $self = shift;
return Math::BigInt -> new($$self / 100);
}
###############################################################################
# Class supports as_int(), which returns a scalar.
package My::Percent::Int2;
sub new {
my $class = shift;
my $num = shift;
return bless \$num, $class;
}
sub as_int {
my $self = shift;
return $$self / 100;
}
###############################################################################
# Class does not support as_int(), but supports as_number(), which returns a
# Math::BigInt.
package My::Percent::Int3;
sub new {
my $class = shift;
my $num = shift;
return bless \$num, $class;
}
sub as_number {
my $self = shift;
return Math::BigInt -> new($$self / 100);
}
###############################################################################
# Class does not support as_int(), but supports as_number(), which returns a
# scalar.
package My::Percent::Int4;
sub new {
my $class = shift;
my $num = shift;
return bless \$num, $class;
}
sub as_number {
my $self = shift;
return $$self / 100;
}
###############################################################################
# Class supports neither as_int() or as_number().
package My::Percent::Int5;
use overload '""' => sub { $_[0] -> as_string(); };
sub new {
my $class = shift;
my $num = shift;
return bless \$num, $class;
}
sub as_string {
my $self = shift;
return $$self / 100;
}
###############################################################################
package Math::BigInt::Subclass;
use base 'Math::BigInt';
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment