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