# simple division

25 messages
12
Open this post in threaded view
|

## simple division

 I have asked this question before and apologize for not remembering it.  How do you do simple division in postgres and get 10/4 with decimals?   This involves cast and numeric in odd ways that are not well explained in the documentation. For instance, you’d expect an example in the Mathematical Functions. But there isn’t.   The documentation of string functions is exemplary. The documentation of mathematical less so. Remember that it may be used by folks like me whose math is shaky. The MySQL documentation is better on this simple operation.     ----- Martin Mueller Professor emeritus of English and Classics Northwestern University
Open this post in threaded view
|

## Re: simple division

 On 12/4/18 12:29 PM, Martin Mueller wrote: I have asked this question before and apologize for not remembering it.  How do you do simple division in postgres and get 10/4 with decimals?   This involves cast and numeric in odd ways that are not well explained in the documentation. For instance, you’d expect an example in the Mathematical Functions. But there isn’t.   The documentation of string functions is exemplary. The documentation of mathematical less so. Remember that it may be used by folks like me whose math is shaky. The MySQL documentation is better on this simple operation. I may be misunderstanding the question but: select cast(x/y as numeric(10,4)); JD     ----- Martin Mueller Professor emeritus of English and Classics Northwestern University     ```-- Command Prompt, Inc. || http://the.postgres.company/ || @cmdpromptinc *** A fault and talent of mine is to tell it exactly how it is. *** PostgreSQL centered full stack support, consulting and development. Advocate: @amplifypostgres || Learn: https://postgresconf.org ***** Unless otherwise stated, opinions are my own. *****```
Open this post in threaded view
|

## RE: simple division

 In reply to this post by Martin Mueller     From: Martin Mueller <[hidden email]> Sent: Tuesday, December 4, 2018 3:30 PM To: pgsql-general <[hidden email]> Subject: simple division   I have asked this question before and apologize for not remembering it.  How do you do simple division in postgres and get 10/4 with decimals?   This involves cast and numeric in odd ways that are not well explained in the documentation. For instance, you’d expect an example in the Mathematical Functions. But there isn’t.   The documentation of string functions is exemplary. The documentation of mathematical less so. Remember that it may be used by folks like me whose math is shaky. The MySQL documentation is better on this simple operation.     ----- Martin Mueller Professor emeritus of English and Classics Northwestern University   There is nothing odd about:   select (12345678.1234/32.5678)::numeric(10,4);   Regards, Igor Neyman
Open this post in threaded view
|

