Skip to content
Advertisement

How to clamp a float in PostgreSQL

I have a number 1.00000001, and I want to clamp it between -1 and 1 to avoid input out of range error on ACOS() function. An MCVE look like this:

SELECT ACOS( 1 + 0.0000000001 );

My ideal would be something like:

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

Advertisement

Answer

The solution I found was:

SELECT ACOS(GREATEST(-1, LEAST(1, 1 + 0.0000000001));
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement