35 #if MICROPY_PY_BUILTINS_FLOAT 40 #if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D 44 #define M_E (2.7182818284590452354) 47 #define M_PI (3.14159265358979323846) 50 typedef struct _mp_obj_float_t {
60 #if MICROPY_FLOAT_HIGH_QUALITY_HASH 62 mp_int_t mp_float_hash(mp_float_t src) {
63 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE 65 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 70 #if MP_ENDIANNESS_LITTLE 71 struct { mp_float_uint_t frc:MP_FLOAT_FRAC_BITS,
exp:MP_FLOAT_EXP_BITS, sgn:1; } p;
73 struct { mp_float_uint_t sgn:1,
exp:MP_FLOAT_EXP_BITS, frc:MP_FLOAT_FRAC_BITS; } p;
79 const int adj_exp = (int)u.p.exp - MP_FLOAT_EXP_BIAS;
86 mp_float_uint_t frc = u.p.frc | ((mp_float_uint_t)1 << MP_FLOAT_FRAC_BITS);
88 if (adj_exp <= MP_FLOAT_FRAC_BITS) {
90 val = (frc >> (MP_FLOAT_FRAC_BITS - adj_exp))
91 ^ (frc & ((1 << (MP_FLOAT_FRAC_BITS - adj_exp)) - 1));
94 val = (
mp_int_t)frc << (adj_exp - MP_FLOAT_FRAC_BITS);
111 mp_float_t o_val = mp_obj_float_get(o_in);
112 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT 114 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C 115 const int precision = 6;
117 const int precision = 7;
121 const int precision = 16;
123 mp_format_float(o_val, buf,
sizeof(buf),
'g', precision,
'\0');
137 return mp_obj_new_float(0);
151 return mp_obj_new_float(mp_obj_get_float(
args[0]));
157 mp_float_t val = mp_obj_float_get(o_in);
166 return mp_obj_new_float(-val);
176 mp_float_t lhs_val = mp_obj_float_get(lhs_in);
177 #if MICROPY_PY_BUILTINS_COMPLEX 179 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs_in);
183 return mp_obj_float_binary_op(op, lhs_val, rhs_in);
189 .
name = MP_QSTR_float,
190 .print = float_print,
191 .make_new = float_make_new,
192 .unary_op = float_unary_op,
193 .binary_op = float_binary_op,
196 #if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D 198 mp_obj_t mp_obj_new_float(mp_float_t value) {
199 mp_obj_float_t *o =
m_new(mp_obj_float_t, 1);
205 mp_float_t mp_obj_float_get(
mp_obj_t self_in) {
213 STATIC void mp_obj_float_divmod(mp_float_t *x, mp_float_t *y) {
218 mp_float_t mod = MICROPY_FLOAT_C_FUN(
fmod)(*x, *y);
219 mp_float_t div = (*x - mod) / *y;
223 mod = MICROPY_FLOAT_C_FUN(
copysign)(0.0, *y);
225 if ((mod < 0.0) != (*y < 0.0)) {
234 floordiv = MICROPY_FLOAT_C_FUN(
copysign)(0.0, *x / *y);
237 floordiv = MICROPY_FLOAT_C_FUN(
floor)(div);
238 if (div - floordiv > 0.5) {
250 if (!mp_obj_get_float_maybe(rhs_in, &rhs_val)) {
270 mp_obj_float_divmod(&lhs_val, &rhs_val);
275 goto zero_division_error;
282 goto zero_division_error;
284 lhs_val = MICROPY_FLOAT_C_FUN(
fmod)(lhs_val, rhs_val);
286 if (lhs_val == 0.0) {
287 lhs_val = MICROPY_FLOAT_C_FUN(
copysign)(0.0, rhs_val);
289 if ((lhs_val < 0.0) != (rhs_val < 0.0)) {
296 if (lhs_val == 0 && rhs_val < 0) {
297 goto zero_division_error;
299 if (lhs_val < 0 && rhs_val != MICROPY_FLOAT_C_FUN(
floor)(rhs_val)) {
300 #if MICROPY_PY_BUILTINS_COMPLEX 306 lhs_val = MICROPY_FLOAT_C_FUN(
pow)(lhs_val, rhs_val);
310 goto zero_division_error;
312 mp_obj_float_divmod(&lhs_val, &rhs_val);
314 mp_obj_new_float(lhs_val),
315 mp_obj_new_float(rhs_val),
328 return mp_obj_new_float(lhs_val);
331 #endif // MICROPY_PY_BUILTINS_FLOAT
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items)
const mp_obj_type_t mp_type_float
const mp_obj_type_t mp_type_ZeroDivisionError
#define MP_OBJ_IS_TYPE(o, t)
int mp_print_str(const mp_print_t *print, const char *str)
#define MP_OBJ_FROM_PTR(p)
void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw)
#define mp_obj_is_float(o)
const mp_obj_type_t mp_type_complex
#define MP_OBJ_NEW_SMALL_INT(small_int)
const char * mp_obj_str_get_data(mp_obj_t self_in, size_t *len)
char * strchr(const char *s, int c)
unsigned long long uint64_t
const mp_obj_type_t mp_type_type
mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex)
NORETURN void mp_raise_ValueError(const char *msg)