Skip to content

py/obj: Fix nan handling in REPR_C and REPR_D. #17531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ports/windows/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ typedef long mp_off_t;
#endif
#endif

// VC++ 2017 fixes
#if (_MSC_VER < 1920)
#define MICROPY_PY_MATH_COPYSIGN_FIX_NAN (1)
#endif

// CL specific definitions

#ifndef __cplusplus
Expand Down
5 changes: 5 additions & 0 deletions py/modmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ MATH_FUN_2(atan2, atan2)
MATH_FUN_1_TO_INT(ceil, ceil)
// copysign(x, y)
static mp_float_t MICROPY_FLOAT_C_FUN(copysign_func)(mp_float_t x, mp_float_t y) {
#if MICROPY_PY_MATH_COPYSIGN_FIX_NAN
if (isnan(y)) {
y = 0.0;
}
#endif
return MICROPY_FLOAT_C_FUN(copysign)(x, y);
}
MATH_FUN_2(copysign, copysign_func)
Expand Down
20 changes: 17 additions & 3 deletions py/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,15 @@ static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))

#if MICROPY_PY_BUILTINS_FLOAT
#define MP_OBJ_NEW_CONST_FLOAT(f) MP_ROM_PTR((mp_obj_t)((((((uint64_t)f) & ~3) | 2) + 0x80800000) & 0xffffffff))
#include <math.h>
// note: MP_OBJ_NEW_CONST_FLOAT should be a MP_ROM_PTR but that macro isn't available yet
#define MP_OBJ_NEW_CONST_FLOAT(f) ((mp_obj_t)((((((uint64_t)f) & ~3) | 2) + 0x80800000) & 0xffffffff))
#define mp_const_float_e MP_OBJ_NEW_CONST_FLOAT(0x402df854)
#define mp_const_float_pi MP_OBJ_NEW_CONST_FLOAT(0x40490fdb)
#define mp_const_float_nan MP_OBJ_NEW_CONST_FLOAT(0x7fc00000)
#if MICROPY_PY_MATH_CONSTANTS
#define mp_const_float_tau MP_OBJ_NEW_CONST_FLOAT(0x40c90fdb)
#define mp_const_float_inf MP_OBJ_NEW_CONST_FLOAT(0x7f800000)
#define mp_const_float_nan MP_OBJ_NEW_CONST_FLOAT(0xffc00000)
#endif

static inline bool mp_obj_is_float(mp_const_obj_t o) {
Expand All @@ -207,6 +209,10 @@ static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
return num.f;
}
static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
if (isnan(f)) {
// prevent creation of bad nanboxed pointers via array.array or struct
return mp_const_float_nan;
}
union {
mp_float_t f;
mp_uint_t u;
Expand Down Expand Up @@ -257,12 +263,13 @@ static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
#error MICROPY_OBJ_REPR_D requires MICROPY_FLOAT_IMPL_DOUBLE
#endif

#include <math.h>
#define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b145769 + 0x8004000000000000))}
#define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
#define mp_const_float_nan {((mp_obj_t)((uint64_t)0x7ff8000000000000 + 0x8004000000000000))}
#if MICROPY_PY_MATH_CONSTANTS
#define mp_const_float_tau {((mp_obj_t)((uint64_t)0x401921fb54442d18 + 0x8004000000000000))}
#define mp_const_float_inf {((mp_obj_t)((uint64_t)0x7ff0000000000000 + 0x8004000000000000))}
#define mp_const_float_nan {((mp_obj_t)((uint64_t)0xfff8000000000000 + 0x8004000000000000))}
#endif

static inline bool mp_obj_is_float(mp_const_obj_t o) {
Expand All @@ -276,6 +283,13 @@ static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
return num.f;
}
static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
if (isnan(f)) {
// prevent creation of bad nanboxed pointers via array.array or struct
struct {
uint64_t r;
} num = mp_const_float_nan;
return num.r;
}
union {
mp_float_t f;
uint64_t r;
Expand Down
8 changes: 7 additions & 1 deletion tests/float/float_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ def test(a):
test(array("f"))
test(array("d"))

print("{:.4f}".format(array("f", bytes(array("I", [0x3DCCCCCC])))[0]))
# hand-crafted floats, including non-standard nan
for float_hex in (0x3DCCCCCC, 0x7F800024, 0x7FC00004):
f = array("f", bytes(array("I", [float_hex])))[0]
if type(f) is float:
print("{:.4e}".format(f))
else:
print(f)
3 changes: 3 additions & 0 deletions tests/float/math_constants_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
raise SystemExit

print(math.tau == 2.0 * math.pi)
print(math.copysign(1.0, math.tau))

print(math.inf == float("inf"))
print(-math.inf == -float("inf"))
print(math.copysign(1.0, math.inf))

print(isnan(math.nan))
print(isnan(-math.nan))
print(math.copysign(1.0, math.nan))
Loading
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