]> granicus.if.org Git - python/commitdiff
Feature #1534
authorChristian Heimes <christian@cheimes.de>
Sat, 1 Dec 2007 11:20:10 +0000 (11:20 +0000)
committerChristian Heimes <christian@cheimes.de>
Sat, 1 Dec 2007 11:20:10 +0000 (11:20 +0000)
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.

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

index a02332abfc807f930119ba04ff61e6a408cf3b81..aad031b4cb2157018e8f700088f598eec72a8d15 100644 (file)
@@ -557,6 +557,23 @@ Floating Point Objects
    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
index f254003236924223e910e44a99ae16f30c8fa388..d33f5320845040863e80b03d3babe098a050d644 100644 (file)
@@ -240,6 +240,48 @@ always available.
       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`.
index bfbc580126fd3e98bdb4cab7acc8d24ef8e67f87..be1a80c04e681fafe37a7d4299ca1b2eafb11eff 100644 (file)
@@ -21,6 +21,10 @@ PyAPI_DATA(PyTypeObject) PyFloat_Type;
 #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). */
index f1f1524c06d5b75efa3609fad447d1cae1700a34..d02412fd74e8279e1675a93d2600000abd597b29 100644 (file)
@@ -329,6 +329,8 @@ class SysModuleTest(unittest.TestCase):
         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:
index 4fa85c683623b3c0e727c4f8f14cc2bfe2d645c9..2023143f523ce77ba5597c6c0ee26b7501f5a08b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
 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 @@ Core and builtins
 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.
 
index bf9b1728621ee5ce1677b0810707ca12e33ffdbc..c76956a6698cfd1ae31f2c43b60b39e48d71ca2c 100644 (file)
@@ -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 @@ fill_free_list(void)
        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)
 {
index 617c38a2d754956c93f2275b15dc1bec901837a8..2a6a8c3e57a6b2a6eea50dbc03261b12bb73fc24 100644 (file)
@@ -1169,6 +1169,8 @@ _PySys_Init(void)
                            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()));