bigint
任意長整数を使用する。
実装としては Math::BigIntラッパーになっており、長整数は Math::Bigintオブジェクトになる。
use bigint;
my $big = 1234567890123456789012345678901234567890;
print ref($big), " $big\n";
↓
Math::BigInt
1234567890123456789012345678901234567890
use bigint lib => 'GMP';
長整数演算を GMPライブラリーで行なう。GMPが未インストールの場合はエラー。
use bigint try => 'GMP';
GMPライブラリーがインストールされていれば、長整数演算を GMPで行なう。
use bigint qw(oct hex)
oct, hex関数を bigintバージョンに置換する。
my
$oct = oct("0123456701234567"); # Math::BigInt->from_oct と同じ
print ref($oct), " $oct\n";
my $hex =
hex("123456789abcdef0123fed"); # Math::BigInt->from_hex
と同じ, 先頭に 0xがあってもOK
print ref($hex), " $hex\n";
↓
Math::BigInt 5744368105847
Math::BigInt
22007822920628982378545133
その他:
-
perl の標準算術演算式で bigint演算ができる。
-
Math::BigInt 同様、bigintと整数・実数の演算結果は bigintになる(小数点以下は切り捨てられる)。
-
Math::BigIntオブジェクトの特殊な値は、簡易な書式で書ける。
Math::BigInt->binf() → inf # 無限大
Math::BigInt->bnan() → NaN # Not a number -
bigint → 10進文字列変換は、Math::BigInt同様、文字列変換( "$variable" )で簡単に出来るが、10進文字列 → bigint変換する関数はない。Math::BigInt->new に頼るしかない。