Commit Graph
33 Commits
Author SHA1 Message Date
OkJaandClaude Opus 5 6b554d96c7 Implement fractional grouping in format specs (#8373)
Support the CPython 3.13+ format spec syntax that puts a grouping option
after the precision, so `format(1234.56789, '.6,f')` gives
'1234.567,890'. The integer and fractional parts group independently and
may use different separators (`,.6_f` -> '1,234.567_890'), so the spec
carries a separate `frac_grouping_option`.

`parse_precision` now consumes the separator that follows the precision
digits and rejects a `,`/`_` mix. A repeated separator is deliberately
left in the spec so the trailing-text check reports it, which is how
CPython arrives at a different message there. A dot followed by neither
digits nor a separator now raises "Format specifier missing precision"
rather than an unrelated error.

Fraction digits group away from the decimal point, so the last group may
be short, and any exponent or percent tail is left intact. The
separators count toward the field width, so zero padding of the integer
part reserves room for them.

'n' takes its separators from the locale and so cannot carry one. The
complex locale path rewrites 'n' to 'g' before delegating, so it has to
validate first; that also makes `format(1+2j, ',n')` fail as it does in
CPython, which it previously did not.

Reference (CPython 3.14), Python/formatter_unicode.c:
- parsing: https://github.com/python/cpython/blob/3.14/Python/formatter_unicode.c#L257-L299
- 'n' rejection: https://github.com/python/cpython/blob/3.14/Python/formatter_unicode.c#L361-L367
- number split: https://github.com/python/cpython/blob/3.14/Python/formatter_unicode.c#L488-L516
- zero-pad width: https://github.com/python/cpython/blob/3.14/Python/formatter_unicode.c#L604-L606

Remove `@unittest.expectedFailure` from tests that now pass
(2 in test_format, 1 in test_float).

Assisted-by: Claude:claude-opus-5

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-07-26 14:31:34 +09:00
Jeong, YunWon d77b80daed Implement round-half-even float.fromhex in common float_ops (#8234)
* Add round-half-even hex float parser to common float_ops

Add float_ops::from_hex returning Result<f64, HexFloatError>, which parses
the coefficient and exponent, rounds round-half-even over the full exponent
range, and detects overflow. Route float.fromhex through it: raise
OverflowError for values too large to represent and ValueError for invalid
input. Remove the expectedFailure on test_from_hex.

Assisted-by: Claude

* Address review: Option-returning byte helpers, const fns, drop test_ prefix

Assisted-by: Claude

* Whitelist fdigits and inity in cspell

Assisted-by: Claude

* Home is_py_ascii_whitespace in rustpython-wtf8; share with common and sre_engine

Assisted-by: Claude

* Use shared wtf8 is_py_ascii_whitespace in bytes_inner

Remove the private copy of is_py_ascii_whitespace in bytes_inner.rs and
import the shared rustpython_common::wtf8::is_py_ascii_whitespace.

Assisted-by: Claude
2026-07-08 10:03:29 +09:00
ChangjoonandShaharNaveh 2d485de90b Round float at the decimal level to match CPython's _Py_dg_dtoa (#7761)
* Round float at the decimal level to match CPython's _Py_dg_dtoa

CPython's `float.__round__` (Objects/floatobject.c) routes through
`_Py_dg_dtoa` and rounds at the decimal level. The previous
`round_float_digits` multiplied by 10**ndigits and rounded at the
IEEE 754 binary level, which diverges for values that aren't exactly
representable. For example, 2.675 stores as 2.67499...; dtoa correctly
rounds it down to 2.67, but `(2.675 * 100.0).round() / 100.0` lands on
2.68 because the multiplication produces a phantom 267.5 tie that
round-half-to-even snaps up.

Rust's `{:.*}` float formatting uses dtoa-style algorithms (Grisu3 +
Dragon4 fallback) and matches CPython's `_Py_dg_dtoa` byte-for-byte.
Replace the multiply-then-round step with `format!` + `parse` for
ndigits >= 0. The ndigits < 0 path is unchanged because dividing
typical inputs by 10**|ndigits| produces genuine ties rather than
synthesizing them.

Verified byte-identical with CPython 3.14.4 over a 108-case random
fuzz plus targeted half-tie probes. Unmasks
`test_float.RoundTestCase.test_matches_float_format` and
`test_previous_round_bugs`.

* Use #[expect] with reason for float_cmp suppression

Co-authored-by: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com>

---------

Co-authored-by: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com>
2026-05-03 00:16:27 +09:00
Lee Dogeon 85e146e868 Add float.from_number classmethod (#7107) 2026-02-13 11:47:12 +00:00
Lee DogeonandClaude Opus 4.6 7b97ff94c6 Mark failing tests
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 18:05:39 +09:00
CPython Developers 7df6664854 Update test_float from v3.14.3 2026-02-13 18:03:21 +09:00
Shahar Naveh 527ce3a872 Update Lib/test/test_float.py from 3.13.7 (#6099)
* Update `Lib/test/test_float.py` from 3.13.7

* Update mathdata

* Unmark passing tests
2025-08-20 14:16:19 +09:00
Shahar Naveh ed8d7157d9 Update test_{complex,float}.py from 3.13.5 (#5961) 2025-07-14 14:20:28 +09:00
Lee Dogeon 5953a938bd chore: test testcases with @unittest.skip decorator (#5871)
Signed-off-by: Lee Dogeon <dev.moreal@gmail.com>
2025-06-30 19:07:58 +09:00
Ashwin Naren a917da3b1a update test_float to 3.13.3 2025-04-15 16:24:10 +09:00
Noa 160363fa46 Fix float parsing (#5643)
* Fix float parsing

* Add rustpython_literal::complex

* Don't call .to_string() on a constant
2025-03-31 14:37:47 +09:00
Ashwin Naren 86e2eb0648 update test_float to 3.12.8 2025-01-19 20:05:34 -08:00
Myeongseon Choi 044f66fba3 copy from cpython v3.12.7 2024-12-05 15:29:35 +09:00
Ankit Goel 8066f36a4e Fix copysign behavior for NaNs (#5396) 2024-09-11 15:19:21 +09:00
Dan Näsman 2335ca0f72 Add proper underscore handling to float and complex types. (#5356) 2024-07-28 12:11:34 +09:00
Andrey Maltsev 77d9c8dcf0 Update test_float.py from Cpython v3.11.2 2023-04-02 14:51:38 +00:00
yt2b 8cb426120b Remove the decorator 2023-01-22 14:44:14 +09:00
yt2b e4102aae8d Remove the decorator 2023-01-15 21:42:25 +09:00
yt2b 8828fd17a0 Alternate float format doesn't work correctly. (#4446) 2023-01-14 16:48:03 +09:00
joohongpark abb994d410 Fix class method 2022-10-16 11:59:05 +09:00
Gilteun Choi 13acb1ce6e Precision bug fix when FormatType is None (#3975) 2022-08-04 22:34:31 +09:00
Gyubong Lee 06056908c9 Fixes nan 2022-07-25 18:07:10 +09:00
Jeong Yunwon 3d27f163e4 adjust failing markers for test_{builtin,decimal,float,range} 2022-07-18 04:38:26 +09:00
CPython developers 33690be35b Update tests from CPython 3.10.5 2022-07-18 04:07:35 +09:00
Kangzhi Shi 3e6e348a67 impl number protocol float 2022-05-30 07:36:08 +09:00
Noa 588247c4a0 Unskip tests 2021-10-30 19:42:35 -05:00
Codemonk-adi a07d3432c1 Redid the commit cause I messed up 2021-09-21 21:58:51 +05:30
Padraic FanningandTetramad 95b514b85d Implement __floor__ for float type
Co-authored-by: Tetramad <cbwjd777@gmail.com>
2021-08-11 14:30:10 +09:00
Padraic FanningandTetramad fae7b0e8ed Implement __ceil__ for float type
Co-authored-by: Tetramad <cbwjd777@gmail.com>
2021-08-11 14:30:10 +09:00
Padraic FanningandTetramad 9f497fb65a Add test_float from CPython 3.9.1
Co-authored-by: Tetramad <cbwjd777@gmail.com>
2021-08-11 14:30:10 +09:00
Noah 1fff67db2c Correct float rounding algorithm 2021-03-25 08:06:58 -05:00
Padraic Fanning fb3ec9758e Mark failed/errored tests 2021-02-20 20:37:25 -05:00
Padraic Fanning 290f464494 Add test_float from CPython 3.8.7 2021-02-20 20:37:24 -05:00