[Python-checkins] r70759 - in python/branches/py3k-short-float-repr: Include/pystrtod.h Objects/floatobject.c Python/pystrtod.c Python/strtod.c
eric.smith
python-checkins at python.org
Mon Mar 30 23:53:56 CEST 2009
Author: eric.smith
Date: Mon Mar 30 23:53:55 2009
New Revision: 70759
Log:
Hooked up new float formats to float's str(); deleted unused strtod.c.
Removed:
python/branches/py3k-short-float-repr/Python/strtod.c
Modified:
python/branches/py3k-short-float-repr/Include/pystrtod.h
python/branches/py3k-short-float-repr/Objects/floatobject.c
python/branches/py3k-short-float-repr/Python/pystrtod.c
Modified: python/branches/py3k-short-float-repr/Include/pystrtod.h
==============================================================================
--- python/branches/py3k-short-float-repr/Include/pystrtod.h (original)
+++ python/branches/py3k-short-float-repr/Include/pystrtod.h Mon Mar 30 23:53:55 2009
@@ -9,6 +9,13 @@
PyAPI_FUNC(double) PyOS_ascii_strtod(const char *str, char **ptr);
PyAPI_FUNC(double) PyOS_ascii_atof(const char *str);
PyAPI_FUNC(char *) PyOS_ascii_formatd(char *buffer, size_t buf_len, const char *format, double d);
+PyAPI_FUNC(char *) PyOS_double_to_string(double val,
+ int mode,
+ char format_code,
+ int precision,
+ int sign,
+ int add_dot_0_if_integer);
+
#ifdef __cplusplus
Modified: python/branches/py3k-short-float-repr/Objects/floatobject.c
==============================================================================
--- python/branches/py3k-short-float-repr/Objects/floatobject.c (original)
+++ python/branches/py3k-short-float-repr/Objects/floatobject.c Mon Mar 30 23:53:55 2009
@@ -379,101 +379,6 @@
}
-/* convert a Python float to a minimal string that evaluates back to that
- float. The output is minimal in the sense of having the least possible
- number of significant digits. */
-
-static void
-format_float_short(char *buf, size_t buflen, PyFloatObject *v)
-{
- double d;
- char *digits, *digits_end;
- int decpt, sign, exp_len;
- size_t digits_len, i;
-
- assert(PyFloat_Check(v));
- d = PyFloat_AS_DOUBLE(v);
-
- /* _Py_dg_dtoa returns a digit string (no decimal point
- or exponent) */
- digits = _Py_dg_dtoa(d, 0, 0, &decpt, &sign, &digits_end);
- assert(digits_end != NULL && digits_end > digits);
- digits_len = digits_end - digits;
-
- if (!isdigit(digits[0])) {
- /* infinities and nans here; adapt Gay's output,
- so convert Infinity to inf and NaN to nan, and
- ignore sign of nan. */
- if (digits[0] == 'i' || digits[0] == 'I') {
- if (sign == 1) {
- *buf++ = '-';
- }
- strncpy(buf, "inf", 3);
- buf += 3;
- } else {
- assert(digits[0] == 'n' || digits[0] == 'N');
- strncpy(buf, "nan", 3);
- buf += 3;
- }
- }
- else if (-4 < decpt && decpt <= 17) {
- if (sign == 1) {
- *buf++ = '-';
- }
- /* use fixed-point notation if 1e-4 <= value < 1e17 */
- if (decpt <= 0) {
- /* output: 0.00...00dd...dd */
- *buf++ = '0';
- *buf++ = '.';
- for (i=0; i < -decpt; i++)
- *buf++ = '0';
- strncpy(buf, digits, digits_len);
- buf += digits_len;
- }
- else if (decpt < digits_len) {
- /* output: dd...dd.dd...dd */
- strncpy(buf, digits, decpt);
- buf += decpt;
- *buf++ = '.';
- strncpy(buf, digits+decpt, digits_len-decpt);
- buf += digits_len-decpt;
- }
- else {
- /* decpt >= digits_len. output: dd...dd00...00.0 */
- strncpy(buf, digits, digits_len);
- buf += digits_len;
- for (i=0; i < decpt-digits_len; i++)
- *buf++ = '0';
- *buf++ = '.';
- *buf++ = '0';
- }
- }
- else {
- /* exponential notation: d[.dddd]e(+|-)ee;
- at least 2 digits in exponent */
- if (sign == 1) {
- *buf++ = '-';
- }
- *buf++ = digits[0];
- if (digits_len > 1) {
- *buf++ = '.';
- strncpy(buf, digits+1, digits_len-1);
- buf += digits_len-1;
- }
- *buf++ = 'e';
- exp_len = sprintf(buf, "%+.02d", decpt-1);
- buf += exp_len;
- }
- *buf++ = '\0';
-}
-
-static void
-format_float(char *buf, size_t buflen, PyFloatObject *v, int precision)
-{
- assert(PyFloat_Check(v));
- format_double(buf, buflen, PyFloat_AS_DOUBLE(v), precision);
-}
-
/* Macro and helper that convert PyObject obj to a C double and store
the value in dbl. If conversion to double raises an exception, obj is
set to NULL, and the function invoking this macro returns NULL. If
@@ -524,20 +429,29 @@
#define PREC_STR 12
static PyObject *
-float_repr(PyFloatObject *v)
+float_str_or_repr(PyFloatObject *v, int mode, int precision)
{
- char buf[100];
- format_float_short(buf, sizeof(buf), v);
+ PyObject *result;
+ char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
+ mode, 'g', precision, 0, 1);
+ if (!buf)
+ return PyErr_NoMemory();
+ result = PyUnicode_FromString(buf);
+ PyMem_Free(buf);
+ return result;
+}
- return PyUnicode_FromString(buf);
+static PyObject *
+float_repr(PyFloatObject *v)
+{
+ /* XXX change PREC_REPR to 0 when mode is supported */
+ return float_str_or_repr(v, 0, PREC_REPR);
}
static PyObject *
float_str(PyFloatObject *v)
{
- char buf[100];
- format_float(buf, sizeof(buf), v, PREC_STR);
- return PyUnicode_FromString(buf);
+ return float_str_or_repr(v, 2, PREC_STR);
}
/* Comparison is pretty much a nightmare. When comparing float to float,
@@ -2067,7 +1981,9 @@
if (PyFloat_CheckExact(p) &&
Py_REFCNT(p) != 0) {
char buf[100];
- format_float(buf, sizeof(buf), p, PREC_STR);
+ format_double(buf, sizeof(buf),
+ PyFloat_AS_DOUBLE(p),
+ PREC_STR);
/* XXX(twouters) cast refcount to
long until %zd is universally
available
Modified: python/branches/py3k-short-float-repr/Python/pystrtod.c
==============================================================================
--- python/branches/py3k-short-float-repr/Python/pystrtod.c (original)
+++ python/branches/py3k-short-float-repr/Python/pystrtod.c Mon Mar 30 23:53:55 2009
@@ -488,3 +488,128 @@
{
return PyOS_ascii_strtod(nptr, NULL);
}
+
+
+/* convert a Python float to a minimal string that evaluates back to that
+ float. The output is minimal in the sense of having the least possible
+ number of significant digits. */
+
+static void
+format_float_short(char *buf, size_t buflen, double d, int mode, int precision)
+{
+ char *digits, *digits_end;
+ int decpt, sign, exp_len;
+ size_t digits_len, i;
+
+ /* _Py_dg_dtoa returns a digit string (no decimal point
+ or exponent) */
+ digits = _Py_dg_dtoa(d, mode, precision, &decpt, &sign, &digits_end);
+ assert(digits_end != NULL && digits_end > digits);
+ digits_len = digits_end - digits;
+
+ if (!isdigit(digits[0])) {
+ /* infinities and nans here; adapt Gay's output,
+ so convert Infinity to inf and NaN to nan, and
+ ignore sign of nan. */
+ if (digits[0] == 'i' || digits[0] == 'I') {
+ if (sign == 1) {
+ *buf++ = '-';
+ }
+ strncpy(buf, "inf", 3);
+ buf += 3;
+ } else {
+ assert(digits[0] == 'n' || digits[0] == 'N');
+ strncpy(buf, "nan", 3);
+ buf += 3;
+ }
+ }
+ else if (-4 < decpt && decpt <= 17) {
+ if (sign == 1) {
+ *buf++ = '-';
+ }
+ /* use fixed-point notation if 1e-4 <= value < 1e17 */
+ if (decpt <= 0) {
+ /* output: 0.00...00dd...dd */
+ *buf++ = '0';
+ *buf++ = '.';
+ for (i=0; i < -decpt; i++)
+ *buf++ = '0';
+ strncpy(buf, digits, digits_len);
+ buf += digits_len;
+ }
+ else if (decpt < digits_len) {
+ /* output: dd...dd.dd...dd */
+ strncpy(buf, digits, decpt);
+ buf += decpt;
+ *buf++ = '.';
+ strncpy(buf, digits+decpt, digits_len-decpt);
+ buf += digits_len-decpt;
+ }
+ else {
+ /* decpt >= digits_len. output: dd...dd00...00.0 */
+ strncpy(buf, digits, digits_len);
+ buf += digits_len;
+ for (i=0; i < decpt-digits_len; i++)
+ *buf++ = '0';
+ *buf++ = '.';
+ *buf++ = '0';
+ }
+ }
+ else {
+ /* exponential notation: d[.dddd]e(+|-)ee;
+ at least 2 digits in exponent */
+ if (sign == 1) {
+ *buf++ = '-';
+ }
+ *buf++ = digits[0];
+ if (digits_len > 1) {
+ *buf++ = '.';
+ strncpy(buf, digits+1, digits_len-1);
+ buf += digits_len-1;
+ }
+ *buf++ = 'e';
+ exp_len = sprintf(buf, "%+.02d", decpt-1);
+ buf += exp_len;
+ }
+ *buf++ = '\0';
+}
+
+PyAPI_FUNC(char *) PyOS_double_to_string(double val,
+ int mode,
+ char format_code,
+ int precision,
+ int sign,
+ int add_dot_0_if_integer)
+{
+ char fmt[32];
+ char* buf = (char *)PyMem_Malloc(512);
+
+// printf("in PyOS_double_to_string\n");
+ if (!buf)
+ return NULL;
+
+ /* XXX validate format_code */
+
+ format_float_short(buf, 512, val, mode, precision);
+
+ if (add_dot_0_if_integer) {
+ /* If the result was just an integer, without a decimal, then
+ add ".0" to the end of the string. */
+ char *cp = buf;
+ if (*cp == '-')
+ cp++;
+ for (; *cp != '\0'; cp++) {
+ /* Any non-digit means it's not an integer;
+ this takes care of NAN and INF as well. */
+ if (!isdigit(Py_CHARMASK(*cp)))
+ break;
+ }
+ if (*cp == '\0') {
+ *cp++ = '.';
+ *cp++ = '0';
+ *cp++ = '\0';
+ }
+ }
+
+ return buf;
+}
Deleted: python/branches/py3k-short-float-repr/Python/strtod.c
==============================================================================
--- python/branches/py3k-short-float-repr/Python/strtod.c Mon Mar 30 23:53:55 2009
+++ (empty file)
@@ -1,156 +0,0 @@
-#include "pyconfig.h"
-
-/* comp.sources.misc strtod(), as posted in comp.lang.tcl,
- with bugfix for "123000.0" and acceptance of space after 'e' sign nuked.
-
- ************************************************************
- * YOU MUST EDIT THE MACHINE-DEPENDENT DEFINITIONS BELOW!!! *
- ************************************************************
-*/
-
-/* File : stdtod.c (Modified version of str2dbl.c)
- Author : Richard A. O'Keefe @ Quintus Computer Systems, Inc.
- Updated: Tuesday August 2nd, 1988
- Defines: double strtod (char *str, char**ptr)
-*/
-
-/* This is an implementation of the strtod() function described in the
- System V manuals, with a different name to avoid linker problems.
- All that str2dbl() does itself is check that the argument is well-formed
- and is in range. It leaves the work of conversion to atof(), which is
- assumed to exist and deliver correct results (if they can be represented).
-
- There are two reasons why this should be provided to the net:
- (a) some UNIX systems do not yet have strtod(), or do not have it
- available in the BSD "universe" (but they do have atof()).
- (b) some of the UNIX systems that *do* have it get it wrong.
- (some crash with large arguments, some assign the wrong *ptr value).
- There is a reason why *we* are providing it: we need a correct version
- of strtod(), and if we give this one away maybe someone will look for
- mistakes in it and fix them for us (:-).
-*/
-
-/* The following constants are machine-specific. MD{MIN,MAX}EXPT are
- integers and MD{MIN,MAX}FRAC are strings such that
- 0.${MDMAXFRAC}e${MDMAXEXPT} is the largest representable double,
- 0.${MDMINFRAC}e${MDMINEXPT} is the smallest representable +ve double
- MD{MIN,MAX}FRAC must not have any trailing zeros.
- The values here are for IEEE-754 64-bit floats.
- It is not perfectly clear to me whether an IEEE infinity should be
- returned for overflow, nor what a portable way of writing one is,
- so HUGE is just 0.MAXFRAC*10**MAXEXPT (this seems still to be the
- UNIX convention).
-
- I do know about <values.h>, but the whole point of this file is that
- we can't always trust that stuff to be there or to be correct.
-*/
-static int MDMINEXPT = -323;
-static char MDMINFRAC[] = "494065645841246544";
-static double ZERO = 0.0;
-
-static int MDMAXEXPT = 309;
-static char MDMAXFRAC[] = "17976931348623157";
-static double HUGE = 1.7976931348623157e308;
-
-extern double atof(const char *); /* Only called when result known to be ok */
-
-#ifdef HAVE_ERRNO_H
-#include <errno.h>
-#endif
-extern int errno;
-
-double strtod(char *str, char **ptr)
-{
- int sign, scale, dotseen;
- int esign, expt;
- char *save;
- register char *sp, *dp;
- register int c;
- char *buforg, *buflim;
- char buffer[64]; /* 45-digit significant + */
- /* 13-digit exponent */
- sp = str;
- while (*sp == ' ') sp++;
- sign = 1;
- if (*sp == '-') sign -= 2, sp++;
- dotseen = 0, scale = 0;
- dp = buffer;
- *dp++ = '0'; *dp++ = '.';
- buforg = dp, buflim = buffer+48;
- for (save = sp; c = *sp; sp++)
- if (c == '.') {
- if (dotseen) break;
- dotseen++;
- } else
- if ((unsigned)(c-'0') > (unsigned)('9'-'0')) {
- break;
- } else
- if (c == '0') {
- if (dp != buforg) {
- /* This is not the first digit, so we want to keep it */
- if (dp < buflim) *dp++ = c;
- if (!dotseen) scale++;
- } else {
- /* No non-zero digits seen yet */
- /* If a . has been seen, scale must be adjusted */
- if (dotseen) scale--;
- }
- } else {
- /* This is a nonzero digit, so we want to keep it */
- if (dp < buflim) *dp++ = c;
- /* If it precedes a ., scale must be adjusted */
- if (!dotseen) scale++;
- }
- if (sp == save) {
- if (ptr) *ptr = str;
- errno = EDOM; /* what should this be? */
- return ZERO;
- }
-
- while (dp > buforg && dp[-1] == '0') --dp;
- if (dp == buforg) *dp++ = '0';
- *dp = '\0';
- /* Now the contents of buffer are
- +--+--------+-+--------+
- |0.|fraction|\|leftover|
- +--+--------+-+--------+
- ^dp points here
- where fraction begins with 0 iff it is "0", and has at most
- 45 digits in it, and leftover is at least 16 characters.
- */
- save = sp, expt = 0, esign = 1;
- do {
- c = *sp++;
- if (c != 'e' && c != 'E') break;
- c = *sp++;
- if (c == '-') esign -= 2, c = *sp++; else
- if (c == '+' /* || c == ' ' */ ) c = *sp++;
- if ((unsigned)(c-'0') > (unsigned)('9'-'0')) break;
- while (c == '0') c = *sp++;
- for (; (unsigned)(c-'0') <= (unsigned)('9'-'0'); c = *sp++)
- expt = expt*10 + c-'0';
- if (esign < 0) expt = -expt;
- save = sp-1;
- } while (0);
- if (ptr) *ptr = save;
- expt += scale;
- /* Now the number is sign*0.fraction*10**expt */
- errno = ERANGE;
- if (expt > MDMAXEXPT) {
- return HUGE*sign;
- } else
- if (expt == MDMAXEXPT) {
- if (strcmp(buforg, MDMAXFRAC) > 0) return HUGE*sign;
- } else
- if (expt < MDMINEXPT) {
- return ZERO*sign;
- } else
- if (expt == MDMINEXPT) {
- if (strcmp(buforg, MDMINFRAC) < 0) return ZERO*sign;
- }
- /* We have now established that the number can be */
- /* represented without overflow or underflow */
- (void) sprintf(dp, "E%d", expt);
- errno = 0;
- return atof(buffer)*sign;
-}
More information about the Python-checkins
mailing list