blob: 3f4a6809399700b5f87b336824e249b56aeea66b [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
Paul Sokolovsky864038d2014-03-31 02:12:40 +03002#include <stdio.h>
3#include <string.h>
Damiend99b0522013-12-21 18:17:45 +00004#include <assert.h>
Rachel Dowdall300c8bd2014-03-20 22:40:38 +00005#include <math.h>
Damiend99b0522013-12-21 18:17:45 +00006
7#include "nlr.h"
8#include "misc.h"
9#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000012#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
14
15#if MICROPY_ENABLE_FLOAT
Damien George8bfec2b2014-03-10 13:27:02 +000016
Dave Hylandsca5a2412014-03-10 00:10:01 -070017#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
Damien George8bfec2b2014-03-10 13:27:02 +000018#include "formatfloat.h"
Dave Hylandsca5a2412014-03-10 00:10:01 -070019#endif
Damiend99b0522013-12-21 18:17:45 +000020
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020021STATIC 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 +000022 mp_obj_float_t *o = o_in;
Dave Hylandsca5a2412014-03-10 00:10:01 -070023#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
24 char buf[32];
25 format_float(o->value, buf, sizeof(buf), 'g', 6, '\0');
26 print(env, "%s", buf);
Damien George60be1cf2014-04-05 20:51:29 +010027 if (strchr(buf, '.') == NULL) {
28 // Python floats always have decimal point
29 print(env, ".0");
30 }
Dave Hylandsca5a2412014-03-10 00:10:01 -070031#else
Paul Sokolovsky864038d2014-03-31 02:12:40 +030032 char buf[32];
33 sprintf(buf, "%.8g", (double) o->value);
34 print(env, buf);
35 if (strchr(buf, '.') == NULL) {
36 // Python floats always have decimal point
37 print(env, ".0");
38 }
Dave Hylandsca5a2412014-03-10 00:10:01 -070039#endif
Damiend99b0522013-12-21 18:17:45 +000040}
41
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020042STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000043 // TODO check n_kw == 0
44
Damien George71c51812014-01-04 20:21:15 +000045 switch (n_args) {
46 case 0:
47 return mp_obj_new_float(0);
48
49 case 1:
Damien George20773972014-02-22 18:12:43 +000050 if (MP_OBJ_IS_STR(args[0])) {
51 // a string, parse it
52 uint l;
53 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George6e48f7f2014-03-21 11:45:46 +000054 return mp_parse_num_decimal(s, l, false, false);
Damien George0c36da02014-03-08 15:24:39 +000055 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
Damien George6e48f7f2014-03-21 11:45:46 +000056 // a float, just return it
Damien George71c51812014-01-04 20:21:15 +000057 return args[0];
58 } else {
Damien George6e48f7f2014-03-21 11:45:46 +000059 // something else, try to cast it to a float
Damien George71c51812014-01-04 20:21:15 +000060 return mp_obj_new_float(mp_obj_get_float(args[0]));
61 }
62
63 default:
Damien Georgeea13f402014-04-05 18:32:08 +010064 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "float takes at most 1 argument, %d given", n_args));
Damien George71c51812014-01-04 20:21:15 +000065 }
66}
67
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020068STATIC mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000069 mp_obj_float_t *o = o_in;
70 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010071 case MP_UNARY_OP_BOOL: return MP_BOOL(o->value != 0);
72 case MP_UNARY_OP_POSITIVE: return o_in;
73 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
Damiend99b0522013-12-21 18:17:45 +000074 default: return NULL; // op not supported
75 }
76}
77
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020078STATIC 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 +000079 mp_obj_float_t *lhs = lhs_in;
Damien George0c36da02014-03-08 15:24:39 +000080 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
Damien Georgee2e3d112014-01-06 22:13:00 +000081 return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
82 } else {
83 return mp_obj_float_binary_op(op, lhs->value, rhs_in);
Damiend99b0522013-12-21 18:17:45 +000084 }
Damiend99b0522013-12-21 18:17:45 +000085}
86
Damien George0c36da02014-03-08 15:24:39 +000087const mp_obj_type_t mp_type_float = {
Damien Georgec5966122014-02-15 16:10:44 +000088 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000089 .name = MP_QSTR_float,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020090 .print = float_print,
91 .make_new = float_make_new,
92 .unary_op = float_unary_op,
93 .binary_op = float_binary_op,
Damiend99b0522013-12-21 18:17:45 +000094};
95
96mp_obj_t mp_obj_new_float(mp_float_t value) {
97 mp_obj_float_t *o = m_new(mp_obj_float_t, 1);
Damien George0c36da02014-03-08 15:24:39 +000098 o->base.type = &mp_type_float;
Damiend99b0522013-12-21 18:17:45 +000099 o->value = value;
100 return (mp_obj_t)o;
101}
102
103mp_float_t mp_obj_float_get(mp_obj_t self_in) {
Damien George0c36da02014-03-08 15:24:39 +0000104 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_float));
Damiend99b0522013-12-21 18:17:45 +0000105 mp_obj_float_t *self = self_in;
106 return self->value;
107}
108
Damien Georgee2e3d112014-01-06 22:13:00 +0000109mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
110 mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
111 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100112 case MP_BINARY_OP_ADD:
113 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
114 case MP_BINARY_OP_SUBTRACT:
115 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
116 case MP_BINARY_OP_MULTIPLY:
117 case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
Paul Sokolovsky96ed2132014-03-31 02:18:09 +0300118 // TODO: verify that C floor matches Python semantics
Damien Georged17926d2014-03-30 13:35:08 +0100119 case MP_BINARY_OP_FLOOR_DIVIDE:
Paul Sokolovsky96ed2132014-03-31 02:18:09 +0300120 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
121 lhs_val = MICROPY_FLOAT_C_FUN(floor)(lhs_val / rhs_val);
122 goto check_zero_division;
Damien Georged17926d2014-03-30 13:35:08 +0100123 case MP_BINARY_OP_TRUE_DIVIDE:
124 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000125 lhs_val /= rhs_val;
Paul Sokolovsky96ed2132014-03-31 02:18:09 +0300126check_zero_division:
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000127 if (isinf(lhs_val)){ // check for division by zero
Damien Georgeea13f402014-04-05 18:32:08 +0100128 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
Rachel Dowdall300c8bd2014-03-20 22:40:38 +0000129 }
130 break;
Damien Georgeb23fbb32014-04-02 12:26:49 +0100131 case MP_BINARY_OP_POWER:
132 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 +0100133 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
134 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
Damien Georgeb8a053a2014-04-11 10:10:37 +0100135 case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_val == rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100136 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);
137 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);
John R. Lentonb8698fc2014-01-11 00:58:59 +0000138
Paul Sokolovskya8e60c12014-03-31 01:38:25 +0300139 default:
Damien Georgeae491052014-04-10 20:08:11 +0100140 return MP_OBJ_NULL; // op not supported
Damien Georgee2e3d112014-01-06 22:13:00 +0000141 }
142 return mp_obj_new_float(lhs_val);
143}
144
Damien George8bfec2b2014-03-10 13:27:02 +0000145#endif // MICROPY_ENABLE_FLOAT