Skip to content

Commit 3a72f95

Browse files
yoctopucedpgeorge
authored andcommitted
py/objint_longlong: Fix longlong interoperability with floats.
Current longlong implementation does not allow a float as RHS of mathematic operators, as it lacks the delegation code present in mpz. Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
1 parent 3c69277 commit 3a72f95

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

py/objint_longlong.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,28 @@ mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_i
165165
rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
166166
} else if (mp_obj_is_exact_type(rhs_in, &mp_type_int)) {
167167
rhs_val = ((mp_obj_int_t *)rhs_in)->val;
168+
#if MICROPY_PY_BUILTINS_FLOAT
169+
} else if (mp_obj_is_float(rhs_in)) {
170+
return mp_obj_float_binary_op(op, (mp_float_t)lhs_val, rhs_in);
171+
#endif
172+
#if MICROPY_PY_BUILTINS_COMPLEX
173+
} else if (mp_obj_is_type(rhs_in, &mp_type_complex)) {
174+
return mp_obj_complex_binary_op(op, (mp_float_t)lhs_val, 0, rhs_in);
175+
#endif
168176
} else {
169177
// delegate to generic function to check for extra cases
170178
return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
171179
}
172180

181+
#if MICROPY_PY_BUILTINS_FLOAT
182+
if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
183+
if (rhs_val == 0) {
184+
goto zero_division;
185+
}
186+
return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
187+
}
188+
#endif
189+
173190
switch (op) {
174191
case MP_BINARY_OP_ADD:
175192
case MP_BINARY_OP_INPLACE_ADD:

tests/float/int_64_float.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# test int64 operation with float/complex
2+
3+
i = 1 << 40
4+
5+
# convert int64 to float on rhs
6+
print("%.5g" % (2.0 * i))
7+
8+
# negative int64 as float
9+
print("%.5g" % float(-i))
10+
11+
# this should convert to float
12+
print("%.5g" % (i / 5))
13+
14+
# these should delegate to float
15+
print("%.5g" % (i * 1.2))
16+
print("%.5g" % (i / 1.2))
17+
18+
# negative power should produce float
19+
print("%.5g" % (i**-1))
20+
print("%.5g" % ((2 + i - i) ** -3))
21+
22+
try:
23+
i / 0
24+
except ZeroDivisionError:
25+
print("ZeroDivisionError")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy