blob: bb44ac95499b7550ec6ccaadaa3f8d35265c58cf [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
2#include <stdint.h>
3#include <string.h>
4#include <assert.h>
5
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
Damien George71c51812014-01-04 20:21:15 +00009#include "mpqstr.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "obj.h"
11#include "runtime0.h"
12
13#if MICROPY_ENABLE_FLOAT
14
15typedef struct _mp_obj_float_t {
16 mp_obj_base_t base;
17 mp_float_t value;
18} mp_obj_float_t;
19
20mp_obj_t mp_obj_new_float(mp_float_t value);
21
Damien George71c51812014-01-04 20:21:15 +000022static void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000023 mp_obj_float_t *o = o_in;
24 print(env, "%.8g", o->value);
25}
26
Damien George93a9b5b2014-01-08 18:48:12 +000027// args are reverse in the array
Damien George71c51812014-01-04 20:21:15 +000028static mp_obj_t float_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
29 switch (n_args) {
30 case 0:
31 return mp_obj_new_float(0);
32
33 case 1:
34 // TODO allow string as arg and parse it
35 if (MP_OBJ_IS_TYPE(args[0], &float_type)) {
36 return args[0];
37 } else {
38 return mp_obj_new_float(mp_obj_get_float(args[0]));
39 }
40
41 default:
42 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "float takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
43 }
44}
45
46static mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000047 mp_obj_float_t *o = o_in;
48 switch (op) {
49 case RT_UNARY_OP_NOT: if (o->value != 0) { return mp_const_true;} else { return mp_const_false; }
50 case RT_UNARY_OP_POSITIVE: return o_in;
51 case RT_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
52 default: return NULL; // op not supported
53 }
54}
55
Damien George71c51812014-01-04 20:21:15 +000056static 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 +000057 mp_obj_float_t *lhs = lhs_in;
Damiend99b0522013-12-21 18:17:45 +000058 if (MP_OBJ_IS_TYPE(rhs_in, &complex_type)) {
Damien Georgee2e3d112014-01-06 22:13:00 +000059 return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
60 } else {
61 return mp_obj_float_binary_op(op, lhs->value, rhs_in);
Damiend99b0522013-12-21 18:17:45 +000062 }
Damiend99b0522013-12-21 18:17:45 +000063}
64
65const mp_obj_type_t float_type = {
66 { &mp_const_type },
67 "float",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +020068 .print = float_print,
69 .make_new = float_make_new,
70 .unary_op = float_unary_op,
71 .binary_op = float_binary_op,
Damiend99b0522013-12-21 18:17:45 +000072};
73
74mp_obj_t mp_obj_new_float(mp_float_t value) {
75 mp_obj_float_t *o = m_new(mp_obj_float_t, 1);
76 o->base.type = &float_type;
77 o->value = value;
78 return (mp_obj_t)o;
79}
80
81mp_float_t mp_obj_float_get(mp_obj_t self_in) {
82 assert(MP_OBJ_IS_TYPE(self_in, &float_type));
83 mp_obj_float_t *self = self_in;
84 return self->value;
85}
86
Damien Georgee2e3d112014-01-06 22:13:00 +000087mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
88 mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
89 switch (op) {
90 case RT_BINARY_OP_ADD:
91 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
92 case RT_BINARY_OP_SUBTRACT:
93 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
94 case RT_BINARY_OP_MULTIPLY:
95 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
96 /* TODO floor(?) the value
97 case RT_BINARY_OP_FLOOR_DIVIDE:
98 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break;
99 */
100 case RT_BINARY_OP_TRUE_DIVIDE:
101 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: lhs_val /= rhs_val; break;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000102
103 case RT_COMPARE_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
104 case RT_COMPARE_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
105 case RT_COMPARE_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);
106 case RT_COMPARE_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);
107
Damien Georgee2e3d112014-01-06 22:13:00 +0000108 return NULL; // op not supported
109 }
110 return mp_obj_new_float(lhs_val);
111}
112
Damiend99b0522013-12-21 18:17:45 +0000113#endif