## Re: simple division

 In reply to this post by Martin Mueller Martin Mueller schrieb am 04.12.2018 um 21:29: > I have asked this question before and apologize for not remembering > it.  How do you do simple division in postgres and get 10/4 with > decimals? In the expression 10/4 both numbers are integers. And an integer divsion does not yield decimals (that's the same as in every strongly typed programming language). I am not entirely sure what the SQL standard says about such an expression, but e.g. SQL Server, SQLite, Firebird and DB2 behave the same as Postgres. That is they apply integer division if all values are integers, and decimal division if at least one value is a decimal. To get a division of decimals you need to specify at least one value as a decimal, e.g. "select 10.0/4" or "select 10/4.0" whatever you prefer.
Open this post in threaded view
|

## Re: simple division

 In reply to this post by Martin Mueller On Tue, Dec 4, 2018 at 1:29 PM Martin Mueller <[hidden email]> wrote: > I have asked this question before and apologize for not remembering it.  How do you do simple division in postgres and get 10/4 with decimals? > This involves cast and numeric in odd ways that are not well explained in the documentation. For instance, you’d expect an example in the Mathematical Functions. But there isn’t. select 10/4, 10.0/4, 10/4.0, 10.0/4.0; The first one returns 2, the rest of them 2.5 - from which one can infer that if both inputs are integer (type) the output is integer (type) - if at least one input is non-integer (type) the output will be as well. If you want to cast...select 10/(4::numeric)... David J.
Open this post in threaded view
|

## Re: simple division

 In reply to this post by Joshua D. Drake On Tue, Dec 4, 2018 at 1:38 PM Joshua D. Drake <[hidden email]> wrote: > I may be misunderstanding the question but: Indeed... > select cast(x/y as numeric(10,4)); Your answer is 2.0 instead of the correct 2.5 - you need to cast before the division, not after. David J.
Open this post in threaded view
|

## Re: simple division

 I didn't formulate my question properly, because the query went like  "select alldefects /wordcount" where alldefects and wordcount are integers.   But none of the different ways of putting the double colon seemed to work. The Postgres notation of this simple procedure is very unintuitive. I haven't been able to remember several times, and most people think of me as a person with a reasonably good memory. There is no obvious place in the documentation to look this up. ﻿On 12/4/18, 2:45 PM, "David G. Johnston" <[hidden email]> wrote:     On Tue, Dec 4, 2018 at 1:38 PM Joshua D. Drake <[hidden email]> wrote:     > I may be misunderstanding the question but:     Indeed...     > select cast(x/y as numeric(10,4));         Your answer is 2.0 instead of the correct 2.5 - you need to cast     before the division, not after.         David J.
Open this post in threaded view
|

## Re: simple division

 On Tue, Dec 4, 2018 at 1:57 PM Martin Mueller <[hidden email]> wrote: > > I didn't formulate my question properly, because the query went like >  "select alldefects /wordcount" > where alldefects and wordcount are integers.   But none of the different ways of putting the double colon seemed to work. IDK...the first thing that came to mind was to just stick it at the end of the expression: select x/y::numeric from (values (10,4)) vals (x,y) And it worked... If you want to propose a concrete documentation patch more power to you but this doesn't come up enough to think that what we have is materially deficient.  I'm sorry you are having trouble with it but the lists do provide quick and customized answers for situations like this. David J.
Open this post in threaded view
|

## Re: simple division

 In reply to this post by Martin Mueller Use CAST() instead of ::. SELECT CAST(alldefects AS NUMEREIC(10,4))/wordcount; On 12/04/2018 02:57 PM, Martin Mueller wrote: > I didn't formulate my question properly, because the query went like >   "select alldefects /wordcount" > where alldefects and wordcount are integers.   But none of the different ways of putting the double colon seemed to work. > > The Postgres notation of this simple procedure is very unintuitive. I haven't been able to remember several times, and most people think of me as a person with a reasonably good memory. There is no obvious place in the documentation to look this up. > > > ﻿On 12/4/18, 2:45 PM, "David G. Johnston" <[hidden email]> wrote: > >      On Tue, Dec 4, 2018 at 1:38 PM Joshua D. Drake <[hidden email]> wrote: >      > I may be misunderstanding the question but: >      Indeed... >      > select cast(x/y as numeric(10,4)); >       >      Your answer is 2.0 instead of the correct 2.5 - you need to cast >      before the division, not after. >       >      David J. >       > -- Angular momentum makes the world go 'round.
Open this post in threaded view
|

## Re: simple division

 In reply to this post by Martin Mueller Am 04.12.18 21:57 schrieb(en) Martin Mueller: > I didn't formulate my question properly, because the query went like >  "select alldefects /wordcount" > where alldefects and wordcount are integers. test=# create table xxx(alldefects bigint, wordcount bigint); CREATE TABLE test=# insert into xxx values (4, 10); INSERT 0 1 test=# insert into xxx values (3, 17); INSERT 0 1 test=# select alldefects::real / wordcount::real from xxx;   ?column? ----------        0.4   0.176471 (2 rows) Hth, Albrecht. attachment0 (499 bytes) Download Attachment
Open this post in threaded view
|

## Re: simple division

 In reply to this post by David G Johnston It worked, and I must have done something wrong. I'm probably not the only person who would find something like the following helpful: division (integer division truncates the result) 10/3 3 division (with decimal results) 10/3::numeric 3.3333 division (rounded) round(10/3::numeric, 2) 3.33 From an end user's the question "how do I divide two integers and limit the number of decimals" is surely a common one. And  if you look it up somewhere, division is probably the search word.  Now you could argue that the user should already know about formatting and rounding.  But some don't. If you wanted to look up a rare wordform in a famous early 20th century dictionary of Old English, you had to know the root form of the word. If you already knew the root form, there is a good chance that you didn't need to look it up in the first place. If you didn't know the root form, the dictionary was no use. In this, single stop shopping for the three most common problems of simple division makes life easier for users. ﻿On 12/4/18, 3:06 PM, "David G. Johnston" <[hidden email]> wrote:     On Tue, Dec 4, 2018 at 1:57 PM Martin Mueller     <[hidden email]> wrote:     >     > I didn't formulate my question properly, because the query went like     >  "select alldefects /wordcount"     > where alldefects and wordcount are integers.   But none of the different ways of putting the double colon seemed to work.         IDK...the first thing that came to mind was to just stick it at the     end of the expression:         select x/y::numeric from (values (10,4)) vals (x,y)         And it worked...         If you want to propose a concrete documentation patch more power to     you but this doesn't come up enough to think that what we have is     materially deficient.  I'm sorry you are having trouble with it but     the lists do provide quick and customized answers for situations like     this.         David J.
Open this post in threaded view
|

## Re: simple division

 In reply to this post by Martin Mueller Martin Mueller schrieb am 04.12.2018 um 21:57: > I didn't formulate my question properly, because the query went like >   "select alldefects /wordcount" > where alldefects and wordcount are integers.   > But none of the different ways of putting the double colon seemed to > work. One way is to make one of the integers a decimal by multiplying with 1.0     select alldefects * 1.0 / wordcount > The Postgres notation of this simple procedure is very unintuitive. I > haven't been able to remember several times, and most people think of > me as a person with a reasonably good memory. Postgres supports the SQL standard's CAST operator:     select cast(alldefects as decimal) / wordcount The "Postgres way" would be:     select alldefects::decimal / wordcount > There is no obvious place in the documentation to look this up. This is covered in the chapter "Type Casts" https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS
Open this post in threaded view
|

## Re: simple division

 In reply to this post by Martin Mueller On 12/4/18 2:36 PM, Martin Mueller wrote: > It worked, and I must have done something wrong. I'm probably not the only person who would find something like the following helpful: > > > division (integer division truncates the result) 10/3 3 The math types might take offense here, with the use of "truncates".  Integer division really ask how many times can one subtract the numerator from the denominator without going negative (or how many times does the numerator "go into" the denominator). It may seem a nuisance, but int division is a useful construct and must be supported (and be the default). (If you have 10 people to transport in cars which hold four (all can drive) 10/4 = 3 ;) )
Open this post in threaded view
|

## Re: simple division

 On 05/12/2018 10:51, Rob Sargent wrote: > > On 12/4/18 2:36 PM, Martin Mueller wrote: >> It worked, and I must have done something wrong. I'm probably not the >> only person who would find something like the following helpful: >> >> >> division (integer division truncates the result)    10/3    3 > > The math types might take offense here, with the use of "truncates".  > Integer division really ask how many times can one subtract the > numerator from the denominator without going negative (or how many > times does the numerator "go into" the denominator). > > It may seem a nuisance, but int division is a useful construct and > must be supported (and be the default). (If you have 10 people to > transport in cars which hold four (all can drive) 10/4 = 3 ;) ) > > > > Hmm... 10 / 4 = 2
Open this post in threaded view
|

## Re: simple division

 > On Dec 4, 2018, at 9:33 PM, Gavin Flower <[hidden email]> wrote: > >> On 05/12/2018 10:51, Rob Sargent wrote: >> >>> On 12/4/18 2:36 PM, Martin Mueller wrote: >>> It worked, and I must have done something wrong. I'm probably not the only person who would find something like the following helpful: >>> >>> >>> division (integer division truncates the result)    10/3    3 >> >> The math types might take offense here, with the use of "truncates".  Integer division really ask how many times can one subtract the numerator from the denominator without going negative (or how many times does the numerator "go into" the denominator). >> >> It may seem a nuisance, but int division is a useful construct and must be supported (and be the default). (If you have 10 people to transport in cars which hold four (all can drive) 10/4 = 3 ;) ) >> >> >> >> > Hmm... > > 10 / 4 = 2 > And two are left stranded!  The point is that integer math has its place. You cant have 2.5 cars. So 10/4 in this context is 3. More correctly the calculation is 10/4 + 10%4>0 ? 1 :0 = 3 (Maybe psql does have % so mod(10,4))
Open this post in threaded view
|

## Re: simple division

 On 05/12/2018 20:07, Rob Sargent wrote: > >> On Dec 4, 2018, at 9:33 PM, Gavin Flower <[hidden email]> wrote: >> >>> On 05/12/2018 10:51, Rob Sargent wrote: >>> >>>> On 12/4/18 2:36 PM, Martin Mueller wrote: >>>> It worked, and I must have done something wrong. I'm probably not the only person who would find something like the following helpful: >>>> >>>> >>>> division (integer division truncates the result)    10/3    3 >>> The math types might take offense here, with the use of "truncates".  Integer division really ask how many times can one subtract the numerator from the denominator without going negative (or how many times does the numerator "go into" the denominator). >>> >>> It may seem a nuisance, but int division is a useful construct and must be supported (and be the default). (If you have 10 people to transport in cars which hold four (all can drive) 10/4 = 3 ;) ) >>> >>> >>> >>> >> Hmm... >> >> 10 / 4 = 2 >> > And two are left stranded! >   The point is that integer math has its place. You cant have 2.5 cars. So 10/4 in this context is 3. > More correctly the calculation is > 10/4 + 10%4>0 ? 1 :0 = 3 > > (Maybe psql does have % so mod(10,4)) > > SELECT ceil(10/4.0); Is what you want for that example.
Open this post in threaded view
|

## Re: simple division

 On Wed, 5 Dec 2018 at 09:13, Gavin Flower <[hidden email]> wrote: > SELECT ceil(10/4.0); > > Is what you want for that example. Except that implies that "number of people who can fit in a car" is a real number, not a whole. 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). Geoff
Open this post in threaded view
|

## Re: simple division

 On 06/12/2018 00:05, Geoff Winkless wrote: > On Wed, 5 Dec 2018 at 09:13, Gavin Flower <[hidden email]> wrote: >> SELECT ceil(10/4.0); >> >> Is what you want for that example. > Except that implies that "number of people who can fit in a car" is a > real number, not a whole. > > 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). > > Geoff > If you divide one integer by another, then it is logical to get an integer as as the answer.
Open this post in threaded view
|

## Re: simple division

 On Wed, 5 Dec 2018 at 12:45, Gavin Flower <[hidden email]> wrote: > If you divide one integer by another, then it is logical to get an > integer as as the answer. Hmm. It might fit with what a computer scientist might expect (or rather, not be surprised about), but I don't think you can say that it's "logical". Where's the logical progression in step 3 here: 1 You asked the computer a question 2 The values you passed to it don't have decimal points ... 4 Ergo, you wanted an answer that was incorrect. Geoff