Skip to content

Commit fac83db

Browse files
committed
Remove underflow error in float division with infinite divisor.
float4_div and float8_div correctly produced zero for zero divided by infinity, but threw an underflow error for nonzero finite values divided by infinity. This seems wrong; at the very least it's inconsistent with the behavior recently implemented for numeric infinities. Remove the error and allow zero to be returned. This patch also removes a useless isinf() test from the overflow checks in these functions (non-Inf divided by Inf can't produce Inf). Extracted from a larger patch; this seems significant outside the context of geometric operators, so it deserves its own commit. Kyotaro Horiguchi Discussion: https://postgr.es/m/CAGf+fX70rWFOk5cd00uMfa__0yP+vtQg5ck7c2Onb-Yczp0URA@mail.gmail.com
1 parent 9e38c2b commit fac83db

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

src/include/utils/float.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ float4_div(const float4 val1, const float4 val2)
225225
if (unlikely(val2 == 0.0f) && !isnan(val1))
226226
float_zero_divide_error();
227227
result = val1 / val2;
228-
if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
228+
if (unlikely(isinf(result)) && !isinf(val1))
229229
float_overflow_error();
230-
if (unlikely(result == 0.0f) && val1 != 0.0f)
230+
if (unlikely(result == 0.0f) && val1 != 0.0f && !isinf(val2))
231231
float_underflow_error();
232232

233233
return result;
@@ -241,9 +241,9 @@ float8_div(const float8 val1, const float8 val2)
241241
if (unlikely(val2 == 0.0) && !isnan(val1))
242242
float_zero_divide_error();
243243
result = val1 / val2;
244-
if (unlikely(isinf(result)) && !isinf(val1) && !isinf(val2))
244+
if (unlikely(isinf(result)) && !isinf(val1))
245245
float_overflow_error();
246-
if (unlikely(result == 0.0) && val1 != 0.0)
246+
if (unlikely(result == 0.0) && val1 != 0.0 && !isinf(val2))
247247
float_underflow_error();
248248

249249
return result;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ SELECT 'Infinity'::float4 / 'Infinity'::float4;
137137
NaN
138138
(1 row)
139139

140+
SELECT '42'::float4 / 'Infinity'::float4;
141+
?column?
142+
----------
143+
0
144+
(1 row)
145+
140146
SELECT 'nan'::float4 / 'nan'::float4;
141147
?column?
142148
----------

src/test/regress/expected/float4.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ SELECT 'Infinity'::float4 / 'Infinity'::float4;
137137
NaN
138138
(1 row)
139139

140+
SELECT '42'::float4 / 'Infinity'::float4;
141+
?column?
142+
----------
143+
0
144+
(1 row)
145+
140146
SELECT 'nan'::float4 / 'nan'::float4;
141147
?column?
142148
----------

src/test/regress/expected/float8.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ SELECT 'Infinity'::float8 / 'Infinity'::float8;
120120
NaN
121121
(1 row)
122122

123+
SELECT '42'::float8 / 'Infinity'::float8;
124+
?column?
125+
----------
126+
0
127+
(1 row)
128+
123129
SELECT 'nan'::float8 / 'nan'::float8;
124130
?column?
125131
----------

src/test/regress/sql/float4.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ SELECT ' INFINITY x'::float4;
4949

5050
SELECT 'Infinity'::float4 + 100.0;
5151
SELECT 'Infinity'::float4 / 'Infinity'::float4;
52+
SELECT '42'::float4 / 'Infinity'::float4;
5253
SELECT 'nan'::float4 / 'nan'::float4;
5354
SELECT 'nan'::float4 / '0'::float4;
5455
SELECT 'nan'::numeric::float4;

src/test/regress/sql/float8.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ SELECT ' INFINITY x'::float8;
4242

4343
SELECT 'Infinity'::float8 + 100.0;
4444
SELECT 'Infinity'::float8 / 'Infinity'::float8;
45+
SELECT '42'::float8 / 'Infinity'::float8;
4546
SELECT 'nan'::float8 / 'nan'::float8;
4647
SELECT 'nan'::float8 / '0'::float8;
4748
SELECT 'nan'::numeric::float8;

0 commit comments

Comments
 (0)