Update fmod module of math

- Implement fmod function with test case
This commit is contained in:
johan.park
2019-10-19 14:34:45 +09:00
parent ff54fc88e8
commit 533aa2bc26
2 changed files with 35 additions and 0 deletions

View File

@@ -246,3 +246,23 @@ assert math.modf(NINF) == (-0.0, NINF)
modf_nan = math.modf(NAN)
assert math.isnan(modf_nan[0])
assert math.isnan(modf_nan[1])
assert math.fmod(10, 1) == 0.0
assert math.fmod(10, 0.5) == 0.0
assert math.fmod(10, 1.5) == 1.0
assert math.fmod(-10, 1) == -0.0
assert math.fmod(-10, 0.5) == -0.0
assert math.fmod(-10, 1.5) == -1.0
assert math.isnan(math.fmod(NAN, 1.)) == True
assert math.isnan(math.fmod(1., NAN)) == True
assert math.isnan(math.fmod(NAN, NAN)) == True
assert_raises(ValueError, lambda: math.fmod(1., 0.))
assert_raises(ValueError, lambda: math.fmod(INF, 1.))
assert_raises(ValueError, lambda: math.fmod(NINF, 1.))
assert_raises(ValueError, lambda: math.fmod(INF, 0.))
assert math.fmod(3.0, INF) == 3.0
assert math.fmod(-3.0, INF) == -3.0
assert math.fmod(3.0, NINF) == 3.0
assert math.fmod(-3.0, NINF) == -3.0
assert math.fmod(0.0, 3.0) == 0.0
assert math.fmod(0.0, NINF) == 0.0

View File

@@ -275,6 +275,20 @@ fn math_modf(x: IntoPyFloat, _vm: &VirtualMachine) -> (f64, f64) {
(x.fract(), x.trunc())
}
fn math_fmod(x: IntoPyFloat, y: IntoPyFloat, vm: &VirtualMachine) -> PyResult<f64> {
let x = x.to_f64();
let y = y.to_f64();
if y.is_infinite() && x.is_finite() {
return Ok(x);
}
let r = x % y;
if r.is_nan() && !x.is_nan() && !y.is_nan() {
return Err(vm.new_value_error("math domain error".to_string()));
}
Ok(r)
}
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
@@ -327,6 +341,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"frexp" => ctx.new_rustfunc(math_frexp),
"ldexp" => ctx.new_rustfunc(math_ldexp),
"modf" => ctx.new_rustfunc(math_modf),
"fmod" => ctx.new_rustfunc(math_fmod),
// Rounding functions:
"trunc" => ctx.new_rustfunc(math_trunc),