postgresql 错误 - 错误 : input is out of range

标签 postgresql function

下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助?

这是错误:

ERROR:  input is out of range
CONTEXT:  PL/pgSQL function "calculate_distance" line 7 at RETURN

********** Error **********

ERROR: input is out of range
SQL state: 22003
Context: PL/pgSQL function "calculate_distance" line 7 at RETURN

函数如下:

 CREATE OR REPLACE FUNCTION calculate_distance(character varying, 
double precision, double precision, 
double precision, double precision)

      RETURNS double precision AS
    $BODY$ 
            DECLARE earth_radius double precision; 

            BEGIN 
                    earth_radius := 3959.0; 

                    RETURN earth_radius * acos(sin($2 / 57.2958) * 
sin($4 / 57.2958) + cos($2/ 57.2958) * cos($4 / 57.2958) 
* cos(($5 / 57.2958) - ($3 / 57.2958)));
            END; 
            $BODY$
      LANGUAGE 'plpgsql' VOLATILE
      COST 100;
    ALTER FUNCTION calculate_distance(character varying, 
double precision, double precision, double precision, 
double precision) OWNER TO postgres;



    //I tried changing (unsuccessfully) that RETURN line to: 

    RETURN CAST( (earth_radius * acos(sin($2 / 57.2958) 
* sin($4 / 57.2958) + cos($2/ 57.2958) * cos($4 / 57.2958) 
* cos(($5 / 57.2958) - ($3 / 57.2958))) ) AS text);

最佳答案

看看这个解决方案:

http://www.postgresql.org/message-id/12376732.post@talk.nabble.com

The problem was indeed ACOS() being outside of the [-1,1] range, and this happened because it was calculating the distance between the same LAT,LONG pair (the same location)

I added a WHERE L1.ID <> L2.ID to stop the reflexive calculation.

Martijn van Oosterhout wrote:

On Sat, Aug 18, 2007 at 03:21:02PM -0700, dustov wrote:

My database just had this new error, and I have no idea why (because I haven't intentionally made any changes to this table). Does anyone have an idea which input is out of range-- or what the problem might be?

在你的查询中,我能想象到的唯一超出范围的是 ACOS() 需要介于 -1 和 1 之间(否则结果 会很复杂)。

我会尝试看看 ACOS 的论据是什么,但它可能是 一些四舍五入的极端情况。

希望对您有所帮助,

我在计算坐标之间的距离时遇到了同样的错误,但上面的提示解决了我的问题。

关于postgresql 错误 - 错误 : input is out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2533386/

相关文章:

由 sql 填充的 Python 表单下拉选项

Python:如何将信息从 .csv 文件导入到 python 作为一个包含元组的列表?

c++枚举作为函数中的参数并在另一个文件中调用该函数

c - 使用 C 函数对两个矩阵求和会出现逻辑错误

jquery - 如果 DIV 具有特定的类,则忽略它

node.js - 省略列名/将对象直接插入 node-postgres

sql - 将数据合并为一行

node.js - 当用户输入错误的电子邮件时,为什么交易失败并已回滚

java - ‘pgp_sym_encrypt’和 ‘pgp_sym_decrypt’带有HSM(硬件安全模块)的机制

c - 以两种不同的方式在升序数组中搜索值