pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
power() function in Windows: "value out of range: underflow"
15+ messages / 4 participants
[nested] [flat]

Commits from this thread:
  2018-05-02 f74d83b303 8fbdd63a52 eab8d6312f 70211459a5 99095b6320  Revert back-branch changes in power()'s behavior for NaN inputs.
  2018-04-29 59c2df3ae8 d9c7f56da4 d6ec3d2375 6bdf1303b3 217d8f3a19 9e4caa2748  Avoid wrong results for power() with NaN input on more platforms.
  2018-04-29 4d864de486 61b200e2f5 534267e7f3 48e0f8cbe0 44ccd11cbb 3b7fba935e  Avoid wrong results for power() with NaN input on some platforms.
* power() function in Windows: "value out of range: underflow"
@ 2018-04-10 08:30 Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-10 14:04 ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  0 siblings, 2 replies; 15+ messages in thread

From: Huong Dangminh @ 2018-04-10 08:30 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; +Cc: Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

Hi,

There are some cases that power() function does not work 
correctly with 'NaN' arguments in Windows environment.
Something like,

postgres=# select power('NaN',11);
ERROR:  value out of range: underflow
postgres=# select power('NaN','NaN');
ERROR:  value out of range: underflow
postgres=# select power(11,'NaN');
ERROR:  value out of range: underflow

In Linux environment, instead of ERROR it returns 'NaN'.

The reason here is,
When pow() in float.c:dpow() is called with 'NaN' arguments,
pow() returns 'NaN' but in Windows environment errno is set to 
EDOM(invalid floating-point exception).
So, PostgreSQL update "result" and cause an ERROR in CHECKFLOATVAL macro.

I think it should be return 'NaN' in all of above cases.
I have tried to create a patch to fix it.
Please confirm the attached file.


---
Thanks and best regards,
Dang Minh Huong
NEC Solution Innovators, Ltd.
http://www.nec-solutioninnovators.co.jp/en/

Attachments:

