Math-ModInt
view release on metacpan or search on metacpan
lib/Math/ModInt.pm view on Meta::CPAN
my $that;
if (ref $this) {
$that = $this->_NEW($int);
$modulus = $this->modulus;
}
else {
$that = $this->_NEW($int, $modulus);
}
my $quot = ($int - $that->residue) / $modulus;
return ($quot, $that);
}
# ----- public methods -----
# constructors
sub mod {
my ($int, $modulus) = @_;
my $class = _load(_best_class($modulus));
return $class->_NEW($int, $modulus);
}
sub divmod {
my ($int, $modulus) = @_;
my $class = _load(_best_class($modulus));
return $class->_NEW2($int, $modulus);
}
sub qmod {
my ($rat, $modulus) = @_;
my $class = _load(_best_class($modulus));
my $num = $class->_NEW($rat->numerator, $modulus);
my $den = $num->_NEW($rat->denominator);
return $num / $den;
}
sub new {
my ($this, $int, $modulus) = @_;
return $this->_NEW($int) if ref $this;
return mod($int, $modulus);
}
sub new2 {
my ($this, $int, $modulus) = @_;
return $this->_NEW2($int) if ref $this;
return divmod($int, $modulus);
}
sub undefined {
UndefinedResult->raise;
return $undefined;
}
# accessors
sub residue {
Nonexistent->raise('undefined residue');
}
sub modulus {
return 0 if __PACKAGE__ eq (caller)[0]; # special case for _oadd etc.
Nonexistent->raise('undefined modulus');
}
sub signed_residue {
my ($this) = @_;
my $r = $this->residue;
my $m = $this->modulus;
my $n = $m - $r;
return $n <= $r? -$n: $r;
}
sub centered_residue {
my ($this) = @_;
my $r = $this->residue;
my $m = $this->modulus;
my $n = $m - $r;
return $n < $r? -$n: $r;
}
sub is_defined {
my ($this) = @_;
return ref $undefined ne ref $this;
}
sub is_undefined {
my ($this) = @_;
return ref $undefined eq ref $this;
}
sub is_zero {
my ($this) = @_;
return 0 == $this->residue;
}
sub is_not_zero {
my ($this) = @_;
return 0 != $this->residue;
}
sub as_string {
my ($this) = @_;
my ($r, $mod) =
$this->is_defined? ($this->residue, $this->modulus): qw(? ?);
return "mod($r, $mod)";
}
# operators
sub inverse { $_[0]->_INV }
BEGIN {
foreach my $method (qw(
_NEW _NEG _INV _ADD _SUB _MUL _DIV _POW
)) {
no strict 'refs';
*{$method} = sub { $undefined };
}
}
# miscellaneous
( run in 2.164 seconds using v1.01-cache-2.11-cpan-97f6503c9c8 )