Numworks Epsilon  1.4.1
Graphing Calculator Operating System
modmath.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013-2017 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include "py/builtin.h"
28 #include "py/runtime.h"
29 
30 #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH
31 
32 #include <math.h>
33 
34 // M_PI is not part of the math.h standard and may not be defined
35 // And by defining our own we can ensure it uses the correct const format.
36 #define MP_PI MICROPY_FLOAT_CONST(3.14159265358979323846)
37 
38 STATIC NORETURN void math_error(void) {
39  mp_raise_ValueError("math domain error");
40 }
41 
42 STATIC mp_obj_t math_generic_1(mp_obj_t x_obj, mp_float_t (*f)(mp_float_t)) {
43  mp_float_t x = mp_obj_get_float(x_obj);
44  mp_float_t ans = f(x);
45  if ((isnan(ans) && !isnan(x)) || (isinf(ans) && !isinf(x))) {
46  math_error();
47  }
48  return mp_obj_new_float(ans);
49 }
50 
51 STATIC mp_obj_t math_generic_2(mp_obj_t x_obj, mp_obj_t y_obj, mp_float_t (*f)(mp_float_t, mp_float_t)) {
52  mp_float_t x = mp_obj_get_float(x_obj);
53  mp_float_t y = mp_obj_get_float(y_obj);
54  mp_float_t ans = f(x, y);
55  if ((isnan(ans) && !isnan(x) && !isnan(y)) || (isinf(ans) && !isinf(x))) {
56  math_error();
57  }
58  return mp_obj_new_float(ans);
59 }
60 
61 #define MATH_FUN_1(py_name, c_name) \
62  STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { \
63  return math_generic_1(x_obj, MICROPY_FLOAT_C_FUN(c_name)); \
64  } \
65  STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
66 
67 #define MATH_FUN_1_TO_BOOL(py_name, c_name) \
68  STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_bool(c_name(mp_obj_get_float(x_obj))); } \
69  STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
70 
71 #define MATH_FUN_1_TO_INT(py_name, c_name) \
72  STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_int_from_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \
73  STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
74 
75 #define MATH_FUN_2(py_name, c_name) \
76  STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj, mp_obj_t y_obj) { \
77  return math_generic_2(x_obj, y_obj, MICROPY_FLOAT_C_FUN(c_name)); \
78  } \
79  STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name);
80 
81 #define MATH_FUN_2_FLT_INT(py_name, c_name) \
82  STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj, mp_obj_t y_obj) { \
83  return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj), mp_obj_get_int(y_obj))); \
84  } \
85  STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name);
86 
87 #if MP_NEED_LOG2
88 #undef log2
89 #undef log2f
90 // 1.442695040888963407354163704 is 1/_M_LN2
91 mp_float_t MICROPY_FLOAT_C_FUN(log2)(mp_float_t x) {
92  return MICROPY_FLOAT_C_FUN(log)(x) * MICROPY_FLOAT_CONST(1.442695040888963407354163704);
93 }
94 #endif
95 
96 // sqrt(x): returns the square root of x
97 MATH_FUN_1(sqrt, sqrt)
98 // pow(x, y): returns x to the power of y
99 MATH_FUN_2(pow, pow)
100 // exp(x)
101 MATH_FUN_1(exp, exp)
102 #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
103 // expm1(x)
104 MATH_FUN_1(expm1, expm1)
105 // log2(x)
106 MATH_FUN_1(log2, log2)
107 // log10(x)
108 MATH_FUN_1(log10, log10)
109 // cosh(x)
110 MATH_FUN_1(cosh, cosh)
111 // sinh(x)
112 MATH_FUN_1(sinh, sinh)
113 // tanh(x)
114 MATH_FUN_1(tanh, tanh)
115 // acosh(x)
116 MATH_FUN_1(acosh, acosh)
117 // asinh(x)
118 MATH_FUN_1(asinh, asinh)
119 // atanh(x)
120 MATH_FUN_1(atanh, atanh)
121 #endif
122 // cos(x)
123 MATH_FUN_1(cos, cos)
124 // sin(x)
125 MATH_FUN_1(sin, sin)
126 // tan(x)
127 MATH_FUN_1(tan, tan)
128 // acos(x)
129 MATH_FUN_1(acos, acos)
130 // asin(x)
131 MATH_FUN_1(asin, asin)
132 // atan(x)
133 MATH_FUN_1(atan, atan)
134 // atan2(y, x)
135 MATH_FUN_2(atan2, atan2)
136 // ceil(x)
137 MATH_FUN_1_TO_INT(ceil, ceil)
138 // copysign(x, y)
139 STATIC mp_float_t MICROPY_FLOAT_C_FUN(copysign_func)(mp_float_t x, mp_float_t y) {
140  return MICROPY_FLOAT_C_FUN(copysign)(x, y);
141 }
142 MATH_FUN_2(copysign, copysign_func)
143 // fabs(x)
144 STATIC mp_float_t MICROPY_FLOAT_C_FUN(fabs_func)(mp_float_t x) {
145  return MICROPY_FLOAT_C_FUN(fabs)(x);
146 }
147 MATH_FUN_1(fabs, fabs_func)
148 // floor(x)
149 MATH_FUN_1_TO_INT(floor, floor) //TODO: delegate to x.__floor__() if x is not a float
150 // fmod(x, y)
151 MATH_FUN_2(fmod, fmod)
152 // isfinite(x)
153 MATH_FUN_1_TO_BOOL(isfinite, isfinite)
154 // isinf(x)
155 MATH_FUN_1_TO_BOOL(isinf, isinf)
156 // isnan(x)
157 MATH_FUN_1_TO_BOOL(isnan, isnan)
158 // trunc(x)
159 MATH_FUN_1_TO_INT(trunc, trunc)
160 // ldexp(x, exp)
161 MATH_FUN_2_FLT_INT(ldexp, ldexp)
162 #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
163 // erf(x): return the error function of x
164 MATH_FUN_1(erf, erf)
165 // erfc(x): return the complementary error function of x
166 MATH_FUN_1(erfc, erfc)
167 // gamma(x): return the gamma function of x
168 MATH_FUN_1(gamma, tgamma)
169 // lgamma(x): return the natural logarithm of the gamma function of x
170 MATH_FUN_1(lgamma, lgamma)
171 #endif
172 //TODO: factorial, fsum
173 
174 // Function that takes a variable number of arguments
175 
176 // log(x[, base])
177 STATIC mp_obj_t mp_math_log(size_t n_args, const mp_obj_t *args) {
178  mp_float_t x = mp_obj_get_float(args[0]);
179  if (x <= (mp_float_t)0.0) {
180  math_error();
181  }
182  mp_float_t l = MICROPY_FLOAT_C_FUN(log)(x);
183  if (n_args == 1) {
184  return mp_obj_new_float(l);
185  } else {
186  mp_float_t base = mp_obj_get_float(args[1]);
187  if (base <= (mp_float_t)0.0) {
188  math_error();
189  } else if (base == (mp_float_t)1.0) {
190  mp_raise_msg(&mp_type_ZeroDivisionError, "division by zero");
191  }
192  return mp_obj_new_float(l / MICROPY_FLOAT_C_FUN(log)(base));
193  }
194 }
195 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_math_log_obj, 1, 2, mp_math_log);
196 
197 // Functions that return a tuple
198 
199 // frexp(x): converts a floating-point number to fractional and integral components
200 STATIC mp_obj_t mp_math_frexp(mp_obj_t x_obj) {
201  int int_exponent = 0;
202  mp_float_t significand = MICROPY_FLOAT_C_FUN(frexp)(mp_obj_get_float(x_obj), &int_exponent);
203  mp_obj_t tuple[2];
204  tuple[0] = mp_obj_new_float(significand);
205  tuple[1] = mp_obj_new_int(int_exponent);
206  return mp_obj_new_tuple(2, tuple);
207 }
208 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_frexp_obj, mp_math_frexp);
209 
210 // modf(x)
211 STATIC mp_obj_t mp_math_modf(mp_obj_t x_obj) {
212  mp_float_t int_part = 0.0;
213  mp_float_t fractional_part = MICROPY_FLOAT_C_FUN(modf)(mp_obj_get_float(x_obj), &int_part);
214  mp_obj_t tuple[2];
215  tuple[0] = mp_obj_new_float(fractional_part);
216  tuple[1] = mp_obj_new_float(int_part);
217  return mp_obj_new_tuple(2, tuple);
218 }
219 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_modf_obj, mp_math_modf);
220 
221 // Angular conversions
222 
223 // radians(x)
224 STATIC mp_obj_t mp_math_radians(mp_obj_t x_obj) {
225  return mp_obj_new_float(mp_obj_get_float(x_obj) * (MP_PI / MICROPY_FLOAT_CONST(180.0)));
226 }
227 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians);
228 
229 // degrees(x)
230 STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) {
231  return mp_obj_new_float(mp_obj_get_float(x_obj) * (MICROPY_FLOAT_CONST(180.0) / MP_PI));
232 }
233 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees);
234 
235 STATIC const mp_rom_map_elem_t mp_module_math_globals_table[] = {
236  { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_math) },
237  { MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e },
238  { MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi },
239  { MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_math_sqrt_obj) },
240  { MP_ROM_QSTR(MP_QSTR_pow), MP_ROM_PTR(&mp_math_pow_obj) },
241  { MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_math_exp_obj) },
242  #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
243  { MP_ROM_QSTR(MP_QSTR_expm1), MP_ROM_PTR(&mp_math_expm1_obj) },
244  #endif
245  { MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&mp_math_log_obj) },
246  #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
247  { MP_ROM_QSTR(MP_QSTR_log2), MP_ROM_PTR(&mp_math_log2_obj) },
248  { MP_ROM_QSTR(MP_QSTR_log10), MP_ROM_PTR(&mp_math_log10_obj) },
249  { MP_ROM_QSTR(MP_QSTR_cosh), MP_ROM_PTR(&mp_math_cosh_obj) },
250  { MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&mp_math_sinh_obj) },
251  { MP_ROM_QSTR(MP_QSTR_tanh), MP_ROM_PTR(&mp_math_tanh_obj) },
252  { MP_ROM_QSTR(MP_QSTR_acosh), MP_ROM_PTR(&mp_math_acosh_obj) },
253  { MP_ROM_QSTR(MP_QSTR_asinh), MP_ROM_PTR(&mp_math_asinh_obj) },
254  { MP_ROM_QSTR(MP_QSTR_atanh), MP_ROM_PTR(&mp_math_atanh_obj) },
255  #endif
256  { MP_ROM_QSTR(MP_QSTR_cos), MP_ROM_PTR(&mp_math_cos_obj) },
257  { MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&mp_math_sin_obj) },
258  { MP_ROM_QSTR(MP_QSTR_tan), MP_ROM_PTR(&mp_math_tan_obj) },
259  { MP_ROM_QSTR(MP_QSTR_acos), MP_ROM_PTR(&mp_math_acos_obj) },
260  { MP_ROM_QSTR(MP_QSTR_asin), MP_ROM_PTR(&mp_math_asin_obj) },
261  { MP_ROM_QSTR(MP_QSTR_atan), MP_ROM_PTR(&mp_math_atan_obj) },
262  { MP_ROM_QSTR(MP_QSTR_atan2), MP_ROM_PTR(&mp_math_atan2_obj) },
263  { MP_ROM_QSTR(MP_QSTR_ceil), MP_ROM_PTR(&mp_math_ceil_obj) },
264  { MP_ROM_QSTR(MP_QSTR_copysign), MP_ROM_PTR(&mp_math_copysign_obj) },
265  { MP_ROM_QSTR(MP_QSTR_fabs), MP_ROM_PTR(&mp_math_fabs_obj) },
266  { MP_ROM_QSTR(MP_QSTR_floor), MP_ROM_PTR(&mp_math_floor_obj) },
267  { MP_ROM_QSTR(MP_QSTR_fmod), MP_ROM_PTR(&mp_math_fmod_obj) },
268  { MP_ROM_QSTR(MP_QSTR_frexp), MP_ROM_PTR(&mp_math_frexp_obj) },
269  { MP_ROM_QSTR(MP_QSTR_ldexp), MP_ROM_PTR(&mp_math_ldexp_obj) },
270  { MP_ROM_QSTR(MP_QSTR_modf), MP_ROM_PTR(&mp_math_modf_obj) },
271  { MP_ROM_QSTR(MP_QSTR_isfinite), MP_ROM_PTR(&mp_math_isfinite_obj) },
272  { MP_ROM_QSTR(MP_QSTR_isinf), MP_ROM_PTR(&mp_math_isinf_obj) },
273  { MP_ROM_QSTR(MP_QSTR_isnan), MP_ROM_PTR(&mp_math_isnan_obj) },
274  { MP_ROM_QSTR(MP_QSTR_trunc), MP_ROM_PTR(&mp_math_trunc_obj) },
275  { MP_ROM_QSTR(MP_QSTR_radians), MP_ROM_PTR(&mp_math_radians_obj) },
276  { MP_ROM_QSTR(MP_QSTR_degrees), MP_ROM_PTR(&mp_math_degrees_obj) },
277  #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
278  { MP_ROM_QSTR(MP_QSTR_erf), MP_ROM_PTR(&mp_math_erf_obj) },
279  { MP_ROM_QSTR(MP_QSTR_erfc), MP_ROM_PTR(&mp_math_erfc_obj) },
280  { MP_ROM_QSTR(MP_QSTR_gamma), MP_ROM_PTR(&mp_math_gamma_obj) },
281  { MP_ROM_QSTR(MP_QSTR_lgamma), MP_ROM_PTR(&mp_math_lgamma_obj) },
282  #endif
283 };
284 
285 STATIC MP_DEFINE_CONST_DICT(mp_module_math_globals, mp_module_math_globals_table);
286 
288  .base = { &mp_type_module },
289  .globals = (mp_obj_dict_t*)&mp_module_math_globals,
290 };
291 
292 #endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH
#define exp(x)
Definition: math.h:176
#define acosh(x)
Definition: math.h:164
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
Definition: runtime.c:1448
#define copysign(x, y)
Definition: math.h:171
#define isfinite(x)
Definition: math.h:41
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items)
Definition: objtuple.c:235
#define MP_DEFINE_CONST_DICT(dict_name, table_name)
Definition: obj.h:317
#define isinf(x)
Definition: math.h:44
const mp_obj_type_t mp_type_ZeroDivisionError
#define lgamma(x)
Definition: math.h:182
#define trunc(x)
Definition: math.h:200
#define atan(x)
Definition: math.h:167
#define MP_ROM_QSTR(q)
Definition: obj.h:241
#define MP_ROM_PTR(p)
Definition: obj.h:242
mp_obj_base_t base
Definition: obj.h:814
#define fabs(x)
Definition: math.h:178
#define STATIC
Definition: mpconfig.h:1178
#define asin(x)
Definition: math.h:165
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name)
Definition: obj.h:285
#define atan2(y, x)
Definition: math.h:168
#define ldexp(x, n)
Definition: math.h:181
#define sin(x)
Definition: math.h:194
#define log(x)
Definition: math.h:184
#define fmod(x, y)
Definition: math.h:180
#define tgamma(x)
Definition: math.h:199
#define atanh(x)
Definition: math.h:169
#define pow(x, y)
Definition: math.h:190
#define cos(x)
Definition: math.h:172
#define erfc(x)
Definition: math.h:175
args
Definition: i18n.py:175
#define tanh(x)
Definition: math.h:198
double frexp(double x, int *eptr)
Definition: s_frexp.c:33
#define NORETURN
Definition: mpconfig.h:1268
#define log10(x)
Definition: math.h:186
#define ceil(x)
Definition: math.h:170
#define sinh(x)
Definition: math.h:195
mp_obj_t mp_obj_new_int(mp_int_t value)
Definition: objint.c:353
#define isnan(x)
Definition: math.h:43
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name)
Definition: obj.h:297
NORETURN void mp_raise_ValueError(const char *msg)
Definition: runtime.c:1456
#define acos(x)
Definition: math.h:163
const mp_obj_type_t mp_type_module
Definition: objmodule.c:94
#define cosh(x)
Definition: math.h:173
#define erf(x)
Definition: math.h:174
#define asinh(x)
Definition: math.h:166
uint64_t mp_obj_t
Definition: obj.h:39
#define log2(x)
Definition: math.h:187
#define floor(x)
Definition: math.h:179
#define expm1(x)
Definition: math.h:177
#define sqrt(x)
Definition: math.h:196
const mp_obj_module_t mp_module_math
#define tan(x)
Definition: math.h:197
double modf(double value, double *iptr)
Definition: s_modf.c:29