Skip to content

Commit b45640f

Browse files
authored
Merge pull request RustPython#2263 from hyperbora/fix-math-acos-and-asin
fix math.acos and math.asin
2 parents f890486 + c6ea8f9 commit b45640f

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Lib/test/test_math.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ def testConstants(self):
249249
self.ftest('e', math.e, 2.718281828459045235360287)
250250
self.assertEqual(math.tau, 2*math.pi)
251251

252-
@unittest.skip('TODO: RUSTPYTHON')
253252
def testAcos(self):
254253
self.assertRaises(TypeError, math.acos)
255254
self.ftest('acos(-1)', math.acos(-1), math.pi)
@@ -272,7 +271,6 @@ def testAcosh(self):
272271
self.assertRaises(ValueError, math.acosh, NINF)
273272
self.assertTrue(math.isnan(math.acosh(NAN)))
274273

275-
@unittest.skip('TODO: RUSTPYTHON')
276274
def testAsin(self):
277275
self.assertRaises(TypeError, math.asin)
278276
self.ftest('asin(-1)', math.asin(-1), -math.pi/2)

vm/src/stdlib/math.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,24 @@ fn math_sqrt(value: IntoPyFloat, vm: &VirtualMachine) -> PyResult<f64> {
136136
}
137137

138138
// Trigonometric functions:
139-
make_math_func!(math_acos, acos);
140-
make_math_func!(math_asin, asin);
139+
fn math_acos(x: IntoPyFloat, vm: &VirtualMachine) -> PyResult<f64> {
140+
let x = x.to_f64();
141+
if x.is_nan() || (-1.0_f64..=1.0_f64).contains(&x) {
142+
Ok(x.acos())
143+
} else {
144+
Err(vm.new_value_error("math domain error".to_owned()))
145+
}
146+
}
147+
148+
fn math_asin(x: IntoPyFloat, vm: &VirtualMachine) -> PyResult<f64> {
149+
let x = x.to_f64();
150+
if x.is_nan() || (-1.0_f64..=1.0_f64).contains(&x) {
151+
Ok(x.asin())
152+
} else {
153+
Err(vm.new_value_error("math domain error".to_owned()))
154+
}
155+
}
156+
141157
make_math_func!(math_atan, atan);
142158

143159
fn math_atan2(y: IntoPyFloat, x: IntoPyFloat) -> f64 {

0 commit comments

Comments
 (0)