pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #18390: exponentiation produces float datatype, but nth-root produces integer
3+ messages / 3 participants
[nested] [flat]

* BUG #18390: exponentiation produces float datatype, but nth-root produces integer
@ 2024-03-13 02:57 PG Bug reporting form <noreply@postgresql.org>
  2024-03-13 12:07 ` Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: PG Bug reporting form @ 2024-03-13 02:57 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: robb@helical.com

The following bug has been logged on the website:

Bug reference:      18390
Logged by:          Robb Allan
Email address:      robb@helical.com
PostgreSQL version: 15.5
Operating system:   Mac OS Sonoma 14.3.1 (23D60)
Description:        

An exponentiation from a float produces a floating point result:

funds=# select (1.1^10);
      ?column?      
--------------------
 2.5937424601000000
(1 row)

However, an nth-root calculation from that float result does NOT produce a
new float result:

funds=# select (2.5937424601000000^(1/10));
      ?column?      
--------------------
 1.0000000000000000
(1 row)


Only when the root factors are floats does the result coerce to a float:

funds=# select (2.5937424601000000^(1.0/10));
        ?column?        
------------------------
 1.10000000000000000000
(1 row)

funds=# select (2.5937424601000000^(1/10.0));
        ?column?        
------------------------
 1.10000000000000000000
(1 row)



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

* Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer
  2024-03-13 02:57 BUG #18390: exponentiation produces float datatype, but nth-root produces integer PG Bug reporting form <noreply@postgresql.org>
@ 2024-03-13 12:07 ` David G. Johnston <david.g.johnston@gmail.com>
  2024-03-13 14:12   ` Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: David G. Johnston @ 2024-03-13 12:07 UTC (permalink / raw)
  To: robb@helical.com <robb@helical.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

On Tuesday, March 12, 2024, PG Bug reporting form <noreply@postgresql.org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference:      18390
> Logged by:          Robb Allan
> Email address:      robb@helical.com
> PostgreSQL version: 15.5
> Operating system:   Mac OS Sonoma 14.3.1 (23D60)
> Description:
>
> However, an nth-root calculation from that float result does NOT produce a
> new float result:
>
> funds=# select (2.5937424601000000^(1/10));
>       ?column?
> --------------------
>  1.0000000000000000
> (1 row)
>
>
1.000000000000…. Is a float.

The issue is you expect dividing two integers to produce a float but that
isn’t how that works.  Dividing two integers produces an integer.  In this
case zero, or .1 rounded down/truncated.

David J.

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

* Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer
  2024-03-13 02:57 BUG #18390: exponentiation produces float datatype, but nth-root produces integer PG Bug reporting form <noreply@postgresql.org>
  2024-03-13 12:07 ` Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer David G. Johnston <david.g.johnston@gmail.com>
@ 2024-03-13 14:12   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2024-03-13 14:12 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: robb@helical.com <robb@helical.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Tuesday, March 12, 2024, PG Bug reporting form <noreply@postgresql.org>
> wrote:
>> However, an nth-root calculation from that float result does NOT produce a
>> new float result:

> The issue is you expect dividing two integers to produce a float but that
> isn’t how that works.  Dividing two integers produces an integer.  In this
> case zero, or .1 rounded down/truncated.

Possibly adding to the OP's confusion: not one of these examples
contains any float arithmetic whatsoever.  They're of type numeric.

=# select pg_typeof(2.5937424601000000^(1.0/10));
 pg_typeof 
-----------
 numeric
(1 row)

See
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS-NUMERIC
which says

    A numeric constant that contains neither a decimal point nor an
    exponent is initially presumed to be type integer if its value
    fits in type integer (32 bits); otherwise it is presumed to be
    type bigint if its value fits in type bigint (64 bits); otherwise
    it is taken to be type numeric. Constants that contain decimal
    points and/or exponents are always initially presumed to be type
    numeric.

A cast to float would occur only if the value is fed to an operator
that doesn't come in an exactly matching datatype.

			regards, tom lane





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


end of thread, other threads:[~2024-03-13 14:12 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-03-13 02:57 BUG #18390: exponentiation produces float datatype, but nth-root produces integer PG Bug reporting form <noreply@postgresql.org>
2024-03-13 12:07 ` David G. Johnston <david.g.johnston@gmail.com>
2024-03-13 14:12   ` 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