mirror of
https://github.com/micropython/micropython.git
synced 2026-01-04 11:10:14 +01:00
py: Change mp_obj_int_is_positive to more general mp_obj_int_sign.
This function returns the sign (-1, 0 or 1) of the integer object.
This commit is contained in:
@@ -217,7 +217,7 @@ int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char
|
|||||||
char prefix_buf[4];
|
char prefix_buf[4];
|
||||||
char *prefix = prefix_buf;
|
char *prefix = prefix_buf;
|
||||||
|
|
||||||
if (mp_obj_int_is_positive(x)) {
|
if (mp_obj_int_sign(x) > 0) {
|
||||||
if (flags & PF_FLAG_SHOW_SIGN) {
|
if (flags & PF_FLAG_SHOW_SIGN) {
|
||||||
*prefix++ = '+';
|
*prefix++ = '+';
|
||||||
} else if (flags & PF_FLAG_SPACE_SIGN) {
|
} else if (flags & PF_FLAG_SPACE_SIGN) {
|
||||||
|
|||||||
11
py/objint.c
11
py/objint.c
@@ -259,8 +259,15 @@ char *mp_obj_int_formatted(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size,
|
|||||||
|
|
||||||
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
|
||||||
|
|
||||||
bool mp_obj_int_is_positive(mp_obj_t self_in) {
|
int mp_obj_int_sign(mp_obj_t self_in) {
|
||||||
return mp_obj_get_int(self_in) >= 0;
|
mp_int_t val = mp_obj_get_int(self_in);
|
||||||
|
if (val < 0) {
|
||||||
|
return -1;
|
||||||
|
} else if (val > 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This must handle int and bool types, and must raise a
|
// This must handle int and bool types, and must raise a
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_
|
|||||||
int base, const char *prefix, char base_char, char comma);
|
int base, const char *prefix, char base_char, char comma);
|
||||||
mp_int_t mp_obj_int_hash(mp_obj_t self_in);
|
mp_int_t mp_obj_int_hash(mp_obj_t self_in);
|
||||||
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len, byte *buf);
|
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len, byte *buf);
|
||||||
bool mp_obj_int_is_positive(mp_obj_t self_in);
|
int mp_obj_int_sign(mp_obj_t self_in);
|
||||||
mp_obj_t mp_obj_int_abs(mp_obj_t self_in);
|
mp_obj_t mp_obj_int_abs(mp_obj_t self_in);
|
||||||
mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in);
|
mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in);
|
||||||
mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in);
|
mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in);
|
||||||
|
|||||||
@@ -71,12 +71,21 @@ void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mp_obj_int_is_positive(mp_obj_t self_in) {
|
int mp_obj_int_sign(mp_obj_t self_in) {
|
||||||
|
mp_longint_impl_t val;
|
||||||
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
||||||
return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
|
val = MP_OBJ_SMALL_INT_VALUE(self_in);
|
||||||
|
} else {
|
||||||
|
mp_obj_int_t *self = self_in;
|
||||||
|
val = self->val;
|
||||||
|
}
|
||||||
|
if (val < 0) {
|
||||||
|
return -1;
|
||||||
|
} else if (val > 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
mp_obj_int_t *self = self_in;
|
|
||||||
return self->val >= 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This must handle int and bool types, and must raise a
|
// This must handle int and bool types, and must raise a
|
||||||
|
|||||||
@@ -113,12 +113,25 @@ void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, mp_uint_t len,
|
|||||||
mpz_as_bytes(&self->mpz, big_endian, len, buf);
|
mpz_as_bytes(&self->mpz, big_endian, len, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mp_obj_int_is_positive(mp_obj_t self_in) {
|
int mp_obj_int_sign(mp_obj_t self_in) {
|
||||||
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
||||||
return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
|
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(self_in);
|
||||||
|
if (val < 0) {
|
||||||
|
return -1;
|
||||||
|
} else if (val > 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
||||||
return !self->mpz.neg;
|
if (self->mpz.len == 0) {
|
||||||
|
return 0;
|
||||||
|
} else if (self->mpz.neg == 0) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This must handle int and bool types, and must raise a
|
// This must handle int and bool types, and must raise a
|
||||||
|
|||||||
Reference in New Issue
Block a user