31 Commits

Author SHA1 Message Date
Changjoon
c2910c06f3 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
4b1ff4b003 Add float.from_number classmethod (#7107) 2026-02-13 11:47:12 +00:00
Lee Dogeon
fb1514b6e8 Mark failing tests
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 18:05:39 +09:00
CPython Developers
18a264882b 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
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 Fanning
95b514b85d Implement __floor__ for float type
Co-authored-by: Tetramad <cbwjd777@gmail.com>
2021-08-11 14:30:10 +09:00
Padraic Fanning
fae7b0e8ed Implement __ceil__ for float type
Co-authored-by: Tetramad <cbwjd777@gmail.com>
2021-08-11 14:30:10 +09:00
Padraic Fanning
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