如何修复 Postgres 9.1 中的输入超出范围错误
How to fix input is out of range error in Postgres 9.1
在 Postgres 9.1 中,以下代码会产生错误
ERROR: input is out of range
CONTEXT: SQL function "gc_dist1" statement 1
如何在 9.1 中修复它?
create or replace function gc_dist1(_lat1 float8, _lon1 float8, _lat2 float8, _lon2 float8)
returns float8 as $$
select ACOS(SIN(radians())*SIN(radians())+COS(radians())*COS(radians())*COS(radians()-radians()))*6371;
$$ language sql immutable;
select gc_dist1(24.6269989013672,59.3357849157094,24.6269989013672,59.3357849121094);
如果失败,因为 acos()
函数不能接受大于 1 的值。
在你的例子中就是这种情况。
SELECT (
SIN(radians(24.6269989013672))
* SIN(radians(24.6269989013672))
+ COS(radians(24.6269989013672))
* COS(radians(24.6269989013672))
* COS(radians(59.3357849121094)-radians(59.3357849157094))
) > 1 -- true
看来你的功能逻辑不对
在 Postgres 9.1 中,以下代码会产生错误
ERROR: input is out of range
CONTEXT: SQL function "gc_dist1" statement 1
如何在 9.1 中修复它?
create or replace function gc_dist1(_lat1 float8, _lon1 float8, _lat2 float8, _lon2 float8)
returns float8 as $$
select ACOS(SIN(radians())*SIN(radians())+COS(radians())*COS(radians())*COS(radians()-radians()))*6371;
$$ language sql immutable;
select gc_dist1(24.6269989013672,59.3357849157094,24.6269989013672,59.3357849121094);
如果失败,因为 acos()
函数不能接受大于 1 的值。
在你的例子中就是这种情况。
SELECT (
SIN(radians(24.6269989013672))
* SIN(radians(24.6269989013672))
+ COS(radians(24.6269989013672))
* COS(radians(24.6269989013672))
* COS(radians(59.3357849121094)-radians(59.3357849157094))
) > 1 -- true
看来你的功能逻辑不对