objfloat.c 6.03 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
#include "runtime0.h"
40
#include "runtime.h"
41

42
#if MICROPY_PY_BUILTINS_FLOAT
43

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

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

69
STATIC 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) {
70
    mp_arg_check_num(n_args, n_kw, 0, 1, false);
71

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

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

93
STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) {
94
95
    mp_obj_float_t *o = o_in;
    switch (op) {
Damien George's avatar
Damien George committed
96
97
98
        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);
99
        default: return MP_OBJ_NULL; // op not supported
100
101
102
    }
}

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

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

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

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

137
mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs_in) {
138
139
    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
140
141
142
143
144
145
        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;
146
        // TODO: verify that C floor matches Python semantics
Damien George's avatar
Damien George committed
147
        case MP_BINARY_OP_FLOOR_DIVIDE:
148
        case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
149
150
151
152
            if (rhs_val == 0) {
                zero_division_error:
                nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
            }
153
            lhs_val = MICROPY_FLOAT_C_FUN(floor)(lhs_val / rhs_val);
154
            break;
Damien George's avatar
Damien George committed
155
        case MP_BINARY_OP_TRUE_DIVIDE:
156
157
158
        case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
            if (rhs_val == 0) {
                goto zero_division_error;
159
            }
160
            lhs_val /= rhs_val;
161
            break;
162
163
        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
164
165
        case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
        case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
166
        case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_val == rhs_val);
Damien George's avatar
Damien George committed
167
168
        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
169

170
        default:
171
            return MP_OBJ_NULL; // op not supported
172
173
174
175
    }
    return mp_obj_new_float(lhs_val);
}

176
#endif // MICROPY_PY_BUILTINS_FLOAT