Skip to content
Snippets Groups Projects
Select Git revision
2 results Searching

qstrdefs.h

Blame
  • Forked from card10 / firmware
    Source project has a limited visibility.
    modmath.c 9.76 KiB
    /*
     * This file is part of the Micro Python project, http://micropython.org/
     *
     * The MIT License (MIT)
     *
     * Copyright (c) 2013, 2014 Damien P. George
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     *
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     */
    
    #include "py/builtin.h"
    
    #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_MATH
    
    #include <math.h>
    
    /// \module math - mathematical functions
    ///
    /// The `math` module provides some basic mathematical funtions for
    /// working with floating-point numbers.
    
    //TODO: Change macros to check for overflow and raise OverflowError or RangeError
    #define MATH_FUN_1(py_name, c_name) \
        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))); } \
        STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
    
    #define MATH_FUN_2(py_name, c_name) \
        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))); } \
        STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_math_## py_name ## _obj, mp_math_ ## py_name);
    
    #define MATH_FUN_1_TO_BOOL(py_name, c_name) \
        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))); } \
        STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
    
    #define MATH_FUN_1_TO_INT(py_name, c_name) \
        STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { mp_int_t x = MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj)); return mp_obj_new_int(x); } \
        STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
    
    #if MP_NEED_LOG2
    // 1.442695040888963407354163704 is 1/_M_LN2
    #define log2(x) (log(x) * 1.442695040888963407354163704)
    #endif
    
    /// \function sqrt(x)
    /// Returns the square root of `x`.
    MATH_FUN_1(sqrt, sqrt)
    /// \function pow(x, y)
    /// Returns `x` to the power of `y`.
    MATH_FUN_2(pow, pow)
    /// \function exp(x)
    MATH_FUN_1(exp, exp)
    #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
    /// \function expm1(x)
    MATH_FUN_1(expm1, expm1)