Re: Rounding Double Precision or Numeric

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Steve Atkins <steve(at)blighty(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Rounding Double Precision or Numeric
Date: 2017-06-01 16:58:31
Message-ID: CAOR=d=378D2nH4=10s0wgPRxk8yHfqDGcWC4gBkaWwtiv=YoGQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jun 1, 2017 at 10:42 AM, Steve Atkins <steve(at)blighty(dot)com> wrote:
>
>> On Jun 1, 2017, at 9:26 AM, Louis Battuello <louis(dot)battuello(at)etasseo(dot)com> wrote:
>>
>> Is the round() function implemented differently for double precision than for numeric? Forgive me if this exists somewhere in the documentation, but I can't seem to find it.
>
> https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL
>
> "When rounding values, the numeric type rounds ties away from zero, while (on most machines) the real and double precision types round ties to the nearest even number.".
>
>> Why does the algorithm vary by data type?
>
> Just guessing, but I'd assume because the NUMERIC type behaves as required by the SQL spec, while float and double are vanilla IEEE754 arithmetic and will do whatever the underlying hardware is configured to do, usually round to nearest even.
>

Exactly. It's important to remember that floats and doubles are
imprecise representations meant for speed, while numeric is designed
for accuracy not speed.

If one needs an exact answer, one does not use floats.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Karl Czajkowski 2017-06-01 17:24:46 Re: jsonb case insensitive search
Previous Message Steve Atkins 2017-06-01 16:42:19 Re: Rounding Double Precision or Numeric