perlnumber > 5.22.1 との差分

perlnumber 5.22.1 と 5.18.1 の差分

11
22=encoding euc-jp
33
44=head1 NAME
55
66=begin original
77
88perlnumber - semantics of numbers and numeric operations in Perl
99
1010=end original
1111
1212perlnumber - Perl での数値と数値操作の意味論
1313
1414=head1 SYNOPSIS
1515
1616=begin original
1717
1818 $n = 1234; # decimal integer
1919 $n = 0b1110011; # binary integer
2020 $n = 01234; # octal integer
2121 $n = 0x1234; # hexadecimal integer
2222 $n = 12.34e-56; # exponential notation
2323 $n = "-12.34e56"; # number specified as a string
2424 $n = "1234"; # number specified as a string
2525
2626=end original
2727
2828 $n = 1234; # 10 進数
2929 $n = 0b1110011; # 2 進数
3030 $n = 01234; # 8 進数
3131 $n = 0x1234; # 16 進数
3232 $n = 12.34e-56; # 指数表現
3333 $n = "-12.34e56"; # 文字として指定された数値
3434 $n = "1234"; # 文字として指定された数値
3535
3636=head1 DESCRIPTION
3737
3838=begin original
3939
4040This document describes how Perl internally handles numeric values.
4141
4242=end original
4343
4444この文書は、Perl が内部で数値をどのように扱うかを記述します。
4545
4646=begin original
4747
4848Perl's operator overloading facility is completely ignored here. Operator
4949overloading allows user-defined behaviors for numbers, such as operations
5050over arbitrarily large integers, floating points numbers with arbitrary
5151precision, operations over "exotic" numbers such as modular arithmetic or
5252p-adic arithmetic, and so on. See L<overload> for details.
5353
5454=end original
5555
5656Perl の演算子オーバーロード機能はここでは完全に無視されます。
5757演算子オーバーロードは、任意の大きさの整数、任意の精度の浮動小数点数、
5858合同算術や p 進数算術などの「特殊な」数値の演算などの数値に関するユーザー定義の
5959振る舞いを指定できます。
6060詳細については L<overload> を参照してください。
6161
6262=head1 Storing numbers
6363
6464(数値の保管)
6565
6666=begin original
6767
6868Perl can internally represent numbers in 3 different ways: as native
6969integers, as native floating point numbers, and as decimal strings.
7070Decimal strings may have an exponential notation part, as in C<"12.34e-56">.
7171I<Native> here means "a format supported by the C compiler which was used
7272to build perl".
7373
7474=end original
7575
7676Perl は内部的に数値を 3 つの異なった方法で表現できます: ネイティブな整数、
7777ネイティブな浮動小数点数、10 進文字列です。
787810 進文字列は C<"12.34e-56"> のように指数部がある場合もあります。
7979ここでの I<ネイティブな> というのは、「perl をビルドする際に使われた C
8080コンパイラが対応している形式」を意味します。
8181
8282=begin original
8383
8484The term "native" does not mean quite as much when we talk about native
8585integers, as it does when native floating point numbers are involved.
8686The only implication of the term "native" on integers is that the limits for
8787the maximal and the minimal supported true integral quantities are close to
8888powers of 2. However, "native" floats have a most fundamental
8989restriction: they may represent only those numbers which have a relatively
9090"short" representation when converted to a binary fraction. For example,
91910.9 cannot be represented by a native float, since the binary fraction
9292for 0.9 is infinite:
9393
9494=end original
9595
9696「ネイティブの」という用語はネイティブな整数に関して話すときにはほとんど
9797意味はなく、ネイティブな浮動小数点数に関わる際に意味があります。
9898整数に対して「ネイティブな」という用語が暗示する唯一のものは、
9999対応している真の整数量の最大値と最小値は 2 のべき乗に近いということです。
100100しかし、「ネイティブな」浮動小数点数は最も基本的な制限を持ちます:
1011012 進数分数に変換したときに相対的に「短い」表現を持つ値のみを表現できます。
102102例えば、0.9 はネイティブな浮動小数点では表現できません; なぜなら 0.9 の
1031032 進数の分数は無限となるからです:
104104
105105 binary0.1110011001100...
106106
107107=begin original
108108
109109with the sequence C<1100> repeating again and again. In addition to this
110110limitation, the exponent of the binary number is also restricted when it
111111is represented as a floating point number. On typical hardware, floating
112112point values can store numbers with up to 53 binary digits, and with binary
113113exponents between -1024 and 1024. In decimal representation this is close
114114to 16 decimal digits and decimal exponents in the range of -304..304.
115115The upshot of all this is that Perl cannot store a number like
11611612345678901234567 as a floating point number on such architectures without
117117loss of information.
118118
119119=end original
120120
121121C<1100> が繰り返されます。
122122この制限にくわえて、2 進数の指数も、浮動小数点数として表現されると
123123制限されます。
124124典型的なハードウェアでは、浮動小数点数は 53 桁までの 2 進数と、
125125-1024 から 1024 までの 2 進数指数を保管できます。
12612610 進表現では、ほぼ 16 桁の 10 進数と -304 から 304 の範囲の指数となります。
127127これら全ての結論は、このようなアーキテクチャでは、Perl は
12812812345678901234567 といった数を情報の欠落なしに浮動小数点数として
129129保管することはできないということです。
130130
131131=begin original
132132
133133Similarly, decimal strings can represent only those numbers which have a
134134finite decimal expansion. Being strings, and thus of arbitrary length, there
135135is no practical limit for the exponent or number of decimal digits for these
136136numbers. (But realize that what we are discussing the rules for just the
137137I<storage> of these numbers. The fact that you can store such "large" numbers
138138does not mean that the I<operations> over these numbers will use all
139139of the significant digits.
140140See L<"Numeric operators and numeric conversions"> for details.)
141141
142142=end original
143143
144144同様に、10 進文字列は有限 10 進記数法を持つ数値のみ表現できます。
145145文字列であるため、従って任意の長さを持つため、これらの数値のための
146146指数部や実数部には実用上の制限はありません。
147147(しかし、議論しているのはこれらの数値の I<保管> に関するものであると
148148いうことを理解してください。
149149このような「大きい」数値を保管できるということは、これらの数値による
150150I<操作> が全ての桁を使うということを意味しません。
151151詳細については L<"Numeric operators and numeric conversions"> を
152152参照してください。)
153153
154154=begin original
155155
156156In fact numbers stored in the native integer format may be stored either
157157in the signed native form, or in the unsigned native form. Thus the limits
158158for Perl numbers stored as native integers would typically be -2**31..2**32-1,
159159with appropriate modifications in the case of 64-bit integers. Again, this
160160does not mean that Perl can do operations only over integers in this range:
161161it is possible to store many more integers in floating point format.
162162
163163=end original
164164
165165実際のところ、ネイティブな整数形式で保管された数値は、符号付きのネイティブな
166166形式か、符号なしのネイティブな形式のどちらかで保管されます。
167167従って、ネイティブな整数として保管される Perl の数値の限界は、典型的には
168168-2**31..2**32-1 で、64 ビット整数の場合は適切に修正されたものになります。
169169再び、これは Perl がこの幅でのみ整数を扱えるということを意味しません:
170170浮動小数点形式によってもっと多くの整数を保管可能です。
171171
172172=begin original
173173
174174Summing up, Perl numeric values can store only those numbers which have
175175a finite decimal expansion or a "short" binary expansion.
176176
177177=end original
178178
179179要約すると、Perl の数値は、有限 10 進数記法か、「短い」2 進数記法を持つ
180180数値のみが格納できます。
181181
182182=head1 Numeric operators and numeric conversions
183183
184184(数値演算子と数値変換)
185185
186186=begin original
187187
188188As mentioned earlier, Perl can store a number in any one of three formats,
189189but most operators typically understand only one of those formats. When
190190a numeric value is passed as an argument to such an operator, it will be
191191converted to the format understood by the operator.
192192
193193=end original
194194
195195前述のように、Perl は 3 つの形式のどれでも数値を格納できますが、
196196ほとんどの演算子は典型的にはこれらの形式の一つだけしか理解しません。
197197数値がそのような演算子の引数として渡されるとき、演算子が理解できる形式へ
198198変換されます。
199199
200200=begin original
201201
202202Six such conversions are possible:
203203
204204=end original
205205
2062066 種類のこのような変換が可能です:
207207
208208=begin original
209209
210210 native integer --> native floating point (*)
211211 native integer --> decimal string
212212 native floating_point --> native integer (*)
213213 native floating_point --> decimal string (*)
214214 decimal string --> native integer
215215 decimal string --> native floating point (*)
216216
217217=end original
218218
219219 ネイティブな整数 --> ネイティブな浮動小数点数 (*)
220220 ネイティブな整数 --> 10 進数文字列
221221 ネイティブな浮動小数点数 --> ネイティブな整数 (*)
222222 ネイティブな浮動小数点数 --> 10 進数文字列 (*)
223223 10 進数文字列 --> ネイティブな整数
224224 10 進数文字列 --> ネイティブな浮動小数点数 (*)
225225
226226=begin original
227227
228228These conversions are governed by the following general rules:
229229
230230=end original
231231
232232これらの変換は、以下の一般的な規則に従います:
233233
234234=over 4
235235
236236=item *
237237
238238=begin original
239239
240240If the source number can be represented in the target form, that
241241representation is used.
242242
243243=end original
244244
245245変換元の数値が変換先の形式で表現できるなら、その表現が使われます。
246246
247247=item *
248248
249249=begin original
250250
251251If the source number is outside of the limits representable in the target form,
252252a representation of the closest limit is used. (I<Loss of information>)
253253
254254=end original
255255
256256変換元の数値が変換先の形式で表現できる限界を超えている場合、最も近い
257257限界値が用いられます。
258258(I<情報の欠落>)
259259
260260=item *
261261
262262=begin original
263263
264264If the source number is between two numbers representable in the target form,
265265a representation of one of these numbers is used. (I<Loss of information>)
266266
267267=end original
268268
269269変換元の数値が変換先の形式で表現できる二つの数値の間にある場合、
270270二つの数値表現のどちらかが使われます。
271271(I<情報の欠落>)
272272
273273=item *
274274
275275=begin original
276276
277277In C<< native floating point --> native integer >> conversions the magnitude
278278of the result is less than or equal to the magnitude of the source.
279279(I<"Rounding to zero".>)
280280
281281=end original
282282
283283C<< ネイティブな浮動小数点数 --> ネイティブな整数 >>
284284変換で、結果の絶対値は変換元の絶対値以下となります。
285285(I<「0 への丸め」。>)
286286
287287=item *
288288
289289=begin original
290290
291291If the C<< decimal string --> native integer >> conversion cannot be done
292292without loss of information, the result is compatible with the conversion
293293sequence C<< decimal_string --> native_floating_point --> native_integer >>.
294294In particular, rounding is strongly biased to 0, though a number like
295295C<"0.99999999999999999999"> has a chance of being rounded to 1.
296296
297297=end original
298298
299299もし C<< 10 進数文字列 --> ネイティブな整数 >>
300300変換が情報の欠落なしに行えない場合、結果は
301301C<< 10 進数文字列 --> ネイティブな浮動小数点数 --> ネイティブな整数 >>
302302という変換に準拠します。
303303特に、丸めは 0 方向に強く偏っていますが、
304304C<"0.99999999999999999999"> のような数が 1 に丸められる可能性はあります。
305305
306306=back
307307
308308=begin original
309309
310310B<RESTRICTION>: The conversions marked with C<(*)> above involve steps
311311performed by the C compiler. In particular, bugs/features of the compiler
312312used may lead to breakage of some of the above rules.
313313
314314=end original
315315
316316B<制限>: 上記で C<(*)> マークが付いている変換は C コンパイラによって
317317行われます。
318318特に、使用しているコンパイラのバグ/仕様が上記のルールの一部を破ることに
319319なるかもしれません。
320320
321321=head1 Flavors of Perl numeric operations
322322
323323(Perl の数値演算子の特色)
324324
325325=begin original
326326
327327Perl operations which take a numeric argument treat that argument in one
328328of four different ways: they may force it to one of the integer/floating/
329329string formats, or they may behave differently depending on the format of
330330the operand. Forcing a numeric value to a particular format does not
331331change the number stored in the value.
332332
333333=end original
334334
335335数値の引数を取る Perl の操作は、引数を 4 つの異なる方法のどれかによって
336336扱われます: 整数/浮動小数点数/文字列数のどれかに強制されるか、
337337オペランドの形式に依存して異なる振る舞いをするかです。
338338特定の形式への数値の強制は、保管されている値は変更しません。
339339
340340=begin original
341341
342342All the operators which need an argument in the integer format treat the
343343argument as in modular arithmetic, e.g., C<mod 2**32> on a 32-bit
344344architecture. C<sprintf "%u", -1> therefore provides the same result as
345345C<sprintf "%u", ~0>.
346346
347347=end original
348348
349349引数として整数形式を必要とする全ての演算子は引数を合同算術として扱います;
350350つまり、32 ビットアーキテクチャでは C<mod 2**32> です。
351351従って、C<sprintf "%u", -1> は C<sprintf "%u", ~0> と同じ結果となります。
352352
353353=over 4
354354
355355=item Arithmetic operators
356356
357357(算術演算子)
358358
359359=begin original
360360
361361The binary operators C<+> C<-> C<*> C</> C<%> C<==> C<!=> C<E<gt>> C<E<lt>>
362362C<E<gt>=> C<E<lt>=> and the unary operators C<-> C<abs> and C<--> will
363363attempt to convert arguments to integers. If both conversions are possible
364364without loss of precision, and the operation can be performed without
365365loss of precision then the integer result is used. Otherwise arguments are
366366converted to floating point format and the floating point result is used.
367367The caching of conversions (as described above) means that the integer
368368conversion does not throw away fractional parts on floating point numbers.
369369
370370=end original
371371
3723722 項演算子 C<+> C<-> C<*> C</> C<%> C<==> C<!=> C<E<gt>> C<E<lt>>
373373C<E<gt>=> C<E<lt>=> と、単項演算子 C<-> C<abs> C<--> は引数を整数に
374374変換しようとします。
375375もし両方の変換は精度を失うことなく可能で、演算が精度を失うことなく
376376実行できるなら、整数の結果が使われます。
377377さもなければ、引数は浮動小数点数形式に変換され、浮動小数点数の結果が
378378使われます。
379379(上述したような)変換のキャッシュは、整数変換が浮動小数点数の小数部を
380380捨てないことを意味します。
381381
382382=item ++
383383
384384=begin original
385385
386386C<++> behaves as the other operators above, except that if it is a string
387387matching the format C</^[a-zA-Z]*[0-9]*\z/> the string increment described
388388in L<perlop> is used.
389389
390390=end original
391391
392392C<++> は上述のその他の演算子と同様に振る舞いますが、もし文字列が
393393C</^[a-zA-Z]*[0-9]*\z/> にマッチングする形式なら、L<perlop> に記述している
394394文字列インクリメントが使われます。
395395
396396=item Arithmetic operators during C<use integer>
397397
398398(C<use integer> 中の算術演算子)
399399
400400=begin original
401401
402402In scopes where C<use integer;> is in force, nearly all the operators listed
403403above will force their argument(s) into integer format, and return an integer
404404result. The exceptions, C<abs>, C<++> and C<-->, do not change their
405405behavior with C<use integer;>
406406
407407=end original
408408
409409C<use integer;> が有効なスコープ中では、上述のほとんど全ての演算子は
410410引数を整数形式に強制し、整数の結果を返します。
411411例外は C<abs>, C<++>, C<--> で、C<use integer;> でも振る舞いは変わりません。
412412
413413=item Other mathematical operators
414414
415415(その他の数学演算子)
416416
417417=begin original
418418
419419Operators such as C<**>, C<sin> and C<exp> force arguments to floating point
420420format.
421421
422422=end original
423423
424424C<**>, C<sin>, C<exp> といった演算子は引数を浮動小数点数に強制します。
425425
426426=item Bitwise operators
427427
428428(ビット単位演算子)
429429
430430=begin original
431431
432432Arguments are forced into the integer format if not strings.
433433
434434=end original
435435
436436引数は、文字列でなければ整数に強制されます。
437437
438438=item Bitwise operators during C<use integer>
439439
440440(C<use integer> 中のビット単位演算子)
441441
442442=begin original
443443
444444forces arguments to integer format. Also shift operations internally use
445445signed integers rather than the default unsigned.
446446
447447=end original
448448
449449引数を整数に強制します。
450450また、シフト操作は、デフォルトの符号なし整数ではなく、符号付き整数を
451451内部的に使います。
452452
453453=item Operators which expect an integer
454454
455455(整数を想定している演算子)
456456
457457=begin original
458458
459459force the argument into the integer format. This is applicable
460460to the third and fourth arguments of C<sysread>, for example.
461461
462462=end original
463463
464464引数を整数に強制します。
465465これは例えば、C<sysread> の第 3 引数と第 4 引数に適用されます。
466466
467467=item Operators which expect a string
468468
469469(文字列を想定している演算子)
470470
471471=begin original
472472
473473force the argument into the string format. For example, this is
474474applicable to C<printf "%s", $value>.
475475
476476=end original
477477
478478引数を文字列に強制します。
479479例えば、これは C<printf "%s", $value> に適用されます。
480480
481481=back
482482
483483=begin original
484484
485485Though forcing an argument into a particular form does not change the
486486stored number, Perl remembers the result of such conversions. In
487487particular, though the first such conversion may be time-consuming,
488488repeated operations will not need to redo the conversion.
489489
490490=end original
491491
492492引数の特定の形式への強制は保管されている数値は変更しませんが、Perl は変換の
493493結果を覚えています。
494494特に、最初の変換が時間がかかるものであったとしても、同じ操作を繰り返しても
495495変換を再実行する必要はありません。
496496
497497=head1 AUTHOR
498498
499499Ilya Zakharevich C<ilya@math.ohio-state.edu>
500500
501501Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>
502502
503503Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org>
504504
505505=head1 SEE ALSO
506506
507507L<overload>, L<perlop>
508508
509509=begin meta
510510
511511Translate: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-)
512512Status: completed
513513
514514=end meta