blob: 7124820041fed73d846d70726ce2029d496b17d8 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 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
Damien George0c36da02014-03-08 15:24:39 +000027#include <math.h>
28
Damien George0c36da02014-03-08 15:24:39 +000029#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030030#include "misc.h"
Damien George0c36da02014-03-08 15:24:39 +000031#include "qstr.h"
32#include "obj.h"
Damien George0c36da02014-03-08 15:24:39 +000033#include "builtin.h"
34
Damien Georgefb510b32014-06-01 13:32:54 +010035#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH
Damien George0c36da02014-03-08 15:24:39 +000036
Damien George30dd23a2014-08-10 17:50:28 +010037/// \module math - mathematical functions
38///
39/// The `math` module provides some basic mathematical funtions for
40/// working with floating-point numbers.
41
Rachel Dowdall249b9c72014-03-22 14:39:33 +000042//TODO: Change macros to check for overflow and raise OverflowError or RangeError
Damien George0c36da02014-03-08 15:24:39 +000043#define MATH_FUN_1(py_name, c_name) \
44 mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \
45 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
46
47#define MATH_FUN_2(py_name, c_name) \
48 mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj, mp_obj_t y_obj) { return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj), mp_obj_get_float(y_obj))); } \
49 STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name);
50
Damien George084ef372014-04-01 21:21:50 +010051#define MATH_FUN_1_TO_BOOL(py_name, c_name) \
Rachel Dowdall249b9c72014-03-22 14:39:33 +000052 mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return MP_BOOL(c_name(mp_obj_get_float(x_obj))); } \
53 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
54
Damien George084ef372014-04-01 21:21:50 +010055#define MATH_FUN_1_TO_INT(py_name, c_name) \
Damien George40f3c022014-07-03 13:25:24 +010056 mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { return mp_obj_new_int((mp_int_t)MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj))); } \
Damien George084ef372014-04-01 21:21:50 +010057 STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
58
Damien Georgedbdfee12014-04-17 17:11:03 +010059// These are also used by cmath.c
Damien George30dd23a2014-08-10 17:50:28 +010060/// \constant e - base of the natural logarithm
Damien Georgedbdfee12014-04-17 17:11:03 +010061const mp_obj_float_t mp_math_e_obj = {{&mp_type_float}, M_E};
Damien George30dd23a2014-08-10 17:50:28 +010062/// \constant pi - the ratio of a circle's circumference to its diameter
Damien Georgedbdfee12014-04-17 17:11:03 +010063const mp_obj_float_t mp_math_pi_obj = {{&mp_type_float}, M_PI};
Damien George0c36da02014-03-08 15:24:39 +000064
Damien George30dd23a2014-08-10 17:50:28 +010065/// \function sqrt(x)
66/// Returns the square root of `x`.
Damien George0c36da02014-03-08 15:24:39 +000067MATH_FUN_1(sqrt, sqrt)
Damien George30dd23a2014-08-10 17:50:28 +010068/// \function pow(x, y)
69/// Returns `x` to the power of `y`.
Damien George0c36da02014-03-08 15:24:39 +000070MATH_FUN_2(pow, pow)
Damien George30dd23a2014-08-10 17:50:28 +010071/// \function exp(x)
Damien George0c36da02014-03-08 15:24:39 +000072MATH_FUN_1(exp, exp)
Damien George30dd23a2014-08-10 17:50:28 +010073/// \function expm1(x)
Damien George8854e1f2014-03-12 21:31:41 +000074MATH_FUN_1(expm1, expm1)
Damien George30dd23a2014-08-10 17:50:28 +010075/// \function log(x)
Damien George0c36da02014-03-08 15:24:39 +000076MATH_FUN_1(log, log)
Damien George30dd23a2014-08-10 17:50:28 +010077/// \function log2(x)
Damien George0c36da02014-03-08 15:24:39 +000078MATH_FUN_1(log2, log2)
Damien George30dd23a2014-08-10 17:50:28 +010079/// \function log10(x)
Damien George0c36da02014-03-08 15:24:39 +000080MATH_FUN_1(log10, log10)
Damien George30dd23a2014-08-10 17:50:28 +010081/// \function cosh(x)
Damien George0c36da02014-03-08 15:24:39 +000082MATH_FUN_1(cosh, cosh)
Damien George30dd23a2014-08-10 17:50:28 +010083/// \function sinh(x)
Damien George0c36da02014-03-08 15:24:39 +000084MATH_FUN_1(sinh, sinh)
Damien George30dd23a2014-08-10 17:50:28 +010085/// \function tanh(x)
Damien George0c36da02014-03-08 15:24:39 +000086MATH_FUN_1(tanh, tanh)
Damien George30dd23a2014-08-10 17:50:28 +010087/// \function acosh(x)
Damien George0c36da02014-03-08 15:24:39 +000088MATH_FUN_1(acosh, acosh)
Damien George30dd23a2014-08-10 17:50:28 +010089/// \function asinh(x)
Damien George0c36da02014-03-08 15:24:39 +000090MATH_FUN_1(asinh, asinh)
Damien George30dd23a2014-08-10 17:50:28 +010091/// \function atanh(x)
Damien George0c36da02014-03-08 15:24:39 +000092MATH_FUN_1(atanh, atanh)
Damien George30dd23a2014-08-10 17:50:28 +010093/// \function cos(x)
Damien George0c36da02014-03-08 15:24:39 +000094MATH_FUN_1(cos, cos)
Damien George30dd23a2014-08-10 17:50:28 +010095/// \function sin(x)
Damien George0c36da02014-03-08 15:24:39 +000096MATH_FUN_1(sin, sin)
Damien George30dd23a2014-08-10 17:50:28 +010097/// \function tan(x)
Damien George0c36da02014-03-08 15:24:39 +000098MATH_FUN_1(tan, tan)
Damien George30dd23a2014-08-10 17:50:28 +010099/// \function acos(x)
Damien George0c36da02014-03-08 15:24:39 +0000100MATH_FUN_1(acos, acos)
Damien George30dd23a2014-08-10 17:50:28 +0100101/// \function asin(x)
Damien George0c36da02014-03-08 15:24:39 +0000102MATH_FUN_1(asin, asin)
Damien George30dd23a2014-08-10 17:50:28 +0100103/// \function atan(x)
Damien George0c36da02014-03-08 15:24:39 +0000104MATH_FUN_1(atan, atan)
Damien George30dd23a2014-08-10 17:50:28 +0100105/// \function atan2(y, x)
Damien George0c36da02014-03-08 15:24:39 +0000106MATH_FUN_2(atan2, atan2)
Damien George30dd23a2014-08-10 17:50:28 +0100107/// \function ceil(x)
Damien George084ef372014-04-01 21:21:50 +0100108MATH_FUN_1_TO_INT(ceil, ceil)
Damien George30dd23a2014-08-10 17:50:28 +0100109/// \function copysign(x, y)
Rachel Dowdall5a14a1d2014-03-20 21:26:51 +0000110MATH_FUN_2(copysign, copysign)
Damien George30dd23a2014-08-10 17:50:28 +0100111/// \function fabs(x)
Rachel Dowdall5a14a1d2014-03-20 21:26:51 +0000112MATH_FUN_1(fabs, fabs)
Damien George30dd23a2014-08-10 17:50:28 +0100113/// \function floor(x)
Damien George084ef372014-04-01 21:21:50 +0100114MATH_FUN_1_TO_INT(floor, floor) //TODO: delegate to x.__floor__() if x is not a float
Damien George30dd23a2014-08-10 17:50:28 +0100115/// \function fmod(x, y)
Rachel Dowdall5a14a1d2014-03-20 21:26:51 +0000116MATH_FUN_2(fmod, fmod)
Damien George30dd23a2014-08-10 17:50:28 +0100117/// \function isfinite(x)
Damien George084ef372014-04-01 21:21:50 +0100118MATH_FUN_1_TO_BOOL(isfinite, isfinite)
Damien George30dd23a2014-08-10 17:50:28 +0100119/// \function isinf(x)
Damien George084ef372014-04-01 21:21:50 +0100120MATH_FUN_1_TO_BOOL(isinf, isinf)
Damien George30dd23a2014-08-10 17:50:28 +0100121/// \function isnan(x)
Damien George084ef372014-04-01 21:21:50 +0100122MATH_FUN_1_TO_BOOL(isnan, isnan)
Damien George30dd23a2014-08-10 17:50:28 +0100123/// \function trunc(x)
Damien George084ef372014-04-01 21:21:50 +0100124MATH_FUN_1_TO_INT(trunc, trunc)
Damien George30dd23a2014-08-10 17:50:28 +0100125/// \function ldexp(x, exp)
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000126MATH_FUN_2(ldexp, ldexp)
Damien George30dd23a2014-08-10 17:50:28 +0100127/// \function erf(x)
128/// Return the error function of `x`.
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000129MATH_FUN_1(erf, erf)
Damien George30dd23a2014-08-10 17:50:28 +0100130/// \function erfc(x)
131/// Return the complementary error function of `x`.
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000132MATH_FUN_1(erfc, erfc)
Damien George30dd23a2014-08-10 17:50:28 +0100133/// \function gamma(x)
134/// Return the gamma function of `x`.
xbe1ea8fcf2014-03-23 02:46:10 -0700135MATH_FUN_1(gamma, tgamma)
Damien George30dd23a2014-08-10 17:50:28 +0100136/// \function lgamma(x)
137/// return the natural logarithm of the gamma function of `x`.
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000138MATH_FUN_1(lgamma, lgamma)
139//TODO: factorial, fsum
Rachel Dowdalla2f2f732014-03-20 21:30:38 +0000140
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000141// Functions that return a tuple
Damien George30dd23a2014-08-10 17:50:28 +0100142
143/// \function frexp(x)
144/// Converts a floating-point number to fractional and integral components.
Damien George62805872014-03-22 20:44:15 +0000145mp_obj_t mp_math_frexp(mp_obj_t x_obj) {
146 int int_exponent = 0;
147 mp_float_t significand = MICROPY_FLOAT_C_FUN(frexp)(mp_obj_get_float(x_obj), &int_exponent);
148 mp_obj_t tuple[2];
149 tuple[0] = mp_obj_new_float(significand);
150 tuple[1] = mp_obj_new_int(int_exponent);
151 return mp_obj_new_tuple(2, tuple);
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000152}
153STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_frexp_obj, mp_math_frexp);
154
Damien George30dd23a2014-08-10 17:50:28 +0100155/// \function modf(x)
Damien George62805872014-03-22 20:44:15 +0000156mp_obj_t mp_math_modf(mp_obj_t x_obj) {
157 mp_float_t int_part = 0.0;
158 mp_float_t fractional_part = MICROPY_FLOAT_C_FUN(modf)(mp_obj_get_float(x_obj), &int_part);
159 mp_obj_t tuple[2];
160 tuple[0] = mp_obj_new_float(fractional_part);
161 tuple[1] = mp_obj_new_float(int_part);
162 return mp_obj_new_tuple(2, tuple);
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000163}
164STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_modf_obj, mp_math_modf);
165
166// Angular conversions
Damien George30dd23a2014-08-10 17:50:28 +0100167
168/// \function radians(x)
Damien George62805872014-03-22 20:44:15 +0000169mp_obj_t mp_math_radians(mp_obj_t x_obj) {
170 return mp_obj_new_float(mp_obj_get_float(x_obj) * M_PI / 180.0);
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000171}
172STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians);
173
Damien George30dd23a2014-08-10 17:50:28 +0100174/// \function degrees(x)
Damien George62805872014-03-22 20:44:15 +0000175mp_obj_t mp_math_degrees(mp_obj_t x_obj) {
176 return mp_obj_new_float(mp_obj_get_float(x_obj) * 180.0 / M_PI);
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000177}
178STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees);
179
Damien George0c36da02014-03-08 15:24:39 +0000180STATIC const mp_map_elem_t mp_module_math_globals_table[] = {
181 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) },
Damien Georgea9256392014-03-20 16:39:22 +0000182 { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj },
Damien George0c36da02014-03-08 15:24:39 +0000183 { MP_OBJ_NEW_QSTR(MP_QSTR_pi), (mp_obj_t)&mp_math_pi_obj },
184 { MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&mp_math_sqrt_obj },
185 { MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_math_pow_obj },
186 { MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&mp_math_exp_obj },
Damien George8854e1f2014-03-12 21:31:41 +0000187 { MP_OBJ_NEW_QSTR(MP_QSTR_expm1), (mp_obj_t)&mp_math_expm1_obj },
Damien George0c36da02014-03-08 15:24:39 +0000188 { MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&mp_math_log_obj },
189 { MP_OBJ_NEW_QSTR(MP_QSTR_log2), (mp_obj_t)&mp_math_log2_obj },
190 { MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&mp_math_log10_obj },
191 { MP_OBJ_NEW_QSTR(MP_QSTR_cosh), (mp_obj_t)&mp_math_cosh_obj },
192 { MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&mp_math_sinh_obj },
193 { MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&mp_math_tanh_obj },
194 { MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&mp_math_acosh_obj },
195 { MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&mp_math_asinh_obj },
196 { MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&mp_math_atanh_obj },
197 { MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&mp_math_cos_obj },
198 { MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&mp_math_sin_obj },
199 { MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&mp_math_tan_obj },
200 { MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&mp_math_acos_obj },
201 { MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&mp_math_asin_obj },
202 { MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&mp_math_atan_obj },
203 { MP_OBJ_NEW_QSTR(MP_QSTR_atan2), (mp_obj_t)&mp_math_atan2_obj },
Rachel Dowdall5a14a1d2014-03-20 21:26:51 +0000204 { MP_OBJ_NEW_QSTR(MP_QSTR_ceil), (mp_obj_t)&mp_math_ceil_obj },
205 { MP_OBJ_NEW_QSTR(MP_QSTR_copysign), (mp_obj_t)&mp_math_copysign_obj },
206 { MP_OBJ_NEW_QSTR(MP_QSTR_fabs), (mp_obj_t)&mp_math_fabs_obj },
207 { MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&mp_math_floor_obj },
Antonin ENFRUNda1fffa2014-05-12 00:21:50 +0200208 { MP_OBJ_NEW_QSTR(MP_QSTR_fmod), (mp_obj_t)&mp_math_fmod_obj },
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000209 { MP_OBJ_NEW_QSTR(MP_QSTR_frexp), (mp_obj_t)&mp_math_frexp_obj },
210 { MP_OBJ_NEW_QSTR(MP_QSTR_ldexp), (mp_obj_t)&mp_math_ldexp_obj },
211 { MP_OBJ_NEW_QSTR(MP_QSTR_modf), (mp_obj_t)&mp_math_modf_obj },
212 { MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_math_isfinite_obj },
213 { MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_math_isinf_obj },
214 { MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_math_isnan_obj },
Rachel Dowdall5a14a1d2014-03-20 21:26:51 +0000215 { MP_OBJ_NEW_QSTR(MP_QSTR_trunc), (mp_obj_t)&mp_math_trunc_obj },
Rachel Dowdall249b9c72014-03-22 14:39:33 +0000216 { MP_OBJ_NEW_QSTR(MP_QSTR_radians), (mp_obj_t)&mp_math_radians_obj },
217 { MP_OBJ_NEW_QSTR(MP_QSTR_degrees), (mp_obj_t)&mp_math_degrees_obj },
218 { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&mp_math_erf_obj },
219 { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&mp_math_erfc_obj },
220 { MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&mp_math_gamma_obj },
221 { MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&mp_math_lgamma_obj },
Damien George0c36da02014-03-08 15:24:39 +0000222};
223
Damien George8b0535e2014-04-05 21:53:54 +0100224STATIC const mp_obj_dict_t mp_module_math_globals = {
225 .base = {&mp_type_dict},
226 .map = {
227 .all_keys_are_qstrs = 1,
228 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200229 .used = MP_ARRAY_SIZE(mp_module_math_globals_table),
230 .alloc = MP_ARRAY_SIZE(mp_module_math_globals_table),
Damien George8b0535e2014-04-05 21:53:54 +0100231 .table = (mp_map_elem_t*)mp_module_math_globals_table,
232 },
Damien George0c36da02014-03-08 15:24:39 +0000233};
234
235const mp_obj_module_t mp_module_math = {
236 .base = { &mp_type_module },
237 .name = MP_QSTR_math,
Damien George8b0535e2014-04-05 21:53:54 +0100238 .globals = (mp_obj_dict_t*)&mp_module_math_globals,
Damien George0c36da02014-03-08 15:24:39 +0000239};
240
Damien Georgefb510b32014-06-01 13:32:54 +0100241#endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH