pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Geoff Winkless <pgsqladmin@geoff.dj>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: simple division
Date: Wed, 05 Dec 2018 10:22:49 -0500
Message-ID: <31281.1544023369@sss.pgh.pa.us> (raw)
In-Reply-To: <CAEzk6ffEqKAcyin0OG5FoW=ayP0Pz8O2EFi8gKjv7qcSLQ7nVg@mail.gmail.com>
References: <4DEACFC2-E8E3-4C61-805B-56A5F76CB819@northwestern.edu>
	<dd63a926-6711-b795-6485-5029b9556213@commandprompt.com>
	<CAKFQuwZXO4WJqEbmWUtnCvDhRfVqYWbFx7bQ8D-Q-mxPRqEKgg@mail.gmail.com>
	<8B35EF9C-8DEF-4298-A646-89D1AC8F63E1@northwestern.edu>
	<CAKFQuwZ-OXBEXjLa0i_1SwyiiiEwMcfuhZyC3cpe2aKZRpcJag@mail.gmail.com>
	<1F5FCEFC-7DEA-46FA-9895-FBF7DFDEC750@northwestern.edu>
	<627868ba-0d4b-8995-d29a-db6b7ca3673d@gmail.com>
	<611dee86-118f-df30-a942-2441fc04668f@archidevsys.co.nz>
	<59110313-9F31-4D7F-BAAC-4223EE751CC7@gmail.com>
	<fda12258-5bac-7094-39e7-4ea55c8854fb@archidevsys.co.nz>
	<CAEzk6ffEqKAcyin0OG5FoW=ayP0Pz8O2EFi8gKjv7qcSLQ7nVg@mail.gmail.com>

Geoff Winkless <pgsqladmin@geoff.dj> writes:
> IMO it's fundamentally broken that SQL doesn't cast the result of a
> divide into a numeric value - the potential for unexpected errors
> creeping into calculations is huge; however that's the standard and
> no-one's going to change it now.
> Having said that it's worth noting that those in the Other Place think
> that it's broken enough to go against the standard (they have a DIV b
> for integer divide and a/b for float).

Well, this isn't really blame-able on the SQL standard; it's a
Postgres-ism.  What the spec says (in SQL99, 6.26 <numeric value
expression>) is

         1) If the declared type of both operands of a dyadic arithmetic
            operator is exact numeric, then the declared type of the
            result is exact numeric, with precision and scale determined
            as follows:

            a) Let S1 and S2 be the scale of the first and second operands
              respectively.

            b) The precision of the result of addition and subtraction is
              implementation-defined, and the scale is the maximum of S1
              and S2.

            c) The precision of the result of multiplication is
              implementation-defined, and the scale is S1 + S2.

            d) The precision and scale of the result of division is
              implementation-defined.

         2) If the declared type of either operand of a dyadic arithmetic
            operator is approximate numeric, then the declared type of the
            result is approximate numeric. The precision of the result is
            implementation-defined.

Postgres' integer types map onto the standard as exact numerics with
scale 0.  (The precision aspect is a bit squishy, since their maximum
values aren't powers of 10, but let's disregard that.)  Postgres'
integer division operator meets the spec with the stipulation that
the "implementation-defined" scale of the result is 0.  Other SQL
implementations can and do define that differently --- if they even
have an "integer" data type, which some do not.

Anyway, the bottom line here is that we're balancing surprise factor
for novices against twenty-plus years of backwards compatibility,
and the latter is going to win.

			regards, tom lane




view thread (25+ messages)  latest in thread

Message-ID: <31281.1544023369@sss.pgh.pa.us>
Permalink:  ../31281.1544023369@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/31281.1544023369@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 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: tgl@sss.pgh.pa.us, pgsqladmin@geoff.dj
  Subject: Re: simple division
  In-Reply-To: <31281.1544023369@sss.pgh.pa.us>
  References: <4DEACFC2-E8E3-4C61-805B-56A5F76CB819@northwestern.edu> <dd63a926-6711-b795-6485-5029b9556213@commandprompt.com> <CAKFQuwZXO4WJqEbmWUtnCvDhRfVqYWbFx7bQ8D-Q-mxPRqEKgg@mail.gmail.com> <8B35EF9C-8DEF-4298-A646-89D1AC8F63E1@northwestern.edu> <CAKFQuwZ-OXBEXjLa0i_1SwyiiiEwMcfuhZyC3cpe2aKZRpcJag@mail.gmail.com> <1F5FCEFC-7DEA-46FA-9895-FBF7DFDEC750@northwestern.edu> <627868ba-0d4b-8995-d29a-db6b7ca3673d@gmail.com> <611dee86-118f-df30-a942-2441fc04668f@archidevsys.co.nz> <59110313-9F31-4D7F-BAAC-4223EE751CC7@gmail.com> <fda12258-5bac-7094-39e7-4ea55c8854fb@archidevsys.co.nz> <CAEzk6ffEqKAcyin0OG5FoW=ayP0Pz8O2EFi8gKjv7qcSLQ7nVg@mail.gmail.com> <31281.1544023369@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