-
Notifications
You must be signed in to change notification settings - Fork 603
perldata: Add details to hex, etc floats #23988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khwilliamson
wants to merge
1
commit into
Perl:blead
Choose a base branch
from
khwilliamson:float_doc
base: blead
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+34
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -448,12 +448,14 @@ integer formats: | |
| 0b011011 # binary | ||
| 0x1.999ap-4 # hexadecimal floating point (the 'p' is required) | ||
| 07.65p2 # octal floating point (the 'p' is required) | ||
| 0o7.65p2 # alternative octal floating point | ||
| 0o7.65p2 # alternative octal floating point ('p' still | ||
| # required) | ||
| 0b101.01p-1 # binary floating point (the 'p' is required) | ||
|
|
||
| You are allowed to use underscores (underbars) in numeric literals | ||
| between digits for legibility (but not multiple underscores in a row: | ||
| C<23__500> is not legal; C<23_500> is). | ||
| between digits for legibility (multiple underscores in a row are | ||
| tolerated but warn: | ||
| C<23__500> warns; C<23_500> is fine, doesn't warn). | ||
| You could, for example, group binary | ||
| digits by threes (as for a Unix-style mode argument such as 0b110_100_100) | ||
| 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 | |
| forms. See L<perlop/"Quote and Quote-like Operators"> for a list. | ||
| X<string, literal> | ||
|
|
||
| Hexadecimal, octal, or binary, representations in string literals | ||
| Hexadecimal, octal, or binary representations in string literals | ||
| (e.g. '0xff') are not automatically converted to their integer | ||
| representation. The hex() and oct() functions make these conversions | ||
| for you. See L<perlfunc/hex> and L<perlfunc/oct> for more details. | ||
|
|
||
| Hexadecimal floating point can start just like a hexadecimal literal, | ||
| and it can be followed by an optional fractional hexadecimal part, | ||
| but it must be followed by C<p>, an optional sign, and a power of two. | ||
| but it must be followed by C<p>, an optional sign, and an exponent. The | ||
| digits in the exponent are interpreted as being base 10 (digits 0-9 are | ||
| accepted), but the actual value is two raised to that power. Thus | ||
|
|
||
| 5e2 == 5 * 10-squared == 500 | ||
| 0x5p2 == 5 * 2-squared == 20 | ||
|
|
||
| The format is useful for accurately presenting floating point values, | ||
| avoiding conversions to or from decimal floating point, and therefore | ||
| avoiding possible loss in precision. Notice that while most current | ||
|
|
@@ -485,7 +493,27 @@ rounding modes, which can differ between CPUs, operating systems, | |
| and compilers, and which Perl doesn't control. | ||
|
|
||
| Octal and binary floating point numbers use the same format as hexadecimal | ||
| floating point numbers, but limited to binary and octal digits, respectively. | ||
| floating point numbers, but the digits in their integer and fractional | ||
| components are limited to binary and octal, respectively. | ||
|
|
||
| Capital 'P' may be used to signify a binary-style exponent, besides | ||
| lowercase 'p'; just as capital 'E' can be used instead of 'e' for | ||
| 10-based. Here are some examples: | ||
|
|
||
| 05p2 # 20 | ||
| 05.0P2 # 20 | ||
| 05.P2 # 20 | ||
| 0b101p2 # 20 | ||
| 0b1p10 # 1024 | ||
| 0B1p11 # 2048 | ||
| 0x5e2 # Illegal | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is misleading; |
||
| 05e2 # Illegal | ||
| 5p2 # Illegal | ||
|
|
||
| Like the corresponding integers, hex, octal, and binary, float | ||
| representations in string literals are not automatically converted to | ||
| their floating point. Use C<eval> for this. (This limitation is due to | ||
| ambiguity with the dot being interpreted as the concatenation operator.) | ||
|
|
||
| You can also embed newlines directly in your strings, i.e., they can end | ||
| on a different line than they begin. This is nice, but if you forget | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An example with a 2 digit exponent for one of the bitwise bases (hex etc) would better illustrate the base-10-ness of the exponent number vs the base-2-ness of the power.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point; I've added two. The problem is to choose something that can be calculated in one's head. And I think that means only binary, and that is not introduced immediately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exponent is always$2^N$ , so the significand doesn't need to be binary:
though perhaps I got to know my powers of two too well.
Binary formats describing the values might be more readable: