LoginSignup
4
4

More than 5 years have passed since last update.

Perlでbignum

Last updated at Posted at 2013-02-12

Perlで扱える数字には、限界がある。

a.pl
#!/usr/bin/env perl
use 5.10.0;
use strict;
use warnings;

say 2**1024;
$ perl a.pl 
inf

そんな時には、bignum(http://search.cpan.org/~flora/bignum-0.30/lib/bignum.pm)

b.pl
#!/usr/bin/env perl
use 5.10.0;
use strict;
use warnings;
use bignum;

say 2**1024;

use bignum;の1行を加えるだけで

$ perl b.pl
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

このとおり

$ time perl a.pl > /dev/null 
perl a.pl > /dev/null  0.01s user 0.00s system 74% cpu 0.016 total
$ time perl b.pl > /dev/null
perl b.pl > /dev/null  0.05s user 0.00s system 95% cpu 0.058 total

(当たり前だけど)計算は遅くなる

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4