Skip to content

Commit 5a14a1d

Browse files
committed
Added various simple functions to math module.
1 parent d02f6ea commit 5a14a1d

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

py/builtinmath.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ MATH_FUN_1(acos, acos)
4040
MATH_FUN_1(asin, asin)
4141
MATH_FUN_1(atan, atan)
4242
MATH_FUN_2(atan2, atan2)
43+
MATH_FUN_1(ceil, ceil)
44+
MATH_FUN_2(copysign, copysign)
45+
MATH_FUN_1(fabs, fabs)
46+
MATH_FUN_1(floor, floor) //TODO: delegate to x.__floor__() if x is not a float
47+
MATH_FUN_2(fmod, fmod)
48+
//MATH_FUN_1(frexp, frexp)
49+
MATH_FUN_1(isfinite, isfinite)
50+
MATH_FUN_1(isinf, isinf)
51+
MATH_FUN_1(isnan, isnan)
52+
MATH_FUN_1(trunc, trunc)
53+
//MATH_FUN_1(, )
54+
//MATH_FUN_1(, )
55+
//MATH_FUN_1(, )
56+
//MATH_FUN_1(, )
57+
//MATH_FUN_1(, )
58+
//MATH_FUN_1(, )
59+
//MATH_FUN_1(, )
60+
//TODO: factorial, fsum, frexp, ldexp, modf
4361

4462
STATIC const mp_map_elem_t mp_module_math_globals_table[] = {
4563
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) },
@@ -65,6 +83,24 @@ STATIC const mp_map_elem_t mp_module_math_globals_table[] = {
6583
{ MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&mp_math_asin_obj },
6684
{ MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&mp_math_atan_obj },
6785
{ MP_OBJ_NEW_QSTR(MP_QSTR_atan2), (mp_obj_t)&mp_math_atan2_obj },
86+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ceil), (mp_obj_t)&mp_math_ceil_obj },
87+
{ MP_OBJ_NEW_QSTR(MP_QSTR_copysign), (mp_obj_t)&mp_math_copysign_obj },
88+
{ MP_OBJ_NEW_QSTR(MP_QSTR_fabs), (mp_obj_t)&mp_math_fabs_obj },
89+
{ MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&mp_math_floor_obj },
90+
{ MP_OBJ_NEW_QSTR(MP_QSTR_fmod), (mp_obj_t)&mp_math_fmod_obj },
91+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_frexp), (mp_obj_t)&mp_math_frexp_obj },
92+
{ MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_math_isfinite_obj },
93+
{ MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_math_isinf_obj },
94+
{ MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_math_isnan_obj },
95+
{ MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj },
96+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_), (mp_obj_t)&mp_math__obj },
97+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_), (mp_obj_t)&mp_math__obj },
98+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_), (mp_obj_t)&mp_math__obj },
99+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_), (mp_obj_t)&mp_math__obj },
100+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_), (mp_obj_t)&mp_math__obj },
101+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_), (mp_obj_t)&mp_math__obj },
102+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_), (mp_obj_t)&mp_math__obj },
103+
//{ MP_OBJ_NEW_QSTR(MP_QSTR_), (mp_obj_t)&mp_math__obj },
68104
};
69105

70106
STATIC const mp_map_t mp_module_math_globals = {

py/qstrdefs.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ Q(acos)
141141
Q(asin)
142142
Q(atan)
143143
Q(atan2)
144+
Q(ceil)
145+
Q(copysign)
146+
Q(fabs)
147+
Q(floor)
148+
Q(fmod)
149+
Q(frexp)
150+
Q(isfinite)
151+
Q(isinf)
152+
Q(isnan)
153+
Q(trunc)
144154

145155
Q(mem_total)
146156
Q(mem_current)

tests/basics/math.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Tests the functions imported from math
2+
3+
from math import *
4+
5+
test_values = [-100., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 100.]
6+
p_test_values = [0.1, 0.5, 1.23456]
7+
unit_range_test_values = [-1., -0.75, -0.5, -0.25, 0., 0.25, 0.5, 0.75, 1.]
8+
#IEEE_test_values = [1, 0, float('NaN'), float('Inf'), -float('NaN'), -float('Inf')]
9+
#TODO: float('NaN')
10+
11+
functions = [(sqrt, p_test_values),
12+
(exp, test_values),
13+
(expm1, test_values),
14+
(log, p_test_values),
15+
(log2, p_test_values),
16+
(log10, p_test_values),
17+
(cosh, test_values),
18+
(sinh, test_values),
19+
(tanh, test_values),
20+
(acosh, [1.0, 5.0, 1.0]),
21+
(asinh, test_values),
22+
(atanh, [-0.99, -0.5, 0.0, 0.5, 0.99]),
23+
(cos, test_values),
24+
(sin, test_values),
25+
(tan, test_values),
26+
(acos, unit_range_test_values),
27+
(asin, unit_range_test_values),
28+
(atan, test_values),
29+
(ceil, test_values),
30+
(fabs, test_values),
31+
(floor, test_values),
32+
#(frexp, test_values),
33+
#(isfinite, [1, 0, float('NaN'), float('Inf')])
34+
(trunc, test_values)
35+
]
36+
37+
for function, test_vals in functions:
38+
for value in test_vals:
39+
print("{:8.7f}".format(function(value)))
40+
41+
binary_functions = [(copysign, [(23., 42.), (-23., 42.), (23., -42.),
42+
(-23., -42.), (1., 0.0), (1., -0.0)])
43+
]
44+
45+
#for function, test_vals in binary_functions:
46+
# for value1, value2 in test_vals:
47+
# print("{:8.7f}".format(function(value1, value2)))
48+
49+

0 commit comments

Comments
 (0)