pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andres Freund <andres@anarazel.de>
To: Kaleb Akalework <kaleb.akalework@asg.com>
Cc: Alvaro Herrera <alvherre@2ndquadrant.com>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: BUG #15812: Select statement of a very big number, with a division operator seems to round up.
Date: Fri, 17 May 2019 09:23:22 -0700
Message-ID: <20190517162322.kbhf2ult2k245t24@alap3.anarazel.de> (raw)
In-Reply-To: <982ab3c88ea14f4593cc45b26acb44f6@asg.com>
References: <15812-adda7f0f3359803c@postgresql.org>
	<20190517160211.GA27103@alvherre.pgsql>
	<982ab3c88ea14f4593cc45b26acb44f6@asg.com>

Hi,

(on postgresql lists please quote emails nicely, and trip irrelevant
pieces)

On 2019-05-17 16:10:52 +0000, Kaleb Akalework wrote:
> Thank you for getting back to me. Well that is not correct the column is defined as numeric(20,0) because we don't expect decimal numbers in the column, 
> but operations on the value should not be dictated by the column
> definition. My table has millions of rows and cannot change the table
> definition due to number of rows and business purposes. The question
> is why is the result of the operation dictated by the column
> definition?

It doesn't have to be the column division - you could just indicate the
desired precision in the divisor. I'd assume that
SELECT BIG_NUM, FLOOR(BIG_NUM/10000000000::numeric(21,10)),  BIG_NUM/10000000000::numeric(21,10) from test_table;

would give you precisely the result you waant?


> If you just did select (3691635539999999999/10000000000) you would get the correct result minus all the numbers after the decimal point
> 
> SELECT (3691635539999999999/10000000000)
> 
> "369163553"
> 
> This seems to be bug, no?

That's just because the types here assumed to be bigint (i.e. 64bit
integers):
postgres[22538][1]=# SELECT pg_typeof(3691635539999999999), pg_typeof(10000000000), pg_typeof(3691635539999999999/10000000000), 3691635539999999999/10000000000;
┌───────────┬───────────┬───────────┬───────────┐
│ pg_typeof │ pg_typeof │ pg_typeof │ ?column?  │
├───────────┼───────────┼───────────┼───────────┤
│ bigint    │ bigint    │ bigint    │ 369163553 │
└───────────┴───────────┴───────────┴───────────┘
(1 row)


Greetings,

Andres Freund





view thread (9+ messages)  latest in thread

Message-ID: <20190517162322.kbhf2ult2k245t24@alap3.anarazel.de>
Permalink:  ../20190517162322.kbhf2ult2k245t24@alap3.anarazel.de/
Also on:    postgresql.org/message-id/20190517162322.kbhf2ult2k245t24@alap3.anarazel.de

 · 

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-bugs@postgresql.org
  Cc: andres@anarazel.de, kaleb.akalework@asg.com, alvherre@2ndquadrant.com
  Subject: Re: BUG #15812: Select statement of a very big number, with a division operator seems to round up.
  In-Reply-To: <20190517162322.kbhf2ult2k245t24@alap3.anarazel.de>
  References: <15812-adda7f0f3359803c@postgresql.org> <20190517160211.GA27103@alvherre.pgsql> <982ab3c88ea14f4593cc45b26acb44f6@asg.com> <20190517162322.kbhf2ult2k245t24@alap3.anarazel.de>

* 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