Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
micropython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
micropython
Commits
aedb8591
Commit
aedb8591
authored
Oct 17, 2015
by
Damien George
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
py: Make float representation configurable with object representation.
parent
7e359c64
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
16 deletions
+34
-16
py/obj.c
py/obj.c
+4
-0
py/obj.h
py/obj.h
+22
-8
py/objfloat.c
py/objfloat.c
+8
-8
No files found.
py/obj.c
View file @
aedb8591
...
...
@@ -44,6 +44,10 @@ mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) {
return
(
mp_obj_t
)
&
mp_type_int
;
}
else
if
(
MP_OBJ_IS_QSTR
(
o_in
))
{
return
(
mp_obj_t
)
&
mp_type_str
;
#if MICROPY_PY_BUILTINS_FLOAT
}
else
if
(
mp_obj_is_float
(
o_in
))
{
return
(
mp_obj_t
)
&
mp_type_float
;
#endif
}
else
{
const
mp_obj_base_t
*
o
=
o_in
;
return
(
mp_obj_t
)
o
->
type
;
...
...
py/obj.h
View file @
aedb8591
...
...
@@ -83,6 +83,17 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))
#if MICROPY_PY_BUILTINS_FLOAT
#define mp_const_float_e ((mp_obj_t)&mp_const_float_e_obj)
#define mp_const_float_pi ((mp_obj_t)&mp_const_float_pi_obj)
extern
const
struct
_mp_obj_float_t
mp_const_float_e_obj
;
extern
const
struct
_mp_obj_float_t
mp_const_float_pi_obj
;
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
mp_float_t
mp_obj_float_get
(
mp_obj_t
self_in
);
mp_obj_t
mp_obj_new_float
(
mp_float_t
value
);
#endif
static
inline
bool
MP_OBJ_IS_OBJ
(
mp_const_obj_t
o
)
{
return
((((
mp_int_t
)(
o
))
&
3
)
==
0
);
}
...
...
@@ -98,6 +109,17 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 3))
#if MICROPY_PY_BUILTINS_FLOAT
#define mp_const_float_e ((mp_obj_t)&mp_const_float_e_obj)
#define mp_const_float_pi ((mp_obj_t)&mp_const_float_pi_obj)
extern
const
struct
_mp_obj_float_t
mp_const_float_e_obj
;
extern
const
struct
_mp_obj_float_t
mp_const_float_pi_obj
;
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
mp_float_t
mp_obj_float_get
(
mp_obj_t
self_in
);
mp_obj_t
mp_obj_new_float
(
mp_float_t
value
);
#endif
static
inline
bool
MP_OBJ_IS_OBJ
(
mp_const_obj_t
o
)
{
return
((((
mp_int_t
)(
o
))
&
1
)
==
0
);
}
...
...
@@ -473,7 +495,6 @@ mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items);
mp_obj_t
mp_obj_new_bytearray_by_ref
(
mp_uint_t
n
,
void
*
items
);
#if MICROPY_PY_BUILTINS_FLOAT
mp_obj_t
mp_obj_new_int_from_float
(
mp_float_t
val
);
mp_obj_t
mp_obj_new_float
(
mp_float_t
val
);
mp_obj_t
mp_obj_new_complex
(
mp_float_t
real
,
mp_float_t
imag
);
#endif
mp_obj_t
mp_obj_new_exception
(
const
mp_obj_type_t
*
exc_type
);
...
...
@@ -564,13 +585,6 @@ void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, mp_uint_
#if MICROPY_PY_BUILTINS_FLOAT
// float
#define mp_const_float_e ((mp_obj_t)&mp_const_float_e_obj)
#define mp_const_float_pi ((mp_obj_t)&mp_const_float_pi_obj)
extern
const
struct
_mp_obj_float_t
mp_const_float_e_obj
;
extern
const
struct
_mp_obj_float_t
mp_const_float_pi_obj
;
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
mp_float_t
mp_obj_float_get
(
mp_obj_t
self_in
);
mp_obj_t
mp_obj_float_binary_op
(
mp_uint_t
op
,
mp_float_t
lhs_val
,
mp_obj_t
rhs
);
// can return MP_OBJ_NULL if op not supported
// complex
...
...
py/objfloat.c
View file @
aedb8591
...
...
@@ -49,7 +49,7 @@ const mp_obj_float_t mp_const_float_pi_obj = {{&mp_type_float}, M_PI};
STATIC
void
float_print
(
const
mp_print_t
*
print
,
mp_obj_t
o_in
,
mp_print_kind_t
kind
)
{
(
void
)
kind
;
mp_
obj_float_t
*
o
=
o_in
;
mp_
float_t
o_val
=
mp_obj_float_get
(
o_in
)
;
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
char
buf
[
16
];
const
int
precision
=
7
;
...
...
@@ -57,7 +57,7 @@ STATIC void float_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
char
buf
[
32
];
const
int
precision
=
16
;
#endif
mp_format_float
(
o
->
value
,
buf
,
sizeof
(
buf
),
'g'
,
precision
,
'\0'
);
mp_format_float
(
o
_val
,
buf
,
sizeof
(
buf
),
'g'
,
precision
,
'\0'
);
mp_print_str
(
print
,
buf
);
if
(
strchr
(
buf
,
'.'
)
==
NULL
&&
strchr
(
buf
,
'e'
)
==
NULL
&&
strchr
(
buf
,
'n'
)
==
NULL
)
{
// Python floats always have decimal point (unless inf or nan)
...
...
@@ -91,24 +91,24 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
}
STATIC
mp_obj_t
float_unary_op
(
mp_uint_t
op
,
mp_obj_t
o_in
)
{
mp_
obj_float_t
*
o
=
o_in
;
mp_
float_t
val
=
mp_obj_float_get
(
o_in
)
;
switch
(
op
)
{
case
MP_UNARY_OP_BOOL
:
return
mp_obj_new_bool
(
o
->
value
!=
0
);
case
MP_UNARY_OP_BOOL
:
return
mp_obj_new_bool
(
val
!=
0
);
case
MP_UNARY_OP_POSITIVE
:
return
o_in
;
case
MP_UNARY_OP_NEGATIVE
:
return
mp_obj_new_float
(
-
o
->
value
);
case
MP_UNARY_OP_NEGATIVE
:
return
mp_obj_new_float
(
-
val
);
default:
return
MP_OBJ_NULL
;
// op not supported
}
}
STATIC
mp_obj_t
float_binary_op
(
mp_uint_t
op
,
mp_obj_t
lhs_in
,
mp_obj_t
rhs_in
)
{
mp_
obj_float_t
*
lhs
=
lhs_in
;
mp_
float_t
lhs_val
=
mp_obj_float_get
(
lhs_in
)
;
#if MICROPY_PY_BUILTINS_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
_val
,
0
,
rhs_in
);
}
else
#endif
{
return
mp_obj_float_binary_op
(
op
,
lhs
->
value
,
rhs_in
);
return
mp_obj_float_binary_op
(
op
,
lhs
_val
,
rhs_in
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment