Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUZ0u-000099-IV for pgsql-general@arkaria.postgresql.org; Wed, 05 Dec 2018 15:23:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUZ0t-0004Co-9p for pgsql-general@arkaria.postgresql.org; Wed, 05 Dec 2018 15:22:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUZ0s-0004Ch-UG for pgsql-general@lists.postgresql.org; Wed, 05 Dec 2018 15:22:59 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUZ0q-0007SH-5w for pgsql-general@lists.postgresql.org; Wed, 05 Dec 2018 15:22:57 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id wB5FMn5g031282; Wed, 5 Dec 2018 10:22:49 -0500 From: Tom Lane To: Geoff Winkless cc: pgsql-general@lists.postgresql.org Subject: Re: simple division In-reply-to: References: <4DEACFC2-E8E3-4C61-805B-56A5F76CB819@northwestern.edu> <8B35EF9C-8DEF-4298-A646-89D1AC8F63E1@northwestern.edu> <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> Comments: In-reply-to Geoff Winkless message dated "Wed, 05 Dec 2018 11:05:49 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31280.1544023369.1@sss.pgh.pa.us> Date: Wed, 05 Dec 2018 10:22:49 -0500 Message-ID: <31281.1544023369@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Geoff Winkless 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 ) 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