Skip to content

Commit db10a82

Browse files
author
johan.park
committed
Update copysign module of math
- Implement copysign function with test case - Add constants of NAN, INF, NINF on top of `math_module.py`
1 parent 00a0e45 commit db10a82

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

tests/snippets/math_module.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import math
22
from testutils import assert_raises
33

4+
NAN = float('nan')
5+
INF = float('inf')
6+
NINF = float('-inf')
7+
48
# assert(math.exp(2) == math.exp(2.0))
59
# assert(math.exp(True) == math.exp(1.0))
610
#
@@ -30,6 +34,37 @@
3034
assert math.ceil(3.3) == 4
3135
assert math.floor(4.4) == 4
3236

37+
assert math.copysign(1, 42) == 1.0
38+
assert math.copysign(0., 42) == 0.0
39+
assert math.copysign(1., -42) == -1.0
40+
assert math.copysign(3, 0.) == 3.0
41+
assert math.copysign(4., -0.) == -4.0
42+
assert_raises(TypeError, math.copysign)
43+
# copysign should let us distinguish signs of zeros
44+
assert math.copysign(1., 0.) == 1.
45+
assert math.copysign(1., -0.) == -1.
46+
assert math.copysign(INF, 0.) == INF
47+
assert math.copysign(INF, -0.) == NINF
48+
assert math.copysign(NINF, 0.) == INF
49+
assert math.copysign(NINF, -0.) == NINF
50+
# and of infinities
51+
assert math.copysign(1., INF) == 1.
52+
assert math.copysign(1., NINF) == -1.
53+
assert math.copysign(INF, INF) == INF
54+
assert math.copysign(INF, NINF) == NINF
55+
assert math.copysign(NINF, INF) == INF
56+
assert math.copysign(NINF, NINF) == NINF
57+
assert math.isnan(math.copysign(NAN, 1.))
58+
assert math.isnan(math.copysign(NAN, INF))
59+
assert math.isnan(math.copysign(NAN, NINF))
60+
assert math.isnan(math.copysign(NAN, NAN))
61+
# copysign(INF, NAN) may be INF or it may be NINF, since
62+
# we don't know whether the sign bit of NAN is set on any
63+
# given platform.
64+
assert math.isinf(math.copysign(INF, NAN))
65+
# similarly, copysign(2., NAN) could be 2. or -2.
66+
assert abs(math.copysign(2., NAN)) == 2.
67+
3368
class A(object):
3469
def __trunc__(self):
3570
return 2
@@ -91,8 +126,8 @@ def __floor__(self):
91126
assert math.ldexp(0.75, 1) == 1.5
92127
assert_raises(TypeError, lambda: math.ldexp(None, None))
93128

94-
assert math.frexp(float('inf')) == (float('inf'), 0)
95-
assert str(math.frexp(float('nan'))) == str((float('nan'), 0))
129+
assert math.frexp(INF) == (INF, 0)
130+
assert str(math.frexp(NAN)) == str((NAN, 0))
96131
assert_raises(TypeError, lambda: math.frexp(None))
97132

98133
assert math.gcd(0, 0) == 0

vm/src/stdlib/math.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ make_math_func_bool!(math_isfinite, is_finite);
4040
make_math_func_bool!(math_isinf, is_infinite);
4141
make_math_func_bool!(math_isnan, is_nan);
4242

43+
fn math_copysign(a: IntoPyFloat, b: IntoPyFloat, _vm: &VirtualMachine) -> f64 {
44+
let a = a.to_f64();
45+
let b = b.to_f64();
46+
if a.is_nan() || b.is_nan(){
47+
a
48+
} else {
49+
a.copysign(b)
50+
}
51+
}
52+
4353
// Power and logarithmic functions:
4454
make_math_func!(math_exp, exp);
4555
make_math_func!(math_expm1, exp_m1);
@@ -213,6 +223,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
213223
"isfinite" => ctx.new_rustfunc(math_isfinite),
214224
"isinf" => ctx.new_rustfunc(math_isinf),
215225
"isnan" => ctx.new_rustfunc(math_isnan),
226+
"copysign" => ctx.new_rustfunc(math_copysign),
216227

217228
// Power and logarithmic functions:
218229
"exp" => ctx.new_rustfunc(math_exp),

0 commit comments

Comments
 (0)