Skip to content

Commit 1ecd195

Browse files
author
christian.heimes
committed
Feature #1534
Added PyFloat_GetMax(), PyFloat_GetMin() and PyFloat_GetInfo() to the float API. Added a dictionary sys.float_info with information about the internal floating point type to the sys module. git-svn-id: http://svn.python.org/projects/python/trunk@59254 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent fdad5da commit 1ecd195

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-0
lines changed

Doc/c-api/concrete.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,23 @@ Floating Point Objects
557557
without error checking.
558558

559559

560+
.. cfunction:: PyObject* PyFloat_GetInfo(void)
561+
562+
Return a :ctype:`PyDictObject` object which contains information about the
563+
precision, minimum and maximum values of a float. It's a thin wrapper
564+
around the header file :file:`float.h`.
565+
566+
567+
.. cfunction:: double PyFloat_GetMax(void)
568+
569+
Return the maximum representable finite float *DBL_MAX* as C :ctype:`double`.
570+
571+
572+
.. cfunction:: double PyFloat_GetMin(void)
573+
574+
Return the minimum normalized positive float *DBL_MIN* as C :ctype:`double`.
575+
576+
560577
.. _complexobjects:
561578

562579
Complex Number Objects

Doc/library/sys.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,48 @@ always available.
240240
Use :mod:`atexit` instead.
241241

242242

243+
.. data:: float_info
244+
245+
A dict holding information about the float type. It contains low level
246+
information about the precision and internal representation. Please study
247+
your system's :file:`float.h` for more information.
248+
249+
+---------------------+--------------------------------------------------+
250+
| key | explanation |
251+
+=====================+==================================================+
252+
| :const:`epsilon` | Difference between 1 and the next representable |
253+
| | floating point number |
254+
+---------------------+--------------------------------------------------+
255+
| :const:`dig` | digits (see :file:`float.h`) |
256+
+---------------------+--------------------------------------------------+
257+
| :const:`mant_dig` | mantissa digits (see :file:`float.h`) |
258+
+---------------------+--------------------------------------------------+
259+
| :const:`max` | maximum representable finite float |
260+
+---------------------+--------------------------------------------------+
261+
| :const:`max_exp` | maximum int e such that radix**(e-1) is in the |
262+
| | range of finite representable floats |
263+
+---------------------+--------------------------------------------------+
264+
| :const:`max_10_exp` | maximum int e such that 10**e is in the |
265+
| | range of finite representable floats |
266+
+---------------------+--------------------------------------------------+
267+
| :const:`min` | Minimum positive normalizer float |
268+
+---------------------+--------------------------------------------------+
269+
| :const:`min_exp` | minimum int e such that radix**(e-1) is a |
270+
| | normalized float |
271+
+---------------------+--------------------------------------------------+
272+
| :const:`min_10_exp` | minimum int e such that 10**e is a normalized |
273+
| | float |
274+
+---------------------+--------------------------------------------------+
275+
| :const:`radix` | radix of exponent |
276+
+---------------------+--------------------------------------------------+
277+
| :const:`rounds` | addition rounds (see :file:`float.h`) |
278+
+---------------------+--------------------------------------------------+
279+
280+
.. note::
281+
282+
The information in the table is simplified.
283+
284+
243285
.. function:: getcheckinterval()
244286

245287
Return the interpreter's "check interval"; see :func:`setcheckinterval`.

Include/floatobject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ PyAPI_DATA(PyTypeObject) PyFloat_Type;
2121
#define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
2222
#define PyFloat_CheckExact(op) (Py_Type(op) == &PyFloat_Type)
2323

24+
PyAPI_FUNC(double) PyFloat_GetMax(void);
25+
PyAPI_FUNC(double) PyFloat_GetMin(void);
26+
PyAPI_FUNC(PyObject *) PyFloat_GetInfo(void);
27+
2428
/* Return Python float from string PyObject. Second argument ignored on
2529
input, and, if non-NULL, NULL is stored into *junk (this tried to serve a
2630
purpose once but can't be made to work as intended). */

