整数演算¶
By default, Perl assumes that it must do most of its arithmetic in floating point. But by saying
デフォルトでは、Perl は演算を浮動小数で行なわなければならないものと しています。 しかし、(もしそうしたいなら)
use integer;
you may tell the compiler to use integer operations (see integer for a detailed explanation) from here to the end of the enclosing BLOCK. An inner BLOCK may countermand this by saying
と書けば、その場所から現在の BLOCK の終わりまでは、整数演算 (詳しい説明は integer を参照してください) を 行なってよいと、コンパイラに指示することができます。 内部の BLOCK で、
no integer;
which lasts until the end of that BLOCK. Note that this doesn't mean everything is an integer, merely that Perl will use integer operations for arithmetic, comparison, and bitwise operators. For example, even under use integer
, if you take the sqrt(2)
, you'll still get 1.4142135623731
or so.
と書けば、その BLOCK の終わりまでは、指示を取り消すことになります。 これは全てを整数だけを使って処理することを意味するわけではなく、 単に Perl が算術、比較、ビット単位演算子で整数演算を するというだけであることに注意してください。 例えば、use integer
の指定があっても、sqrt(2)
とすると、 1.4142135623731
といった結果が返ってきます。
Used on numbers, the bitwise operators (&
|
^
~
<<
>>
) always produce integral results. (But see also "Bitwise String Operators".) However, use integer
still has meaning for them. By default, their results are interpreted as unsigned integers, but if use integer
is in effect, their results are interpreted as signed integers. For example, ~0
usually evaluates to a large integral value. However, use integer; ~0
is -1
on two's-complement machines.
数値を使う場合、ビット単位演算子 (&
|
^
~
<<
>>
) は 常に整数の結果を生成します。 (但し "Bitwise String Operators" も参照して下さい。) しかし、それでも use integer
は意味があります。 デフォルトでは、これらの結果は符号なし整数として解釈されますが、 use integer
が有効の場合は、符号付き整数として解釈されます。 例えば、~0
は通常大きな整数の値として評価されます。 しかし、use integer; ~0
は 2 の補数のマシンでは -1
になります。
浮動小数点数演算¶
While use integer
provides integer-only arithmetic, there is no analogous mechanism to provide automatic rounding or truncation to a certain number of decimal places. For rounding to a certain number of digits, sprintf()
or printf()
is usually the easiest route. See perlfaq4.
use integer
が整数演算を提供する一方、数を特定の桁で自動的に丸めたり 切り捨てたりする機構はありません。 数を丸めるには、sprintf()
や printf()
を使うのが一番簡単な方法です。 perlfaq4 を参照して下さい。
Floating-point numbers are only approximations to what a mathematician would call real numbers. There are infinitely more reals than floats, so some corners must be cut. For example:
浮動小数点数は数学者が実数と呼ぶものの近似でしかありません。 実数は浮動小数点より無限に続くので、多少角が丸められます。 例えば:
printf "%.20g\n", 123456789123456789;
# produces 123456789123456784
Testing for exact floating-point equality or inequality is not a good idea. Here's a (relatively expensive) work-around to compare whether two floating-point numbers are equal to a particular number of decimal places. See Knuth, volume II, for a more robust treatment of this topic.
浮動小数点数が等しいかどうかをちょうど同じかどうかで比較するのはよい 考えではありません。 以下に、二つの浮動小数点数が指定された桁まで等しいかどうかを 比較する(比較的重い)次善の策を示します。 この問題に関するより厳密な扱いについては Knuth, volume II を参照して下さい。
sub fp_equal {
my ($X, $Y, $POINTS) = @_;
my ($tX, $tY);
$tX = sprintf("%.${POINTS}g", $X);
$tY = sprintf("%.${POINTS}g", $Y);
return $tX eq $tY;
}
The POSIX module (part of the standard perl distribution) implements ceil()
, floor()
, and other mathematical and trigonometric functions. The Math::Complex
module (part of the standard perl distribution) defines mathematical functions that work on both the reals and the imaginary numbers. Math::Complex
is not as efficient as POSIX, but POSIX can't work with complex numbers.
POSIX モジュール(Perl 標準配布パッケージの一部) は ceil()
, floor()
及び その他の数学関数や三角関数を実装しています。 Math::Complex
モジュール(Perl 標準配布パッケージの一部)は実数と虚数の 両方で動作する数学関数を定義しています。 Math::Complex
は POSIX ほど効率的ではありませんが、POSIX は複素数は 扱えません。
Rounding in financial applications can have serious implications, and the rounding method used should be specified precisely. In these cases, it probably pays not to trust whichever system rounding is being used by Perl, but to instead implement the rounding function you need yourself.
金融アプリケーションにおける丸めは深刻な影響を与える可能性があり、 使用する丸めメソッドは指定された精度で行われるべきです。 このような場合、Perl が使用するシステム丸めを信用せず、 代わりに自分自身で丸め関数を実装するべきです。
より大きな数¶
The standard Math::BigInt
, Math::BigRat
, and Math::BigFloat
modules, along with the bignum
, bigint
, and bigrat
pragmas, provide variable-precision arithmetic and overloaded operators, although they're currently pretty slow. At the cost of some space and considerable speed, they avoid the normal pitfalls associated with limited-precision representations.
標準の Math::BigInt
, Math::BigRat
, Math::BigFloat
モジュールと bignum
, bigint
, bigrat
プラグマは多倍長演算を提供し、 演算子をオーバーロードしますが、これらは現在のところかなり遅いです。 多少の領域とかなりの速度を犠牲にして、桁数が制限されていることによる ありがちな落とし穴を避けることができます。
use 5.010;
use bigint; # easy interface to Math::BigInt
$x = 123456789123456789;
say $x * $x;
+15241578780673678515622620750190521
Or with rationals:
あるいは分数を使って:
use 5.010;
use bigrat;
$x = 3/22;
$y = 4/6;
say "x/y is ", $x/$y;
say "x*y is ", $x*$y;
x/y is 9/44
x*y is 1/11
Several modules let you calculate with unlimited or fixed precision (bound only by memory and CPU time). There are also some non-standard modules that provide faster implementations via external C libraries.
(メモリと CPU 時間のみに依存する)無制限か固定の精度での計算ができる モジュールがいくつかあります。 さらに外部 C ライブラリを使ってより速い実装を提供する 非標準のモジュールもあります。
Here is a short, but incomplete summary:
以下は短いですが不完全なリストです。
Math::String treat string sequences like numbers
Math::FixedPrecision calculate with a fixed precision
Math::Currency for currency calculations
Bit::Vector manipulate bit vectors fast (uses C)
Math::BigIntFast Bit::Vector wrapper for big numbers
Math::Pari provides access to the Pari C library
Math::Cephes uses the external Cephes C library (no
big numbers)
Math::Cephes::Fraction fractions via the Cephes library
Math::GMP another one using an external C library
Math::GMPz an alternative interface to libgmp's big ints
Math::GMPq an interface to libgmp's fraction numbers
Math::GMPf an interface to libgmp's floating point numbers
Math::String 文字列を数値のように扱う
Math::FixedPrecision 固定精度で計算する
Math::Currency 通貨の計算用
Bit::Vector (C を使って)ビットベクタを速く操作する
Math::BigIntFast 大きな数のための Bit::Vector のラッパー
Math::Pari Pari C ライブラリへのアクセスを提供する
Math::Cephes 外部の Cephes C を使う(大きな数はなし)
Math::Cephes::Fraction Cephes ライブラリを使った分数
Math::GMP これも外部 C ライブラリを使う
Math::GMPz libgmp の大きな整数へのもう一つのインターフェース
Math::GMPq libgmp の分数へのインターフェース
Math::GMPf libgmp の浮動小数点のインターフェース
Choose wisely.
うまく選んでください。
POD ERRORS¶
Hey! The above document had some coding errors, which are explained below:
- Around line 7:
-
You forgot a '=back' before '=head2'
You forgot a '=back' before '=head2'
- Around line 291:
-
=back without =over
- Around line 294:
-
=back without =over