1N/A###############################################################################
1N/A# core math lib for BigInt, representing big numbers by normal int/float's
1N/A# for testing only, will fail any bignum test if range is exceeded
1N/A
1N/Apackage Math::BigInt::Scalar;
1N/A
1N/Ause 5.005;
1N/Ause strict;
1N/A# use warnings; # dont use warnings for older Perls
1N/A
1N/Arequire Exporter;
1N/A
1N/Ause vars qw/@ISA $VERSION/;
1N/A@ISA = qw(Exporter);
1N/A
1N/A$VERSION = '0.12';
1N/A
1N/Asub api_version() { 1; }
1N/A
1N/A##############################################################################
1N/A# global constants, flags and accessory
1N/A
1N/A# constants for easier life
1N/Amy $nan = 'NaN';
1N/A
1N/A##############################################################################
1N/A# create objects from various representations
1N/A
1N/Asub _new
1N/A {
1N/A # create scalar ref from string
1N/A my $d = $_[1];
1N/A my $x = $d; # make copy
1N/A \$x;
1N/A }
1N/A
1N/Asub _from_hex
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _from_bin
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _zero
1N/A {
1N/A my $x = 0; \$x;
1N/A }
1N/A
1N/Asub _one
1N/A {
1N/A my $x = 1; \$x;
1N/A }
1N/A
1N/Asub _two
1N/A {
1N/A my $x = 2; \$x;
1N/A }
1N/A
1N/Asub _ten
1N/A {
1N/A my $x = 10; \$x;
1N/A }
1N/A
1N/Asub _copy
1N/A {
1N/A my $x = $_[1];
1N/A my $z = $$x;
1N/A \$z;
1N/A }
1N/A
1N/A# catch and throw away
1N/Asub import { }
1N/A
1N/A##############################################################################
1N/A# convert back to string and number
1N/A
1N/Asub _str
1N/A {
1N/A # make string
1N/A "${$_[1]}";
1N/A }
1N/A
1N/Asub _num
1N/A {
1N/A # make a number
1N/A 0+${$_[1]};
1N/A }
1N/A
1N/Asub _zeros
1N/A {
1N/A my $x = $_[1];
1N/A
1N/A $x =~ /\d(0*)$/;
1N/A length($1 || '');
1N/A }
1N/A
1N/Asub _rsft
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _lsft
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _mod
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _gcd
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _sqrt
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _root
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _fac
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _modinv
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _modpow
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _log_int
1N/A {
1N/A # not used
1N/A }
1N/A
1N/Asub _as_hex
1N/A {
1N/A sprintf("0x%x",${$_[1]});
1N/A }
1N/A
1N/Asub _as_bin
1N/A {
1N/A sprintf("0b%b",${$_[1]});
1N/A }
1N/A
1N/A##############################################################################
1N/A# actual math code
1N/A
1N/Asub _add
1N/A {
1N/A my ($c,$x,$y) = @_;
1N/A $$x += $$y;
1N/A return $x;
1N/A }
1N/A
1N/Asub _sub
1N/A {
1N/A my ($c,$x,$y) = @_;
1N/A $$x -= $$y;
1N/A return $x;
1N/A }
1N/A
1N/Asub _mul
1N/A {
1N/A my ($c,$x,$y) = @_;
1N/A $$x *= $$y;
1N/A return $x;
1N/A }
1N/A
1N/Asub _div
1N/A {
1N/A my ($c,$x,$y) = @_;
1N/A
1N/A my $u = int($$x / $$y); my $r = $$x % $$y; $$x = $u;
1N/A return ($x,\$r) if wantarray;
1N/A return $x;
1N/A }
1N/A
1N/Asub _pow
1N/A {
1N/A my ($c,$x,$y) = @_;
1N/A my $u = $$x ** $$y; $$x = $u;
1N/A return $x;
1N/A }
1N/A
1N/Asub _and
1N/A {
1N/A my ($c,$x,$y) = @_;
1N/A my $u = int($$x) & int($$y); $$x = $u;
1N/A return $x;
1N/A }
1N/A
1N/Asub _xor
1N/A {
1N/A my ($c,$x,$y) = @_;
1N/A my $u = int($$x) ^ int($$y); $$x = $u;
1N/A return $x;
1N/A }
1N/A
1N/Asub _or
1N/A {
1N/A my ($c,$x,$y) = @_;
1N/A my $u = int($$x) | int($$y); $$x = $u;
1N/A return $x;
1N/A }
1N/A
1N/Asub _inc
1N/A {
1N/A my ($c,$x) = @_;
1N/A my $u = int($$x)+1; $$x = $u;
1N/A return $x;
1N/A }
1N/A
1N/Asub _dec
1N/A {
1N/A my ($c,$x) = @_;
1N/A my $u = int($$x)-1; $$x = $u;
1N/A return $x;
1N/A }
1N/A
1N/A##############################################################################
1N/A# testing
1N/A
1N/Asub _acmp
1N/A {
1N/A my ($c,$x, $y) = @_;
1N/A return ($$x <=> $$y);
1N/A }
1N/A
1N/Asub _len
1N/A {
1N/A return length("${$_[1]}");
1N/A }
1N/A
1N/Asub _digit
1N/A {
1N/A # return the nth digit, negative values count backward
1N/A # 0 is the rightmost digit
1N/A my ($c,$x,$n) = @_;
1N/A
1N/A $n ++; # 0 => 1, 1 => 2
1N/A return substr($$x,-$n,1); # 1 => -1, -2 => 2 etc
1N/A }
1N/A
1N/A##############################################################################
1N/A# _is_* routines
1N/A
1N/Asub _is_zero
1N/A {
1N/A # return true if arg is zero
1N/A my ($c,$x) = @_;
1N/A ($$x == 0) <=> 0;
1N/A }
1N/A
1N/Asub _is_even
1N/A {
1N/A # return true if arg is even
1N/A my ($c,$x) = @_;
1N/A (!($$x & 1)) <=> 0;
1N/A }
1N/A
1N/Asub _is_odd
1N/A {
1N/A # return true if arg is odd
1N/A my ($c,$x) = @_;
1N/A ($$x & 1) <=> 0;
1N/A }
1N/A
1N/Asub _is_one
1N/A {
1N/A # return true if arg is one
1N/A my ($c,$x) = @_;
1N/A ($$x == 1) <=> 0;
1N/A }
1N/A
1N/Asub _is_two
1N/A {
1N/A # return true if arg is one
1N/A my ($c,$x) = @_;
1N/A ($$x == 2) <=> 0;
1N/A }
1N/A
1N/Asub _is_ten
1N/A {
1N/A # return true if arg is one
1N/A my ($c,$x) = @_;
1N/A ($$x == 10) <=> 0;
1N/A }
1N/A
1N/A###############################################################################
1N/A# check routine to test internal state of corruptions
1N/A
1N/Asub _check
1N/A {
1N/A # no checks yet, pull it out from the test suite
1N/A my ($c,$x) = @_;
1N/A return "$x is not a reference" if !ref($x);
1N/A return 0;
1N/A }
1N/A
1N/A1;
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/AMath::BigInt::Scalar - Pure Perl module to test Math::BigInt with scalars
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/AProvides support for big integer calculations via means of 'small' int/floats.
1N/AOnly for testing purposes, since it will fail at large values. But it is simple
1N/Aenough not to introduce bugs on it's own and to serve as a testbed.
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/APlease see Math::BigInt::Calc.
1N/A
1N/A=head1 LICENSE
1N/A
1N/AThis program is free software; you may redistribute it and/or modify it under
1N/Athe same terms as Perl itself.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ATels http://bloodgate.com in 2001.
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AL<Math::BigInt>, L<Math::BigInt::Calc>.
1N/A
1N/A=cut