From 1406f80c3c5949089a0589fa9a17161b7a839fb1 Mon Sep 17 00:00:00 2001 From: Dongin Kim Date: Sat, 11 Sep 2021 19:47:26 +0900 Subject: [PATCH 1/3] fix failed test remainder in test_math add conditions for below rules 1. remainder(x, inf) is x, for non-NaN non-infinite x 2. According to IEEE 754-2008 7.2(f) 2-1. remainder(x, 0) for non-NaN x is invalid operation 2-2. remainder(infinity, x) for non-NaN x is invalid operation --- Lib/test/test_math.py | 2 -- vm/src/stdlib/math.rs | 12 +++++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 7b989428c..267f4c990 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -1296,8 +1296,6 @@ class MathTests(unittest.TestCase): self.ftest('radians(-45)', math.radians(-45), -math.pi/4) self.ftest('radians(0)', math.radians(0), 0) - # TODO: RUSTPYTHON - @unittest.expectedFailure @requires_IEEE_754 def testRemainder(self): from fractions import Fraction diff --git a/vm/src/stdlib/math.rs b/vm/src/stdlib/math.rs index bc5da2f98..094dcde14 100644 --- a/vm/src/stdlib/math.rs +++ b/vm/src/stdlib/math.rs @@ -678,9 +678,10 @@ fn math_fmod(x: IntoPyFloat, y: IntoPyFloat, vm: &VirtualMachine) -> PyResult PyResult { let x = x.to_f64(); let y = y.to_f64(); + if x.is_finite() && y.is_finite() { if y == 0.0 { - return Ok(std::f64::NAN); + return Err(vm.new_value_error("math domain error".to_owned())); } let absx = x.abs(); @@ -696,6 +697,15 @@ fn math_remainder(x: IntoPyFloat, y: IntoPyFloat, vm: &VirtualMachine) -> PyResu return Ok(1.0_f64.copysign(x) * r); } + if x.is_finite() && y.is_infinite() { + return Ok(fmod(x, y)) + } + if x.is_infinite() && y == 0.0 { + return Err(vm.new_value_error("math domain error".to_owned())); + } + if x.is_infinite() && !y.is_nan() { + return Err(vm.new_value_error("math domain error".to_owned())); + } if x.is_nan() { return Ok(x); From 7055a1cb34f72e86a7e125ed428df265f0aa9a2a Mon Sep 17 00:00:00 2001 From: Dongin Kim Date: Sat, 11 Sep 2021 21:18:38 +0900 Subject: [PATCH 2/3] apply cargo fmt --- vm/src/stdlib/math.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/stdlib/math.rs b/vm/src/stdlib/math.rs index 094dcde14..dad93ddde 100644 --- a/vm/src/stdlib/math.rs +++ b/vm/src/stdlib/math.rs @@ -678,7 +678,7 @@ fn math_fmod(x: IntoPyFloat, y: IntoPyFloat, vm: &VirtualMachine) -> PyResult PyResult { let x = x.to_f64(); let y = y.to_f64(); - + if x.is_finite() && y.is_finite() { if y == 0.0 { return Err(vm.new_value_error("math domain error".to_owned())); @@ -698,7 +698,7 @@ fn math_remainder(x: IntoPyFloat, y: IntoPyFloat, vm: &VirtualMachine) -> PyResu return Ok(1.0_f64.copysign(x) * r); } if x.is_finite() && y.is_infinite() { - return Ok(fmod(x, y)) + return Ok(fmod(x, y)); } if x.is_infinite() && y == 0.0 { return Err(vm.new_value_error("math domain error".to_owned())); From 516df295d52171821096c273ddff1608c064f90b Mon Sep 17 00:00:00 2001 From: Dongin Kim Date: Sat, 11 Sep 2021 22:54:16 +0900 Subject: [PATCH 3/3] Reduce duplicated conditions Co-authored-by: Jim Fasarakis-Hilliard --- vm/src/stdlib/math.rs | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/vm/src/stdlib/math.rs b/vm/src/stdlib/math.rs index dad93ddde..3d43cdd65 100644 --- a/vm/src/stdlib/math.rs +++ b/vm/src/stdlib/math.rs @@ -697,29 +697,17 @@ fn math_remainder(x: IntoPyFloat, y: IntoPyFloat, vm: &VirtualMachine) -> PyResu return Ok(1.0_f64.copysign(x) * r); } - if x.is_finite() && y.is_infinite() { - return Ok(fmod(x, y)); - } - if x.is_infinite() && y == 0.0 { - return Err(vm.new_value_error("math domain error".to_owned())); - } if x.is_infinite() && !y.is_nan() { return Err(vm.new_value_error("math domain error".to_owned())); } - - if x.is_nan() { - return Ok(x); - } - if y.is_nan() { - return Ok(y); - } - if x.is_infinite() { - return Ok(std::f64::NAN); + if x.is_nan() || y.is_nan() { + return Ok(f64::NAN); } if y.is_infinite() { - return Err(vm.new_value_error("math domain error".to_owned())); + Ok(x) + } else { + Err(vm.new_value_error("math domain error".to_owned())) } - Ok(x) } #[derive(FromArgs)]