power_NaN.PATCH (application/octet-stream, 1.6K) download diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 6522c08..99725bf 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -1573,7 +1573,7 @@ dpow(PG_FUNCTION_ARGS) */ errno = 0; result = pow(arg1, arg2); - if (errno == EDOM && isnan(result)) + if (errno == EDOM && isnan(result) && !(isnan(arg1) || isnan(arg2))) { if ((fabs(arg1) > 1 && arg2 >= 0) || (fabs(arg1) < 1 && arg2 < 0)) /* The sign of Inf is not significant in this case. */ diff --git a/src/test/regress/expected/float8.out b/src/test/regress/expected/float8.out index 20c985e..6d7cdc3 100644 --- a/src/test/regress/expected/float8.out +++ b/src/test/regress/expected/float8.out @@ -340,6 +340,24 @@ SELECT power(float8 '144', float8 '0.5'); 12 (1 row) +SELECT power(float8 'NaN', float8 '0.5'); + power +------- + NaN +(1 row) + +SELECT power(float8 '144', float8 'NaN'); + power +------- + NaN +(1 row) + +SELECT power(float8 'NaN', float8 'NaN'); + power +------- + NaN +(1 row) + -- take exp of ln(f.f1) SELECT '' AS three, f.f1, exp(ln(f.f1)) AS exp_ln_f1 FROM FLOAT8_TBL f diff --git a/src/test/regress/sql/float8.sql b/src/test/regress/sql/float8.sql index 215e7a4..dca90f7 100644 --- a/src/test/regress/sql/float8.sql +++ b/src/test/regress/sql/float8.sql @@ -108,6 +108,9 @@ SELECT '' AS three, f.f1, |/f.f1 AS sqrt_f1 -- power SELECT power(float8 '144', float8 '0.5'); +SELECT power(float8 'NaN', float8 '0.5'); +SELECT power(float8 '144', float8 'NaN'); +SELECT power(float8 'NaN', float8 'NaN'); -- take exp of ln(f.f1) SELECT '' AS three, f.f1, exp(ln(f.f1)) AS exp_ln_f1
^ permalink raw reply [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
@ 2018-04-10 14:04 ` Euler Taveira <euler@timbira.com.br>
  2018-04-10 23:46   ` RE: power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  1 sibling, 1 reply; 15+ messages in thread

From: Euler Taveira @ 2018-04-10 14:04 UTC (permalink / raw)
  To: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

2018-04-10 5:30 GMT-03:00 Huong Dangminh <huo-dangminh@ys.jp.nec.com>:
> There are some cases that power() function does not work
> correctly with 'NaN' arguments in Windows environment.
> Something like,
>
What is your exact OS version? What is your postgres version? I tested
with Windows 10 (10.0.16299) x64 and couldn't reproduce the error.

> postgres=# select power('NaN',11);
> ERROR:  value out of range: underflow
> postgres=# select power('NaN','NaN');
> ERROR:  value out of range: underflow
> postgres=# select power(11,'NaN');
> ERROR:  value out of range: underflow
>
> In Linux environment, instead of ERROR it returns 'NaN'.
>
Could you show us a simple test case? Print arguments, result and errno.


-- 
   Euler Taveira                                   Timbira -
http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* RE: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-10 14:04 ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
@ 2018-04-10 23:46   ` Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  0 siblings, 0 replies; 15+ messages in thread

From: Huong Dangminh @ 2018-04-10 23:46 UTC (permalink / raw)
  To: Euler Taveira <euler@timbira.com.br>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

Hi,
Thanks for response

# I will add this thread to current CF soon.

> 2018-04-10 5:30 GMT-03:00 Huong Dangminh <huo-dangminh@ys.jp.nec.com>:
> > There are some cases that power() function does not work correctly
> > with 'NaN' arguments in Windows environment.
> > Something like,
> >
> What is your exact OS version? What is your postgres version? I tested with
> Windows 10 (10.0.16299) x64 and couldn't reproduce the error.

I think it is not depended on OS version but the visual c++ runtime libraries version 
(It seem also be included in installers).

My environment:
====================================

- PostgreSQL: 9.6.8
  # I am using the EDB PostgreSQL Installer
  # It also can reproduce in PostgreSQL 10.3 or 9.5.12

  >postgres -V
  postgres (PostgreSQL) 9.6.8

  >psql -c "select version()"
                             version
  -------------------------------------------------------------
   PostgreSQL 9.6.8, compiled by Visual C++ build 1800, 64-bit
  (1 row)

  >psql -c "select 'NaN'^11"
  ERROR:  value out of range: underflow

- OS: Microsoft Windows 10 Pro 10.0.14393 build 14393

=============================

> > postgres=# select power('NaN',11);
> > ERROR:  value out of range: underflow
> > postgres=# select power('NaN','NaN');
> > ERROR:  value out of range: underflow
> > postgres=# select power(11,'NaN');
> > ERROR:  value out of range: underflow
> >
> > In Linux environment, instead of ERROR it returns 'NaN'.
> >
> Could you show us a simple test case? Print arguments, result and errno.

It just is my confirmation when debug PostgreSQL in Windows environment.
# I built PostgreSQL with VC++ 2012 in debug mode and confirmed.
I don't know how to print those values.


---
Thanks and best regards,
Dang Minh Huong
NEC Solution Innovators, Ltd.
http://www.nec-solutioninnovators.co.jp/en/


^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
@ 2018-04-11 03:13 ` David Rowley <david.rowley@2ndquadrant.com>
  2018-04-11 04:10   ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
  2018-04-29 21:19   ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 2 replies; 15+ messages in thread

From: David Rowley @ 2018-04-11 03:13 UTC (permalink / raw)
  To: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

On 10 April 2018 at 20:30, Huong Dangminh <huo-dangminh@ys.jp.nec.com> wrote:
> Hi,
>
> There are some cases that power() function does not work
> correctly with 'NaN' arguments in Windows environment.
> Something like,
>
> postgres=# select power('NaN',11);
> ERROR:  value out of range: underflow
> postgres=# select power('NaN','NaN');
> ERROR:  value out of range: underflow
> postgres=# select power(11,'NaN');
> ERROR:  value out of range: underflow
>
> In Linux environment, instead of ERROR it returns 'NaN'.
>
> The reason here is,
> When pow() in float.c:dpow() is called with 'NaN' arguments,
> pow() returns 'NaN' but in Windows environment errno is set to
> EDOM(invalid floating-point exception).
> So, PostgreSQL update "result" and cause an ERROR in CHECKFLOATVAL macro.

I can recreate this when building with MSVC 2012. I confirm that I see
the same as you. Microsoft are setting errno to EDOM in the above 3
cases, where in Linux the result is still NaN, just the errno is not
set.

Looking at [1], it says:

* If x or y is a NaN, a NaN shall be returned (unless specified
elsewhere in this description).

* For any value of y (including NaN), if x is +1, 1.0 shall be returned.

* For any value of x (including NaN), if y is ±0, 1.0 shall be returned.

Which Microsoft seem to not have broken, they're just also setting the
errno to EDOM in the first case, which seems a little strange, but the
standard there does not seem to mention that it shouldn't do that.

You patch fixes the problem for me, and importantly the two following
cases still work:

postgres=# select power(1,'NaN');
 power
-------
     1
(1 row)


postgres=# select power('NaN', 0);
 power
-------
     1
(1 row)


There's no mention in the SQL standard about NaN handling in the above
two cases, but I wonder if it's worth also adding a couple of tests
for the above two cases to ensure all platforms are doing the same
thing... ?

I'd also rather see this line:

if (errno == EDOM && isnan(result) && !(isnan(arg1) || isnan(arg2)))

written as:

if (errno == EDOM && isnan(result) && !isnan(arg1) && !isnan(arg2))

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/pow.html

-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
@ 2018-04-11 04:10   ` Euler Taveira <euler@timbira.com.br>
  2018-04-11 04:42     ` RE: power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  1 sibling, 1 reply; 15+ messages in thread

From: Euler Taveira @ 2018-04-11 04:10 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

2018-04-11 0:13 GMT-03:00 David Rowley <david.rowley@2ndquadrant.com>:
> I can recreate this when building with MSVC 2012. I confirm that I see
> the same as you. Microsoft are setting errno to EDOM in the above 3
> cases, where in Linux the result is still NaN, just the errno is not
> set.
>
FWIW, I tested in MSVC 2017 (15.6.4) and it worked like expected.
Looking at [1], it seems there could be nasty bugs when using math
functions in MSVC < 2013 (or 2015). I don't have some older MSVC here
to try /fp:OPTIONHERE [2] to see if it makes any difference.


[1] https://docs.microsoft.com/en-us/cpp/porting/floating-point-migration-issues
[2] https://msdn.microsoft.com/pt-br/library/e7s85ffb.aspx


-- 
   Euler Taveira                                   Timbira -
http://www.timbira.com.br/
   PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* RE: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-11 04:10   ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
@ 2018-04-11 04:42     ` Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 04:47       ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-29 19:24       ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 15+ messages in thread

From: Huong Dangminh @ 2018-04-11 04:42 UTC (permalink / raw)
  To: Euler Taveira <euler@timbira.com.br>; David Rowley <david.rowley@2ndquadrant.com>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

Thanks for confirming.
 
> 2018-04-11 0:13 GMT-03:00 David Rowley <david.rowley@2ndquadrant.com>:
> > I can recreate this when building with MSVC 2012. I confirm that I see
> > the same as you. Microsoft are setting errno to EDOM in the above 3
> > cases, where in Linux the result is still NaN, just the errno is not
> > set.
> >
> FWIW, I tested in MSVC 2017 (15.6.4) and it worked like expected.
> Looking at [1], it seems there could be nasty bugs when using math functions
> in MSVC < 2013 (or 2015). I don't have some older MSVC here to try
> /fp:OPTIONHERE [2] to see if it makes any difference.

Thanks.
Anyway currently PostgreSQL installers seem not be compiled with MSVC 2017, 
so it should be fix for current users?
I updated the patch as David Rowley mentioned.


---
Thanks and best regards,
Dang Minh Huong
NEC Solution Innovators, Ltd.
http://www.nec-solutioninnovators.co.jp/en/



Attachments:

dpow_NaN_V2.patch (application/octet-stream, 2.2K) download diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 6522c08..1483d9f 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -1570,10 +1570,14 @@ dpow(PG_FUNCTION_ARGS) * using something different from our floor() test to decide it's * invalid). Other platforms (HPPA) return errno == ERANGE and a large * (HUGE_VAL) but finite result to signal overflow. + * + * Note that some versions of Visual Studio set errno to EDOM when + * arg1 or arg2 is a NaN. + * So we need to except those cases here. */ errno = 0; result = pow(arg1, arg2); - if (errno == EDOM && isnan(result)) + if (errno == EDOM && isnan(result) && !isnan(arg1) && !isnan(arg2)) { if ((fabs(arg1) > 1 && arg2 >= 0) || (fabs(arg1) < 1 && arg2 < 0)) /* The sign of Inf is not significant in this case. */ diff --git a/src/test/regress/expected/float8.out b/src/test/regress/expected/float8.out index 20c985e..ede9aaf 100644 --- a/src/test/regress/expected/float8.out +++ b/src/test/regress/expected/float8.out @@ -340,6 +340,36 @@ SELECT power(float8 '144', float8 '0.5'); 12 (1 row) +SELECT power(float8 'NaN', float8 '0.5'); + power +------- + NaN +(1 row) + +SELECT power(float8 '144', float8 'NaN'); + power +------- + NaN +(1 row) + +SELECT power(float8 'NaN', float8 'NaN'); + power +------- + NaN +(1 row) + +SELECT power(float8 '1',float8 'NaN'); + power +------- + 1 +(1 row) + +SELECT power(float8 'NaN',float8 '0'); + power +------- + 1 +(1 row) + -- take exp of ln(f.f1) SELECT '' AS three, f.f1, exp(ln(f.f1)) AS exp_ln_f1 FROM FLOAT8_TBL f diff --git a/src/test/regress/sql/float8.sql b/src/test/regress/sql/float8.sql index 215e7a4..9d1d607 100644 --- a/src/test/regress/sql/float8.sql +++ b/src/test/regress/sql/float8.sql @@ -108,6 +108,11 @@ SELECT '' AS three, f.f1, |/f.f1 AS sqrt_f1 -- power SELECT power(float8 '144', float8 '0.5'); +SELECT power(float8 'NaN', float8 '0.5'); +SELECT power(float8 '144', float8 'NaN'); +SELECT power(float8 'NaN', float8 'NaN'); +SELECT power(float8 '1',float8 'NaN'); +SELECT power(float8 'NaN',float8 '0'); -- take exp of ln(f.f1) SELECT '' AS three, f.f1, exp(ln(f.f1)) AS exp_ln_f1
^ permalink raw reply [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-11 04:10   ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
  2018-04-11 04:42     ` RE: power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
@ 2018-04-11 04:47       ` David Rowley <david.rowley@2ndquadrant.com>
  2018-04-11 04:54         ` RE: power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  1 sibling, 1 reply; 15+ messages in thread

From: David Rowley @ 2018-04-11 04:47 UTC (permalink / raw)
  To: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; +Cc: Euler Taveira <euler@timbira.com.br>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

On 11 April 2018 at 16:42, Huong Dangminh <huo-dangminh@ys.jp.nec.com> wrote:
> I updated the patch as David Rowley mentioned.

Looks fine to me. Please add to the next commitfest.

-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* RE: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-11 04:10   ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
  2018-04-11 04:42     ` RE: power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 04:47       ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
@ 2018-04-11 04:54         ` Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  0 siblings, 0 replies; 15+ messages in thread

From: Huong Dangminh @ 2018-04-11 04:54 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Euler Taveira <euler@timbira.com.br>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

> > I updated the patch as David Rowley mentioned.
> 
> Looks fine to me. Please add to the next commitfest.

Thanks. Added.

> --
>  David Rowley                   http://www.2ndQuadrant.com/
>  PostgreSQL Development, 24x7 Support, Training & Services
> 

---
Thanks and best regards,
Dang Minh Huong
NEC Solution Innovators, Ltd.
http://www.nec-solutioninnovators.co.jp/en/



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-11 04:10   ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
  2018-04-11 04:42     ` RE: power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
@ 2018-04-29 19:24       ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-04-29 21:59         ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  1 sibling, 1 reply; 15+ messages in thread

From: Tom Lane @ 2018-04-29 19:24 UTC (permalink / raw)
  To: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; +Cc: Euler Taveira <euler@timbira.com.br>; David Rowley <david.rowley@2ndquadrant.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

Huong Dangminh <huo-dangminh@ys.jp.nec.com> writes:
>> 2018-04-11 0:13 GMT-03:00 David Rowley <david.rowley@2ndquadrant.com>:
>>> I can recreate this when building with MSVC 2012. I confirm that I see
>>> the same as you. Microsoft are setting errno to EDOM in the above 3
>>> cases, where in Linux the result is still NaN, just the errno is not
>>> set.

> I updated the patch as David Rowley mentioned.

Pushed.  I'd mainly note that you need to update all the variant float8
expected-files, not just the primary one.  (Sometimes this requires a
bit of guesswork, but here we're expecting all platforms to produce
the same result.  The buildfarm should tell us if I got it wrong.)

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-11 04:10   ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
  2018-04-11 04:42     ` RE: power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-29 19:24       ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-04-29 21:59         ` David Rowley <david.rowley@2ndquadrant.com>
  2018-04-29 22:10           ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 15+ messages in thread

From: David Rowley @ 2018-04-29 21:59 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; Euler Taveira <euler@timbira.com.br>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

On 30 April 2018 at 07:24, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Pushed.  I'd mainly note that you need to update all the variant float8
> expected-files, not just the primary one.  (Sometimes this requires a
> bit of guesswork, but here we're expecting all platforms to produce
> the same result.  The buildfarm should tell us if I got it wrong.)

gaur does not seem happy with this. I get the impression that pow(1,
NaN) and pow(NaN, 0) on that machine must be returning NaN and setting
errno to EDOM, and now that we're only using that code path when both
are are non-NaN we no longer hit the special case which does result =
1;

I wonder if it's better just to hard code these two cases before even
calling the pow() function.

-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-11 04:10   ` Re: power() function in Windows: "value out of range: underflow" Euler Taveira <euler@timbira.com.br>
  2018-04-11 04:42     ` RE: power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-29 19:24       ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
  2018-04-29 21:59         ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
@ 2018-04-29 22:10           ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 15+ messages in thread

From: Tom Lane @ 2018-04-29 22:10 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; Euler Taveira <euler@timbira.com.br>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

David Rowley <david.rowley@2ndquadrant.com> writes:
> I wonder if it's better just to hard code these two cases before even
> calling the pow() function.

Yeah, see my followup --- I also found out that SUSv2 (POSIX 1997)
doesn't require either of these special cases, which helps explain
why the inconsistency on older platforms.

Hacking on a patch now.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
@ 2018-04-29 21:19   ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-04-29 22:10     ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  1 sibling, 1 reply; 15+ messages in thread

From: Tom Lane @ 2018-04-29 21:19 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

David Rowley <david.rowley@2ndquadrant.com> writes:
> You patch fixes the problem for me, and importantly the two following
> cases still work:

> postgres=# select power(1,'NaN');
>  power
> -------
>      1
> (1 row)

> postgres=# select power('NaN', 0);
>  power
> -------
>      1
> (1 row)

> There's no mention in the SQL standard about NaN handling in the above
> two cases, but I wonder if it's worth also adding a couple of tests
> for the above two cases to ensure all platforms are doing the same
> thing... ?

So the early returns from the buildfarm say "no, they aren't".  It looks
like older BSDen know the NaN^0 = 1 rule, but they return NaN for 1^NaN.
Aside from the failure buildfarm member nightjar is showing, I've
confirmed this behavior on a NetBSD 5.2 installation I had laying around,
and there's also evidence in the related man page:

http://netbsd.gw.com/cgi-bin/man-cgi?pow+3+NetBSD-5.2.3

which goes to great lengths to justify NaN^0 = 1 while saying nothing
to suggest that 1^NaN might not yield NaN.

I'm not sure if we should add more special-case code for that, or just
remove that test case again.  Historically we've not really felt that it
was our job to mask oddities of the platform's math library, so the fact
that power() is worrying about this seems like unjustified scope creep.
On the other hand, we do have a bunch of special cases there already,
so refusing to handle older BSD would be a tad inconsistent.

And, while we're on the subject ... some of my machines give

postgres=# select power(-1,'NaN');
ERROR:  a negative number raised to a non-integer power yields a complex result

POSIX says this should return NaN, not an error (and my old NetBSD
installation does that, as does numeric_power).  Should we change it?

I'm a bit inclined to handle all the NaN-input cases with explicit
code before we do anything that relies on libc's behavior.

BTW, numeric_power just emits NaN for NaN input, without either of
these special cases.  But that's platform-independent, so I'm hesitant
to consider back-patching a change there; if we change that, I'd vote
for doing so only in HEAD.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-29 21:19   ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-04-29 22:10     ` David Rowley <david.rowley@2ndquadrant.com>
  2018-04-29 22:22       ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 15+ messages in thread

From: David Rowley @ 2018-04-29 22:10 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

On 30 April 2018 at 09:19, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> http://netbsd.gw.com/cgi-bin/man-cgi?pow+3+NetBSD-5.2.3
>
> which goes to great lengths to justify NaN^0 = 1 while saying nothing
> to suggest that 1^NaN might not yield NaN.
>
> I'm not sure if we should add more special-case code for that, or just
> remove that test case again.  Historically we've not really felt that it
> was our job to mask oddities of the platform's math library, so the fact
> that power() is worrying about this seems like unjustified scope creep.
> On the other hand, we do have a bunch of special cases there already,
> so refusing to handle older BSD would be a tad inconsistent.

(Sorry missed your reply before I sent my last one)

Wouldn't this machine have returned 1 before this patch though? Surely
changing this behaviour this plaetform in favour of fixing on another
is worse than doing nothing. If that's the case, I think the only
option is to add a special case or revert this and document that the
behaviour may vary depending on the platform's implementation of
pow(). I think the special case is worth it, since there's already
some for the error cases.


-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-29 21:19   ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
  2018-04-29 22:10     ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
@ 2018-04-29 22:22       ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-04-29 23:24         ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  0 siblings, 1 reply; 15+ messages in thread

From: Tom Lane @ 2018-04-29 22:22 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

David Rowley <david.rowley@2ndquadrant.com> writes:
> Wouldn't this machine have returned 1 before this patch though?

No, don't think so, because it doesn't set EDOM for the case.

Basically what we're doing here is making sure that we get results
conforming to current POSIX even on machines that predate that
standard.  There are more of them floating around than I'd have
expected, but it still seems like a good change to make.  Maybe
there's an argument for not back-patching, though?

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: power() function in Windows: "value out of range: underflow"
  2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
  2018-04-11 03:13 ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-29 21:19   ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
  2018-04-29 22:10     ` Re: power() function in Windows: "value out of range: underflow" David Rowley <david.rowley@2ndquadrant.com>
  2018-04-29 22:22       ` Re: power() function in Windows: "value out of range: underflow" Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-04-29 23:24         ` David Rowley <david.rowley@2ndquadrant.com>
  0 siblings, 0 replies; 15+ messages in thread

From: David Rowley @ 2018-04-29 23:24 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Huong Dangminh <huo-dangminh@ys.jp.nec.com>; Pg Hackers <pgsql-hackers@postgresql.org>; Akio Iwaasa <aki-iwaasa@vt.jp.nec.com>

On 30 April 2018 at 10:22, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> David Rowley <david.rowley@2ndquadrant.com> writes:
>> Wouldn't this machine have returned 1 before this patch though?
>
> No, don't think so, because it doesn't set EDOM for the case.
>
> Basically what we're doing here is making sure that we get results
> conforming to current POSIX even on machines that predate that
> standard.  There are more of them floating around than I'd have
> expected, but it still seems like a good change to make.  Maybe
> there's an argument for not back-patching, though?

I think we should back patch and try to be consistent about the
power(float8 1.0, 'NaN') and power('NaN', float8 0.0) cases. The
archives don't show any complaints about power() with NaN until this
one, so I imagine the number of people affected by this is small.
However, I think if we're willing to try to make MSVC consistent with
other platforms on this topic then there's no reason to draw the line
there and ignore other platforms that we claim to support.

POSIX seems like a good standard to follow for this in the absence of
guidance from the SQL standard.

-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services




^ permalink  raw  reply  [nested|flat] 15+ messages in thread


end of thread, other threads:[~2018-04-29 23:24 UTC | newest]

Thread overview: 15+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-04-10 08:30 power() function in Windows: "value out of range: underflow" Huong Dangminh <huo-dangminh@ys.jp.nec.com>
2018-04-10 14:04 ` Euler Taveira <euler@timbira.com.br>
2018-04-10 23:46   ` Huong Dangminh <huo-dangminh@ys.jp.nec.com>
2018-04-11 03:13 ` David Rowley <david.rowley@2ndquadrant.com>
2018-04-11 04:10   ` Euler Taveira <euler@timbira.com.br>
2018-04-11 04:42     ` Huong Dangminh <huo-dangminh@ys.jp.nec.com>
2018-04-11 04:47       ` David Rowley <david.rowley@2ndquadrant.com>
2018-04-11 04:54         ` Huong Dangminh <huo-dangminh@ys.jp.nec.com>
2018-04-29 19:24       ` Tom Lane <tgl@sss.pgh.pa.us>
2018-04-29 21:59         ` David Rowley <david.rowley@2ndquadrant.com>
2018-04-29 22:10           ` Tom Lane <tgl@sss.pgh.pa.us>
2018-04-29 21:19   ` Tom Lane <tgl@sss.pgh.pa.us>
2018-04-29 22:10     ` David Rowley <david.rowley@2ndquadrant.com>
2018-04-29 22:22       ` Tom Lane <tgl@sss.pgh.pa.us>
2018-04-29 23:24         ` David Rowley <david.rowley@2ndquadrant.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox