Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 27 | #include "py/builtin.h" |
Damien George | 48d867b | 2017-06-15 11:54:41 +1000 | [diff] [blame^] | 28 | #include "py/runtime.h" |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 29 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 30 | #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 31 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include <math.h> |
| 33 | |
Colin Hogben | a896951 | 2016-05-09 22:11:34 +0100 | [diff] [blame] | 34 | // M_PI is not part of the math.h standard and may not be defined |
Damien George | 561844f | 2016-11-03 12:33:01 +1100 | [diff] [blame] | 35 | // And by defining our own we can ensure it uses the correct const format. |
| 36 | #define MP_PI MICROPY_FLOAT_CONST(3.14159265358979323846) |
Colin Hogben | a896951 | 2016-05-09 22:11:34 +0100 | [diff] [blame] | 37 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 38 | /// \module math - mathematical functions |
| 39 | /// |
| 40 | /// The `math` module provides some basic mathematical funtions for |
| 41 | /// working with floating-point numbers. |
| 42 | |
Michael Buesch | 17298af | 2015-12-10 13:28:37 +0100 | [diff] [blame] | 43 | STATIC NORETURN void math_error(void) { |
Damien George | 48d867b | 2017-06-15 11:54:41 +1000 | [diff] [blame^] | 44 | mp_raise_ValueError("math domain error"); |
Michael Buesch | 17298af | 2015-12-10 13:28:37 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 47 | #define MATH_FUN_1(py_name, c_name) \ |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 48 | STATIC 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))); } \ |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 49 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); |
| 50 | |
| 51 | #define MATH_FUN_2(py_name, c_name) \ |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 52 | STATIC 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))); } \ |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 53 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name); |
| 54 | |
Damien George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 55 | #define MATH_FUN_1_TO_BOOL(py_name, c_name) \ |
Paul Sokolovsky | 1b586f3 | 2015-10-11 12:09:43 +0300 | [diff] [blame] | 56 | 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 Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 57 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); |
| 58 | |
Damien George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 59 | #define MATH_FUN_1_TO_INT(py_name, c_name) \ |
Damien George | febeff4 | 2017-03-23 22:57:08 +1100 | [diff] [blame] | 60 | 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 George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 61 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); |
| 62 | |
Michael Buesch | 17298af | 2015-12-10 13:28:37 +0100 | [diff] [blame] | 63 | #define MATH_FUN_1_ERRCOND(py_name, c_name, error_condition) \ |
| 64 | STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { \ |
| 65 | mp_float_t x = mp_obj_get_float(x_obj); \ |
| 66 | if (error_condition) { \ |
| 67 | math_error(); \ |
| 68 | } \ |
| 69 | return mp_obj_new_float(MICROPY_FLOAT_C_FUN(c_name)(x)); \ |
| 70 | } \ |
| 71 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name); |
| 72 | |
Paul Sokolovsky | 6d1eabf | 2015-11-13 13:24:39 +0200 | [diff] [blame] | 73 | #if MP_NEED_LOG2 |
| 74 | // 1.442695040888963407354163704 is 1/_M_LN2 |
| 75 | #define log2(x) (log(x) * 1.442695040888963407354163704) |
| 76 | #endif |
| 77 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 78 | /// \function sqrt(x) |
| 79 | /// Returns the square root of `x`. |
Michael Buesch | 17298af | 2015-12-10 13:28:37 +0100 | [diff] [blame] | 80 | MATH_FUN_1_ERRCOND(sqrt, sqrt, (x < (mp_float_t)0.0)) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 81 | /// \function pow(x, y) |
| 82 | /// Returns `x` to the power of `y`. |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 83 | MATH_FUN_2(pow, pow) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 84 | /// \function exp(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 85 | MATH_FUN_1(exp, exp) |
Paul Sokolovsky | 0d6116d | 2015-11-14 00:46:12 +0200 | [diff] [blame] | 86 | #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 87 | /// \function expm1(x) |
Damien George | 8854e1f | 2014-03-12 21:31:41 +0000 | [diff] [blame] | 88 | MATH_FUN_1(expm1, expm1) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 89 | /// \function log2(x) |
Michael Buesch | 17298af | 2015-12-10 13:28:37 +0100 | [diff] [blame] | 90 | MATH_FUN_1_ERRCOND(log2, log2, (x <= (mp_float_t)0.0)) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 91 | /// \function log10(x) |
Michael Buesch | 17298af | 2015-12-10 13:28:37 +0100 | [diff] [blame] | 92 | MATH_FUN_1_ERRCOND(log10, log10, (x <= (mp_float_t)0.0)) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 93 | /// \function cosh(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 94 | MATH_FUN_1(cosh, cosh) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 95 | /// \function sinh(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 96 | MATH_FUN_1(sinh, sinh) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 97 | /// \function tanh(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 98 | MATH_FUN_1(tanh, tanh) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 99 | /// \function acosh(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 100 | MATH_FUN_1(acosh, acosh) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 101 | /// \function asinh(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 102 | MATH_FUN_1(asinh, asinh) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 103 | /// \function atanh(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 104 | MATH_FUN_1(atanh, atanh) |
Damien George | a24eafa | 2015-11-17 14:10:13 +0000 | [diff] [blame] | 105 | #endif |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 106 | /// \function cos(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 107 | MATH_FUN_1(cos, cos) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 108 | /// \function sin(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 109 | MATH_FUN_1(sin, sin) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 110 | /// \function tan(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 111 | MATH_FUN_1(tan, tan) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 112 | /// \function acos(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 113 | MATH_FUN_1(acos, acos) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 114 | /// \function asin(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 115 | MATH_FUN_1(asin, asin) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 116 | /// \function atan(x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 117 | MATH_FUN_1(atan, atan) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 118 | /// \function atan2(y, x) |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 119 | MATH_FUN_2(atan2, atan2) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 120 | /// \function ceil(x) |
Damien George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 121 | MATH_FUN_1_TO_INT(ceil, ceil) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 122 | /// \function copysign(x, y) |
Rachel Dowdall | 5a14a1d | 2014-03-20 21:26:51 +0000 | [diff] [blame] | 123 | MATH_FUN_2(copysign, copysign) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 124 | /// \function fabs(x) |
Rachel Dowdall | 5a14a1d | 2014-03-20 21:26:51 +0000 | [diff] [blame] | 125 | MATH_FUN_1(fabs, fabs) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 126 | /// \function floor(x) |
Damien George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 127 | MATH_FUN_1_TO_INT(floor, floor) //TODO: delegate to x.__floor__() if x is not a float |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 128 | /// \function fmod(x, y) |
Rachel Dowdall | 5a14a1d | 2014-03-20 21:26:51 +0000 | [diff] [blame] | 129 | MATH_FUN_2(fmod, fmod) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 130 | /// \function isfinite(x) |
Damien George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 131 | MATH_FUN_1_TO_BOOL(isfinite, isfinite) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 132 | /// \function isinf(x) |
Damien George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 133 | MATH_FUN_1_TO_BOOL(isinf, isinf) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 134 | /// \function isnan(x) |
Damien George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 135 | MATH_FUN_1_TO_BOOL(isnan, isnan) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 136 | /// \function trunc(x) |
Damien George | 084ef37 | 2014-04-01 21:21:50 +0100 | [diff] [blame] | 137 | MATH_FUN_1_TO_INT(trunc, trunc) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 138 | /// \function ldexp(x, exp) |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 139 | MATH_FUN_2(ldexp, ldexp) |
Damien George | 5cbeace | 2015-02-22 14:48:18 +0000 | [diff] [blame] | 140 | #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 141 | /// \function erf(x) |
| 142 | /// Return the error function of `x`. |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 143 | MATH_FUN_1(erf, erf) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 144 | /// \function erfc(x) |
| 145 | /// Return the complementary error function of `x`. |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 146 | MATH_FUN_1(erfc, erfc) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 147 | /// \function gamma(x) |
| 148 | /// Return the gamma function of `x`. |
xbe | 1ea8fcf | 2014-03-23 02:46:10 -0700 | [diff] [blame] | 149 | MATH_FUN_1(gamma, tgamma) |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 150 | /// \function lgamma(x) |
| 151 | /// return the natural logarithm of the gamma function of `x`. |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 152 | MATH_FUN_1(lgamma, lgamma) |
Damien George | 5cbeace | 2015-02-22 14:48:18 +0000 | [diff] [blame] | 153 | #endif |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 154 | //TODO: factorial, fsum |
Rachel Dowdall | a2f2f73 | 2014-03-20 21:30:38 +0000 | [diff] [blame] | 155 | |
Damien George | 6f49520 | 2015-06-13 21:56:23 +0100 | [diff] [blame] | 156 | // Function that takes a variable number of arguments |
| 157 | |
| 158 | // log(x[, base]) |
Damien George | 4b72b3a | 2016-01-03 14:21:40 +0000 | [diff] [blame] | 159 | STATIC mp_obj_t mp_math_log(size_t n_args, const mp_obj_t *args) { |
Michael Buesch | 17298af | 2015-12-10 13:28:37 +0100 | [diff] [blame] | 160 | mp_float_t x = mp_obj_get_float(args[0]); |
| 161 | if (x <= (mp_float_t)0.0) { |
| 162 | math_error(); |
| 163 | } |
| 164 | mp_float_t l = MICROPY_FLOAT_C_FUN(log)(x); |
Damien George | 6f49520 | 2015-06-13 21:56:23 +0100 | [diff] [blame] | 165 | if (n_args == 1) { |
| 166 | return mp_obj_new_float(l); |
| 167 | } else { |
Michael Buesch | 17298af | 2015-12-10 13:28:37 +0100 | [diff] [blame] | 168 | mp_float_t base = mp_obj_get_float(args[1]); |
| 169 | if (base <= (mp_float_t)0.0) { |
| 170 | math_error(); |
| 171 | } |
| 172 | return mp_obj_new_float(l / MICROPY_FLOAT_C_FUN(log)(base)); |
Damien George | 6f49520 | 2015-06-13 21:56:23 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_math_log_obj, 1, 2, mp_math_log); |
| 176 | |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 177 | // Functions that return a tuple |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 178 | |
| 179 | /// \function frexp(x) |
| 180 | /// Converts a floating-point number to fractional and integral components. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 181 | STATIC mp_obj_t mp_math_frexp(mp_obj_t x_obj) { |
Damien George | 6280587 | 2014-03-22 20:44:15 +0000 | [diff] [blame] | 182 | int int_exponent = 0; |
| 183 | mp_float_t significand = MICROPY_FLOAT_C_FUN(frexp)(mp_obj_get_float(x_obj), &int_exponent); |
| 184 | mp_obj_t tuple[2]; |
| 185 | tuple[0] = mp_obj_new_float(significand); |
| 186 | tuple[1] = mp_obj_new_int(int_exponent); |
| 187 | return mp_obj_new_tuple(2, tuple); |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 188 | } |
| 189 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_frexp_obj, mp_math_frexp); |
| 190 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 191 | /// \function modf(x) |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 192 | STATIC mp_obj_t mp_math_modf(mp_obj_t x_obj) { |
Damien George | 6280587 | 2014-03-22 20:44:15 +0000 | [diff] [blame] | 193 | mp_float_t int_part = 0.0; |
| 194 | mp_float_t fractional_part = MICROPY_FLOAT_C_FUN(modf)(mp_obj_get_float(x_obj), &int_part); |
| 195 | mp_obj_t tuple[2]; |
| 196 | tuple[0] = mp_obj_new_float(fractional_part); |
| 197 | tuple[1] = mp_obj_new_float(int_part); |
| 198 | return mp_obj_new_tuple(2, tuple); |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 199 | } |
| 200 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_modf_obj, mp_math_modf); |
| 201 | |
| 202 | // Angular conversions |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 203 | |
| 204 | /// \function radians(x) |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 205 | STATIC mp_obj_t mp_math_radians(mp_obj_t x_obj) { |
Damien George | 561844f | 2016-11-03 12:33:01 +1100 | [diff] [blame] | 206 | return mp_obj_new_float(mp_obj_get_float(x_obj) * (MP_PI / MICROPY_FLOAT_CONST(180.0))); |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 207 | } |
| 208 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_radians_obj, mp_math_radians); |
| 209 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 210 | /// \function degrees(x) |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 211 | STATIC mp_obj_t mp_math_degrees(mp_obj_t x_obj) { |
Damien George | 561844f | 2016-11-03 12:33:01 +1100 | [diff] [blame] | 212 | return mp_obj_new_float(mp_obj_get_float(x_obj) * (MICROPY_FLOAT_CONST(180.0) / MP_PI)); |
Rachel Dowdall | 249b9c7 | 2014-03-22 14:39:33 +0000 | [diff] [blame] | 213 | } |
| 214 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees); |
| 215 | |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 216 | STATIC const mp_rom_map_elem_t mp_module_math_globals_table[] = { |
| 217 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_math) }, |
| 218 | { MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e }, |
| 219 | { MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi }, |
| 220 | { MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_math_sqrt_obj) }, |
| 221 | { MP_ROM_QSTR(MP_QSTR_pow), MP_ROM_PTR(&mp_math_pow_obj) }, |
| 222 | { MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_math_exp_obj) }, |
Paul Sokolovsky | 863d4cd | 2015-11-14 00:55:54 +0200 | [diff] [blame] | 223 | #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 224 | { MP_ROM_QSTR(MP_QSTR_expm1), MP_ROM_PTR(&mp_math_expm1_obj) }, |
Paul Sokolovsky | 863d4cd | 2015-11-14 00:55:54 +0200 | [diff] [blame] | 225 | #endif |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 226 | { MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&mp_math_log_obj) }, |
Damien George | a24eafa | 2015-11-17 14:10:13 +0000 | [diff] [blame] | 227 | #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 228 | { MP_ROM_QSTR(MP_QSTR_log2), MP_ROM_PTR(&mp_math_log2_obj) }, |
| 229 | { MP_ROM_QSTR(MP_QSTR_log10), MP_ROM_PTR(&mp_math_log10_obj) }, |
| 230 | { MP_ROM_QSTR(MP_QSTR_cosh), MP_ROM_PTR(&mp_math_cosh_obj) }, |
| 231 | { MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&mp_math_sinh_obj) }, |
| 232 | { MP_ROM_QSTR(MP_QSTR_tanh), MP_ROM_PTR(&mp_math_tanh_obj) }, |
| 233 | { MP_ROM_QSTR(MP_QSTR_acosh), MP_ROM_PTR(&mp_math_acosh_obj) }, |
| 234 | { MP_ROM_QSTR(MP_QSTR_asinh), MP_ROM_PTR(&mp_math_asinh_obj) }, |
| 235 | { MP_ROM_QSTR(MP_QSTR_atanh), MP_ROM_PTR(&mp_math_atanh_obj) }, |
Damien George | a24eafa | 2015-11-17 14:10:13 +0000 | [diff] [blame] | 236 | #endif |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 237 | { MP_ROM_QSTR(MP_QSTR_cos), MP_ROM_PTR(&mp_math_cos_obj) }, |
| 238 | { MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&mp_math_sin_obj) }, |
| 239 | { MP_ROM_QSTR(MP_QSTR_tan), MP_ROM_PTR(&mp_math_tan_obj) }, |
| 240 | { MP_ROM_QSTR(MP_QSTR_acos), MP_ROM_PTR(&mp_math_acos_obj) }, |
| 241 | { MP_ROM_QSTR(MP_QSTR_asin), MP_ROM_PTR(&mp_math_asin_obj) }, |
| 242 | { MP_ROM_QSTR(MP_QSTR_atan), MP_ROM_PTR(&mp_math_atan_obj) }, |
| 243 | { MP_ROM_QSTR(MP_QSTR_atan2), MP_ROM_PTR(&mp_math_atan2_obj) }, |
| 244 | { MP_ROM_QSTR(MP_QSTR_ceil), MP_ROM_PTR(&mp_math_ceil_obj) }, |
| 245 | { MP_ROM_QSTR(MP_QSTR_copysign), MP_ROM_PTR(&mp_math_copysign_obj) }, |
| 246 | { MP_ROM_QSTR(MP_QSTR_fabs), MP_ROM_PTR(&mp_math_fabs_obj) }, |
| 247 | { MP_ROM_QSTR(MP_QSTR_floor), MP_ROM_PTR(&mp_math_floor_obj) }, |
| 248 | { MP_ROM_QSTR(MP_QSTR_fmod), MP_ROM_PTR(&mp_math_fmod_obj) }, |
| 249 | { MP_ROM_QSTR(MP_QSTR_frexp), MP_ROM_PTR(&mp_math_frexp_obj) }, |
| 250 | { MP_ROM_QSTR(MP_QSTR_ldexp), MP_ROM_PTR(&mp_math_ldexp_obj) }, |
| 251 | { MP_ROM_QSTR(MP_QSTR_modf), MP_ROM_PTR(&mp_math_modf_obj) }, |
| 252 | { MP_ROM_QSTR(MP_QSTR_isfinite), MP_ROM_PTR(&mp_math_isfinite_obj) }, |
| 253 | { MP_ROM_QSTR(MP_QSTR_isinf), MP_ROM_PTR(&mp_math_isinf_obj) }, |
| 254 | { MP_ROM_QSTR(MP_QSTR_isnan), MP_ROM_PTR(&mp_math_isnan_obj) }, |
| 255 | { MP_ROM_QSTR(MP_QSTR_trunc), MP_ROM_PTR(&mp_math_trunc_obj) }, |
| 256 | { MP_ROM_QSTR(MP_QSTR_radians), MP_ROM_PTR(&mp_math_radians_obj) }, |
| 257 | { MP_ROM_QSTR(MP_QSTR_degrees), MP_ROM_PTR(&mp_math_degrees_obj) }, |
Damien George | 5cbeace | 2015-02-22 14:48:18 +0000 | [diff] [blame] | 258 | #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 259 | { MP_ROM_QSTR(MP_QSTR_erf), MP_ROM_PTR(&mp_math_erf_obj) }, |
| 260 | { MP_ROM_QSTR(MP_QSTR_erfc), MP_ROM_PTR(&mp_math_erfc_obj) }, |
| 261 | { MP_ROM_QSTR(MP_QSTR_gamma), MP_ROM_PTR(&mp_math_gamma_obj) }, |
| 262 | { MP_ROM_QSTR(MP_QSTR_lgamma), MP_ROM_PTR(&mp_math_lgamma_obj) }, |
Damien George | 5cbeace | 2015-02-22 14:48:18 +0000 | [diff] [blame] | 263 | #endif |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
Damien George | 3b603f2 | 2014-11-29 14:39:27 +0000 | [diff] [blame] | 266 | STATIC MP_DEFINE_CONST_DICT(mp_module_math_globals, mp_module_math_globals_table); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 267 | |
| 268 | const mp_obj_module_t mp_module_math = { |
| 269 | .base = { &mp_type_module }, |
Damien George | 8b0535e | 2014-04-05 21:53:54 +0100 | [diff] [blame] | 270 | .globals = (mp_obj_dict_t*)&mp_module_math_globals, |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 273 | #endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH |