如何在 PostgreSQL 中限制一个浮点数

How to clamp a float in PostgreSQL

我有一个数字 1.00000001,我想将它限制在 -1 和 1 之间以避免 输入超出范围错误 ACOS() 函数。 MCVE 看起来像这样:

SELECT ACOS( 1 + 0.0000000001 );

我的理想是这样的:

SELECT ACOS( CLAMP(1 + 0.0000000001, -1, 1) );  

我找到的解决方案是:

SELECT ACOS(GREATEST(-1, LEAST(1, 1 + 0.0000000001));
-- example: clamp(subject, min, max)
CREATE FUNCTION clamp(integer, integer, integer) RETURNS integer
    AS 'select GREATEST(, LEAST(, ));'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;

-- example: clamp_above(subject, max)
CREATE FUNCTION clamp_above(integer, integer) RETURNS integer
    AS 'select LEAST(, );'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;

-- example: clamp_below(subject, min)
CREATE FUNCTION clamp_below(integer, integer) RETURNS integer
    AS 'select GREATEST(, );'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;