Skip to content

Commit b745378

Browse files
committed
perldata: Add details to hex, etc floats
1 parent d5c9865 commit b745378

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

pod/perldata.pod

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,14 @@ integer formats:
448448
0b011011 # binary
449449
0x1.999ap-4 # hexadecimal floating point (the 'p' is required)
450450
07.65p2 # octal floating point (the 'p' is required)
451-
0o7.65p2 # alternative octal floating point
451+
0o7.65p2 # alternative octal floating point ('p' still
452+
# required)
452453
0b101.01p-1 # binary floating point (the 'p' is required)
453454

454455
You are allowed to use underscores (underbars) in numeric literals
455-
between digits for legibility (but not multiple underscores in a row:
456-
C<23__500> is not legal; C<23_500> is).
456+
between digits for legibility (multiple underscores in a row are
457+
tolerated but warn:
458+
C<23__500> warns; C<23_500> is fine, doesn't warn).
457459
You could, for example, group binary
458460
digits by threes (as for a Unix-style mode argument such as 0b110_100_100)
459461
or by fours (to represent nibbles, as in 0b1010_0110) or in other groups.
@@ -468,14 +470,20 @@ characters such as newline, tab, etc., as well as some more exotic
468470
forms. See L<perlop/"Quote and Quote-like Operators"> for a list.
469471
X<string, literal>
470472

471-
Hexadecimal, octal, or binary, representations in string literals
473+
Hexadecimal, octal, or binary representations in string literals
472474
(e.g. '0xff') are not automatically converted to their integer
473475
representation. The hex() and oct() functions make these conversions
474476
for you. See L<perlfunc/hex> and L<perlfunc/oct> for more details.
475477

476478
Hexadecimal floating point can start just like a hexadecimal literal,
477479
and it can be followed by an optional fractional hexadecimal part,
478-
but it must be followed by C<p>, an optional sign, and a power of two.
480+
but it must be followed by C<p>, an optional sign, and an exponent. The
481+
digits in the exponent are interpreted as being base 10 (digits 0-9 are
482+
accepted), but the actual value is two raised to that power. Thus
483+
484+
5e2 == 5 * 10-squared == 500
485+
0x5p2 == 5 * 2-squared == 20
486+
479487
The format is useful for accurately presenting floating point values,
480488
avoiding conversions to or from decimal floating point, and therefore
481489
avoiding possible loss in precision. Notice that while most current
@@ -485,7 +493,27 @@ rounding modes, which can differ between CPUs, operating systems,
485493
and compilers, and which Perl doesn't control.
486494

487495
Octal and binary floating point numbers use the same format as hexadecimal
488-
floating point numbers, but limited to binary and octal digits, respectively.
496+
floating point numbers, but the digits in their integer and fractional
497+
components are limited to binary and octal, respectively.
498+
499+
Capital 'P' may be used to signify a binary-style exponent, besides
500+
lowercase 'p'; just as capital 'E' can be used instead of 'e' for
501+
10-based. Here are some examples:
502+
503+
05p2 # 20
504+
05.0P2 # 20
505+
05.P2 # 20
506+
0b101p2 # 20
507+
0b1p10 # 1024
508+
0B1p11 # 2048
509+
0x5e2 # Illegal
510+
05e2 # Illegal
511+
5p2 # Illegal
512+
513+
Like the corresponding integers, hex, octal, and binary, float
514+
representations in string literals are not automatically converted to
515+
their floating point. Use C<eval> for this. (This limitation is due to
516+
ambiguity with the dot being interpreted as the concatenation operator.)
489517

490518
You can also embed newlines directly in your strings, i.e., they can end
491519
on a different line than they begin. This is nice, but if you forget

0 commit comments

Comments
 (0)