Commit b1bd06a
committed
gh-NNNN: Add regression tests for round(x, k) with negative k and large x
Add RoundTestCase.test_round_neg_ndigits_large to Lib/test/test_float.py.
These 20 cases expose the failure mode of the pure-arithmetic fallback
for float.__round__ with negative ndigits: dividing a large float by
10^(-k) loses ULPs, pushing a value that is already a precise multiple
of 10^k to the adjacent float, causing the round-trip to return the
wrong neighbour.
Each test value is already an exact multiple of 10^k within float
precision, so round(x, k) must return x unchanged. The arithmetic
fallback (x / 10^k, round, * 10^k) returns the adjacent float instead,
1 ULP off from the correct answer.
The test is decorated with @skipUnless float_repr_style == 'short'
because the arithmetic fallback is the only implementation on platforms
without a correctly-rounded decimal conversion (_PY_SHORT_FLOAT_REPR==0),
and those platforms are known to have this limitation.
Cases were found by random search comparing the dtoa string round-trip
against the arithmetic fallback across 500 000 random large floats.1 parent 8ff0b7d commit b1bd06a
1 file changed
+40
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
962 | 962 | | |
963 | 963 | | |
964 | 964 | | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
965 | 1005 | | |
966 | 1006 | | |
967 | 1007 | | |
| |||
0 commit comments