pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #17546: power() function - value is distorted via automatic type cast
4+ messages / 3 participants
[nested] [flat]

* BUG #17546: power() function - value is distorted via automatic type cast
@ 2022-07-11 15:20  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 4+ messages in thread

From: PG Bug reporting form @ 2022-07-11 15:20 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: su.sinodan@gmail.com

The following bug has been logged on the website:

Bug reference:      17546
Logged by:          Su Sinodan
Email address:      su.sinodan@gmail.com
PostgreSQL version: 14.2
Operating system:   Windows Server 2016 (10.0.14393 Build 1439)
Description:        

When a certain number (p, s) with a fractional part (specific examples -
1.11 and 69.96) is multiplied by a power (10, N), the resulting value is
distorted.
However, if the power(10, N) is manually converted to an integer, the
resulting value is correct.
It looks like there is some kind of problem with the automatic type
conversion of the power() function.

Test query:
with w_degree as (
                   select 0::integer as degree union all
                   select 1::integer as degree union all
                   select 2::integer as degree union all
                   select 3::integer as degree union all
                   select 4::integer as degree union all
                   select 5::integer as degree union all
                   select 6::integer as degree union all
                   select 7::integer as degree union all
                   select 8::integer as degree union all
                   select 9::integer as degree
                 )
select
  power(10, w.degree::integer)                                  as
"power()"
, 1.11::numeric(20, 5) * power(10, w.degree::integer)           as " 1.11 *
power()"
, 1.11::numeric(20, 5) * power(10, w.degree::integer)::integer  as " 1.11 *
power()::integer"
, 69.96::numeric(20, 5) * power(10, w.degree::integer)          as "69.96 *
power()"
, 69.96::numeric(20, 5) * power(10, w.degree::integer)::integer as "69.96 *
power()::integer"
from w_degree w;

Result:
  power()   |   1.11 * power()   |  1.11 * power()::integer |  69.96 *
power()  | 69.96 * power()::integer
------------+--------------------+--------------------------+-------------------+--------------------------
          1 |               1.11 |                  1.11000 |            
69.96 |                 69.96000
         10 | 11.100000000000001 |                 11.10000 |
699.5999999999999 |                699.60000
        100 | 111.00000000000001 |                111.00000 |
6995.999999999999 |               6996.00000
       1000 |               1110 |               1110.00000 |            
69960 |              69960.00000
      10000 | 11100.000000000002 |              11100.00000 |
699599.9999999999 |             699600.00000
     100000 | 111000.00000000001 |             111000.00000 |
6995999.999999999 |            6996000.00000
    1000000 |            1110000 |            1110000.00000 |         
69960000 |           69960000.00000
   10000000 | 11100000.000000002 |           11100000.00000 |
699599999.9999999 |          699600000.00000
  100000000 | 111000000.00000001 |          111000000.00000 |
6995999999.999999 |         6996000000.00000
 1000000000 |         1110000000 |         1110000000.00000 |      
69960000000 |        69960000000.00000
(10 rows)



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

* Re: BUG #17546: power() function - value is distorted via automatic type cast
@ 2022-07-11 15:49  Tom Lane <tgl@sss.pgh.pa.us>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2022-07-11 15:49 UTC (permalink / raw)
  To: su.sinodan@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org

PG Bug reporting form <noreply@postgresql.org> writes:
> When a certain number (p, s) with a fractional part (specific examples -
> 1.11 and 69.96) is multiplied by a power (10, N), the resulting value is
> distorted.
> However, if the power(10, N) is manually converted to an integer, the
> resulting value is correct.
> It looks like there is some kind of problem with the automatic type
> conversion of the power() function.

Your query is invoking the float8 variant of power(), which is
necessarily of limited precision.  If I change "w.degree::integer"
to "w.degree::numeric", then it invokes the numeric variant of
power(), and I get correctly rounded results (much more slowly
unfortunately :-().  So I don't see any bug here.

It might be surprising that the parser prefers float8 over numeric
when the given function arguments don't exactly match either one.
But that's a very ancient decision that we're not going to change.
There is support for it in the SQL standard, which directs
implementations to prefer inexact numeric types over exact ones
when they have to make a choice.

			regards, tom lane





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

* Re: BUG #17546: power() function - value is distorted via automatic type cast
@ 2022-07-11 16:04  Антон Потапов <su.sinodan@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Антон Потапов @ 2022-07-11 16:04 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

<div>Is there any chance that you will reflect this in the documentation?</div><div>Thank you.</div><div> </div><div>11.07.2022, 18:49, "Tom Lane" &lt;tgl@sss.pgh.pa.us&gt;:</div><blockquote><p>PG Bug reporting form &lt;<a href="mailto:noreply@postgresql.org" rel="noopener noreferrer">noreply@postgresql.org</a>&gt; writes:</p><blockquote> When a certain number (p, s) with a fractional part (specific examples -<br /> 1.11 and 69.96) is multiplied by a power (10, N), the resulting value is<br /> distorted.<br /> However, if the power(10, N) is manually converted to an integer, the<br /> resulting value is correct.<br /> It looks like there is some kind of problem with the automatic type<br /> conversion of the power() function.</blockquote><p><br />Your query is invoking the float8 variant of power(), which is<br />necessarily of limited precision. If I change "w.degree::integer"<br />to "w.degree::numeric", then it invokes the numeric variant of<br />power(), and I get correctly rounded results (much more slowly<br />unfortunately :-(). So I don't see any bug here.<br /><br />It might be surprising that the parser prefers float8 over numeric<br />when the given function arguments don't exactly match either one.<br />But that's a very ancient decision that we're not going to change.<br />There is support for it in the SQL standard, which directs<br />implementations to prefer inexact numeric types over exact ones<br />when they have to make a choice.<br /><br />                        regards, tom lane</p></blockquote>

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

* Re: BUG #17546: power() function - value is distorted via automatic type cast
@ 2022-07-11 18:56  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Антон Потапов <su.sinodan@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2022-07-11 18:56 UTC (permalink / raw)
  To: Антон Потапов <su.sinodan@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

=?utf-8?B?0JDQvdGC0L7QvSDQn9C+0YLQsNC/0L7Qsg==?= <su.sinodan@gmail.com> writes:
> Is there any chance that you will reflect this in the documentation?

It is documented --- see under
	https://www.postgresql.org/docs/current/datatype-numeric.html
and
	https://www.postgresql.org/docs/current/typeconv.html
(particularly example 10.3, which points out that float8 is
considered a preferred type).  We are not going to repeat all that
material under every single affected function, if that's what
you're asking for.

			regards, tom lane





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


end of thread, other threads:[~2022-07-11 18:56 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-07-11 15:20 BUG #17546: power() function - value is distorted via automatic type cast PG Bug reporting form <noreply@postgresql.org>
2022-07-11 15:49 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-07-11 16:04   ` Антон Потапов <su.sinodan@gmail.com>
2022-07-11 18:56     ` Tom Lane <tgl@sss.pgh.pa.us>

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