pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Exponentiation confusion
Date: Thu, 13 Oct 2022 19:05:27 +0200
Message-ID: <20221013170527.3k3yeh6t2sjdlueg@hjp.at> (raw)
In-Reply-To: <12a40226-70ac-3a3b-3d3a-fdaf9e32d312@aklaver.com>
References: <12a40226-70ac-3a3b-3d3a-fdaf9e32d312@aklaver.com>

On 2022-10-13 09:20:51 -0700, Adrian Klaver wrote:
> In trying to answer an SO question I ran across this:
> 
> Postgres version 14.5
> 
Same for 11.17. So it's been like that for some time, maybe forever.


> select power(10, -18);
>  power
> -------
>  1e-18
> (1 row)
> 
> select power(10, -18::numeric);
>        power
> --------------------
>  0.0000000000000000
> 
> 
> Why is the cast throwing off the result?

It seems that the number of decimals depends only on the first argument:

hjp=> select power(10::numeric, -2::numeric);
╔════════════════════╗
║       power        ║
╟────────────────────╢
║ 0.0100000000000000 ║
╚════════════════════╝
(1 row)
hjp=> select power(10::numeric, -16::numeric);
╔════════════════════╗
║       power        ║
╟────────────────────╢
║ 0.0000000000000001 ║
╚════════════════════╝
(1 row)
hjp=> select power(10::numeric, -18::numeric);
╔════════════════════╗
║       power        ║
╟────────────────────╢
║ 0.0000000000000000 ║
╚════════════════════╝
(1 row)

hjp=> select power(10::numeric, 18::numeric);
╔══════════════════════════════════════╗
║                power                 ║
╟──────────────────────────────────────╢
║ 1000000000000000000.0000000000000000 ║
╚══════════════════════════════════════╝
(1 row)

hjp=> select power(10::numeric(32,30), 18::numeric);
╔════════════════════════════════════════════════════╗
║                       power                        ║
╟────────────────────────────────────────────────────╢
║ 1000000000000000000.000000000000000000000000000000 ║
╚════════════════════════════════════════════════════╝
(1 row)
hjp=> select power(10::numeric(32,30), -16::numeric);
╔══════════════════════════════════╗
║              power               ║
╟──────────────────────────────────╢
║ 0.000000000000000100000000000000 ║
╚══════════════════════════════════╝
(1 row)


So the number of decimals by default isn't sufficient to represent
10^-18. You have to explicitely increase it.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (10+ messages)  latest in thread

Message-ID: <20221013170527.3k3yeh6t2sjdlueg@hjp.at>
Permalink:  ../20221013170527.3k3yeh6t2sjdlueg@hjp.at/
Also on:    postgresql.org/message-id/20221013170527.3k3yeh6t2sjdlueg@hjp.at

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hjp-pgsql@hjp.at
  Subject: Re: Exponentiation confusion
  In-Reply-To: <20221013170527.3k3yeh6t2sjdlueg@hjp.at>
  References: <12a40226-70ac-3a3b-3d3a-fdaf9e32d312@aklaver.com> <20221013170527.3k3yeh6t2sjdlueg@hjp.at>

* 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