Lib/test/test_sys.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ def test_attributes(self):
329329
self.assert_(isinstance(sys.copyright, basestring))
330330
self.assert_(isinstance(sys.exec_prefix, basestring))
331331
self.assert_(isinstance(sys.executable, basestring))
332+
self.assert_(isinstance(sys.float_info, dict))
333+
self.assertEqual(len(sys.float_info), 11)
332334
self.assert_(isinstance(sys.hexversion, int))
333335
self.assert_(isinstance(sys.maxint, int))
334336
if test.test_support.have_unicode:

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Issue #1534: Added ``PyFloat_GetMax()``, ``PyFloat_GetMin()`` and
16+
``PyFloat_GetInfo()`` to the float API.
17+
1518
- Issue #1521: On 64bit platforms, using PyArgs_ParseTuple with the t# of w#
1619
format code incorrectly truncated the length to an int, even when
1720
PY_SSIZE_T_CLEAN is set. The str.decode method used to return incorrect
@@ -301,6 +304,9 @@ Core and builtins
301304
Library
302305
-------
303306

307+
- Issue #1534: Added a dictionary sys.float_info with information about the
308+
internal floating point type to the sys module.
309+
304310
- Issue 1429818: patch for trace and doctest modules so they play nicely
305311
together.
306312

Objects/floatobject.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Python.h"
88

99
#include <ctype.h>
10+
#include <float.h>
1011

1112
#if !defined(__STDC__)
1213
extern double fmod(double, double);
@@ -46,6 +47,52 @@ fill_free_list(void)
4647
return p + N_FLOATOBJECTS - 1;
4748
}
4849

50+
double
51+
PyFloat_GetMax(void)
52+
{
53+
return DBL_MAX;
54+
}
55+
56+
double
57+
PyFloat_GetMin(void)
58+
{
59+
return DBL_MIN;
60+
}
61+
62+
PyObject *
63+
PyFloat_GetInfo(void)
64+
{
65+
PyObject *d, *tmp;
66+
67+
#define SET_FLOAT_CONST(d, key, const) \
68+
tmp = PyFloat_FromDouble(const); \
69+
if (tmp == NULL) return NULL; \
70+
if (PyDict_SetItemString(d, key, tmp)) return NULL; \
71+
Py_DECREF(tmp)
72+
#define SET_INT_CONST(d, key, const) \
73+
tmp = PyInt_FromLong(const); \
74+
if (tmp == NULL) return NULL; \
75+
if (PyDict_SetItemString(d, key, tmp)) return NULL; \
76+
Py_DECREF(tmp)
77+
78+
d = PyDict_New();
79+
80+
SET_FLOAT_CONST(d, "max", DBL_MAX);
81+
SET_INT_CONST(d, "max_exp", DBL_MAX_EXP);
82+
SET_INT_CONST(d, "max_10_exp", DBL_MAX_10_EXP);
83+
SET_FLOAT_CONST(d, "min", DBL_MIN);
84+
SET_INT_CONST(d, "min_exp", DBL_MIN_EXP);
85+
SET_INT_CONST(d, "min_10_exp", DBL_MIN_10_EXP);
86+
SET_INT_CONST(d, "dig", DBL_DIG);
87+
SET_INT_CONST(d, "mant_dig", DBL_MANT_DIG);
88+
SET_FLOAT_CONST(d, "epsilon", DBL_EPSILON);
89+
SET_INT_CONST(d, "radix", FLT_RADIX);
90+
SET_INT_CONST(d, "rounds", FLT_ROUNDS);
91+
92+
return d;
93+
}
94+
95+
4996
PyObject *
5097
PyFloat_FromDouble(double fval)
5198
{

Python/sysmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,8 @@ _PySys_Init(void)
11691169
PyInt_FromLong(PyInt_GetMax()));
11701170
SET_SYS_FROM_STRING("py3kwarning",
11711171
PyBool_FromLong(Py_Py3kWarningFlag));
1172+
SET_SYS_FROM_STRING("float_info",
1173+
PyFloat_GetInfo());
11721174
#ifdef Py_USING_UNICODE
11731175
SET_SYS_FROM_STRING("maxunicode",
11741176
PyInt_FromLong(PyUnicode_GetMax()));

0 commit comments

Comments
 (0)