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 | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
Paul Sokolovsky | 864038d | 2014-03-31 02:12:40 +0300 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <string.h> |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 30 | #include <assert.h> |
Rachel Dowdall | 300c8bd | 2014-03-20 22:40:38 +0000 | [diff] [blame] | 31 | #include <math.h> |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 32 | |
Paul Sokolovsky | f54bcbf | 2014-05-02 17:47:01 +0300 | [diff] [blame] | 33 | #include "mpconfig.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 34 | #include "nlr.h" |
| 35 | #include "misc.h" |
Damien George | 55baff4 | 2014-01-21 21:40:13 +0000 | [diff] [blame] | 36 | #include "qstr.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 37 | #include "obj.h" |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 38 | #include "parsenum.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 39 | #include "runtime0.h" |
Damien George | ee7a880 | 2014-05-11 18:37:21 +0100 | [diff] [blame] | 40 | #include "runtime.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 41 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 42 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | 8bfec2b | 2014-03-10 13:27:02 +0000 | [diff] [blame] | 43 | |
Dave Hylands | ca5a241 | 2014-03-10 00:10:01 -0700 | [diff] [blame] | 44 | #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT |
Damien George | 8bfec2b | 2014-03-10 13:27:02 +0000 | [diff] [blame] | 45 | #include "formatfloat.h" |
Dave Hylands | ca5a241 | 2014-03-10 00:10:01 -0700 | [diff] [blame] | 46 | #endif |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 47 | |
Paul Sokolovsky | d5df6cd | 2014-02-12 18:15:40 +0200 | [diff] [blame] | 48 | STATIC void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 49 | mp_obj_float_t *o = o_in; |
Dave Hylands | ca5a241 | 2014-03-10 00:10:01 -0700 | [diff] [blame] | 50 | #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT |
Damien George | 20beff9 | 2014-09-11 22:24:45 +0100 | [diff] [blame] | 51 | char buf[16]; |
| 52 | format_float(o->value, buf, sizeof(buf), 'g', 7, '\0'); |
Dave Hylands | ca5a241 | 2014-03-10 00:10:01 -0700 | [diff] [blame] | 53 | print(env, "%s", buf); |
Damien George | 20beff9 | 2014-09-11 22:24:45 +0100 | [diff] [blame] | 54 | if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL) { |
Damien George | 60be1cf | 2014-04-05 20:51:29 +0100 | [diff] [blame] | 55 | // Python floats always have decimal point |
| 56 | print(env, ".0"); |
| 57 | } |
Dave Hylands | ca5a241 | 2014-03-10 00:10:01 -0700 | [diff] [blame] | 58 | #else |
Paul Sokolovsky | 864038d | 2014-03-31 02:12:40 +0300 | [diff] [blame] | 59 | char buf[32]; |
Damien George | 20beff9 | 2014-09-11 22:24:45 +0100 | [diff] [blame] | 60 | sprintf(buf, "%.16g", (double) o->value); |
Paul Sokolovsky | 864038d | 2014-03-31 02:12:40 +0300 | [diff] [blame] | 61 | print(env, buf); |
Damien George | 20beff9 | 2014-09-11 22:24:45 +0100 | [diff] [blame] | 62 | if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL) { |
Paul Sokolovsky | 864038d | 2014-03-31 02:12:40 +0300 | [diff] [blame] | 63 | // Python floats always have decimal point |
| 64 | print(env, ".0"); |
| 65 | } |
Dave Hylands | ca5a241 | 2014-03-10 00:10:01 -0700 | [diff] [blame] | 66 | #endif |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 69 | STATIC mp_obj_t float_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
Damien George | ee7a880 | 2014-05-11 18:37:21 +0100 | [diff] [blame] | 70 | mp_arg_check_num(n_args, n_kw, 0, 1, false); |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 71 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 72 | switch (n_args) { |
| 73 | case 0: |
| 74 | return mp_obj_new_float(0); |
| 75 | |
| 76 | case 1: |
Damien George | ee7a880 | 2014-05-11 18:37:21 +0100 | [diff] [blame] | 77 | default: |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 78 | if (MP_OBJ_IS_STR(args[0])) { |
| 79 | // a string, parse it |
Damien George | d182b98 | 2014-08-30 14:19:41 +0100 | [diff] [blame] | 80 | mp_uint_t l; |
Damien George | 2077397 | 2014-02-22 18:12:43 +0000 | [diff] [blame] | 81 | const char *s = mp_obj_str_get_data(args[0], &l); |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 82 | return mp_parse_num_decimal(s, l, false, false); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 83 | } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) { |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 84 | // a float, just return it |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 85 | return args[0]; |
| 86 | } else { |
Damien George | 6e48f7f | 2014-03-21 11:45:46 +0000 | [diff] [blame] | 87 | // something else, try to cast it to a float |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 88 | return mp_obj_new_float(mp_obj_get_float(args[0])); |
| 89 | } |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 93 | STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 94 | mp_obj_float_t *o = o_in; |
| 95 | switch (op) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 96 | case MP_UNARY_OP_BOOL: return MP_BOOL(o->value != 0); |
| 97 | case MP_UNARY_OP_POSITIVE: return o_in; |
| 98 | case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value); |
Damien George | 6ac5dce | 2014-05-21 19:42:43 +0100 | [diff] [blame] | 99 | default: return MP_OBJ_NULL; // op not supported |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 103 | STATIC mp_obj_t float_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 104 | mp_obj_float_t *lhs = lhs_in; |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 105 | #if MICROPY_PY_BUILTINS_COMPLEX |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 106 | if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) { |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 107 | return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in); |
Paul Sokolovsky | 3b6f7b9 | 2014-06-20 01:48:35 +0300 | [diff] [blame] | 108 | } else |
| 109 | #endif |
| 110 | { |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 111 | return mp_obj_float_binary_op(op, lhs->value, rhs_in); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 112 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 115 | const mp_obj_type_t mp_type_float = { |
Damien George | c596612 | 2014-02-15 16:10:44 +0000 | [diff] [blame] | 116 | { &mp_type_type }, |
Damien George | a71c83a | 2014-02-15 11:34:50 +0000 | [diff] [blame] | 117 | .name = MP_QSTR_float, |
Paul Sokolovsky | 860ffb0 | 2014-01-05 22:34:09 +0200 | [diff] [blame] | 118 | .print = float_print, |
| 119 | .make_new = float_make_new, |
| 120 | .unary_op = float_unary_op, |
| 121 | .binary_op = float_binary_op, |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | mp_obj_t mp_obj_new_float(mp_float_t value) { |
| 125 | mp_obj_float_t *o = m_new(mp_obj_float_t, 1); |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 126 | o->base.type = &mp_type_float; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 127 | o->value = value; |
| 128 | return (mp_obj_t)o; |
| 129 | } |
| 130 | |
| 131 | mp_float_t mp_obj_float_get(mp_obj_t self_in) { |
Damien George | 0c36da0 | 2014-03-08 15:24:39 +0000 | [diff] [blame] | 132 | assert(MP_OBJ_IS_TYPE(self_in, &mp_type_float)); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 133 | mp_obj_float_t *self = self_in; |
| 134 | return self->value; |
| 135 | } |
| 136 | |
Damien George | ecc88e9 | 2014-08-30 00:35:11 +0100 | [diff] [blame] | 137 | mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs_in) { |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 138 | mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible) |
| 139 | switch (op) { |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 140 | case MP_BINARY_OP_ADD: |
| 141 | case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break; |
| 142 | case MP_BINARY_OP_SUBTRACT: |
| 143 | case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break; |
| 144 | case MP_BINARY_OP_MULTIPLY: |
| 145 | case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 146 | case MP_BINARY_OP_FLOOR_DIVIDE: |
Paul Sokolovsky | 96ed213 | 2014-03-31 02:18:09 +0300 | [diff] [blame] | 147 | case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: |
Damien George | 9dcc60d | 2014-04-13 17:45:30 +0100 | [diff] [blame] | 148 | if (rhs_val == 0) { |
| 149 | zero_division_error: |
Damien George | 8594ce2 | 2014-09-13 18:43:09 +0100 | [diff] [blame^] | 150 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero")); |
Rachel Dowdall | 300c8bd | 2014-03-20 22:40:38 +0000 | [diff] [blame] | 151 | } |
Damien George | 8594ce2 | 2014-09-13 18:43:09 +0100 | [diff] [blame^] | 152 | // Python specs require that x == (x//y)*y + (x%y) so we must |
| 153 | // call divmod to compute the correct floor division, which |
| 154 | // returns the floor divide in lhs_val. |
| 155 | mp_obj_float_divmod(&lhs_val, &rhs_val); |
Damien George | 9dcc60d | 2014-04-13 17:45:30 +0100 | [diff] [blame] | 156 | break; |
| 157 | case MP_BINARY_OP_TRUE_DIVIDE: |
| 158 | case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: |
| 159 | if (rhs_val == 0) { |
| 160 | goto zero_division_error; |
| 161 | } |
| 162 | lhs_val /= rhs_val; |
Rachel Dowdall | 300c8bd | 2014-03-20 22:40:38 +0000 | [diff] [blame] | 163 | break; |
Damien George | 8594ce2 | 2014-09-13 18:43:09 +0100 | [diff] [blame^] | 164 | case MP_BINARY_OP_MODULO: |
| 165 | case MP_BINARY_OP_INPLACE_MODULO: |
| 166 | if (rhs_val == 0) { |
| 167 | goto zero_division_error; |
| 168 | } |
| 169 | lhs_val = MICROPY_FLOAT_C_FUN(fmod)(lhs_val, rhs_val); |
| 170 | // Python specs require that mod has same sign as second operand |
| 171 | if (lhs_val == 0.0) { |
| 172 | lhs_val = MICROPY_FLOAT_C_FUN(copysign)(0.0, rhs_val); |
| 173 | } else { |
| 174 | if ((lhs_val < 0.0) != (rhs_val < 0.0)) { |
| 175 | lhs_val += rhs_val; |
| 176 | } |
| 177 | } |
| 178 | break; |
Damien George | b23fbb3 | 2014-04-02 12:26:49 +0100 | [diff] [blame] | 179 | case MP_BINARY_OP_POWER: |
| 180 | case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break; |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 181 | case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); |
| 182 | case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); |
Damien George | b8a053a | 2014-04-11 10:10:37 +0100 | [diff] [blame] | 183 | case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_val == rhs_val); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 184 | case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); |
| 185 | case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); |
John R. Lenton | b8698fc | 2014-01-11 00:58:59 +0000 | [diff] [blame] | 186 | |
Paul Sokolovsky | a8e60c1 | 2014-03-31 01:38:25 +0300 | [diff] [blame] | 187 | default: |
Damien George | 6ac5dce | 2014-05-21 19:42:43 +0100 | [diff] [blame] | 188 | return MP_OBJ_NULL; // op not supported |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 189 | } |
| 190 | return mp_obj_new_float(lhs_val); |
| 191 | } |
| 192 | |
Damien George | 8594ce2 | 2014-09-13 18:43:09 +0100 | [diff] [blame^] | 193 | void mp_obj_float_divmod(mp_float_t *x, mp_float_t *y) { |
| 194 | // logic here follows that of CPython |
| 195 | // https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations |
| 196 | // x == (x//y)*y + (x%y) |
| 197 | // divmod(x, y) == (x//y, x%y) |
| 198 | mp_float_t mod = MICROPY_FLOAT_C_FUN(fmod)(*x, *y); |
| 199 | mp_float_t div = (*x - mod) / *y; |
| 200 | |
| 201 | // Python specs require that mod has same sign as second operand |
| 202 | if (mod == 0.0) { |
| 203 | mod = MICROPY_FLOAT_C_FUN(copysign)(0.0, *y); |
| 204 | } else { |
| 205 | if ((mod < 0.0) != (*y < 0.0)) { |
| 206 | mod += *y; |
| 207 | div -= 1.0; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | mp_float_t floordiv; |
| 212 | if (div == 0.0) { |
| 213 | // if division is zero, take the correct sign of zero |
| 214 | floordiv = MICROPY_FLOAT_C_FUN(copysign)(0.0, *x / *y); |
| 215 | } else { |
| 216 | // Python specs require that x == (x//y)*y + (x%y) |
| 217 | floordiv = MICROPY_FLOAT_C_FUN(floor)(div); |
| 218 | if (div - floordiv > 0.5) { |
| 219 | floordiv += 1.0; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // return results |
| 224 | *x = floordiv; |
| 225 | *y = mod; |
| 226 | } |
| 227 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 228 | #endif // MICROPY_PY_BUILTINS_FLOAT |