blob: 0af41990ff8af760206e1a0ddba3840ef5308cff [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 George7f9d1d62015-04-09 23:56:15 +010042STATIC 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 +000043 (void)kind;
Damiend99b0522013-12-21 18:17:45 +000044 mp_obj_float_t *o = o_in;
Dave Hylandsca5a2412014-03-10 00:10:01 -070045#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
Damien George20beff92014-09-11 22:24:45 +010046 char buf[16];
stijn861670b2015-05-16 10:54:19 +020047 const int precision = 7;
Dave Hylandsca5a2412014-03-10 00:10:01 -070048#else
Paul Sokolovsky864038d2014-03-31 02:12:40 +030049 char buf[32];
stijn861670b2015-05-16 10:54:19 +020050 const int precision = 16;
51#endif
52 mp_format_float(o->value, buf, sizeof(buf), 'g', precision, '\0');
Damien George7f9d1d62015-04-09 23:56:15 +010053 mp_print_str(print, buf);
Damien George956d7652015-04-22 16:51:29 +010054 if (strchr(buf, '.') == NULL && strchr(buf, 'e') == NULL && strchr(buf, 'n') == NULL) {
55 // Python floats always have decimal point (unless inf or nan)
Damien George7f9d1d62015-04-09 23:56:15 +010056 mp_print_str(print, ".0");
Paul Sokolovsky864038d2014-03-31 02:12:40 +030057 }
Damiend99b0522013-12-21 18:17:45 +000058}
59
Damien Georgeecc88e92014-08-30 00:35:11 +010060STATIC 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 Georgeff8dd3f2015-01-20 12:47:20 +000061 (void)type_in;
Damien Georgeee7a8802014-05-11 18:37:21 +010062 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Damien George20006db2014-01-18 14:10:48 +000063
Damien George71c51812014-01-04 20:21:15 +000064 switch (n_args) {
65 case 0:
66 return mp_obj_new_float(0);
67
68 case 1:
Damien Georgeee7a8802014-05-11 18:37:21 +010069 default:
Damien George20773972014-02-22 18:12:43 +000070 if (MP_OBJ_IS_STR(args[0])) {
71 // a string, parse it
Damien Georged182b982014-08-30 14:19:41 +010072 mp_uint_t l;
Damien George20773972014-02-22 18:12:43 +000073 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George7d414a12015-02-08 01:57:40 +000074 return mp_parse_num_decimal(s, l, false, false, NULL);
Damien George0c36da02014-03-08 15:24:39 +000075 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
Damien George6e48f7f2014-03-21 11:45:46 +000076 // a float, just return it
Damien George71c51812014-01-04 20:21:15 +000077 return args[0];
78 } else {
Damien George6e48f7f2014-03-21 11:45:46 +000079 // something else, try to cast it to a float
Damien George71c51812014-01-04 20:21:15 +000080 return mp_obj_new_float(mp_obj_get_float(args[0]));
81 }
Damien George71c51812014-01-04 20:21:15 +000082 }
83}
84
Damien Georgeecc88e92014-08-30 00:35:11 +010085STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000086 mp_obj_float_t *o = o_in;
87 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010088 case MP_UNARY_OP_BOOL: return MP_BOOL(o->value != 0);
89 case MP_UNARY_OP_POSITIVE: return o_in;
90 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
Damien George6ac5dce2014-05-21 19:42:43 +010091 default: return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +000092 }
93}
94
Damien Georgeecc88e92014-08-30 00:35:11 +010095STATIC mp_obj_t float_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee2e3d112014-01-06 22:13:00 +000096 mp_obj_float_t *lhs = lhs_in;
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +030097#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +000098 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
Damien Georgee2e3d112014-01-06 22:13:00 +000099 return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300100 } else
101#endif
102 {
Damien Georgee2e3d112014-01-06 22:13:00 +0000103 return mp_obj_float_binary_op(op, lhs->value, rhs_in);
Damiend99b0522013-12-21 18:17:45 +0000104 }
Damiend99b0522013-12-21 18:17:45 +0000105}
106
Damien George0c36da02014-03-08 15:24:39 +0000107const mp_obj_type_t mp_type_float = {
Damien Georgec5966122014-02-15 16:10:44 +0000108 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000109 .name = MP_QSTR_float,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200110 .print = float_print,
111 .make_new = float_make_new,
112 .unary_op = float_unary_op,
113 .binary_op = float_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000114};
115
116mp_obj_t mp_obj_new_float(mp_float_t value) {
117 mp_obj_float_t *o = m_new(mp_obj_float_t, 1);
Damien George0c36da02014-03-08 15:24:39 +0000118 o->base.type = &mp_type_float;
Damiend99b0522013-12-21 18:17:45 +0000119 o->value = value;
120 return (mp_obj_t)o;
121}
122
123mp_float_t mp_obj_float_get(mp_obj_t self_in) {
Damien George0c36da02014-03-08 15:24:39 +0000124 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_float));
Damiend99b0522013-12-21 18:17:45 +0000125 mp_obj_float_t *self = self_in;
126 return self->value;
127}
128
Damien Georgec5029bc2015-06-13 22:00:10 +0100129STATIC void mp_obj_float_divmod(mp_float_t *x, mp_float_t *y) {
130 // logic here follows that of CPython
131 // https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations
132 // x == (x//y)*y + (x%y)
133 // divmod(x, y) == (x//y, x%y)
134 mp_float_t mod = MICROPY_FLOAT_C_FUN(fmod)(*x, *y);
135 mp_float_t div = (*x - mod) / *y;
136
137 // Python specs require that mod has same sign as second operand
138 if (mod == 0.0) {
139 mod = MICROPY_FLOAT_C_FUN(copysign)(0.0, *y);
140 } else {
141 if ((mod < 0.0) != (*y < 0.0)) {
142 mod += *y;
143 div -= 1.0;
144 }
145 }
146
147 mp_float_t floordiv;
148 if (div == 0.0) {
149 // if division is zero, take the correct sign of zero
150 floordiv = MICROPY_FLOAT_C_FUN(copysign)(0.0, *x / *y);
151 } else {
152 // Python specs require that x == (x//y)*y + (x%y)
153 floordiv = MICROPY_FLOAT_C_FUN(floor)(div);
154 if (div - floordiv > 0.5) {
155 floordiv += 1.0;
156 }
157 }
158
159 // return results
160 *x = floordiv;
161 *y = mod;
162}
163
Damien Georgeecc88e92014-08-30 00:35:11 +0100164mp_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 +0000165 mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
166 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100167 case MP_BINARY_OP_ADD:
168 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
169 case MP_BINARY_OP_SUBTRACT:
170 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
171 case MP_BINARY_OP_MULTIPLY:
172 case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
Damien Georged17926d2014-03-30 13:35:08 +0100173 case MP_BINARY_OP_FLOOR_DIVIDE:
Paul Sokolovsky96ed2132014-03-31 02:18:09 +0300174 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien George9dcc60d2014-04-13 17:45:30 +0100175 if (rhs_val == 0) {
176 zero_division_error:
Damien George8594ce22014-09-13 18:43:09 +0100177 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000178 }
Damien George8594ce22014-09-13 18:43:09 +0100179 // Python specs require that x == (x//y)*y + (x%y) so we must
180 // call divmod to compute the correct floor division, which
181 // returns the floor divide in lhs_val.
182 mp_obj_float_divmod(&lhs_val, &rhs_val);
Damien George9dcc60d2014-04-13 17:45:30 +0100183 break;
184 case MP_BINARY_OP_TRUE_DIVIDE:
185 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
186 if (rhs_val == 0) {
187 goto zero_division_error;
188 }
189 lhs_val /= rhs_val;
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000190 break;
Damien George8594ce22014-09-13 18:43:09 +0100191 case MP_BINARY_OP_MODULO:
192 case MP_BINARY_OP_INPLACE_MODULO:
193 if (rhs_val == 0) {
194 goto zero_division_error;
195 }
196 lhs_val = MICROPY_FLOAT_C_FUN(fmod)(lhs_val, rhs_val);
197 // Python specs require that mod has same sign as second operand
198 if (lhs_val == 0.0) {
199 lhs_val = MICROPY_FLOAT_C_FUN(copysign)(0.0, rhs_val);
200 } else {
201 if ((lhs_val < 0.0) != (rhs_val < 0.0)) {
202 lhs_val += rhs_val;
203 }
204 }
205 break;
Damien Georgeb23fbb32014-04-02 12:26:49 +0100206 case MP_BINARY_OP_POWER:
207 case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break;
Damien Georgec5029bc2015-06-13 22:00:10 +0100208 case MP_BINARY_OP_DIVMOD: {
209 if (rhs_val == 0) {
210 goto zero_division_error;
211 }
212 mp_obj_float_divmod(&lhs_val, &rhs_val);
213 mp_obj_t tuple[2] = {
214 mp_obj_new_float(lhs_val),
215 mp_obj_new_float(rhs_val),
216 };
217 return mp_obj_new_tuple(2, tuple);
218 }
Damien Georged17926d2014-03-30 13:35:08 +0100219 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
220 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
Damien Georgeb8a053a2014-04-11 10:10:37 +0100221 case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_val == rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100222 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);
223 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);
John R. Lentonb8698fc2014-01-11 00:58:59 +0000224
Paul Sokolovskya8e60c12014-03-31 01:38:25 +0300225 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100226 return MP_OBJ_NULL; // op not supported
Damien Georgee2e3d112014-01-06 22:13:00 +0000227 }
228 return mp_obj_new_float(lhs_val);
229}
230
Damien Georgefb510b32014-06-01 13:32:54 +0100231#endif // MICROPY_PY_BUILTINS_FLOAT