Skip to content

Commit 4fb6aeb

Browse files
committed
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
1 parent 6ca7cd8 commit 4fb6aeb

File tree

6 files changed

+22
-2
lines changed

6 files changed

+22
-2
lines changed

src/include/utils/float.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ float4_div(const float4 val1, const float4 val2)
222222
{
223223
float4 result;
224224

225-
if (unlikely(val2 == 0.0f))
225+
if (unlikely(val2 == 0.0f) && !isnan(val1))
226226
float_zero_divide_error();
227227
result = val1 / val2;
228228
if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
@@ -238,7 +238,7 @@ float8_div(const float8 val1, const float8 val2)
238238
{
239239
float8 result;
240240

241-
if (unlikely(val2 == 0.0))
241+
if (unlikely(val2 == 0.0) && !isnan(val1))
242242
float_zero_divide_error();
243243
result = val1 / val2;
244244
if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))

src/test/regress/expected/float4-misrounded-input.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ SELECT 'nan'::float4 / 'nan'::float4;
143143
NaN
144144
(1 row)
145145

146+
SELECT 'nan'::float4 / '0'::float4;
147+
?column?
148+
----------
149+
NaN
150+
(1 row)
151+
146152
SELECT 'nan'::numeric::float4;
147153
float4
148154
--------

src/test/regress/expected/float4.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ SELECT 'nan'::float4 / 'nan'::float4;
143143
NaN
144144
(1 row)
145145

146+
SELECT 'nan'::float4 / '0'::float4;
147+
?column?
148+
----------
149+
NaN
150+
(1 row)
151+
146152
SELECT 'nan'::numeric::float4;
147153
float4
148154
--------

src/test/regress/expected/float8.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ SELECT 'nan'::float8 / 'nan'::float8;
126126
NaN
127127
(1 row)
128128

129+
SELECT 'nan'::float8 / '0'::float8;
130+
?column?
131+
----------
132+
NaN
133+
(1 row)
134+
129135
SELECT 'nan'::numeric::float8;
130136
float8
131137
--------

src/test/regress/sql/float4.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SELECT ' INFINITY x'::float4;
5050
SELECT 'Infinity'::float4 + 100.0;
5151
SELECT 'Infinity'::float4 / 'Infinity'::float4;
5252
SELECT 'nan'::float4 / 'nan'::float4;
53+
SELECT 'nan'::float4 / '0'::float4;
5354
SELECT 'nan'::numeric::float4;
5455

5556
SELECT '' AS five, * FROM FLOAT4_TBL;

src/test/regress/sql/float8.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ SELECT ' INFINITY x'::float8;
4343
SELECT 'Infinity'::float8 + 100.0;
4444
SELECT 'Infinity'::float8 / 'Infinity'::float8;
4545
SELECT 'nan'::float8 / 'nan'::float8;
46+
SELECT 'nan'::float8 / '0'::float8;
4647
SELECT 'nan'::numeric::float8;
4748

4849
SELECT '' AS five, * FROM FLOAT8_TBL;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy