-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
py/formatfloat: Format ALL whole-number floats without decimal cruft. #8905
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
Closed
Closed
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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# check a case where rounding was suppressed inappropriately when "f" was | ||
# promoted to "e" for large numbers. | ||
v = 8.888e32 | ||
print("%.2f" % v) # '%.2f' format with e32 becomes '%.2e', expect 8.89e+32. |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
8.89e+32 |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Test that integers format to exact values. | ||
|
||
for b in [13, 123, 457, 23456]: | ||
for r in range(1, 10): | ||
e_fmt = "{:." + str(r) + "e}" | ||
f_fmt = "{:." + str(r) + "f}" | ||
g_fmt = "{:." + str(r) + "g}" | ||
for e in range(0, 5): | ||
f = b * (10**e) | ||
title = str(b) + " x 10^" + str(e) | ||
print(title, "with format", e_fmt, "gives", e_fmt.format(f)) | ||
print(title, "with format", f_fmt, "gives", f_fmt.format(f)) | ||
print(title, "with format", g_fmt, "gives", g_fmt.format(f)) | ||
|
||
# Check that powers of 10 (that fit in float32) format correctly. | ||
for i in range(31): | ||
# It works to 12 digits on all platforms *except* qemu-arm, where | ||
# 10^11 comes out as 10000000820 or something. | ||
print("{:.7g}".format(float("1e" + str(i)))) | ||
|
||
# 16777215 is 2^24 - 1, the largest integer that can be completely held | ||
# in a float32. | ||
print("{:f}".format(16777215)) | ||
# 4294967040 = 16777215 * 128 is the largest integer that is exactly | ||
# represented by a float32 and that will also fit within a (signed) int32. | ||
# The upper bound of our integer-handling code is actually double this, | ||
# but that constant might cause trouble on systems using 32 bit ints. | ||
print("{:f}".format(2147483520)) | ||
# Very large positive integers can be a test for precision and resolution. | ||
# This is a weird way to represent 1e38 (largest power of 10 for float32). | ||
print("{:.6e}".format(float("9" * 30 + "e8"))) |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Test formatting of very large ints. | ||
# Relies on double-precision floats. | ||
|
||
import array | ||
import sys | ||
|
||
# Challenging way to express 1e200 and 1e100. | ||
print("{:.12e}".format(float("9" * 400 + "e-200"))) | ||
print("{:.12e}".format(float("9" * 400 + "e-300"))) | ||
|
||
# These correspond to the binary representation of 1e200 in float64s: | ||
v1 = 0x54B249AD2594C37D # 1e100 | ||
v2 = 0x6974E718D7D7625A # 1e200 | ||
print("{:.12e}".format(array.array("d", v1.to_bytes(8, sys.byteorder))[0])) | ||
print("{:.12e}".format(array.array("d", v2.to_bytes(8, sys.byteorder))[0])) |
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
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
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.
Please add the 2 test cases using the explicit representation and array.array
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.
Won't that fail on builds with
I guess there's some way to gate or condition tests based on build conditions. I'll look into it.
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.
I'm going with a separate
tests/float/float_format_int_doubleprec.py
, which I'll add to theif upy_float_precision < 64
skips inrun-tests.py
.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.
Sounds good!