aboutsummaryrefslogtreecommitdiff
path: root/src/test/regress/sql/numeric.sql
diff options
context:
space:
mode:
authorDean Rasheed <dean.a.rasheed@gmail.com>2021-08-05 09:30:37 +0100
committerDean Rasheed <dean.a.rasheed@gmail.com>2021-08-05 09:30:37 +0100
commit43644bd3b234091c4bfad0bf6d7d88f90c52aaf5 (patch)
tree75c5bf1c244833ac79f6846e3baec25fa3a19d23 /src/test/regress/sql/numeric.sql
parent165506217df417c7ca11770599e64d5e93a96fb6 (diff)
downloadpostgresql-43644bd3b234091c4bfad0bf6d7d88f90c52aaf5.tar.gz
postgresql-43644bd3b234091c4bfad0bf6d7d88f90c52aaf5.zip
Fix division-by-zero error in to_char() with 'EEEE' format.
This fixes a long-standing bug when using to_char() to format a numeric value in scientific notation -- if the value's exponent is less than -NUMERIC_MAX_DISPLAY_SCALE-1 (-1001), it produced a division-by-zero error. The reason for this error was that get_str_from_var_sci() divides its input by 10^exp, which it produced using power_var_int(). However, the underflow test in power_var_int() causes it to return zero if the result scale is too small. That's not a problem for power_var_int()'s only other caller, power_var(), since that limits the rscale to 1000, but in get_str_from_var_sci() the exponent can be much smaller, requiring a much larger rscale. Fix by introducing a new function to compute 10^exp directly, with no rscale limit. This also allows 10^exp to be computed more efficiently, without any numeric multiplication, division or rounding. Discussion: https://postgr.es/m/CAEZATCWhojfH4whaqgUKBe8D5jNHB8ytzemL-PnRx+KCTyMXmg@mail.gmail.com
Diffstat (limited to 'src/test/regress/sql/numeric.sql')
-rw-r--r--src/test/regress/sql/numeric.sql8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql
index 2ad4f3e7387..972e3aaec49 100644
--- a/src/test/regress/sql/numeric.sql
+++ b/src/test/regress/sql/numeric.sql
@@ -798,6 +798,14 @@ SELECT '' AS to_char_34, to_char('100'::numeric, 'f"\\ool"999');
SELECT '' AS to_char_35, to_char('100'::numeric, 'f"ool\"999');
SELECT '' AS to_char_36, to_char('100'::numeric, 'f"ool\\"999');
+-- Test scientific notation with various exponents
+WITH v(exp) AS
+ (VALUES(-16379),(-16378),(-1234),(-789),(-45),(-5),(-4),(-3),(-2),(-1),(0),
+ (1),(2),(3),(4),(5),(38),(275),(2345),(45678),(131070),(131071))
+SELECT exp,
+ to_char(('1.2345e'||exp)::numeric, '9.999EEEE') as numeric
+FROM v;
+
-- TO_NUMBER()
--
SET lc_numeric = 'C';