blob: 5cf9954175c56a8e95e52e22b0162c19923745dc [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
Damiend99b0522013-12-21 18:17:45 +000027#include <stdlib.h>
Paul Sokolovsky864038d2014-03-31 02:12:40 +030028#include <stdio.h>
29#include <string.h>
Damiend99b0522013-12-21 18:17:45 +000030#include <assert.h>
31
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/nlr.h"
33#include "py/parsenum.h"
34#include "py/runtime0.h"
35#include "py/runtime.h"
Damiend99b0522013-12-21 18:17:45 +000036
Damien Georgefb510b32014-06-01 13:32:54 +010037#if MICROPY_PY_BUILTINS_FLOAT
Damien George8bfec2b2014-03-10 13:27:02 +000038
Damien George1ef26b32015-03-14 23:11:25 +000039#include <math.h>
Damien George51dfcb42015-01-01 20:27:54 +000040#include "py/formatfloat.h"
Damiend99b0522013-12-21 18:17:45 +000041
Damien Georgeb8cfb0d2015-11-27 17:09:11 +000042#if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D
Damien George183edef2015-10-17 23:20:57 +010043
Colin Hogbena8969512016-05-09 22:11:34 +010044// M_E and M_PI are not part of the math.h standard and may not be defined
45#ifndef M_E
46#define M_E (2.7182818284590452354)
47#endif
48#ifndef M_PI
49#define M_PI (3.14159265358979323846)
50#endif
51
Damien George7e359c62015-08-20 23:42:35 +010052typedef struct _mp_obj_float_t {
53 mp_obj_base_t base;
54 mp_float_t value;
55} mp_obj_float_t;
56
57const mp_obj_float_t mp_const_float_e_obj = {{&mp_type_float}, M_E};
58const mp_obj_float_t mp_const_float_pi_obj = {{&mp_type_float}, M_PI};
59
Damien George183edef2015-10-17 23:20:57 +010060#endif
61
Damien Georgea73501b2017-04-06 17:27:33 +100062#if MICROPY_FLOAT_HIGH_QUALITY_HASH
63// must return actual integer value if it fits in mp_int_t
64mp_int_t mp_float_hash(mp_float_t src) {
65#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
66typedef uint64_t mp_float_uint_t;
67#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
68typedef uint32_t mp_float_uint_t;
69#endif
70 union {
71 mp_float_t f;
72 #if MP_ENDIANNESS_LITTLE
73 struct { mp_float_uint_t frc:MP_FLOAT_FRAC_BITS, exp:MP_FLOAT_EXP_BITS, sgn:1; } p;
74 #else
75 struct { mp_float_uint_t sgn:1, exp:MP_FLOAT_EXP_BITS, frc:MP_FLOAT_FRAC_BITS; } p;
76 #endif
77 mp_float_uint_t i;
78 } u = {.f = src};
79
80 mp_int_t val;
81 const int adj_exp = (int)u.p.exp - MP_FLOAT_EXP_BIAS;
82 if (adj_exp < 0) {
83 // value < 1; must be sure to handle 0.0 correctly (ie return 0)
84 val = u.i;
85 } else {
86 // if adj_exp is max then: u.p.frc==0 indicates inf, else NaN
87 // else: 1 <= value
88 mp_float_uint_t frc = u.p.frc | ((mp_float_uint_t)1 << MP_FLOAT_FRAC_BITS);
89
90 if (adj_exp <= MP_FLOAT_FRAC_BITS) {
91 // number may have a fraction; xor the integer part with the fractional part
92 val = (frc >> (MP_FLOAT_FRAC_BITS - adj_exp))
93 ^ (frc & ((1 << (MP_FLOAT_FRAC_BITS - adj_exp)) - 1));
94 } else if ((unsigned int)adj_exp < BITS_PER_BYTE * sizeof(mp_int_t) - 1) {
95 // the number is a (big) whole integer and will fit in val's signed-width
96 val = (mp_int_t)frc << (adj_exp - MP_FLOAT_FRAC_BITS);
97 } else {
98 // integer part will overflow val's width so just use what bits we can
99 val = frc;
100 }
101 }
102
103 if (u.p.sgn) {
104 val = -val;
105 }
106
107 return val;
108}
109#endif
110
Damien George7f9d1d62015-04-09 23:56:15 +0100111STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000112 (void)kind;
Damien Georgeaedb8592015-10-17 22:57:34 +0100113 mp_float_t o_val = mp_obj_float_get(o_in);
Dave Hylandsca5a2412014-03-10 00:10:01 -0700114#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
Damien George20beff92014-09-11 22:24:45 +0100115 char buf[16];
stijn861670b2015-05-16 10:54:19 +0200116 const int precision = 7;
Dave Hylandsca5a2412014-03-10 00:10:01 -0700117#else
Paul Sokolovsky864038d2014-03-31 02:12:40 +0300118 char buf[32];
stijn861670b2015-05-16 10:54:19 +0200119 const int precision = 16;
120#endif
Damien Georgeaedb8592015-10-17 22:57:34 +0100121 mp_format_float(o_val, buf, sizeof(buf), 'g', precision, '\0');
Damien George7f9d1d62015-04-09 23:56:15 +0100122 mp_print_str(print, buf);
Damien George956d7652015-04-22 16:51:29 +0100123 if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
124 // Python floats always have decimal point (unless inf or nan)
Damien George7f9d1d62015-04-09 23:56:15 +0100125 mp_print_str(print, ".0");
Paul Sokolovsky864038d2014-03-31 02:12:40 +0300126 }
Damiend99b0522013-12-21 18:17:45 +0000127}
128
Damien George5b3f0b72016-01-03 15:55:55 +0000129STATIC mp_obj_t float_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000130 (void)type_in;
Damien Georgeee7a8802014-05-11 18:37:21 +0100131 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Damien George20006db2014-01-18 14:10:48 +0000132
Damien George71c51812014-01-04 20:21:15 +0000133 switch (n_args) {
134 case 0:
135 return mp_obj_new_float(0);
136
137 case 1:
Damien Georgeee7a8802014-05-11 18:37:21 +0100138 default:
Damien George20773972014-02-22 18:12:43 +0000139 if (MP_OBJ_IS_STR(args[0])) {
140 // a string, parse it
Damien George6b341072017-03-25 19:48:18 +1100141 size_t l;
Damien George20773972014-02-22 18:12:43 +0000142 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George7d414a12015-02-08 01:57:40 +0000143 return mp_parse_num_decimal(s, l, false, false, NULL);
Damien Georgeaaef1852015-08-20 23:30:12 +0100144 } else if (mp_obj_is_float(args[0])) {
Damien George6e48f7f2014-03-21 11:45:46 +0000145 // a float, just return it
Damien George71c51812014-01-04 20:21:15 +0000146 return args[0];
147 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000148 // something else, try to cast it to a float
Damien George71c51812014-01-04 20:21:15 +0000149 return mp_obj_new_float(mp_obj_get_float(args[0]));
150 }
Damien George71c51812014-01-04 20:21:15 +0000151 }
152}
153
Damien Georgeecc88e92014-08-30 00:35:11 +0100154STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damien Georgeaedb8592015-10-17 22:57:34 +0100155 mp_float_t val = mp_obj_float_get(o_in);
Damiend99b0522013-12-21 18:17:45 +0000156 switch (op) {
Damien Georgeaedb8592015-10-17 22:57:34 +0100157 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(val != 0);
Damien George19f2e472017-04-04 11:57:21 +1000158 case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(val));
Damien Georged17926d2014-03-30 13:35:08 +0100159 case MP_UNARY_OP_POSITIVE: return o_in;
Damien Georgeaedb8592015-10-17 22:57:34 +0100160 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val);
Damien George6ac5dce2014-05-21 19:42:43 +0100161 default: return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000162 }
163}
164
Damien Georgeecc88e92014-08-30 00:35:11 +0100165STATIC mp_obj_t float_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgeaedb8592015-10-17 22:57:34 +0100166 mp_float_t lhs_val = mp_obj_float_get(lhs_in);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300167#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000168 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
Damien Georgeaedb8592015-10-17 22:57:34 +0100169 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs_in);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300170 } else
171#endif
172 {
Damien Georgeaedb8592015-10-17 22:57:34 +0100173 return mp_obj_float_binary_op(op, lhs_val, rhs_in);
Damiend99b0522013-12-21 18:17:45 +0000174 }
Damiend99b0522013-12-21 18:17:45 +0000175}
176
Damien George0c36da02014-03-08 15:24:39 +0000177const mp_obj_type_t mp_type_float = {
Damien Georgec5966122014-02-15 16:10:44 +0000178 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000179 .name = MP_QSTR_float,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200180 .print = float_print,
181 .make_new = float_make_new,
182 .unary_op = float_unary_op,
183 .binary_op = float_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000184};
185
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000186#if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D
Damien George183edef2015-10-17 23:20:57 +0100187
Damiend99b0522013-12-21 18:17:45 +0000188mp_obj_t mp_obj_new_float(mp_float_t value) {
189 mp_obj_float_t *o = m_new(mp_obj_float_t, 1);
Damien George0c36da02014-03-08 15:24:39 +0000190 o->base.type = &mp_type_float;
Damiend99b0522013-12-21 18:17:45 +0000191 o->value = value;
Damien George999cedb2015-11-27 17:01:44 +0000192 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000193}
194
195mp_float_t mp_obj_float_get(mp_obj_t self_in) {
Damien Georgeaaef1852015-08-20 23:30:12 +0100196 assert(mp_obj_is_float(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000197 mp_obj_float_t *self = MP_OBJ_TO_PTR(self_in);
Damiend99b0522013-12-21 18:17:45 +0000198 return self->value;
199}
200
Damien George183edef2015-10-17 23:20:57 +0100201#endif
202
Damien Georgec5029bc2015-06-13 22:00:10 +0100203STATIC void mp_obj_float_divmod(mp_float_t *x, mp_float_t *y) {
204 // logic here follows that of CPython
205 // https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
206 // x == (x//y)*y + (x%y)
207 // divmod(x, y) == (x//y, x%y)
208 mp_float_t mod = MICROPY_FLOAT_C_FUN(fmod)(*x, *y);
209 mp_float_t div = (*x - mod) / *y;
210
211 // Python specs require that mod has same sign as second operand
212 if (mod == 0.0) {
213 mod = MICROPY_FLOAT_C_FUN(copysign)(0.0, *y);
214 } else {
215 if ((mod < 0.0) != (*y < 0.0)) {
216 mod += *y;
217 div -= 1.0;
218 }
219 }
220
221 mp_float_t floordiv;
222 if (div == 0.0) {
223 // if division is zero, take the correct sign of zero
224 floordiv = MICROPY_FLOAT_C_FUN(copysign)(0.0, *x / *y);
225 } else {
226 // Python specs require that x == (x//y)*y + (x%y)
227 floordiv = MICROPY_FLOAT_C_FUN(floor)(div);
228 if (div - floordiv > 0.5) {
229 floordiv += 1.0;
230 }
231 }
232
233 // return results
234 *x = floordiv;
235 *y = mod;
236}
237
Damien Georgeecc88e92014-08-30 00:35:11 +0100238mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs_in) {
Damien Georgee2e3d112014-01-06 22:13:00 +0000239 mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
240 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100241 case MP_BINARY_OP_ADD:
242 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
243 case MP_BINARY_OP_SUBTRACT:
244 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
245 case MP_BINARY_OP_MULTIPLY:
246 case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
Damien Georged17926d2014-03-30 13:35:08 +0100247 case MP_BINARY_OP_FLOOR_DIVIDE:
Paul Sokolovsky96ed2132014-03-31 02:18:09 +0300248 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien George9dcc60d2014-04-13 17:45:30 +0100249 if (rhs_val == 0) {
250 zero_division_error:
Damien George7d0d7212016-10-17 12:17:37 +1100251 mp_raise_msg(&mp_type_ZeroDivisionError, "division by zero");
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000252 }
Damien George8594ce22014-09-13 18:43:09 +0100253 // Python specs require that x == (x//y)*y + (x%y) so we must
254 // call divmod to compute the correct floor division, which
255 // returns the floor divide in lhs_val.
256 mp_obj_float_divmod(&lhs_val, &rhs_val);
Damien George9dcc60d2014-04-13 17:45:30 +0100257 break;
258 case MP_BINARY_OP_TRUE_DIVIDE:
259 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
260 if (rhs_val == 0) {
261 goto zero_division_error;
262 }
263 lhs_val /= rhs_val;
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000264 break;
Damien George8594ce22014-09-13 18:43:09 +0100265 case MP_BINARY_OP_MODULO:
266 case MP_BINARY_OP_INPLACE_MODULO:
267 if (rhs_val == 0) {
268 goto zero_division_error;
269 }
270 lhs_val = MICROPY_FLOAT_C_FUN(fmod)(lhs_val, rhs_val);
271 // Python specs require that mod has same sign as second operand
272 if (lhs_val == 0.0) {
273 lhs_val = MICROPY_FLOAT_C_FUN(copysign)(0.0, rhs_val);
274 } else {
275 if ((lhs_val < 0.0) != (rhs_val < 0.0)) {
276 lhs_val += rhs_val;
277 }
278 }
279 break;
Damien Georgeb23fbb32014-04-02 12:26:49 +0100280 case MP_BINARY_OP_POWER:
Damien George4b8ec522017-02-03 00:01:37 +1100281 case MP_BINARY_OP_INPLACE_POWER:
282 if (lhs_val == 0 && rhs_val < 0) {
283 goto zero_division_error;
284 }
285 lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val);
286 break;
Damien Georgec5029bc2015-06-13 22:00:10 +0100287 case MP_BINARY_OP_DIVMOD: {
288 if (rhs_val == 0) {
289 goto zero_division_error;
290 }
291 mp_obj_float_divmod(&lhs_val, &rhs_val);
292 mp_obj_t tuple[2] = {
293 mp_obj_new_float(lhs_val),
294 mp_obj_new_float(rhs_val),
295 };
296 return mp_obj_new_tuple(2, tuple);
297 }
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300298 case MP_BINARY_OP_LESS: return mp_obj_new_bool(lhs_val < rhs_val);
299 case MP_BINARY_OP_MORE: return mp_obj_new_bool(lhs_val > rhs_val);
300 case MP_BINARY_OP_EQUAL: return mp_obj_new_bool(lhs_val == rhs_val);
301 case MP_BINARY_OP_LESS_EQUAL: return mp_obj_new_bool(lhs_val <= rhs_val);
302 case MP_BINARY_OP_MORE_EQUAL: return mp_obj_new_bool(lhs_val >= rhs_val);
John R. Lentonb8698fc2014-01-11 00:58:59 +0000303
Paul Sokolovskya8e60c12014-03-31 01:38:25 +0300304 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100305 return MP_OBJ_NULL; // op not supported
Damien Georgee2e3d112014-01-06 22:13:00 +0000306 }
307 return mp_obj_new_float(lhs_val);
308}
309
Damien Georgefb510b32014-06-01 13:32:54 +0100310#endif // MICROPY_PY_BUILTINS_FLOAT