Thread: BUG #18390: exponentiation produces float datatype, but nth-root produces integer
BUG #18390: exponentiation produces float datatype, but nth-root produces integer
From
PG Bug reporting form
Date:
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)
Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer
From
"David G. Johnston"
Date:
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.
Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer
From
Tom Lane
Date:
"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