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

#!/usr/bin/perl use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump); use bigint; say STDERR "AAAA ", (1 % 0); # Should fail say STDERR "BBBB ", dump(1 % 0); if (1) {use Math::BigInt; my $a = Math::BigInt::new(1); my $b = Math::BigInt::new(0); my $c = $a->bmod($b); say STDERR "CCCC ", dump($c); } # AAAA 1 # BBBB bless({ _a => undef, _p => undef, sign => "+", value => [1] }, +"Math::BigInt") # CCCC bless({ sign => NaN, value => [0] }, "Math::BigInt")

I think line AAAA should complain or return undef, not 1 to be consistent with the results at BBBB and CCCC. If this is not consistent, then please tell me where can I report this as a problem?

Replies are listed 'Best First'.
Re: Test modulus zero in bigint
by philiprbrenan (Monk) on Sep 11, 2016 at 23:15 UTC
    use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump); use bigint; say STDERR "AAAA ", (1 % 0); # Should fail say STDERR "BBBB ", dump(1 % 0); if (1) {use Math::BigInt; my $a = Math::BigInt::new(1); my $b = Math::BigInt::new(0); my $c = $a->bmod($b); say STDERR "CCCC ", $c; say STDERR "DDDD ", dump($c); } # AAAA 1 # BBBB bless({ _a => undef, _p => undef, sign => "+", value => [1] }, +"Math::BigInt") # CCCC NaN # DDDD bless({ sign => NaN, value => [0] }, "Math::BigInt")

    The crucial diffrence is between AAAA and CCCC.

      my $a = Math::BigInt::new(1);

      Thats not how you create objects, you're supposed to write  my $x = Math::BigInt->new(1);

        Makes sense to me :) kinda

        perl -Mbignum -E " for $x( -1,0,1){ $y = 0; sub F{say $x % $y} F; $x +->bdiv; F;} " -1 -inf 0 NaN 1 inf

        The bdiv seems to force/convert the numeric representation to stringy value via binf/bnan

        Its a bit odd but details of division by zero are in comments https://metacpan.org/source/PJACKLAM/Math-BigInt-1.999726/lib/Math/BigInt.pm

        # Divide by zero and modulo zero. # # Division: Use the common convention that x / 0 is inf with the s +ame 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 (mo +d 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