Re: Re: PostgreSQL needs percentage function

From: "Peter J(dot) Holzer" <hjp-pgsql(at)hjp(dot)at>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Re: PostgreSQL needs percentage function
Date: 2017-12-18 19:30:23
Message-ID: 20171218193023.r42hiywc3cegedwy@hjp.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2017-12-18 18:13:58 +0200, Nick Dro wrote:
> I know how to implement this. It's not the issue.
> It's very easy to implement absolute value as well yet still PostgreSQL gives
> abs(x) function which is build in function.
> My claim is that if there is a build in function for absolute value why not for
> percentage? Both are very basic mathematical operations.

Apart from historic reasons, abs(x) is actually quite awkward to
write inline: You need to write x 3 times:

case when x >= 0 then x else -x end

Which is already quite a bit longer than abs(x), but it gets worse when
x is a complicated expression:

abs((graph_width + graph_border_width) / pixels_per_character)
becomes
case
when ((graph_width + graph_border_width) / pixels_per_character) >= 0
then ((graph_width + graph_border_width) / pixels_per_character)
else -((graph_width + graph_border_width) / pixels_per_character)
end

And the expression is evaluated twice, which is a problem if is contains
a function call with a side effekt. abs(random()*3-1) is not the same as
case random()*3-1 >= 0 then random()*3-1 else -(random()*3-1) end.

So while primitive, abs() is actually useful as a function.

percent(a, b) isn't even shorter than (a * b / 100), and it doesn't get
better for more complicated expressions. It' just less readable (does
percent(a, b) mean (a * b / 100) or (a / b * 100) or (b / a * 100)? Is
there even a factor of 100 involved? (Is should be, because that's what
"cent" means, but it's missing in percent_rank()). If such a function
existed, I would avoid it.

hp

--
_ | Peter J. Holzer | we build much bigger, better disasters now
|_|_) | | because we have much more sophisticated
| | | hjp(at)hjp(dot)at | management tools.
__/ | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter J. Holzer 2017-12-18 19:35:54 Re: Re: PostgreSQL needs percentage function
Previous Message Alvaro Herrera 2017-12-18 19:19:58 Re: reclaiming space from heavily used tables?