py/objfloat: Fix abs(-0.0) so it returns 0.0.
Nan and inf (signed and unsigned) are also handled correctly by using signbit (they were also handled correctly with "val<0", but that didn't handle -0.0 correctly). A test case is added for this behaviour.
This commit is contained in:
parent
8960a28238
commit
b3eadf3f3d
|
@ -161,8 +161,7 @@ STATIC mp_obj_t float_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
|
|||
case MP_UNARY_OP_POSITIVE: return o_in;
|
||||
case MP_UNARY_OP_NEGATIVE: return mp_obj_new_float(-val);
|
||||
case MP_UNARY_OP_ABS: {
|
||||
// TODO check for NaN etc
|
||||
if (val < 0) {
|
||||
if (signbit(val)) {
|
||||
return mp_obj_new_float(-val);
|
||||
} else {
|
||||
return o_in;
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
# test builtin abs function with float args
|
||||
|
||||
for val in (
|
||||
'1.0',
|
||||
'-1.0',
|
||||
'0.0',
|
||||
'-0.0',
|
||||
'nan',
|
||||
'-nan',
|
||||
'inf',
|
||||
'-inf',
|
||||
):
|
||||
print(val, abs(float(val)))
|
Loading…
Reference in New Issue