1 |
|
2 |
=encoding euc-jp |
3 |
|
4 |
=head1 NAME |
5 |
|
6 |
=begin original |
7 |
|
8 |
perlnumber - semantics of numbers and numeric operations in Perl |
9 |
|
10 |
=end original |
11 |
|
12 |
perlnumber - Perl での数値と数値操作の意味論 |
13 |
|
14 |
=head1 SYNOPSIS |
15 |
|
16 |
=begin original |
17 |
|
18 |
$n = 1234; # decimal integer |
19 |
$n = 0b1110011; # binary integer |
20 |
$n = 01234; # octal integer |
21 |
$n = 0x1234; # hexadecimal integer |
22 |
$n = 12.34e-56; # exponential notation |
23 |
$n = "-12.34e56"; # number specified as a string |
24 |
$n = "1234"; # number specified as a string |
25 |
|
26 |
=end original |
27 |
|
28 |
$n = 1234; # 10 進数 |
29 |
$n = 0b1110011; # 2 進数 |
30 |
$n = 01234; # 8 進数 |
31 |
$n = 0x1234; # 16 進数 |
32 |
$n = 12.34e-56; # 指数表現 |
33 |
$n = "-12.34e56"; # 文字として指定された数値 |
34 |
$n = "1234"; # 文字として指定された数値 |
35 |
|
36 |
=head1 DESCRIPTION |
37 |
|
38 |
=begin original |
39 |
|
40 |
This document describes how Perl internally handles numeric values. |
41 |
|
42 |
=end original |
43 |
|
44 |
この文書は、Perl が内部で数値をどのように扱うかを記述します。 |
45 |
|
46 |
=begin original |
47 |
|
48 |
Perl's operator overloading facility is completely ignored here. Operator |
49 |
overloading allows user-defined behaviors for numbers, such as operations |
50 |
over arbitrarily large integers, floating points numbers with arbitrary |
51 |
precision, operations over "exotic" numbers such as modular arithmetic or |
52 |
p-adic arithmetic, and so on. See L<overload> for details. |
53 |
|
54 |
=end original |
55 |
|
56 |
Perl の演算子オーバーロード機能はここでは完全に無視されます。 |
57 |
演算子オーバーロードは、任意の大きさの整数、任意の精度の浮動小数点数、 |
58 |
合同算術や p 進数算術などの「特殊な」数値の演算などの数値に関するユーザー定義の |
59 |
振る舞いを指定できます。 |
60 |
詳細については L<overload> を参照してください。 |
61 |
|
62 |
=head1 Storing numbers |
63 |
|
64 |
(数値の保管) |
65 |
|
66 |
=begin original |
67 |
|
68 |
Perl can internally represent numbers in 3 different ways: as native |
69 |
integers, as native floating point numbers, and as decimal strings. |
70 |
Decimal strings may have an exponential notation part, as in C<"12.34e-56">. |
71 |
I<Native> here means "a format supported by the C compiler which was used |
72 |
to build perl". |
73 |
|
74 |
=end original |
75 |
|
76 |
Perl は内部的に数値を 3 つの異なった方法で表現できます: ネイティブな整数、 |
77 |
ネイティブな浮動小数点数、10 進文字列です。 |
78 |
10 進文字列は C<"12.34e-56"> のように指数部がある場合もあります。 |
79 |
ここでの I<ネイティブな> というのは、「perl をビルドする際に使われた C |
80 |
コンパイラが対応している形式」を意味します。 |
81 |
|
82 |
=begin original |
83 |
|
84 |
The term "native" does not mean quite as much when we talk about native |
85 |
integers, as it does when native floating point numbers are involved. |
86 |
The only implication of the term "native" on integers is that the limits for |
87 |
the maximal and the minimal supported true integral quantities are close to |
88 |
powers of 2. However, "native" floats have a most fundamental |
89 |
restriction: they may represent only those numbers which have a relatively |
90 |
"short" representation when converted to a binary fraction. For example, |
91 |
0.9 cannot be represented by a native float, since the binary fraction |
92 |
for 0.9 is infinite: |
93 |
|
94 |
=end original |
95 |
|
96 |
「ネイティブの」という用語はネイティブな整数に関して話すときにはほとんど |
97 |
意味はなく、ネイティブな浮動小数点数に関わる際に意味があります。 |
98 |
整数に対して「ネイティブな」という用語が暗示する唯一のものは、 |
99 |
対応している真の整数量の最大値と最小値は 2 のべき乗に近いということです。 |
100 |
しかし、「ネイティブな」浮動小数点数は最も基本的な制限を持ちます: |
101 |
2 進数分数に変換したときに相対的に「短い」表現を持つ値のみを表現できます。 |
102 |
例えば、0.9 はネイティブな浮動小数点では表現できません; なぜなら 0.9 の |
103 |
2 進数の分数は無限となるからです: |
104 |
|
105 |
binary0.1110011001100... |
106 |
|
107 |
=begin original |
108 |
|
109 |
with the sequence C<1100> repeating again and again. In addition to this |
110 |
limitation, the exponent of the binary number is also restricted when it |
111 |
is represented as a floating point number. On typical hardware, floating |
112 |
point values can store numbers with up to 53 binary digits, and with binary |
113 |
exponents between -1024 and 1024. In decimal representation this is close |
114 |
to 16 decimal digits and decimal exponents in the range of -304..304. |
115 |
The upshot of all this is that Perl cannot store a number like |
116 |
12345678901234567 as a floating point number on such architectures without |
117 |
loss of information. |
118 |
|
119 |
=end original |
120 |
|
121 |
C<1100> が繰り返されます。 |
122 |
この制限にくわえて、2 進数の指数も、浮動小数点数として表現されると |
123 |
制限されます。 |
124 |
典型的なハードウェアでは、浮動小数点数は 53 桁までの 2 進数と、 |
125 |
-1024 から 1024 までの 2 進数指数を保管できます。 |
126 |
10 進表現では、ほぼ 16 桁の 10 進数と -304 から 304 の範囲の指数となります。 |
127 |
これら全ての結論は、このようなアーキテクチャでは、Perl は |
128 |
12345678901234567 といった数を情報の欠落なしに浮動小数点数として |
129 |
保管することはできないということです。 |
130 |
|
131 |
=begin original |
132 |
|
133 |
Similarly, decimal strings can represent only those numbers which have a |
134 |
finite decimal expansion. Being strings, and thus of arbitrary length, there |
135 |
is no practical limit for the exponent or number of decimal digits for these |
136 |
numbers. (But realize that what we are discussing the rules for just the |
137 |
I<storage> of these numbers. The fact that you can store such "large" numbers |
138 |
does not mean that the I<operations> over these numbers will use all |
139 |
of the significant digits. |
140 |
See L<"Numeric operators and numeric conversions"> for details.) |
141 |
|
142 |
=end original |
143 |
|
144 |
同様に、10 進文字列は有限 10 進記数法を持つ数値のみ表現できます。 |
145 |
文字列であるため、従って任意の長さを持つため、これらの数値のための |
146 |
指数部や実数部には実用上の制限はありません。 |
147 |
(しかし、議論しているのはこれらの数値の I<保管> に関するものであると |
148 |
いうことを理解してください。 |
149 |
このような「大きい」数値を保管できるということは、これらの数値による |
150 |
I<操作> が全ての桁を使うということを意味しません。 |
151 |
詳細については L<"Numeric operators and numeric conversions"> を |
152 |
参照してください。) |
153 |
|
154 |
=begin original |
155 |
|
156 |
In fact numbers stored in the native integer format may be stored either |
157 |
in the signed native form, or in the unsigned native form. Thus the limits |
158 |
for Perl numbers stored as native integers would typically be -2**31..2**32-1, |
159 |
with appropriate modifications in the case of 64-bit integers. Again, this |
160 |
does not mean that Perl can do operations only over integers in this range: |
161 |
it is possible to store many more integers in floating point format. |
162 |
|
163 |
=end original |
164 |
|
165 |
実際のところ、ネイティブな整数形式で保管された数値は、符号付きのネイティブな |
166 |
形式か、符号なしのネイティブな形式のどちらかで保管されます。 |
167 |
従って、ネイティブな整数として保管される Perl の数値の限界は、典型的には |
168 |
-2**31..2**32-1 で、64 ビット整数の場合は適切に修正されたものになります。 |
169 |
再び、これは Perl がこの幅でのみ整数を扱えるということを意味しません: |
170 |
浮動小数点形式によってもっと多くの整数を保管可能です。 |
171 |
|
172 |
=begin original |
173 |
|
174 |
Summing up, Perl numeric values can store only those numbers which have |
175 |
a finite decimal expansion or a "short" binary expansion. |
176 |
|
177 |
=end original |
178 |
|
179 |
要約すると、Perl の数値は、有限 10 進数記法か、「短い」2 進数記法を持つ |
180 |
数値のみが格納できます。 |
181 |
|
182 |
=head1 Numeric operators and numeric conversions |
183 |
|
184 |
(数値演算子と数値変換) |
185 |
|
186 |
=begin original |
187 |
|
188 |
As mentioned earlier, Perl can store a number in any one of three formats, |
189 |
but most operators typically understand only one of those formats. When |
190 |
a numeric value is passed as an argument to such an operator, it will be |
191 |
converted to the format understood by the operator. |
192 |
|
193 |
=end original |
194 |
|
195 |
前述のように、Perl は 3 つの形式のどれでも数値を格納できますが、 |
196 |
ほとんどの演算子は典型的にはこれらの形式の一つだけしか理解しません。 |
197 |
数値がそのような演算子の引数として渡されるとき、演算子が理解できる形式へ |
198 |
変換されます。 |
199 |
|
200 |
=begin original |
201 |
|
202 |
Six such conversions are possible: |
203 |
|
204 |
=end original |
205 |
|
206 |
6 種類のこのような変換が可能です: |
207 |
|
208 |
=begin original |
209 |
|
210 |
native integer --> native floating point (*) |
211 |
native integer --> decimal string |
212 |
native floating_point --> native integer (*) |
213 |
native floating_point --> decimal string (*) |
214 |
decimal string --> native integer |
215 |
decimal string --> native floating point (*) |
216 |
|
217 |
=end original |
218 |
|
219 |
ネイティブな整数 --> ネイティブな浮動小数点数 (*) |
220 |
ネイティブな整数 --> 10 進数文字列 |
221 |
ネイティブな浮動小数点数 --> ネイティブな整数 (*) |
222 |
ネイティブな浮動小数点数 --> 10 進数文字列 (*) |
223 |
10 進数文字列 --> ネイティブな整数 |
224 |
10 進数文字列 --> ネイティブな浮動小数点数 (*) |
225 |
|
226 |
=begin original |
227 |
|
228 |
These conversions are governed by the following general rules: |
229 |
|
230 |
=end original |
231 |
|
232 |
これらの変換は、以下の一般的な規則に従います: |
233 |
|
234 |
=over 4 |
235 |
|
236 |
=item * |
237 |
|
238 |
=begin original |
239 |
|
240 |
If the source number can be represented in the target form, that |
241 |
representation is used. |
242 |
|
243 |
=end original |
244 |
|
245 |
変換元の数値が変換先の形式で表現できるなら、その表現が使われます。 |
246 |
|
247 |
=item * |
248 |
|
249 |
=begin original |
250 |
|
251 |
If the source number is outside of the limits representable in the target form, |
252 |
a representation of the closest limit is used. (I<Loss of information>) |
253 |
|
254 |
=end original |
255 |
|
256 |
変換元の数値が変換先の形式で表現できる限界を超えている場合、最も近い |
257 |
限界値が用いられます。 |
258 |
(I<情報の欠落>) |
259 |
|
260 |
=item * |
261 |
|
262 |
=begin original |
263 |
|
264 |
If the source number is between two numbers representable in the target form, |
265 |
a representation of one of these numbers is used. (I<Loss of information>) |
266 |
|
267 |
=end original |
268 |
|
269 |
変換元の数値が変換先の形式で表現できる二つの数値の間にある場合、 |
270 |
二つの数値表現のどちらかが使われます。 |
271 |
(I<情報の欠落>) |
272 |
|
273 |
=item * |
274 |
|
275 |
=begin original |
276 |
|
277 |
In C<< native floating point --> native integer >> conversions the magnitude |
278 |
of the result is less than or equal to the magnitude of the source. |
279 |
(I<"Rounding to zero".>) |
280 |
|
281 |
=end original |
282 |
|
283 |
C<<ネイティブな浮動小数点数 --> ネイティブな整数>> 変換で、結果の絶対値は |
284 |
変換元の絶対値以下となります。 |
285 |
(I<「0 への丸め」。>) |
286 |
|
287 |
=item * |
288 |
|
289 |
=begin original |
290 |
|
291 |
If the C<< decimal string --> native integer >> conversion cannot be done |
292 |
without loss of information, the result is compatible with the conversion |
293 |
sequence C<< decimal_string --> native_floating_point --> native_integer >>. |
294 |
In particular, rounding is strongly biased to 0, though a number like |
295 |
C<"0.99999999999999999999"> has a chance of being rounded to 1. |
296 |
|
297 |
=end original |
298 |
|
299 |
もし C<<10 進数文字列 --> ネイティブな整数>> 変換が情報の欠落なしに |
300 |
行えない場合、結果は |
301 |
C<<10 進数文字列 --> ネイティブな浮動小数点数 --> ネイティブな整数>> という |
302 |
変換に準拠します。 |
303 |
特に、丸めは 0 方向に強く偏っていますが、 |
304 |
C<"0.99999999999999999999"> のような数が 1 に丸められる可能性はあります。 |
305 |
|
306 |
=back |
307 |
|
308 |
=begin original |
309 |
|
310 |
B<RESTRICTION>: The conversions marked with C<(*)> above involve steps |
311 |
performed by the C compiler. In particular, bugs/features of the compiler |
312 |
used may lead to breakage of some of the above rules. |
313 |
|
314 |
=end original |
315 |
|
316 |
B<制限>: 上記で C<(*)> マークが付いている変換は C コンパイラによって |
317 |
行われます。 |
318 |
特に、使用しているコンパイラのバグ/仕様が上記のルールの一部を破ることに |
319 |
なるかもしれません。 |
320 |
|
321 |
=head1 Flavors of Perl numeric operations |
322 |
|
323 |
(Perl の数値演算子の特色) |
324 |
|
325 |
=begin original |
326 |
|
327 |
Perl operations which take a numeric argument treat that argument in one |
328 |
of four different ways: they may force it to one of the integer/floating/ |
329 |
string formats, or they may behave differently depending on the format of |
330 |
the operand. Forcing a numeric value to a particular format does not |
331 |
change the number stored in the value. |
332 |
|
333 |
=end original |
334 |
|
335 |
数値の引数を取る Perl の操作は、引数を 4 つの異なる方法のどれかによって |
336 |
扱われます: 整数/浮動小数点数/文字列数のどれかに強制されるか、 |
337 |
オペランドの形式に依存して異なる振る舞いをするかです。 |
338 |
特定の形式への数値の強制は、保管されている値は変更しません。 |
339 |
|
340 |
=begin original |
341 |
|
342 |
All the operators which need an argument in the integer format treat the |
343 |
argument as in modular arithmetic, e.g., C<mod 2**32> on a 32-bit |
344 |
architecture. C<sprintf "%u", -1> therefore provides the same result as |
345 |
C<sprintf "%u", ~0>. |
346 |
|
347 |
=end original |
348 |
|
349 |
引数として整数形式を必要とする全ての演算子は引数を合同算術として扱います; |
350 |
つまり、32 ビットアーキテクチャでは C<mod 2**32> です。 |
351 |
従って、C<sprintf "%u", -1> は C<sprintf "%u", ~0> と同じ結果となります。 |
352 |
|
353 |
=over 4 |
354 |
|
355 |
=item Arithmetic operators |
356 |
|
357 |
(算術演算子) |
358 |
|
359 |
=begin original |
360 |
|
361 |
The binary operators C<+> C<-> C<*> C</> C<%> C<==> C<!=> C<E<gt>> C<E<lt>> |
362 |
C<E<gt>=> C<E<lt>=> and the unary operators C<-> C<abs> and C<--> will |
363 |
attempt to convert arguments to integers. If both conversions are possible |
364 |
without loss of precision, and the operation can be performed without |
365 |
loss of precision then the integer result is used. Otherwise arguments are |
366 |
converted to floating point format and the floating point result is used. |
367 |
The caching of conversions (as described above) means that the integer |
368 |
conversion does not throw away fractional parts on floating point numbers. |
369 |
|
370 |
=end original |
371 |
|
372 |
2 項演算子 C<+> C<-> C<*> C</> C<%> C<==> C<!=> C<E<gt>> C<E<lt>> |
373 |
C<E<gt>=> C<E<lt>=> と、単項演算子 C<-> C<abs> C<--> は引数を整数に |
374 |
変換しようとします。 |
375 |
もし両方の変換は精度を失うことなく可能で、演算が精度を失うことなく |
376 |
実行できるなら、整数の結果が使われます。 |
377 |
さもなければ、引数は浮動小数点数形式に変換され、浮動小数点数の結果が |
378 |
使われます。 |
379 |
(上述したような)変換のキャッシュは、整数変換が浮動小数点数の小数部を |
380 |
捨てないことを意味します。 |
381 |
|
382 |
=item ++ |
383 |
|
384 |
=begin original |
385 |
|
386 |
C<++> behaves as the other operators above, except that if it is a string |
387 |
matching the format C</^[a-zA-Z]*[0-9]*\z/> the string increment described |
388 |
in L<perlop> is used. |
389 |
|
390 |
=end original |
391 |
|
392 |
C<++> は上述のその他の演算子と同様に振る舞いますが、もし文字列が |
393 |
C</^[a-zA-Z]*[0-9]*\z/> にマッチングする形式なら、L<perlop> に記述している |
394 |
文字列インクリメントが使われます。 |
395 |
|
396 |
=item Arithmetic operators during C<use integer> |
397 |
|
398 |
(C<use integer> 中の算術演算子) |
399 |
|
400 |
=begin original |
401 |
|
402 |
In scopes where C<use integer;> is in force, nearly all the operators listed |
403 |
above will force their argument(s) into integer format, and return an integer |
404 |
result. The exceptions, C<abs>, C<++> and C<-->, do not change their |
405 |
behavior with C<use integer;> |
406 |
|
407 |
=end original |
408 |
|
409 |
C<use integer;> が有効なスコープ中では、上述のほとんど全ての演算子は |
410 |
引数を整数形式に強制し、整数の結果を返します。 |
411 |
例外は C<abs>, C<++>, C<--> で、C<use integer;> でも振る舞いは変わりません。 |
412 |
|
413 |
=item Other mathematical operators |
414 |
|
415 |
(その他の数学演算子) |
416 |
|
417 |
=begin original |
418 |
|
419 |
Operators such as C<**>, C<sin> and C<exp> force arguments to floating point |
420 |
format. |
421 |
|
422 |
=end original |
423 |
|
424 |
C<**>, C<sin>, C<exp> といった演算子は引数を浮動小数点数に強制します。 |
425 |
|
426 |
=item Bitwise operators |
427 |
|
428 |
(ビット単位演算子) |
429 |
|
430 |
=begin original |
431 |
|
432 |
Arguments are forced into the integer format if not strings. |
433 |
|
434 |
=end original |
435 |
|
436 |
引数は、文字列でなければ整数に強制されます。 |
437 |
|
438 |
=item Bitwise operators during C<use integer> |
439 |
|
440 |
(C<use integer> 中のビット単位演算子) |
441 |
|
442 |
=begin original |
443 |
|
444 |
forces arguments to integer format. Also shift operations internally use |
445 |
signed integers rather than the default unsigned. |
446 |
|
447 |
=end original |
448 |
|
449 |
引数を整数に強制します。 |
450 |
また、シフト操作は、デフォルトの符号なし整数ではなく、符号付き整数を |
451 |
内部的に使います。 |
452 |
|
453 |
=item Operators which expect an integer |
454 |
|
455 |
(整数を想定している演算子) |
456 |
|
457 |
=begin original |
458 |
|
459 |
force the argument into the integer format. This is applicable |
460 |
to the third and fourth arguments of C<sysread>, for example. |
461 |
|
462 |
=end original |
463 |
|
464 |
引数を整数に強制します。 |
465 |
これは例えば、C<sysread> の第 3 引数と第 4 引数に適用されます。 |
466 |
|
467 |
=item Operators which expect a string |
468 |
|
469 |
(文字列を想定している演算子) |
470 |
|
471 |
=begin original |
472 |
|
473 |
force the argument into the string format. For example, this is |
474 |
applicable to C<printf "%s", $value>. |
475 |
|
476 |
=end original |
477 |
|
478 |
引数を文字列に強制します。 |
479 |
例えば、これは C<printf "%s", $value> に適用されます。 |
480 |
|
481 |
=back |
482 |
|
483 |
=begin original |
484 |
|
485 |
Though forcing an argument into a particular form does not change the |
486 |
stored number, Perl remembers the result of such conversions. In |
487 |
particular, though the first such conversion may be time-consuming, |
488 |
repeated operations will not need to redo the conversion. |
489 |
|
490 |
=end original |
491 |
|
492 |
引数の特定の形式への強制は保管されている数値は変更しませんが、Perl は変換の |
493 |
結果を覚えています。 |
494 |
特に、最初の変換が時間がかかるものであったとしても、同じ操作を繰り返しても |
495 |
変換を再実行する必要はありません。 |
496 |
|
497 |
=head1 AUTHOR |
498 |
|
499 |
Ilya Zakharevich C<ilya@math.ohio-state.edu> |
500 |
|
501 |
Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com> |
502 |
|
503 |
Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org> |
504 |
|
505 |
=head1 SEE ALSO |
506 |
|
507 |
L<overload>, L<perlop> |
508 |
|
509 |
=begin meta |
510 |
|
511 |
Translate: Kentaro Shirakata <argrath@ub32.org> (5.10.0-) |
512 |
|
513 |
=end meta |
514 |
|