Commit 4fb6aeb4 authored by Tom Lane's avatar Tom Lane

Make floating-point "NaN / 0" return NaN instead of raising an error.

This is more consistent with the IEEE 754 spec and our treatment of
NaNs elsewhere; in particular, the case has always acted that way in
"numeric" arithmetic.

Noted by Dean Rasheed.

Discussion: https://postgr.es/m/3421746.1594927785@sss.pgh.pa.us
parent 6ca7cd89
...@@ -222,7 +222,7 @@ float4_div(const float4 val1, const float4 val2) ...@@ -222,7 +222,7 @@ float4_div(const float4 val1, const float4 val2)
{ {
float4 result; float4 result;
if (unlikely(val2 == 0.0f)) if (unlikely(val2 == 0.0f) && !isnan(val1))
float_zero_divide_error(); float_zero_divide_error();
result = val1 / val2; result = val1 / val2;
if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2)) if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
...@@ -238,7 +238,7 @@ float8_div(const float8 val1, const float8 val2) ...@@ -238,7 +238,7 @@ float8_div(const float8 val1, const float8 val2)
{ {
float8 result; float8 result;
if (unlikely(val2 == 0.0)) if (unlikely(val2 == 0.0) && !isnan(val1))
float_zero_divide_error(); float_zero_divide_error();
result = val1 / val2; result = val1 / val2;
if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2)) if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
......
...@@ -143,6 +143,12 @@ SELECT 'nan'::float4 / 'nan'::float4; ...@@ -143,6 +143,12 @@ SELECT 'nan'::float4 / 'nan'::float4;
NaN NaN
(1 row) (1 row)
SELECT 'nan'::float4 / '0'::float4;
?column?
----------
NaN
(1 row)
SELECT 'nan'::numeric::float4; SELECT 'nan'::numeric::float4;
float4 float4
-------- --------
......
...@@ -143,6 +143,12 @@ SELECT 'nan'::float4 / 'nan'::float4; ...@@ -143,6 +143,12 @@ SELECT 'nan'::float4 / 'nan'::float4;
NaN NaN
(1 row) (1 row)
SELECT 'nan'::float4 / '0'::float4;
?column?
----------
NaN
(1 row)
SELECT 'nan'::numeric::float4; SELECT 'nan'::numeric::float4;
float4 float4
-------- --------
......
...@@ -126,6 +126,12 @@ SELECT 'nan'::float8 / 'nan'::float8; ...@@ -126,6 +126,12 @@ SELECT 'nan'::float8 / 'nan'::float8;
NaN NaN
(1 row) (1 row)
SELECT 'nan'::float8 / '0'::float8;
?column?
----------
NaN
(1 row)
SELECT 'nan'::numeric::float8; SELECT 'nan'::numeric::float8;
float8 float8
-------- --------
......
...@@ -50,6 +50,7 @@ SELECT ' INFINITY x'::float4; ...@@ -50,6 +50,7 @@ SELECT ' INFINITY x'::float4;
SELECT 'Infinity'::float4 + 100.0; SELECT 'Infinity'::float4 + 100.0;
SELECT 'Infinity'::float4 / 'Infinity'::float4; SELECT 'Infinity'::float4 / 'Infinity'::float4;
SELECT 'nan'::float4 / 'nan'::float4; SELECT 'nan'::float4 / 'nan'::float4;
SELECT 'nan'::float4 / '0'::float4;
SELECT 'nan'::numeric::float4; SELECT 'nan'::numeric::float4;
SELECT '' AS five, * FROM FLOAT4_TBL; SELECT '' AS five, * FROM FLOAT4_TBL;
......
...@@ -43,6 +43,7 @@ SELECT ' INFINITY x'::float8; ...@@ -43,6 +43,7 @@ SELECT ' INFINITY x'::float8;
SELECT 'Infinity'::float8 + 100.0; SELECT 'Infinity'::float8 + 100.0;
SELECT 'Infinity'::float8 / 'Infinity'::float8; SELECT 'Infinity'::float8 / 'Infinity'::float8;
SELECT 'nan'::float8 / 'nan'::float8; SELECT 'nan'::float8 / 'nan'::float8;
SELECT 'nan'::float8 / '0'::float8;
SELECT 'nan'::numeric::float8; SELECT 'nan'::numeric::float8;
SELECT '' AS five, * FROM FLOAT8_TBL; SELECT '' AS five, * FROM FLOAT8_TBL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment