diff Objects/floatobject.c @ 44085:c218d1ac916e legacy-trunk

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.
author Christian Heimes <christian@cheimes.de>
date Sat, 01 Dec 2007 11:20:10 +0000
parents 7630248b593c
children 24749a380409
line wrap: on
line diff
--- a/Objects/floatobject.c	Sat Dec 01 01:03:20 2007 +0000
+++ b/Objects/floatobject.c	Sat Dec 01 11:20:10 2007 +0000
@@ -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)
 {