blob: 35bb44bea37c70afb4cc8803a9405a48b92ee0fc [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
Damien George25e14062017-10-04 21:00:05 +11006 * Copyright (c) 2013-2017 Damien P. George
Damien George04b91472014-05-03 23:27:38 +01007 *
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
Damien George51dfcb42015-01-01 20:27:54 +000027#include "py/builtin.h"
Damien George48d867b2017-06-15 11:54:41 +100028#include "py/runtime.h"
Damien George0c36da02014-03-08 15:24:39 +000029
Damien Georgefb510b32014-06-01 13:32:54 +010030#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH
Damien George0c36da02014-03-08 15:24:39 +000031
Damien George51dfcb42015-01-01 20:27:54 +000032#include <math.h>
33
Colin Hogbena8969512016-05-09 22:11:34 +010034// M_PI is not part of the math.h standard and may not be defined
Damien George561844f2016-11-03 12:33:01 +110035// And by defining our own we can ensure it uses the correct const format.
36#define MP_PI MICROPY_FLOAT_CONST(3.14159265358979323846)
Colin Hogbena8969512016-05-09 22:11:34 +010037
Michael Buesch17298af2015-12-10 13:28:37 +010038STATIC NORETURN void math_error(void) {
Damien George48d867b2017-06-15 11:54:41 +100039 mp_raise_ValueError("math domain error");
Michael Buesch17298af2015-12-10 13:28:37 +010040}
41
Damien George25e14062017-10-04 21:00:05 +110042STATIC 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}
Damien George0c36da02014-03-08 15:24:39 +000050
Damien George25e14062017-10-04 21:00:05 +110051STATIC 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);
Damien George0c36da02014-03-08 15:24:39 +000066
Damien George084ef372014-04-01 21:21:50 +010067#define MATH_FUN_1_TO_BOOL(py_name, c_name) \
Paul Sokolovsky1b586f32015-10-11 12:09:43 +030068 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))); } \
Rachel Dowdall249b9c72014-03-22 14:39:33 +000069 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
70
Damien George084ef372014-04-01 21:21:50 +010071#define MATH_FUN_1_TO_INT(py_name, c_name) \
Damien Georgefebeff42017-03-23 22:57:08 +110072 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))); } \
Damien George084ef372014-04-01 21:21:50 +010073 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
74
Damien George25e14062017-10-04 21:00:05 +110075#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)); \
Michael Buesch17298af2015-12-10 13:28:37 +010078 } \
Damien George25e14062017-10-04 21:00:05 +110079 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);
Michael Buesch17298af2015-12-10 13:28:37 +010086
Paul Sokolovsky6d1eabf2015-11-13 13:24:39 +020087#if MP_NEED_LOG2
Damien Georged8d4e4d2017-10-04 21:46:38 +110088#undef log2
89#undef log2f
Paul Sokolovsky6d1eabf2015-11-13 13:24:39 +020090// 1.442695040888963407354163704 is 1/_M_LN2
Damien Georged8d4e4d2017-10-04 21:46:38 +110091mp_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}
Paul Sokolovsky6d1eabf2015-11-13 13:24:39 +020094#endif
95
Damien George0102ee02017-08-30 21:02:00 +100096// sqrt(x): returns the square root of x
Damien George25e14062017-10-04 21:00:05 +110097MATH_FUN_1(sqrt, sqrt)
Damien George0102ee02017-08-30 21:02:00 +100098// pow(x, y): returns x to the power of y
Damien George0c36da02014-03-08 15:24:39 +000099MATH_FUN_2(pow, pow)
Damien George0102ee02017-08-30 21:02:00 +1000100// exp(x)
Damien George0c36da02014-03-08 15:24:39 +0000101MATH_FUN_1(exp, exp)
Paul Sokolovsky0d6116d2015-11-14 00:46:12 +0200102#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
Damien George0102ee02017-08-30 21:02:00 +1000103// expm1(x)
Damien George8854e1f2014-03-12 21:31:41 +0000104MATH_FUN_1(expm1, expm1)
Damien George0102ee02017-08-30 21:02:00 +1000105// log2(x)
Damien George25e14062017-10-04 21:00:05 +1100106MATH_FUN_1(log2, log2)
Damien George0102ee02017-08-30 21:02:00 +1000107// log10(x)
Damien George25e14062017-10-04 21:00:05 +1100108MATH_FUN_1(log10, log10)
Damien George0102ee02017-08-30 21:02:00 +1000109// cosh(x)
Damien George0c36da02014-03-08 15:24:39 +0000110MATH_FUN_1(cosh, cosh)
Damien George0102ee02017-08-30 21:02:00 +1000111// sinh(x)
Damien George0c36da02014-03-08 15:24:39 +0000112MATH_FUN_1(sinh, sinh)
Damien George0102ee02017-08-30 21:02:00 +1000113// tanh(x)
Damien George0c36da02014-03-08 15:24:39 +0000114MATH_FUN_1(tanh, tanh)
Damien George0102ee02017-08-30 21:02:00 +1000115// acosh(x)
Damien George0c36da02014-03-08 15:24:39 +0000116MATH_FUN_1(acosh, acosh)
Damien George0102ee02017-08-30 21:02:00 +1000117// asinh(x)
Damien George0c36da02014-03-08 15:24:39 +0000118MATH_FUN_1(asinh, asinh)
Damien George0102ee02017-08-30 21:02:00 +1000119// atanh(x)
Damien George0c36da02014-03-08 15:24:39 +0000120MATH_FUN_1(atanh, atanh)
Damien Georgea24eafa2015-11-17 14:10:13 +0000121#endif
Damien George0102ee02017-08-30 21:02:00 +1000122// cos(x)
Damien George0c36da02014-03-08 15:24:39 +0000123MATH_FUN_1(cos, cos)
Damien George0102ee02017-08-30 21:02:00 +1000124// sin(x)
Damien George0c36da02014-03-08 15:24:39 +0000125MATH_FUN_1(sin, sin)
Damien George0102ee02017-08-30 21:02:00 +1000126// tan(x)
Damien George0c36da02014-03-08 15:24:39 +0000127MATH_FUN_1(tan, tan)
Damien George0102ee02017-08-30 21:02:00 +1000128// acos(x)
Damien George0c36da02014-03-08 15:24:39 +0000129MATH_FUN_1(acos, acos)
Damien George0102ee02017-08-30 21:02:00 +1000130// asin(x)
Damien George0c36da02014-03-08 15:24:39 +0000131MATH_FUN_1(asin, asin)
Damien George0102ee02017-08-30 21:02:00 +1000132// atan(x)
Damien George0c36da02014-03-08 15:24:39 +0000133MATH_FUN_1(atan, atan)
Damien George0102ee02017-08-30 21:02:00 +1000134// atan2(y, x)
Damien George0c36da02014-03-08 15:24:39 +0000135MATH_FUN_2(atan2, atan2)
Damien George0102ee02017-08-30 21:02:00 +1000136// ceil(x)
Damien George084ef372014-04-01 21:21:50 +0100137MATH_FUN_1_TO_INT(ceil, ceil)
Damien George0102ee02017-08-30 21:02:00 +1000138// copysign(x, y)
Damien George25e14062017-10-04 21:00:05 +1100139STATIC 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}
142MATH_FUN_2(copysign, copysign_func)
Damien George0102ee02017-08-30 21:02:00 +1000143// fabs(x)
Damien George25e14062017-10-04 21:00:05 +1100144STATIC mp_float_t MICROPY_FLOAT_C_FUN(fabs_func)(mp_float_t x) {
145 return MICROPY_FLOAT_C_FUN(fabs)(x);
146}
147MATH_FUN_1(fabs, fabs_func)
Damien George0102ee02017-08-30 21:02:00 +1000148// floor(x)
Damien George084ef372014-04-01 21:21:50 +0100149MATH_FUN_1_TO_INT(floor, floor) //TODO: delegate to x.__floor__() if x is not a float
Damien George0102ee02017-08-30 21:02:00 +1000150// fmod(x, y)
Rachel Dowdall5a14a1d2014-03-20 21:26:51 +0000151MATH_FUN_2(fmod, fmod)
Damien George0102ee02017-08-30 21:02:00 +1000152// isfinite(x)
Damien George084ef372014-04-01 21:21:50 +0100153MATH_FUN_1_TO_BOOL(isfinite, isfinite)
Damien George0102ee02017-08-30 21:02:00 +1000154// isinf(x)
Damien George084ef372014-04-01 21:21:50 +0100155MATH_FUN_1_TO_BOOL(isinf, isinf)
Damien George0102ee02017-08-30 21:02:00 +1000156// isnan(x)
Damien George084ef372014-04-01 21:21:50 +0100157MATH_FUN_1_TO_BOOL(isnan, isnan)
Damien George0102ee02017-08-30 21:02:00 +1000158// trunc(x)
Damien George084ef372014-04-01 21:21:50 +0100159MATH_FUN_1_TO_INT(trunc, trunc)
Damien George0102ee02017-08-30 21:02:00 +1000160// ldexp(x, exp)
Damien George25e14062017-10-04 21:00:05 +1100161MATH_FUN_2_FLT_INT(ldexp, ldexp)
Damien George5cbeace2015-02-22 14:48:18 +0000162#if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
Damien George0102ee02017-08-30 21:02:00 +1000163// erf(x): return the error function of x
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000164MATH_FUN_1(erf, erf)
Damien George0102ee02017-08-30 21:02:00 +1000165// erfc(x): return the complementary error function of x
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000166MATH_FUN_1(erfc, erfc)
Damien George0102ee02017-08-30 21:02:00 +1000167// gamma(x): return the gamma function of x
xbe1ea8fcf2014-03-23 02:46:10 -0700168MATH_FUN_1(gamma, tgamma)
Damien George0102ee02017-08-30 21:02:00 +1000169// lgamma(x): return the natural logarithm of the gamma function of x
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000170MATH_FUN_1(lgamma, lgamma)
Damien George5cbeace2015-02-22 14:48:18 +0000171#endif
Christopher Swenson8c656752018-08-27 10:32:21 +1000172//TODO: fsum
Rachel Dowdalla2f2f732014-03-20 21:30:38 +0000173
stijnaf5c9982019-07-02 10:28:44 +0200174#if MICROPY_PY_MATH_ISCLOSE
175STATIC mp_obj_t mp_math_isclose(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
176 enum { ARG_a, ARG_b, ARG_rel_tol, ARG_abs_tol };
177 static const mp_arg_t allowed_args[] = {
178 {MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ},
179 {MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ},
180 {MP_QSTR_rel_tol, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
181 {MP_QSTR_abs_tol, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(0)}},
182 };
183 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
184 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
185 const mp_float_t a = mp_obj_get_float(args[ARG_a].u_obj);
186 const mp_float_t b = mp_obj_get_float(args[ARG_b].u_obj);
187 const mp_float_t rel_tol = args[ARG_rel_tol].u_obj == MP_OBJ_NULL
188 ? (mp_float_t)1e-9 : mp_obj_get_float(args[ARG_rel_tol].u_obj);
189 const mp_float_t abs_tol = mp_obj_get_float(args[ARG_abs_tol].u_obj);
190 if (rel_tol < (mp_float_t)0.0 || abs_tol < (mp_float_t)0.0) {
191 math_error();
192 }
193 if (a == b) {
194 return mp_const_true;
195 }
196 const mp_float_t difference = MICROPY_FLOAT_C_FUN(fabs)(a - b);
197 if (isinf(difference)) { // Either a or b is inf
198 return mp_const_false;
199 }
200 if ((difference <= abs_tol) ||
201 (difference <= MICROPY_FLOAT_C_FUN(fabs)(rel_tol * a)) ||
202 (difference <= MICROPY_FLOAT_C_FUN(fabs)(rel_tol * b))) {
203 return mp_const_true;
204 }
205 return mp_const_false;
206}
207MP_DEFINE_CONST_FUN_OBJ_KW(mp_math_isclose_obj, 2, mp_math_isclose);
208#endif
209
Damien George6f495202015-06-13 21:56:23 +0100210// Function that takes a variable number of arguments
211
212// log(x[, base])
Damien George4b72b3a2016-01-03 14:21:40 +0000213STATIC mp_obj_t mp_math_log(size_t n_args, const mp_obj_t *args) {
Michael Buesch17298af2015-12-10 13:28:37 +0100214 mp_float_t x = mp_obj_get_float(args[0]);
215 if (x <= (mp_float_t)0.0) {
216 math_error();
217 }
218 mp_float_t l = MICROPY_FLOAT_C_FUN(log)(x);
Damien George6f495202015-06-13 21:56:23 +0100219 if (n_args == 1) {
220 return mp_obj_new_float(l);
221 } else {
Michael Buesch17298af2015-12-10 13:28:37 +0100222 mp_float_t base = mp_obj_get_float(args[1]);
223 if (base <= (mp_float_t)0.0) {
224 math_error();
Damien George6b8b56f2017-07-04 02:15:11 +1000225 } else if (base == (mp_float_t)1.0) {
Damien Georgeb01f66c2018-06-20 21:02:11 +1000226 mp_raise_msg(&mp_type_ZeroDivisionError, "divide by zero");
Michael Buesch17298af2015-12-10 13:28:37 +0100227 }
228 return mp_obj_new_float(l / MICROPY_FLOAT_C_FUN(log)(base));
Damien George6f495202015-06-13 21:56:23 +0100229 }
230}
231STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_math_log_obj, 1, 2, mp_math_log);
232
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000233// Functions that return a tuple
Damien George30dd23a2014-08-10 17:50:28 +0100234
Damien George0102ee02017-08-30 21:02:00 +1000235// frexp(x): converts a floating-point number to fractional and integral components
Damien George969a6b32014-12-10 22:07:04 +0000236STATIC mp_obj_t mp_math_frexp(mp_obj_t x_obj) {
Damien George62805872014-03-22 20:44:15 +0000237 int int_exponent = 0;
238 mp_float_t significand = MICROPY_FLOAT_C_FUN(frexp)(mp_obj_get_float(x_obj), &int_exponent);
239 mp_obj_t tuple[2];
240 tuple[0] = mp_obj_new_float(significand);
241 tuple[1] = mp_obj_new_int(int_exponent);
242 return mp_obj_new_tuple(2, tuple);
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000243}
244STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_frexp_obj, mp_math_frexp);
245
Damien George0102ee02017-08-30 21:02:00 +1000246// modf(x)
Damien George969a6b32014-12-10 22:07:04 +0000247STATIC mp_obj_t mp_math_modf(mp_obj_t x_obj) {
Damien George62805872014-03-22 20:44:15 +0000248 mp_float_t int_part = 0.0;
249 mp_float_t fractional_part = MICROPY_FLOAT_C_FUN(modf)(mp_obj_get_float(x_obj), &int_part);
250 mp_obj_t tuple[2];
251 tuple[0] = mp_obj_new_float(fractional_part);
252 tuple[1] = mp_obj_new_float(int_part);
253 return mp_obj_new_tuple(2, tuple);
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000254}
255STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_modf_obj, mp_math_modf);
256
257// Angular conversions
Damien George30dd23a2014-08-10 17:50:28 +0100258
Damien George0102ee02017-08-30 21:02:00 +1000259// radians(x)
Damien George969a6b32014-12-10 22:07:04 +0000260STATIC mp_obj_t mp_math_radians(mp_obj_t x_obj) {
Damien George561844f2016-11-03 12:33:01 +1100261 return mp_obj_new_float(mp_obj_get_float(x_obj) * (MP_PI / MICROPY_FLOAT_CONST(180.0)));
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000262}
263STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians);
264
Damien George0102ee02017-08-30 21:02:00 +1000265// degrees(x)
Damien George969a6b32014-12-10 22:07:04 +0000266STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) {
Damien George561844f2016-11-03 12:33:01 +1100267 return mp_obj_new_float(mp_obj_get_float(x_obj) * (MICROPY_FLOAT_CONST(180.0) / MP_PI));
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000268}
269STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees);
270
Christopher Swenson8c656752018-08-27 10:32:21 +1000271#if MICROPY_PY_MATH_FACTORIAL
272
273#if MICROPY_OPT_MATH_FACTORIAL
274
275// factorial(x): slightly efficient recursive implementation
276STATIC mp_obj_t mp_math_factorial_inner(mp_uint_t start, mp_uint_t end) {
277 if (start == end) {
278 return mp_obj_new_int(start);
279 } else if (end - start == 1) {
280 return mp_binary_op(MP_BINARY_OP_MULTIPLY, MP_OBJ_NEW_SMALL_INT(start), MP_OBJ_NEW_SMALL_INT(end));
281 } else if (end - start == 2) {
282 mp_obj_t left = MP_OBJ_NEW_SMALL_INT(start);
283 mp_obj_t middle = MP_OBJ_NEW_SMALL_INT(start + 1);
284 mp_obj_t right = MP_OBJ_NEW_SMALL_INT(end);
285 mp_obj_t tmp = mp_binary_op(MP_BINARY_OP_MULTIPLY, left, middle);
286 return mp_binary_op(MP_BINARY_OP_MULTIPLY, tmp, right);
287 } else {
288 mp_uint_t middle = start + ((end - start) >> 1);
289 mp_obj_t left = mp_math_factorial_inner(start, middle);
290 mp_obj_t right = mp_math_factorial_inner(middle + 1, end);
291 return mp_binary_op(MP_BINARY_OP_MULTIPLY, left, right);
292 }
293}
294STATIC mp_obj_t mp_math_factorial(mp_obj_t x_obj) {
295 mp_int_t max = mp_obj_get_int(x_obj);
296 if (max < 0) {
297 mp_raise_msg(&mp_type_ValueError, "negative factorial");
298 } else if (max == 0) {
299 return MP_OBJ_NEW_SMALL_INT(1);
300 }
301 return mp_math_factorial_inner(1, max);
302}
303
304#else
305
306// factorial(x): squared difference implementation
307// based on http://www.luschny.de/math/factorial/index.html
308STATIC mp_obj_t mp_math_factorial(mp_obj_t x_obj) {
309 mp_int_t max = mp_obj_get_int(x_obj);
310 if (max < 0) {
311 mp_raise_msg(&mp_type_ValueError, "negative factorial");
312 } else if (max <= 1) {
313 return MP_OBJ_NEW_SMALL_INT(1);
314 }
315 mp_int_t h = max >> 1;
316 mp_int_t q = h * h;
317 mp_int_t r = q << 1;
318 if (max & 1) {
319 r *= max;
320 }
321 mp_obj_t prod = MP_OBJ_NEW_SMALL_INT(r);
322 for (mp_int_t num = 1; num < max - 2; num += 2) {
323 q -= num;
324 prod = mp_binary_op(MP_BINARY_OP_MULTIPLY, prod, MP_OBJ_NEW_SMALL_INT(q));
325 }
326 return prod;
327}
328
329#endif
330
331STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_factorial_obj, mp_math_factorial);
332
333#endif
334
Damien Georgecbf76742015-11-27 13:38:15 +0000335STATIC const mp_rom_map_elem_t mp_module_math_globals_table[] = {
336 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_math) },
337 { MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e },
338 { MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi },
339 { MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_math_sqrt_obj) },
340 { MP_ROM_QSTR(MP_QSTR_pow), MP_ROM_PTR(&mp_math_pow_obj) },
341 { MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_math_exp_obj) },
Paul Sokolovsky863d4cd2015-11-14 00:55:54 +0200342 #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
Damien Georgecbf76742015-11-27 13:38:15 +0000343 { MP_ROM_QSTR(MP_QSTR_expm1), MP_ROM_PTR(&mp_math_expm1_obj) },
Paul Sokolovsky863d4cd2015-11-14 00:55:54 +0200344 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000345 { MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&mp_math_log_obj) },
Damien Georgea24eafa2015-11-17 14:10:13 +0000346 #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
Damien Georgecbf76742015-11-27 13:38:15 +0000347 { MP_ROM_QSTR(MP_QSTR_log2), MP_ROM_PTR(&mp_math_log2_obj) },
348 { MP_ROM_QSTR(MP_QSTR_log10), MP_ROM_PTR(&mp_math_log10_obj) },
349 { MP_ROM_QSTR(MP_QSTR_cosh), MP_ROM_PTR(&mp_math_cosh_obj) },
350 { MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&mp_math_sinh_obj) },
351 { MP_ROM_QSTR(MP_QSTR_tanh), MP_ROM_PTR(&mp_math_tanh_obj) },
352 { MP_ROM_QSTR(MP_QSTR_acosh), MP_ROM_PTR(&mp_math_acosh_obj) },
353 { MP_ROM_QSTR(MP_QSTR_asinh), MP_ROM_PTR(&mp_math_asinh_obj) },
354 { MP_ROM_QSTR(MP_QSTR_atanh), MP_ROM_PTR(&mp_math_atanh_obj) },
Damien Georgea24eafa2015-11-17 14:10:13 +0000355 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000356 { MP_ROM_QSTR(MP_QSTR_cos), MP_ROM_PTR(&mp_math_cos_obj) },
357 { MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&mp_math_sin_obj) },
358 { MP_ROM_QSTR(MP_QSTR_tan), MP_ROM_PTR(&mp_math_tan_obj) },
359 { MP_ROM_QSTR(MP_QSTR_acos), MP_ROM_PTR(&mp_math_acos_obj) },
360 { MP_ROM_QSTR(MP_QSTR_asin), MP_ROM_PTR(&mp_math_asin_obj) },
361 { MP_ROM_QSTR(MP_QSTR_atan), MP_ROM_PTR(&mp_math_atan_obj) },
362 { MP_ROM_QSTR(MP_QSTR_atan2), MP_ROM_PTR(&mp_math_atan2_obj) },
363 { MP_ROM_QSTR(MP_QSTR_ceil), MP_ROM_PTR(&mp_math_ceil_obj) },
364 { MP_ROM_QSTR(MP_QSTR_copysign), MP_ROM_PTR(&mp_math_copysign_obj) },
365 { MP_ROM_QSTR(MP_QSTR_fabs), MP_ROM_PTR(&mp_math_fabs_obj) },
366 { MP_ROM_QSTR(MP_QSTR_floor), MP_ROM_PTR(&mp_math_floor_obj) },
367 { MP_ROM_QSTR(MP_QSTR_fmod), MP_ROM_PTR(&mp_math_fmod_obj) },
368 { MP_ROM_QSTR(MP_QSTR_frexp), MP_ROM_PTR(&mp_math_frexp_obj) },
369 { MP_ROM_QSTR(MP_QSTR_ldexp), MP_ROM_PTR(&mp_math_ldexp_obj) },
370 { MP_ROM_QSTR(MP_QSTR_modf), MP_ROM_PTR(&mp_math_modf_obj) },
371 { MP_ROM_QSTR(MP_QSTR_isfinite), MP_ROM_PTR(&mp_math_isfinite_obj) },
372 { MP_ROM_QSTR(MP_QSTR_isinf), MP_ROM_PTR(&mp_math_isinf_obj) },
373 { MP_ROM_QSTR(MP_QSTR_isnan), MP_ROM_PTR(&mp_math_isnan_obj) },
stijnaf5c9982019-07-02 10:28:44 +0200374 #if MICROPY_PY_MATH_ISCLOSE
375 { MP_ROM_QSTR(MP_QSTR_isclose), MP_ROM_PTR(&mp_math_isclose_obj) },
376 #endif
Damien Georgecbf76742015-11-27 13:38:15 +0000377 { MP_ROM_QSTR(MP_QSTR_trunc), MP_ROM_PTR(&mp_math_trunc_obj) },
378 { MP_ROM_QSTR(MP_QSTR_radians), MP_ROM_PTR(&mp_math_radians_obj) },
379 { MP_ROM_QSTR(MP_QSTR_degrees), MP_ROM_PTR(&mp_math_degrees_obj) },
Christopher Swenson8c656752018-08-27 10:32:21 +1000380 #if MICROPY_PY_MATH_FACTORIAL
381 { MP_ROM_QSTR(MP_QSTR_factorial), MP_ROM_PTR(&mp_math_factorial_obj) },
382 #endif
Damien George5cbeace2015-02-22 14:48:18 +0000383 #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
Damien Georgecbf76742015-11-27 13:38:15 +0000384 { MP_ROM_QSTR(MP_QSTR_erf), MP_ROM_PTR(&mp_math_erf_obj) },
385 { MP_ROM_QSTR(MP_QSTR_erfc), MP_ROM_PTR(&mp_math_erfc_obj) },
386 { MP_ROM_QSTR(MP_QSTR_gamma), MP_ROM_PTR(&mp_math_gamma_obj) },
387 { MP_ROM_QSTR(MP_QSTR_lgamma), MP_ROM_PTR(&mp_math_lgamma_obj) },
Damien George5cbeace2015-02-22 14:48:18 +0000388 #endif
Damien George0c36da02014-03-08 15:24:39 +0000389};
390
Damien George3b603f22014-11-29 14:39:27 +0000391STATIC MP_DEFINE_CONST_DICT(mp_module_math_globals, mp_module_math_globals_table);
Damien George0c36da02014-03-08 15:24:39 +0000392
393const mp_obj_module_t mp_module_math = {
394 .base = { &mp_type_module },
Damien George8b0535e2014-04-05 21:53:54 +0100395 .globals = (mp_obj_dict_t*)&mp_module_math_globals,
Damien George0c36da02014-03-08 15:24:39 +0000396};
397
Damien Georgefb510b32014-06-01 13:32:54 +0100398#endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH