Skip to content

Commit dd69672

Browse files
stinosdpgeorge
authored andcommitted
py/modmath: Add math.tau, math.nan and math.inf constants.
Configurable by the new MICROPY_PY_MATH_CONSTANTS option.
1 parent e0b8d69 commit dd69672

File tree

9 files changed

+79
-0
lines changed

9 files changed

+79
-0
lines changed

ports/unix/variants/coverage/mpconfigvariant.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define MICROPY_PY_BUILTINS_HELP (1)
4949
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
5050
#define MICROPY_PY_SYS_GETSIZEOF (1)
51+
#define MICROPY_PY_MATH_CONSTANTS (1)
5152
#define MICROPY_PY_MATH_FACTORIAL (1)
5253
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
5354
#define MICROPY_PY_IO_BUFFEREDWRITER (1)

ports/unix/variants/dev/mpconfigvariant.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#define MICROPY_PY_BUILTINS_HELP (1)
3535
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
36+
#define MICROPY_PY_MATH_CONSTANTS (1)
3637
#define MICROPY_PY_SYS_SETTRACE (1)
3738
#define MICROPY_PY_UOS_VFS (1)
3839
#define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)

ports/windows/variants/dev/mpconfigvariant.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#define MICROPY_PY_BUILTINS_HELP (1)
3232
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
33+
#define MICROPY_PY_MATH_CONSTANTS (1)
3334
#define MICROPY_PY_SYS_SETTRACE (1)
3435
#define MICROPY_PERSISTENT_CODE_SAVE (1)
3536
#define MICROPY_COMP_CONST (0)

py/modmath.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ STATIC const mp_rom_map_elem_t mp_module_math_globals_table[] = {
371371
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_math) },
372372
{ MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e },
373373
{ MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi },
374+
#if MICROPY_PY_MATH_CONSTANTS
375+
{ MP_ROM_QSTR(MP_QSTR_tau), mp_const_float_tau },
376+
{ MP_ROM_QSTR(MP_QSTR_inf), mp_const_float_inf },
377+
{ MP_ROM_QSTR(MP_QSTR_nan), mp_const_float_nan },
378+
#endif
374379
{ MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_math_sqrt_obj) },
375380
{ MP_ROM_QSTR(MP_QSTR_pow), MP_ROM_PTR(&mp_math_pow_obj) },
376381
{ MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_math_exp_obj) },

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,11 @@ typedef double mp_float_t;
12211221
#define MICROPY_PY_MATH (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_CORE_FEATURES)
12221222
#endif
12231223

1224+
// Whether to provide all math module constants (Python 3.5+), or just pi and e.
1225+
#ifndef MICROPY_PY_MATH_CONSTANTS
1226+
#define MICROPY_PY_MATH_CONSTANTS (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
1227+
#endif
1228+
12241229
// Whether to provide special math functions: math.{erf,erfc,gamma,lgamma}
12251230
#ifndef MICROPY_PY_MATH_SPECIAL_FUNCTIONS
12261231
#define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)

py/obj.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,18 @@ static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
104104
#if MICROPY_PY_BUILTINS_FLOAT
105105
#define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
106106
#define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
107+
#if MICROPY_PY_MATH_CONSTANTS
108+
#define mp_const_float_tau MP_ROM_PTR(&mp_const_float_tau_obj)
109+
#define mp_const_float_inf MP_ROM_PTR(&mp_const_float_inf_obj)
110+
#define mp_const_float_nan MP_ROM_PTR(&mp_const_float_nan_obj)
111+
#endif
107112
extern const struct _mp_obj_float_t mp_const_float_e_obj;
108113
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
114+
#if MICROPY_PY_MATH_CONSTANTS
115+
extern const struct _mp_obj_float_t mp_const_float_tau_obj;
116+
extern const struct _mp_obj_float_t mp_const_float_inf_obj;
117+
extern const struct _mp_obj_float_t mp_const_float_nan_obj;
118+
#endif
109119

110120
#define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
111121
mp_float_t mp_obj_float_get(mp_obj_t self_in);
@@ -139,8 +149,18 @@ static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
139149
#if MICROPY_PY_BUILTINS_FLOAT
140150
#define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
141151
#define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
152+
#if MICROPY_PY_MATH_CONSTANTS
153+
#define mp_const_float_tau MP_ROM_PTR(&mp_const_float_tau_obj)
154+
#define mp_const_float_inf MP_ROM_PTR(&mp_const_float_inf_obj)
155+
#define mp_const_float_nan MP_ROM_PTR(&mp_const_float_nan_obj)
156+
#endif
142157
extern const struct _mp_obj_float_t mp_const_float_e_obj;
143158
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
159+
#if MICROPY_PY_MATH_CONSTANTS
160+
extern const struct _mp_obj_float_t mp_const_float_tau_obj;
161+
extern const struct _mp_obj_float_t mp_const_float_inf_obj;
162+
extern const struct _mp_obj_float_t mp_const_float_nan_obj;
163+
#endif
144164

145165
#define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
146166
mp_float_t mp_obj_float_get(mp_obj_t self_in);
@@ -162,6 +182,11 @@ static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
162182
#if MICROPY_PY_BUILTINS_FLOAT
163183
#define mp_const_float_e MP_ROM_PTR((mp_obj_t)(((0x402df854 & ~3) | 2) + 0x80800000))
164184
#define mp_const_float_pi MP_ROM_PTR((mp_obj_t)(((0x40490fdb & ~3) | 2) + 0x80800000))
185+
#if MICROPY_PY_MATH_CONSTANTS
186+
#define mp_const_float_tau MP_ROM_PTR((mp_obj_t)(((0x40c90fdb & ~3) | 2) + 0x80800000))
187+
#define mp_const_float_inf MP_ROM_PTR((mp_obj_t)(((0x7f800000 & ~3) | 2) + 0x80800000))
188+
#define mp_const_float_nan MP_ROM_PTR((mp_obj_t)(((0xffc00000 & ~3) | 2) + 0x80800000))
189+
#endif
165190

166191
static inline bool mp_obj_is_float(mp_const_obj_t o) {
167192
return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006;
@@ -226,6 +251,11 @@ static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
226251

227252
#define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b145769 + 0x8004000000000000))}
228253
#define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
254+
#if MICROPY_PY_MATH_CONSTANTS
255+
#define mp_const_float_tau {((mp_obj_t)((uint64_t)0x401921fb54442d18 + 0x8004000000000000))}
256+
#define mp_const_float_inf {((mp_obj_t)((uint64_t)0x7ff0000000000000 + 0x8004000000000000))}
257+
#define mp_const_float_nan {((mp_obj_t)((uint64_t)0xfff8000000000000 + 0x8004000000000000))}
258+
#endif
229259

230260
static inline bool mp_obj_is_float(mp_const_obj_t o) {
231261
return ((uint64_t)(o) & 0xfffc000000000000) != 0;

py/objfloat.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ typedef struct _mp_obj_float_t {
5454

5555
const mp_obj_float_t mp_const_float_e_obj = {{&mp_type_float}, (mp_float_t)M_E};
5656
const mp_obj_float_t mp_const_float_pi_obj = {{&mp_type_float}, (mp_float_t)M_PI};
57+
#if MICROPY_PY_MATH_CONSTANTS
58+
#ifndef NAN
59+
#error NAN macro is not defined
60+
#endif
61+
const mp_obj_float_t mp_const_float_tau_obj = {{&mp_type_float}, (mp_float_t)(2.0 * M_PI)};
62+
const mp_obj_float_t mp_const_float_inf_obj = {{&mp_type_float}, (mp_float_t)INFINITY};
63+
const mp_obj_float_t mp_const_float_nan_obj = {{&mp_type_float}, (mp_float_t)NAN};
64+
#endif
5765

5866
#endif
5967

tests/float/math_constants.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Tests various constants of the math module.
2+
try:
3+
import math
4+
from math import exp, cos
5+
except ImportError:
6+
print("SKIP")
7+
raise SystemExit
8+
9+
print(math.e == exp(1.0))
10+
11+
print(cos(math.pi))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tests constants of the math module available only with MICROPY_PY_MATH_CONSTANTS.
2+
try:
3+
import math
4+
from math import isnan
5+
6+
math.tau
7+
except (ImportError, AttributeError):
8+
print("SKIP")
9+
raise SystemExit
10+
11+
print(math.tau == 2.0 * math.pi)
12+
13+
print(math.inf == float("inf"))
14+
print(-math.inf == -float("inf"))
15+
16+
print(isnan(math.nan))
17+
print(isnan(-math.nan))

0 commit comments

Comments
 (0)