Search Postgresql Archives

Re: Re: PostgreSQL needs percentage function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxx         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/>

Attachment: signature.asc
Description: PGP signature


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux