py: Separate MICROPY_PY_BUILTINS_COMPLEX from MICROPY_PY_BUILTINS_FLOAT.

One thing is wanting to do 1 / 2 and get something else but 0, and quite
another - doing rocket science ;-).
This commit is contained in:
Paul Sokolovsky 2014-06-20 01:48:35 +03:00
parent 7efbd325bb
commit 3b6f7b95eb
9 changed files with 24 additions and 3 deletions

View File

@ -113,10 +113,12 @@ mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
} else { } else {
return o_in; return o_in;
} }
#if MICROPY_PY_BUILTINS_COMPLEX
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) { } else if (MP_OBJ_IS_TYPE(o_in, &mp_type_complex)) {
mp_float_t real, imag; mp_float_t real, imag;
mp_obj_complex_get(o_in, &real, &imag); mp_obj_complex_get(o_in, &real, &imag);
return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)); return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag));
#endif
#endif #endif
} else { } else {
assert(0); assert(0);

View File

@ -44,7 +44,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool }, { MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool },
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes }, { MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes },
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray }, { MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray },
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_COMPLEX
{ MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex }, { MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex },
#endif #endif
{ MP_OBJ_NEW_QSTR(MP_QSTR_dict), (mp_obj_t)&mp_type_dict }, { MP_OBJ_NEW_QSTR(MP_QSTR_dict), (mp_obj_t)&mp_type_dict },

View File

@ -223,6 +223,10 @@ typedef double mp_float_t;
#define MICROPY_PY_BUILTINS_FLOAT (0) #define MICROPY_PY_BUILTINS_FLOAT (0)
#endif #endif
#ifndef MICROPY_PY_BUILTINS_COMPLEX
#define MICROPY_PY_BUILTINS_COMPLEX (MICROPY_PY_BUILTINS_FLOAT)
#endif
// Enable features which improve CPython compatibility // Enable features which improve CPython compatibility
// but may lead to more code size/memory usage. // but may lead to more code size/memory usage.
// TODO: Originally intended as generic category to not // TODO: Originally intended as generic category to not

View File

@ -274,6 +274,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
} }
} }
#if MICROPY_PY_BUILTINS_COMPLEX
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) { void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
if (arg == mp_const_false) { if (arg == mp_const_false) {
*real = 0; *real = 0;
@ -297,6 +298,7 @@ void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
} }
} }
#endif #endif
#endif
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) { void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items) {
if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) { if (MP_OBJ_IS_TYPE(o, &mp_type_tuple)) {

View File

@ -36,7 +36,7 @@
#include "runtime0.h" #include "runtime0.h"
#include "runtime.h" #include "runtime.h"
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_COMPLEX
#include <math.h> #include <math.h>

View File

@ -102,9 +102,12 @@ STATIC mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
STATIC mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { STATIC mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
mp_obj_float_t *lhs = lhs_in; mp_obj_float_t *lhs = lhs_in;
#if MICROPY_PY_BUILTINS_COMPLEX
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) { if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in); return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in);
} else { } else
#endif
{
return mp_obj_float_binary_op(op, lhs->value, rhs_in); return mp_obj_float_binary_op(op, lhs->value, rhs_in);
} }
} }

View File

@ -121,8 +121,10 @@ mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) { } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in); return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
#if MICROPY_PY_BUILTINS_COMPLEX
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) { } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in); return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
#endif
#endif #endif
} else { } else {
// delegate to generic function to check for extra cases // delegate to generic function to check for extra cases

View File

@ -35,6 +35,7 @@
#include "parsenumbase.h" #include "parsenumbase.h"
#include "parsenum.h" #include "parsenum.h"
#include "smallint.h" #include "smallint.h"
#include "runtime.h"
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
#include <math.h> #include <math.h>
@ -252,10 +253,15 @@ mp_obj_t mp_parse_num_decimal(const char *str, uint len, bool allow_imag, bool f
} }
// return the object // return the object
#if MICROPY_PY_BUILTINS_COMPLEX
if (imag) { if (imag) {
return mp_obj_new_complex(0, dec_val); return mp_obj_new_complex(0, dec_val);
} else if (force_complex) { } else if (force_complex) {
return mp_obj_new_complex(dec_val, 0); return mp_obj_new_complex(dec_val, 0);
#else
if (imag || force_complex) {
mp_not_implemented("complex values not supported");
#endif
} else { } else {
return mp_obj_new_float(dec_val); return mp_obj_new_float(dec_val);
} }

View File

@ -426,6 +426,7 @@ mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
} else { } else {
return res; return res;
} }
#if MICROPY_PY_BUILTINS_COMPLEX
} else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) { } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
mp_obj_t res = mp_obj_complex_binary_op(op, lhs_val, 0, rhs); mp_obj_t res = mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
if (res == MP_OBJ_NULL) { if (res == MP_OBJ_NULL) {
@ -433,6 +434,7 @@ mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
} else { } else {
return res; return res;
} }
#endif
#endif #endif
} }
} }