Re: Allow round() function to accept float and double precision
Dean Rasheed <dean.a.rasheed@gmail.com>
From: Dean Rasheed <dean.a.rasheed@gmail.com>
To: David Rowley <dgrowleyml@gmail.com>
Cc: "David G. Johnston" <david.g.johnston@gmail.com>,
Tom Lane <tgl@sss.pgh.pa.us>, Sayyid Ali Sajjad Rizavi <sasrizavi@gmail.com>,
pgsql-hackers@lists.postgresql.org
Date: 2022-12-01T08:55:33Z
Lists: pgsql-hackers
On Thu, 1 Dec 2022 at 02:58, David Rowley <dgrowleyml@gmail.com> wrote: > > On Thu, 1 Dec 2022 at 15:41, David G. Johnston > <david.g.johnston@gmail.com> wrote: > > I don't get the point of adding a function here (or at least one called round) - the type itself is inexact so, as you say, it is actually more of a type conversion with an ability to specify precision, which is exactly what you get today when you write 1.48373::numeric(20,3) - though it is a bit annoying having to specify an arbitrary precision. > > An additional problem with that which you might have missed is that > you'd need to know what to specify in the precision part of the > typemod. You might start getting errors one day if you don't select a > value large enough. That problem does not exist with round(). Having > to specify 131072 each time does not sound like a great solution, it's > not exactly a very memorable number. > I don't really see the point of such a function either. Casting to numeric(1000, n) will work fine in all cases AFAICS (1000 being the maximum allowed precision in a numeric typemod, and somewhat more memorable). Note that double precision numbers range in magnitude from something like 2.2e-308 to 1.8e308, so you won't ever get an error (except, I suppose, if you also chose "n" larger than 692 or so, but that would be silly, given the input). > > At present round does allow you to specify a negative position to round at positions to the left of the decimal point (this is undocumented though...) which the actual cast cannot do, but that seems like a marginal case. Note that, as of PG15, "n" can be negative in such typemods, if you want to round before the decimal point. The fact that passing a negative scale to round() isn't documented does seem like an oversight though... Regards, Dean
Commits
-
Doc: add example of round(v, s) with negative s.
- e10799aa252c 15.2 landed
- a475157e6bf0 13.10 landed
- a109a0bb50ad 16.0 landed
- 7ec2bfe9e031 14.7 landed