py: Implement float and complex == and !=.

Addresses issue #462.
This commit is contained in:
Damien George 2014-04-11 10:10:37 +01:00
parent 686afc5c0a
commit b8a053aeb1
3 changed files with 20 additions and 29 deletions

View File

@ -149,31 +149,20 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) { if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
return false; return false;
} else { } else {
if (MP_OBJ_IS_SMALL_INT(o2)) { if (MP_OBJ_IS_SMALL_INT(o1)) {
mp_obj_t temp = o1; o1 = o2; o2 = temp; mp_obj_t temp = o2; o2 = o1; o1 = temp;
} }
// o1 is the SMALL_INT, o2 is not // o2 is the SMALL_INT, o1 is not
mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(o1); // fall through to generic op
if (o2 == mp_const_false) {
return val == 0;
} else if (o2 == mp_const_true) {
return val == 1;
} else if (MP_OBJ_IS_TYPE(o2, &mp_type_int)) {
// If o2 is long int, dispatch to its virtual methods
mp_obj_base_t *o = o2;
if (o->type->binary_op != NULL) {
mp_obj_t r = o->type->binary_op(MP_BINARY_OP_EQUAL, o2, o1);
return r == mp_const_true ? true : false;
}
}
return false;
} }
} else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) { } else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) {
return mp_obj_str_equal(o1, o2); return mp_obj_str_equal(o1, o2);
} else { }
mp_obj_base_t *o = o1;
if (o->type->binary_op != NULL) { // generic type, call binary_op(MP_BINARY_OP_EQUAL)
mp_obj_t r = o->type->binary_op(MP_BINARY_OP_EQUAL, o1, o2); mp_obj_type_t *type = mp_obj_get_type(o1);
if (type->binary_op != NULL) {
mp_obj_t r = type->binary_op(MP_BINARY_OP_EQUAL, o1, o2);
if (r != MP_OBJ_NULL) { if (r != MP_OBJ_NULL) {
return r == mp_const_true ? true : false; return r == mp_const_true ? true : false;
} }
@ -183,7 +172,6 @@ bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
"Equality for '%s' and '%s' types not yet implemented", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2))); "Equality for '%s' and '%s' types not yet implemented", mp_obj_get_type_str(o1), mp_obj_get_type_str(o2)));
return false; return false;
} }
}
machine_int_t mp_obj_get_int(mp_obj_t arg) { machine_int_t mp_obj_get_int(mp_obj_t arg) {
// This function essentially performs implicit type conversion to int // This function essentially performs implicit type conversion to int

View File

@ -205,6 +205,8 @@ mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_im
break; break;
} }
case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_real == rhs_real && lhs_imag == rhs_imag);
default: default:
return MP_OBJ_NULL; // op not supported return MP_OBJ_NULL; // op not supported
} }

View File

@ -132,6 +132,7 @@ check_zero_division:
case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break; case MP_BINARY_OP_INPLACE_POWER: lhs_val = MICROPY_FLOAT_C_FUN(pow)(lhs_val, rhs_val); break;
case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val);
case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val);
case MP_BINARY_OP_EQUAL: return MP_BOOL(lhs_val == rhs_val);
case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); 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); case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val);