pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: robb@helical.com <robb@helical.com>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer
Date: Wed, 13 Mar 2024 10:12:10 -0400
Message-ID: <2043864.1710339130@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwYyJT+HFcOqGA5nawKA4yOnMdbGdqRkKbBLZ5KWFzcg7Q@mail.gmail.com>
References: <18390-03499fe4f5a0ac71@postgresql.org>
	<CAKFQuwYyJT+HFcOqGA5nawKA4yOnMdbGdqRkKbBLZ5KWFzcg7Q@mail.gmail.com>

"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





view thread (3+ messages)

Message-ID: <2043864.1710339130@sss.pgh.pa.us>
Permalink:  ../2043864.1710339130@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2043864.1710339130@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com, robb@helical.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #18390: exponentiation produces float datatype, but nth-root produces integer
  In-Reply-To: <2043864.1710339130@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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