blob: 6732f31215f53122320c9b683d89b41f35b733d4 [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>
Rachel Dowdall300c8bd2014-03-20 22:40:38 +000031#include <math.h>
Damiend99b0522013-12-21 18:17:45 +000032
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030033#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000034#include "nlr.h"
35#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000036#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000037#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000038#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
Damien Georgeee7a8802014-05-11 18:37:21 +010040#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000041
42#if MICROPY_ENABLE_FLOAT
Damien George8bfec2b2014-03-10 13:27:02 +000043
Dave Hylandsca5a2412014-03-10 00:10:01 -070044#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
Damien George8bfec2b2014-03-10 13:27:02 +000045#include "formatfloat.h"
Dave Hylandsca5a2412014-03-10 00:10:01 -070046#endif
Damiend99b0522013-12-21 18:17:45 +000047
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020048STATIC void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000049 mp_obj_float_t *o = o_in;
Dave Hylandsca5a2412014-03-10 00:10:01 -070050#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
51 char buf[32];
52 format_float(o->value, buf, sizeof(buf), 'g', 6, '\0');
53 print(env, "%s", buf);
Damien George60be1cf2014-04-05 20:51:29 +010054 if (strchr(buf, '.') == NULL) {
55 // Python floats always have decimal point
56 print(env, ".0");
57 }
Dave Hylandsca5a2412014-03-10 00:10:01 -070058#else
Paul Sokolovsky864038d2014-03-31 02:12:40 +030059 char buf[32];
Paul Sokolovsky7de53772014-04-18 21:12:52 +030060 sprintf(buf, "%.17g", (double) o->value);
Paul Sokolovsky864038d2014-03-31 02:12:40 +030061 print(env, buf);
62 if (strchr(buf, '.') == NULL) {
63 // Python floats always have decimal point
64 print(env, ".0");
65 }
Dave Hylandsca5a2412014-03-10 00:10:01 -070066#endif
Damiend99b0522013-12-21 18:17:45 +000067}
68
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020069STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georgeee7a8802014-05-11 18:37:21 +010070 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Damien George20006db2014-01-18 14:10:48 +000071
Damien George71c51812014-01-04 20:21:15 +000072 switch (n_args) {
73 case 0:
74 return mp_obj_new_float(0);
75
76 case 1:
Damien Georgeee7a8802014-05-11 18:37:21 +010077 default:
Damien George20773972014-02-22 18:12:43 +000078 if (MP_OBJ_IS_STR(args[0])) {
79 // a string, parse it
80 uint l;
81 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George6e48f7f2014-03-21 11:45:46 +000082 return mp_parse_num_decimal(s, l, false, false);
Damien George0c36da02014-03-08 15:24:39 +000083 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
Damien George6e48f7f2014-03-21 11:45:46 +000084 // a float, just return it
Damien George71c51812014-01-04 20:21:15 +000085 return args[0];
86 } else {
Damien George6e48f7f2014-03-21 11:45:46 +000087 // something else, try to cast it to a float
Damien George71c51812014-01-04 20:21:15 +000088 return mp_obj_new_float(mp_obj_get_float(args[0]));
89 }
Damien George71c51812014-01-04 20:21:15 +000090 }
91}
92
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020093STATIC mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000094 mp_obj_float_t *o = o_in;
95 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010096 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 George6ac5dce2014-05-21 19:42:43 +010099 default: return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000100 }
101}
102
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200103STATIC mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee2e3d112014-01-06 22:13:00 +0000104 mp_obj_float_t *lhs = lhs_in;
Damien George0c36da02014-03-08 15:24:39 +0000105 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
Damien Georgee2e3d112014-01-06 22:13:00 +0000106 return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
107 } else {
108 return mp_obj_float_binary_op(op, lhs->value, rhs_in);
Damiend99b0522013-12-21 18:17:45 +0000109 }
Damiend99b0522013-12-21 18:17:45 +0000110}
111
Damien George0c36da02014-03-08 15:24:39 +0000112const mp_obj_type_t mp_type_float = {
Damien Georgec5966122014-02-15 16:10:44 +0000113 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000114 .name = MP_QSTR_float,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200115 .print = float_print,
116 .make_new = float_make_new,
117 .unary_op = float_unary_op,
118 .binary_op = float_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000119};
120
121mp_obj_t mp_obj_new_float(mp_float_t value) {
122 mp_obj_float_t *o = m_new(mp_obj_float_t, 1);
Damien George0c36da02014-03-08 15:24:39 +0000123 o->base.type = &mp_type_float;
Damiend99b0522013-12-21 18:17:45 +0000124 o->value = value;
125 return (mp_obj_t)o;
126}
127
128mp_float_t mp_obj_float_get(mp_obj_t self_in) {
Damien George0c36da02014-03-08 15:24:39 +0000129 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_float));
Damiend99b0522013-12-21 18:17:45 +0000130 mp_obj_float_t *self = self_in;
131 return self->value;
132}
133
Damien Georgee2e3d112014-01-06 22:13:00 +0000134mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
135 mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
136 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100137 case MP_BINARY_OP_ADD:
138 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
139 case MP_BINARY_OP_SUBTRACT:
140 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
141 case MP_BINARY_OP_MULTIPLY:
142 case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
Paul Sokolovsky96ed2132014-03-31 02:18:09 +0300143 // TODO: verify that C floor matches Python semantics
Damien Georged17926d2014-03-30 13:35:08 +0100144 case MP_BINARY_OP_FLOOR_DIVIDE:
Paul Sokolovsky96ed2132014-03-31 02:18:09 +0300145 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien George9dcc60d2014-04-13 17:45:30 +0100146 if (rhs_val == 0) {
147 zero_division_error:
Damien Georgeea13f402014-04-05 18:32:08 +0100148 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000149 }
Damien George9dcc60d2014-04-13 17:45:30 +0100150 lhs_val = MICROPY_FLOAT_C_FUN(floor)(lhs_val / rhs_val);
151 break;
152 case MP_BINARY_OP_TRUE_DIVIDE:
153 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
154 if (rhs_val == 0) {
155 goto zero_division_error;
156 }
157 lhs_val /= rhs_val;
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000158 break;
Damien Georgeb23fbb32014-04-02 12:26:49 +0100159 case MP_BINARY_OP_POWER:
160 case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break;
Damien Georged17926d2014-03-30 13:35:08 +0100161 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
162 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
Damien Georgeb8a053a2014-04-11 10:10:37 +0100163 case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_val == rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100164 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);
165 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);
John R. Lentonb8698fc2014-01-11 00:58:59 +0000166
Paul Sokolovskya8e60c12014-03-31 01:38:25 +0300167 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100168 return MP_OBJ_NULL; // op not supported
Damien Georgee2e3d112014-01-06 22:13:00 +0000169 }
170 return mp_obj_new_float(lhs_val);
171}
172
Damien George8bfec2b2014-03-10 13:27:02 +0000173#endif // MICROPY_ENABLE_FLOAT