Skip to content

Commit 5127d05

Browse files
committed
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 <module> 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' ``` ) ```
1 parent 3ef134b commit 5127d05

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

src/core/IronPython/Modules/Builtin.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,10 @@ public static object repr(CodeContext/*!*/ context, object? o)
13131313
return DoubleOps.__round__(d);
13141314
}
13151315

1316+
if (number is float f) {
1317+
return SingleOps.__round__(f);
1318+
}
1319+
13161320
if (number is int i) {
13171321
return Int32Ops.__round__(i);
13181322
}
@@ -1338,6 +1342,9 @@ public static object repr(CodeContext/*!*/ context, object? o)
13381342
if (number is double d) {
13391343
return DoubleOps.__round__(d, ndi);
13401344
}
1345+
if (number is float f) {
1346+
return SingleOps.__round__(f, ndi);
1347+
}
13411348
if (number is int i) {
13421349
return Int32Ops.__round__(i, ndi);
13431350
}
@@ -1350,6 +1357,9 @@ public static object repr(CodeContext/*!*/ context, object? o)
13501357
if (number is double d) {
13511358
return DoubleOps.__round__(d, ndbi);
13521359
}
1360+
if (number is float f) {
1361+
return SingleOps.__round__(f, ndbi);
1362+
}
13531363
if (number is int i) {
13541364
return Int32Ops.__round__(i, ndbi);
13551365
}

src/core/IronPython/Runtime/Operations/FloatOps.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,5 +1187,9 @@ public static int __hash__(float x) {
11871187
public static double __float__(float x) {
11881188
return x;
11891189
}
1190+
1191+
public static object __round__(float self) => DoubleOps.__round__(self);
1192+
1193+
public static float __round__(float self, object ndigits) => (float)DoubleOps.__round__(self, ndigits);
11901194
}
11911195
}

0 commit comments

Comments
 (0)