summaryrefslogtreecommitdiff
path: root/py/objfloat.c
diff options
context:
space:
mode:
authorJohn R. Lenton <jlenton@gmail.com>2014-01-05 22:29:03 +0000
committerJohn R. Lenton <jlenton@gmail.com>2014-01-05 22:29:03 +0000
commitba3f87c94776538fece5e87ff1d7de547930397a (patch)
tree3112b328cff278168a1ef48fc37a65778ea9324a /py/objfloat.c
parent6e1e98f8648d327098a03ce8d175c9854dd06cc8 (diff)
parent12e2656472bf53e467c066eda6f3e177a97210ca (diff)
Merge remote-tracking branch 'upstream/master' into list_reverse
Diffstat (limited to 'py/objfloat.c')
-rw-r--r--py/objfloat.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/py/objfloat.c b/py/objfloat.c
index f151fe25a..8fc925e0b 100644
--- a/py/objfloat.c
+++ b/py/objfloat.c
@@ -6,6 +6,7 @@
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
+#include "mpqstr.h"
#include "obj.h"
#include "runtime0.h"
@@ -18,12 +19,30 @@ typedef struct _mp_obj_float_t {
mp_obj_t mp_obj_new_float(mp_float_t value);
-void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
+static void float_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
mp_obj_float_t *o = o_in;
print(env, "%.8g", o->value);
}
-mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
+static mp_obj_t float_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
+ switch (n_args) {
+ case 0:
+ return mp_obj_new_float(0);
+
+ case 1:
+ // TODO allow string as arg and parse it
+ if (MP_OBJ_IS_TYPE(args[0], &float_type)) {
+ return args[0];
+ } else {
+ return mp_obj_new_float(mp_obj_get_float(args[0]));
+ }
+
+ default:
+ 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));
+ }
+}
+
+static mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
mp_obj_float_t *o = o_in;
switch (op) {
case RT_UNARY_OP_NOT: if (o->value != 0) { return mp_const_true;} else { return mp_const_false; }
@@ -33,7 +52,7 @@ mp_obj_t float_unary_op(int op, mp_obj_t o_in) {
}
}
-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) {
if (MP_OBJ_IS_TYPE(rhs_in, &complex_type)) {
return complex_type.binary_op(op, lhs_in, rhs_in);
}
@@ -61,6 +80,7 @@ const mp_obj_type_t float_type = {
{ &mp_const_type },
"float",
float_print,
+ float_make_new, // make_new
NULL, // call_n
float_unary_op,
float_binary_op,