diff options
-rw-r--r-- | MANIFEST | 1 | ||||
-rw-r--r-- | pod/Makefile | 4 | ||||
-rw-r--r-- | pod/buildtoc | 2 | ||||
-rw-r--r-- | pod/perl.pod | 1 | ||||
-rw-r--r-- | pod/perlnumber.pod | 185 | ||||
-rw-r--r-- | pod/perltoc.pod | 37 | ||||
-rw-r--r-- | pod/roffitall | 2 |
7 files changed, 225 insertions, 7 deletions
@@ -1091,6 +1091,7 @@ pod/perllol.pod How to use lists of lists pod/perlmod.pod Module mechanism info pod/perlmodinstall.pod Installing CPAN Modules pod/perlmodlib.pod Module policy info +pod/perlnumber.pod Semantics of numbers and numeric operations pod/perlobj.pod Object info pod/perlop.pod Operator info pod/perlopentut.pod open() tutorial diff --git a/pod/Makefile b/pod/Makefile index 52f7e1e365..50fb270865 100644 --- a/pod/Makefile +++ b/pod/Makefile @@ -48,6 +48,7 @@ POD = \ perlthrtut.pod \ perldbmfilter.pod \ perldebug.pod \ + perlnumber.pod \ perldiag.pod \ perlsec.pod \ perltrap.pod \ @@ -113,6 +114,7 @@ MAN = \ perlthrtut.man \ perldbmfilter.man \ perldebug.man \ + perlnumber.man \ perldiag.man \ perlsec.man \ perltrap.man \ @@ -178,6 +180,7 @@ HTML = \ perlthrtut.html \ perldbmfilter.html \ perldebug.html \ + perlnumber.html \ perldiag.html \ perlsec.html \ perltrap.html \ @@ -243,6 +246,7 @@ TEX = \ perlthrtut.tex \ perldbmfilter.tex \ perldebug.tex \ + perlnumber.tex \ perldiag.tex \ perlsec.tex \ perltrap.tex \ diff --git a/pod/buildtoc b/pod/buildtoc index dd8638ac0b..d947ddccee 100644 --- a/pod/buildtoc +++ b/pod/buildtoc @@ -11,7 +11,7 @@ sub output ($); perlmod perlmodlib perlmodinstall perlfork perlform perllocale perlref perlreftut perldsc perllol perlboot perltoot perltootc perlobj perltie perlbot perlipc - perldbmfilter perldebug + perldbmfilter perldebug perlnumber perldiag perlsec perltrap perlport perlstyle perlpod perlbook perlembed perlapio perlxs perlxstut perlguts perlcall perlcompile perlapi perlintern perlhist diff --git a/pod/perl.pod b/pod/perl.pod index 9fc6c1dbd3..0414fa4f29 100644 --- a/pod/perl.pod +++ b/pod/perl.pod @@ -55,6 +55,7 @@ sections: perlcompile Perl compiler suite intro perldebug Perl debugging perldiag Perl diagnostic messages + perlnumber Perl number semantics perlsec Perl security perltrap Perl traps for the unwary perlport Perl portability guide diff --git a/pod/perlnumber.pod b/pod/perlnumber.pod new file mode 100644 index 0000000000..c05b066b43 --- /dev/null +++ b/pod/perlnumber.pod @@ -0,0 +1,185 @@ +=head1 NAME + +perlnumber - semantics of numbers and numeric operations in Perl + +=head1 SYNOPSIS + + $n = 1234; # decimal integer + $n = 0b1110011; # binary integer + $n = 01234; # octal integer + $n = 0x1234; # hexadecimal integer + $n = 12.34e-56; # exponential notation + $n = "-12.34e56"; # number specified as a string + $n = "1234"; # number specified as a string + $n = v49.50.51.52; # number specified as a string, which in + # turn is specified in terms of numbers :-) + +=head1 DESCRIPTION + +This document describes how Perl internally handles numeric values. + +Perl's operator overloading facility is completely ignored here. Operator +overloading allows user-defined behaviors for numbers, such as operations +over arbitrarily large integers, floating points numbers with arbitrary +precision, operations over "exotic" numbers such as modular arithmetic or +p-adic arithmetic, and so on. See L<perlovl> for details. + +=head1 Storing numbers + +Perl can internally represents numbers in 3 different ways: as native +integers, as native floating point numbers, and as decimal strings. +Decimal strings may have an exponential notation part, as in C<"12.34e-56">. +I<Native> here means "a format supported by the C compiler which was used +to build perl". + +The term "native" does not mean quite as much when we talk about native +integers, as it does when native floating point numbers are involved. +The only implication of the term "native" on integers is that the limits for +the maximal and the minimal supported true integral quantities are close to +powers of 2. However, for "native" floats have a most fundamental +restriction: they may represent only those numbers which have a relatively +"short" representation when converted to a binary fraction. For example, +0.9 cannot be respresented by a native float, since the binary fraction +for 0.9 is infinite: + + binary0.1110011001100... + +with the sequence C<1100> repeating again and again. In addition to this +limitation, the exponent of the binary number is also restricted when it +is represented as a floating point number. On typical hardware, floating +point values can store numbers with up to 53 binary digits, and with binary +exponents between -1024 and 1024. In decimal representation this is close +to 16 decimal digits and decimal exponents in the range of -304..304. +The upshot of all this is that Perl cannot store a number like +12345678901234567 as a floating point number on such architectures without +loss of information. + +Similarly, decimal strings may represent only those numbers which have a +finite decimal expansion. Being strings, and thus of arbitrary length, there +is no practical limit for the exponent or number of decimal digits for these +numbers. (But realize that what we are discussing the rules for just the +I<storage> of these numbers. The fact that you can store such "large" numbers +does not mean that that the I<operations> over these numbers will use all +of the significant digits. +See L<"Numeric operations and numeric conversions"> for details.) + +In fact numbers stored in the native integer format may be stored either +in the signed native form, or in the unsigned native form. Thus the limits +for Perl numbers stored as native integers would typically be -2**31..2**32-1, +with appropriate modifications in the case of 64-bit integers. Again, this +does not mean that Perl can do operations only over integers in this range: +it is possible to store many more integers in floating point format. + +Summing up, Perl numeric values can store only those numbers which have +a finite decimal expansion or a "short" binary expansion. + +=head1 Numeric operators and numeric conversions + +As mentioned earlier, Perl can store a number in any one of three formats, +but most operators typically understand only one of those formats. When +a numeric value is passed as an argument to such an operator, it will be +converted to the format understood by the operator. + +Six such conversions are possible: + + native integer --> native floating point (*) + native integer --> decimal string + native floating_point --> native integer (*) + native floating_point --> decimal string (*) + decimal string --> native integer + decimal string --> native floating point (*) + +These conversions are governed by the following general rules: + +=over + +=item * + +If the source number can be represented in the target form, that +representation is used. + +=item * + +If the source number is outside of the limits representable in the target form, +a representation of the closest limit is used. (I<Loss of information>) + +=item * + +If the source number is between two numbers representable in the target form, +a representation of one of these numbers is used. (I<Loss of information>) + +=item * + +In C<< native floating point --> native integer >> conversions the magnitude +of the result is less than or equal to the magnitude of the source. +(I<"Rounding to zero".>) + +=item * + +If the C<< decimal string --> native integer >> conversion cannot be done +without loss of information, the result is compatible with the conversion +sequence C<< decimal_string --> native_floating_point --> native_integer >>. +In particular, rounding is strongly biased to 0, though a number like +C<"0.99999999999999999999"> has a chance of being rounded to 1. + +=back + +B<RESTRICTION>: The conversions marked with C<(*)> above involve steps +performed by the C compiler. In particular, bugs/features of the compiler +used may lead to breakage of some of the above rules. + +=head1 Flavors of Perl numeric operations + +Perl operations which take a numeric argument treat that argument in one +of four different ways: they may force it to one of the integer/floating/ +string formats, or they may behave differently depending on the format of +the operand. Forcing a numeric value to a particular format does not +change the number stored in the value. + +All the operators which need an argument in the integer format treat the +argument as in modular arithmetic, e.g., C<mod 2**32> on a 32-bit +architecture. C<sprintf "%u", -1> therefore provides the same result as +C<sprintf "%u", ~0>. + +=over + +=item Arithmetic operators except, C<no integer> + +force the argument into the floating point format. + +=item Arithmetic operators except, C<use integer> + +=item Bitwise operators, C<no integer> + +force the argument into the integer format if it is not a string. + +=item Bitwise operators, C<use integer> + +force the argument into the integer format + +=item Operators which expect an integer + +force the argument into the integer format. This is applicable +to the third and fourth arguments of C<sysread>, for example. + +=item Operators which expect a string + +force the argument into the string format. For example, this is +applicable to C<printf "%s", $value>. + +=back + +Though forcing an argument into a particular form does not change the +stored number, Perl remembers the result of such conversions. In +particular, though the first such conversion may be time-consuming, +repeated operations will not need to redo the conversion. + +=head1 AUTHOR + +Ilya Zakharevich C<ilya@math.ohio-state.edu> + +Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com> + +=head1 SEE ALSO + +L<perlovl> diff --git a/pod/perltoc.pod b/pod/perltoc.pod index bc45dd88ad..83d40d4241 100644 --- a/pod/perltoc.pod +++ b/pod/perltoc.pod @@ -1608,7 +1608,7 @@ y/SEARCHLIST/REPLACEMENTLIST/cdsUC Finding the end, Removal of backslashes before delimiters, Interpolation, C<<<'EOF'>, C<m''>, C<s'''>, C<tr///>, C<y///>, C<''>, C<q//>, C<"">, -C<``>, C<qq//>, C<qx//>, C<<file*globE<gt>>, C<?RE?>, C</RE/>, C<m/RE/>, +C<``>, C<qq//>, C<qx//>, C<< <file*glob> >>, C<?RE?>, C</RE/>, C<m/RE/>, C<s/RE/foo/>,, Interpolation of regular expressions, Optimization of regular expressions @@ -2923,6 +2923,31 @@ C<anchored(TYPE)> =back +=head2 perlnumber - semantics of numbers and numeric operations in Perl + +=over + +=item SYNOPSIS + +=item DESCRIPTION + +=item Storing numbers + +=item Numeric operators and numeric conversions + +=item Flavors of Perl numeric operations + +Arithmetic operators except, C<no integer>, Arithmetic operators except, +C<use integer>, Bitwise operators, C<no integer>, Bitwise operators, C<use +integer>, Operators which expect an integer, Operators which expect a +string + +=item AUTHOR + +=item SEE ALSO + +=back + =head2 perldiag - various Perl diagnostics =over @@ -3617,9 +3642,9 @@ C<void save_hptr(HV **hptr)> An Error Handler, An Event Driven Program -=item THE PERL_CALL FUNCTIONS +=item THE CALL_ FUNCTIONS -perl_call_sv, perl_call_pv, perl_call_method, perl_call_argv +call_sv, call_pv, call_method, call_argv =item FLAG VALUES @@ -3665,11 +3690,11 @@ perl_call_sv, perl_call_pv, perl_call_method, perl_call_argv =item Using G_KEEPERR -=item Using perl_call_sv +=item Using call_sv -=item Using perl_call_argv +=item Using call_argv -=item Using perl_call_method +=item Using call_method =item Using GIMME_V diff --git a/pod/roffitall b/pod/roffitall index 9f9e3e9e49..ebfcc7e264 100644 --- a/pod/roffitall +++ b/pod/roffitall @@ -49,6 +49,7 @@ toroff=` $mandir/perlreftut.1 \ $mandir/perldsc.1 \ $mandir/perllol.1 \ + $mandir/perlboot.1 \ $mandir/perltoot.1 \ $mandir/perlobj.1 \ $mandir/perltie.1 \ @@ -56,6 +57,7 @@ toroff=` $mandir/perlipc.1 \ $mandir/perlthrtut.1 \ $mandir/perldebug.1 \ + $mandir/perlnumber.1 \ $mandir/perldiag.1 \ $mandir/perlsec.1 \ $mandir/perltrap.1 \ |