[Python-checkins] r59254 - in python/trunk: Doc/c-api/concrete.rst Doc/library/sys.rst Include/floatobject.h Lib/test/test_sys.py Misc/NEWS Objects/floatobject.c Python/sysmodule.c

christian.heimes python-checkins at python.org
Sat Dec 1 12:20:11 CET 2007


Author: christian.heimes
Date: Sat Dec  1 12:20:10 2007
New Revision: 59254

Modified:
   python/trunk/Doc/c-api/concrete.rst
   python/trunk/Doc/library/sys.rst
   python/trunk/Include/floatobject.h
   python/trunk/Lib/test/test_sys.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/floatobject.c
   python/trunk/Python/sysmodule.c
Log:
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.

Modified: python/trunk/Doc/c-api/concrete.rst
==============================================================================
--- python/trunk/Doc/c-api/concrete.rst	(original)
+++ python/trunk/Doc/c-api/concrete.rst	Sat Dec  1 12:20:10 2007
@@ -557,6 +557,23 @@
    without error checking.
 
 
+.. cfunction:: PyObject* PyFloat_GetInfo(void)
+
+   Return a :ctype:`PyDictObject` object which contains information about the
+   precision, minimum and maximum values of a float. It's a thin wrapper
+   around the header file :file:`float.h`.
+
+
+.. cfunction:: double PyFloat_GetMax(void)
+
+   Return the maximum representable finite float *DBL_MAX* as C :ctype:`double`.
+
+
+.. cfunction:: double PyFloat_GetMin(void)
+
+   Return the minimum normalized positive float *DBL_MIN* as C :ctype:`double`.
+
+
 .. _complexobjects:
 
 Complex Number Objects

Modified: python/trunk/Doc/library/sys.rst
==============================================================================
--- python/trunk/Doc/library/sys.rst	(original)
+++ python/trunk/Doc/library/sys.rst	Sat Dec  1 12:20:10 2007
@@ -240,6 +240,48 @@
       Use :mod:`atexit` instead.
 
 
+.. data:: float_info
+
+   A dict holding information about the float type. It contains low level
+   information about the precision and internal representation. Please study
+   your system's :file:`float.h` for more information.
+
+   +---------------------+--------------------------------------------------+
+   | key                 |  explanation                                     |
+   +=====================+==================================================+
+   | :const:`epsilon`    | Difference between 1 and the next representable  |
+   |                     | floating point number                            |
+   +---------------------+--------------------------------------------------+
+   | :const:`dig`        | digits (see :file:`float.h`)                     |
+   +---------------------+--------------------------------------------------+
+   | :const:`mant_dig`   | mantissa digits (see :file:`float.h`)            |
+   +---------------------+--------------------------------------------------+
+   | :const:`max`        | maximum representable finite float               |
+   +---------------------+--------------------------------------------------+
+   | :const:`max_exp`    | maximum int e such that radix**(e-1) is in the   |
+   |                     | range of finite representable floats             |
+   +---------------------+--------------------------------------------------+
+   | :const:`max_10_exp` | maximum int e such that 10**e is in the          |
+   |                     | range of finite representable floats             |
+   +---------------------+--------------------------------------------------+
+   | :const:`min`        | Minimum positive normalizer float                |
+   +---------------------+--------------------------------------------------+
+   | :const:`min_exp`    | minimum int e such that radix**(e-1) is a        |
+   |                     | normalized float                                 |
+   +---------------------+--------------------------------------------------+
+   | :const:`min_10_exp` | minimum int e such that 10**e is a normalized    |
+   |                     | float                                            |
+   +---------------------+--------------------------------------------------+
+   | :const:`radix`      | radix of exponent                                |
+   +---------------------+--------------------------------------------------+
+   | :const:`rounds`     | addition rounds (see :file:`float.h`)            |
+   +---------------------+--------------------------------------------------+
+
+   .. note::
+
+      The information in the table is simplified.
+
+
 .. function:: getcheckinterval()
 
    Return the interpreter's "check interval"; see :func:`setcheckinterval`.

Modified: python/trunk/Include/floatobject.h
==============================================================================
--- python/trunk/Include/floatobject.h	(original)
+++ python/trunk/Include/floatobject.h	Sat Dec  1 12:20:10 2007
@@ -21,6 +21,10 @@
 #define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
 #define PyFloat_CheckExact(op) (Py_Type(op) == &PyFloat_Type)
 
+PyAPI_FUNC(double) PyFloat_GetMax(void);
+PyAPI_FUNC(double) PyFloat_GetMin(void);
+PyAPI_FUNC(PyObject *) PyFloat_GetInfo(void);
+
 /* Return Python float from string PyObject.  Second argument ignored on
    input, and, if non-NULL, NULL is stored into *junk (this tried to serve a
    purpose once but can't be made to work as intended). */

Modified: python/trunk/Lib/test/test_sys.py
==============================================================================
--- python/trunk/Lib/test/test_sys.py	(original)
+++ python/trunk/Lib/test/test_sys.py	Sat Dec  1 12:20:10 2007
@@ -329,6 +329,8 @@
         self.assert_(isinstance(sys.copyright, basestring))
         self.assert_(isinstance(sys.exec_prefix, basestring))
         self.assert_(isinstance(sys.executable, basestring))
+        self.assert_(isinstance(sys.float_info, dict))
+        self.assertEqual(len(sys.float_info), 11)
         self.assert_(isinstance(sys.hexversion, int))
         self.assert_(isinstance(sys.maxint, int))
         if test.test_support.have_unicode:

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Dec  1 12:20:10 2007
@@ -12,6 +12,9 @@
 Core and builtins
 -----------------
 
+- Issue #1534: Added ``PyFloat_GetMax()``, ``PyFloat_GetMin()`` and
+  ``PyFloat_GetInfo()`` to the float API.
+
 - Issue #1521: On 64bit platforms, using PyArgs_ParseTuple with the t# of w#
   format code incorrectly truncated the length to an int, even when
   PY_SSIZE_T_CLEAN is set.  The str.decode method used to return incorrect
@@ -301,6 +304,9 @@
 Library
 -------
 
+- Issue #1534: Added a dictionary sys.float_info with information about the
+  internal floating point type to the sys module.
+
 - Issue 1429818: patch for trace and doctest modules so they play nicely
   together.
 

Modified: python/trunk/Objects/floatobject.c
==============================================================================
--- python/trunk/Objects/floatobject.c	(original)
+++ python/trunk/Objects/floatobject.c	Sat Dec  1 12:20:10 2007
@@ -7,6 +7,7 @@
 #include "Python.h"
 
 #include <ctype.h>
+#include <float.h>
 
 #if !defined(__STDC__)
 extern double fmod(double, double);
@@ -46,6 +47,52 @@
 	return p + N_FLOATOBJECTS - 1;
 }
 
+double
+PyFloat_GetMax(void)
+{
+	return DBL_MAX;
+}
+
+double
+PyFloat_GetMin(void)
+{
+	return DBL_MIN;
+}
+
+PyObject *
+PyFloat_GetInfo(void)
+{
+	PyObject *d, *tmp;
+
+#define SET_FLOAT_CONST(d, key, const) \
+	tmp = PyFloat_FromDouble(const); \
+	if (tmp == NULL) return NULL; \
+	if (PyDict_SetItemString(d, key, tmp)) return NULL; \
+	Py_DECREF(tmp)
+#define SET_INT_CONST(d, key, const) \
+	tmp = PyInt_FromLong(const); \
+	if (tmp == NULL) return NULL; \
+	if (PyDict_SetItemString(d, key, tmp)) return NULL; \
+	Py_DECREF(tmp)
+
+	d = PyDict_New();
+
+	SET_FLOAT_CONST(d, "max", DBL_MAX);
+	SET_INT_CONST(d, "max_exp", DBL_MAX_EXP);
+	SET_INT_CONST(d, "max_10_exp", DBL_MAX_10_EXP);
+	SET_FLOAT_CONST(d, "min", DBL_MIN);
+	SET_INT_CONST(d, "min_exp", DBL_MIN_EXP);
+	SET_INT_CONST(d, "min_10_exp", DBL_MIN_10_EXP);
+	SET_INT_CONST(d, "dig", DBL_DIG);
+	SET_INT_CONST(d, "mant_dig", DBL_MANT_DIG);
+	SET_FLOAT_CONST(d, "epsilon", DBL_EPSILON);
+	SET_INT_CONST(d, "radix", FLT_RADIX);
+	SET_INT_CONST(d, "rounds", FLT_ROUNDS);
+
+	return d;
+}
+
+
 PyObject *
 PyFloat_FromDouble(double fval)
 {

Modified: python/trunk/Python/sysmodule.c
==============================================================================
--- python/trunk/Python/sysmodule.c	(original)
+++ python/trunk/Python/sysmodule.c	Sat Dec  1 12:20:10 2007
@@ -1169,6 +1169,8 @@
 			    PyInt_FromLong(PyInt_GetMax()));
 	SET_SYS_FROM_STRING("py3kwarning",
 			    PyBool_FromLong(Py_Py3kWarningFlag));
+	SET_SYS_FROM_STRING("float_info",
+			    PyFloat_GetInfo());
 #ifdef Py_USING_UNICODE
 	SET_SYS_FROM_STRING("maxunicode",
 			    PyInt_FromLong(PyUnicode_GetMax()));


More information about the Python-checkins mailing list