From 4d721ac45967f64415bafb82039a652f0a451e2c Mon Sep 17 00:00:00 2001
From: Chapman Flack <chap@anastigmatix.net>
Date: Mon, 26 Feb 2018 01:26:01 -0500
Subject: [PATCH 2/2] Proof of concept fix of float[48]_numeric.

This patch simply uses the new FLT_DECIMAL_DIG/DBL_DECIMAL_DIG
constants in place of the FLT_DIG/DBL_DIG, just to see what happens.
It uses the double-underscore versions of the new constants, to
avoid mucking with the build procedure to change the C standard
to C11 where they would be visible without underscores. Needless to say,
this POC patch won't work where those constants aren't available, and
isn't proposed as a final patch.

It passes the newly added cast-no-precision-loss regression test.
It changes the output of an existing numeric test and a few numeric
aggregate tests.

I have adjusted the numeric test: its purpose is to check the direction
of numeric rounding of ties, but the value to be rounded was computed
in float8 and then cast to numeric, and because negative powers of ten
aren't tidy in binary, it can turn out that the float8 computation
produces a correctly-rounded-53-bit-result that is on the nearer-to-zero
side of a tie, and now that that result is correctly cast, the resulting
numeric doesn't round in the away-from-zero direction.

I changed that test because I concluded it wasn't meant to test
float8-to-numeric casting, but only the rounding of tied numerics,
so I just made the inner expression be typed numeric, and the expected
output is unchanged.

The three aggregate tests with changed output are working from a table
of float4 values, and my assumption is they are now simply producing
the correct aggregate results given the full precision of the input
values, but I haven't confirmed that yet, so this patch leaves those
three failing for now.
---
 src/backend/utils/adt/numeric.c       |  4 ++--
 src/test/regress/expected/numeric.out | 12 ++++++------
 src/test/regress/sql/numeric.sql      | 12 ++++++------
 3 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 6f40072..c05de08 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -3224,7 +3224,7 @@ float8_numeric(PG_FUNCTION_ARGS)
 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 				 errmsg("cannot convert infinity to numeric")));
 
-	snprintf(buf, sizeof(buf), "%.*g", DBL_DIG, val);
+	snprintf(buf, sizeof(buf), "%.*g", __DBL_DECIMAL_DIG__, val);
 
 	init_var(&result);
 
@@ -3295,7 +3295,7 @@ float4_numeric(PG_FUNCTION_ARGS)
 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 				 errmsg("cannot convert infinity to numeric")));
 
-	snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
+	snprintf(buf, sizeof(buf), "%.*g", __FLT_DECIMAL_DIG__, val);
 
 	init_var(&result);
 
diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out
index c186385..0c757a3 100644
--- a/src/test/regress/expected/numeric.out
+++ b/src/test/regress/expected/numeric.out
@@ -753,12 +753,12 @@ SELECT a, ceil(a), ceiling(a), floor(a), round(a) FROM ceil_floor_round;
 DROP TABLE ceil_floor_round;
 -- Check rounding, it should round ties away from zero.
 SELECT i as pow,
-	round((-2.5 * 10 ^ i)::numeric, -i),
-	round((-1.5 * 10 ^ i)::numeric, -i),
-	round((-0.5 * 10 ^ i)::numeric, -i),
-	round((0.5 * 10 ^ i)::numeric, -i),
-	round((1.5 * 10 ^ i)::numeric, -i),
-	round((2.5 * 10 ^ i)::numeric, -i)
+	round((-2.5 * 10::numeric ^ i), -i),
+	round((-1.5 * 10::numeric ^ i), -i),
+	round((-0.5 * 10::numeric ^ i), -i),
+	round((0.5 * 10::numeric ^ i), -i),
+	round((1.5 * 10::numeric ^ i), -i),
+	round((2.5 * 10::numeric ^ i), -i)
 FROM generate_series(-5,5) AS t(i);
  pow |  round   |  round   |  round   |  round  |  round  |  round  
 -----+----------+----------+----------+---------+---------+---------
diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql
index dab9ae2..c096c43 100644
--- a/src/test/regress/sql/numeric.sql
+++ b/src/test/regress/sql/numeric.sql
@@ -677,12 +677,12 @@ DROP TABLE ceil_floor_round;
 
 -- Check rounding, it should round ties away from zero.
 SELECT i as pow,
-	round((-2.5 * 10 ^ i)::numeric, -i),
-	round((-1.5 * 10 ^ i)::numeric, -i),
-	round((-0.5 * 10 ^ i)::numeric, -i),
-	round((0.5 * 10 ^ i)::numeric, -i),
-	round((1.5 * 10 ^ i)::numeric, -i),
-	round((2.5 * 10 ^ i)::numeric, -i)
+	round((-2.5 * 10::numeric ^ i), -i),
+	round((-1.5 * 10::numeric ^ i), -i),
+	round((-0.5 * 10::numeric ^ i), -i),
+	round((0.5 * 10::numeric ^ i), -i),
+	round((1.5 * 10::numeric ^ i), -i),
+	round((2.5 * 10::numeric ^ i), -i)
 FROM generate_series(-5,5) AS t(i);
 
 -- Testing for width_bucket(). For convenience, we test both the
-- 
2.7.3

