objfloat.c 6.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
#include <stdlib.h>
28
29
#include <stdio.h>
#include <string.h>
30
#include <assert.h>
31
#include <math.h>
32

33
#include "mpconfig.h"
34
35
#include "nlr.h"
#include "misc.h"
36
#include "qstr.h"
37
#include "obj.h"
38
#include "parsenum.h"
39
40
41
#include "runtime0.h"

#if MICROPY_ENABLE_FLOAT
42

43
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
44
#include "formatfloat.h"
45
#endif
46

47
STATIC void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
48
    mp_obj_float_t *o = o_in;
49
50
51
52
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
    char buf[32];
    format_float(o->value, buf, sizeof(buf), 'g', 6, '\0');
    print(env, "%s", buf);
53
54
55
56
    if (strchr(buf, '.') == NULL) {
        // Python floats always have decimal point
        print(env, ".0");
    }
57
#else
58
    char buf[32];
59
    sprintf(buf, "%.17g", (double) o->value);
60
61
62
63
64
    print(env, buf);
    if (strchr(buf, '.') == NULL) {
        // Python floats always have decimal point
        print(env, ".0");
    }
65
#endif
66
67
}

68
STATIC mp_obj_t float_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
69
70
    // TODO check n_kw == 0

71
72
73
74
75
    switch (n_args) {
        case 0:
            return mp_obj_new_float(0);

        case 1:
76
77
78
79
            if (MP_OBJ_IS_STR(args[0])) {
                // a string, parse it
                uint l;
                const char *s = mp_obj_str_get_data(args[0], &l);
80
                return mp_parse_num_decimal(s, l, false, false);
81
            } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
82
                // a float, just return it
83
84
                return args[0];
            } else {
85
                // something else, try to cast it to a float
86
87
88
89
                return mp_obj_new_float(mp_obj_get_float(args[0]));
            }

        default:
90
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "float takes at most 1 argument, %d given", n_args));
91
92
93
    }
}

94
STATIC mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
95
96
    mp_obj_float_t *o = o_in;
    switch (op) {
Damien George's avatar
Damien George committed
97
98
99
        case MP_UNARY_OP_BOOL: return MP_BOOL(o->value != 0);
        case MP_UNARY_OP_POSITIVE: return o_in;
        case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-o->value);
100
101
102
103
        default: return NULL; // op not supported
    }
}

104
STATIC mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
105
    mp_obj_float_t *lhs = lhs_in;
106
    if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
107
108
109
        return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
    } else {
        return mp_obj_float_binary_op(op, lhs->value, rhs_in);
110
111
112
    }
}

113
const mp_obj_type_t mp_type_float = {
114
    { &mp_type_type },
115
    .name = MP_QSTR_float,
116
117
118
119
    .print = float_print,
    .make_new = float_make_new,
    .unary_op = float_unary_op,
    .binary_op = float_binary_op,
120
121
122
123
};

mp_obj_t mp_obj_new_float(mp_float_t value) {
    mp_obj_float_t *o = m_new(mp_obj_float_t, 1);
124
    o->base.type = &mp_type_float;
125
126
127
128
129
    o->value = value;
    return (mp_obj_t)o;
}

mp_float_t mp_obj_float_get(mp_obj_t self_in) {
130
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_float));
131
132
133
134
    mp_obj_float_t *self = self_in;
    return self->value;
}

135
136
137
mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
    mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
    switch (op) {
Damien George's avatar
Damien George committed
138
139
140
141
142
143
        case MP_BINARY_OP_ADD:
        case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
        case MP_BINARY_OP_SUBTRACT:
        case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
        case MP_BINARY_OP_MULTIPLY:
        case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
144
        // TODO: verify that C floor matches Python semantics
Damien George's avatar
Damien George committed
145
        case MP_BINARY_OP_FLOOR_DIVIDE:
146
        case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
147
148
149
150
            if (rhs_val == 0) {
                zero_division_error:
                nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
            }
151
            lhs_val = MICROPY_FLOAT_C_FUN(floor)(lhs_val / rhs_val);
152
            break;
Damien George's avatar
Damien George committed
153
        case MP_BINARY_OP_TRUE_DIVIDE:
154
155
156
        case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
            if (rhs_val == 0) {
                goto zero_division_error;
157
            }
158
            lhs_val /= rhs_val;
159
            break;
160
161
        case MP_BINARY_OP_POWER:
        case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break;
Damien George's avatar
Damien George committed
162
163
        case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
        case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
164
        case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_val == rhs_val);
Damien George's avatar
Damien George committed
165
166
        case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val);
        case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);
John R. Lenton's avatar
John R. Lenton committed
167

168
        default:
169
            return MP_OBJ_NOT_SUPPORTED;
170
171
172
173
    }
    return mp_obj_new_float(lhs_val);
}

174
#endif // MICROPY_ENABLE_FLOAT