From 5127d05775ef50e46eb40236e7e49f6f1583c5c4 Mon Sep 17 00:00:00 2001 From: Mick Phillips Date: Wed, 15 Jul 2026 11:46:14 -0700 Subject: [PATCH] Add SingleOps.__round__ and optimizations in Builtin.round. Testing === ``` >>> import System >>> v = 987.654321 >>> assert round(System.Double(v)) == round(System.Single(v)) >>> print(round(System.Double(v), 3), round(System.Single(v), 3)) 987.654 987.654 ``` (Attempted to add assertions in tests/suite/test_builtinfunc.py, but attempting to run fails with ``` RUNNING IRONPYTHON TESTS USING THESE FLAGS: Traceback (most recent call last): File "run.py", line 330, in File "run.py", line 295, in runTest File "run.py", line 241, in runTestSlow File "run.py", line 226, in run_one_command AttributeError: 'module' object has no attribute 'popen3' ``` ) ``` --- src/core/IronPython/Modules/Builtin.cs | 10 ++++++++++ src/core/IronPython/Runtime/Operations/FloatOps.cs | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/core/IronPython/Modules/Builtin.cs b/src/core/IronPython/Modules/Builtin.cs index 92d81bbd9..372955d65 100644 --- a/src/core/IronPython/Modules/Builtin.cs +++ b/src/core/IronPython/Modules/Builtin.cs @@ -1313,6 +1313,10 @@ public static object repr(CodeContext/*!*/ context, object? o) return DoubleOps.__round__(d); } + if (number is float f) { + return SingleOps.__round__(f); + } + if (number is int i) { return Int32Ops.__round__(i); } @@ -1338,6 +1342,9 @@ public static object repr(CodeContext/*!*/ context, object? o) if (number is double d) { return DoubleOps.__round__(d, ndi); } + if (number is float f) { + return SingleOps.__round__(f, ndi); + } if (number is int i) { return Int32Ops.__round__(i, ndi); } @@ -1350,6 +1357,9 @@ public static object repr(CodeContext/*!*/ context, object? o) if (number is double d) { return DoubleOps.__round__(d, ndbi); } + if (number is float f) { + return SingleOps.__round__(f, ndbi); + } if (number is int i) { return Int32Ops.__round__(i, ndbi); } diff --git a/src/core/IronPython/Runtime/Operations/FloatOps.cs b/src/core/IronPython/Runtime/Operations/FloatOps.cs index a086f5e62..bc3f076ef 100644 --- a/src/core/IronPython/Runtime/Operations/FloatOps.cs +++ b/src/core/IronPython/Runtime/Operations/FloatOps.cs @@ -1187,5 +1187,9 @@ public static int __hash__(float x) { public static double __float__(float x) { return x; } + + public static object __round__(float self) => DoubleOps.__round__(self); + + public static float __round__(float self, object ndigits) => (float)DoubleOps.__round__(self, ndigits); } }