summaryrefslogtreecommitdiff
path: root/src/port
diff options
context:
space:
mode:
authorAndrew Gierth2019-02-13 15:19:44 +0000
committerAndrew Gierth2019-02-13 15:19:44 +0000
commitf397e08599a3c3c08b3af3b318c531db5882f57d (patch)
treeea36c543b24efced48b31c931ac33e673eee3fd2 /src/port
parent37d9916020286caec810f4de61fbd0de3568454d (diff)
Use strtof() and not strtod() for float4 input.
Using strtod() creates a double-rounding problem; the input decimal value is first rounded to the nearest double; rounding that to the nearest float may then give an incorrect result. An example is that 7.038531e-26 when input via strtod and then rounded to float4 gives 0xAE43FEp-107 instead of the correct 0xAE43FDp-107. Values output by earlier PG versions with extra_float_digits=3 should all be read in with the same values as previously. However, values supplied by other software using shortest representations could be mis-read. On platforms that lack a strtof() entirely, we fall back to the old incorrect rounding behavior. (As strtof() is required by C99, such platforms are considered of primarily historical interest.) On VS2013, some workarounds are used to get correct error handling. The regression tests now test for the correct input values, so platforms that lack strtof() will need resultmap entries. An entry for HP-UX 10 is included (more may be needed). Reviewed-By: Tom Lane Discussion: https://postgr.es/m/871s5emitx.fsf@news-spur.riddles.org.uk Discussion: https://postgr.es/m/87d0owlqpv.fsf@news-spur.riddles.org.uk
Diffstat (limited to 'src/port')
-rw-r--r--src/port/strtof.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/port/strtof.c b/src/port/strtof.c
new file mode 100644
index 00000000000..7aca8e2e3df
--- /dev/null
+++ b/src/port/strtof.c
@@ -0,0 +1,123 @@
+/*-------------------------------------------------------------------------
+ *
+ * strtof.c
+ *
+ * Portions Copyright (c) 2019, PostgreSQL Global Development Group
+ *
+ *
+ * IDENTIFICATION
+ * src/port/strtof.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "c.h"
+
+#include <float.h>
+#include <math.h>
+
+#ifndef HAVE_STRTOF
+/*
+ * strtof() is part of C99; this version is only for the benefit of obsolete
+ * platforms. As such, it is known to return incorrect values for edge cases,
+ * which have to be allowed for in variant files for regression test results
+ * for any such platform.
+ */
+
+float
+strtof(const char *nptr, char **endptr)
+{
+ int caller_errno = errno;
+ double dresult;
+ float fresult;
+
+ errno = 0;
+ dresult = strtod(nptr, endptr);
+ fresult = (float) dresult;
+
+ if (errno == 0)
+ {
+ /*
+ * Value might be in-range for double but not float.
+ */
+ if (dresult != 0 && fresult == 0)
+ caller_errno = ERANGE; /* underflow */
+ if (!isinf(dresult) && isinf(fresult))
+ caller_errno = ERANGE; /* overflow */
+ }
+ else
+ caller_errno = errno;
+
+ errno = caller_errno;
+ return fresult;
+}
+
+#elif HAVE_BUGGY_WINDOWS_STRTOF
+/*
+ * On Windows, there's a slightly different problem: VS2013 has a strtof()
+ * that returns the correct results for valid input, but may fail to report an
+ * error for underflow or overflow, returning 0 instead. Work around that by
+ * trying strtod() when strtof() returns 0.0 or [+-]Inf, and calling it an
+ * error if the result differs. Also, strtof() doesn't handle subnormal input
+ * well, so prefer to round the strtod() result in such cases. (Normally we'd
+ * just say "too bad" if strtof() doesn't support subnormals, but since we're
+ * already in here fixing stuff, we might as well do the best fix we can.)
+ */
+float
+pg_strtof(const char *nptr, char **endptr)
+{
+ int caller_errno = errno;
+ float fresult;
+
+ errno = 0;
+ fresult = (strtof)(nptr, endptr);
+ if (errno)
+ {
+ /* On error, just return the error to the caller. */
+ return fresult;
+ }
+ else if ((*endptr == nptr) || isnan(fresult) ||
+ ((fresult >= FLT_MIN || fresult <= -FLT_MIN) && !isinf(fresult)))
+ {
+ /*
+ * If we got nothing parseable, or if we got a non-0 non-subnormal
+ * finite value (or NaN) without error, then return that to the caller
+ * without error.
+ */
+ errno = caller_errno;
+ return fresult;
+ }
+ else
+ {
+ /*
+ * Try again. errno is already 0 here.
+ */
+ double dresult = strtod(nptr, NULL);
+ if (errno)
+ {
+ /* On error, just return the error */
+ return fresult;
+ }
+ else if ((dresult == 0.0 && fresult == 0.0) ||
+ (isinf(dresult) && isinf(fresult) && (fresult == dresult)))
+ {
+ /* both values are 0 or infinities of the same sign */
+ errno = caller_errno;
+ return fresult;
+ }
+ else if ((dresult > 0 && dresult <= FLT_MIN && (float)dresult != 0.0) ||
+ (dresult < 0 && dresult >= -FLT_MIN && (float)dresult != 0.0))
+ {
+ /* subnormal but nonzero value */
+ errno = caller_errno;
+ return (float) dresult;
+ }
+ else
+ {
+ errno = ERANGE;
+ return fresult;
+ }
+ }
+}
+
+#endif