如何检查数字是否为NaN [英] How to check if number is NaN
问题描述
我需要测试PostgreSQL中的数字/浮点值是否不是数字(NaN).请注意,"PostgreSQL将NaN
值视为相等" ,因此此C ++技巧无效.由于我在PostgreSQL 9.3中没有看到任何isnan
函数,因此这是我的最佳尝试:
I need to test if a numeric/float value in PostgreSQL is not a number (NaN). Note that "PostgreSQL treats NaN
values as equal", so this C++ trick doesn't work. As I'm not seeing any isnan
function in PostgreSQL 9.3, here is my best attempt to make one:
create or replace function isnan(double precision) returns boolean as
$$select $1::text = 'NaN'::text$$ language sql;
有没有更好的方法来测试NaN
?
Is there any better way to test for NaN
s?
推荐答案
有没有更好的方法来测试NaN?
Is there any better way to test for NaNs?
简单比较是否相等:
SELECT double precision 'NaN' = double precision 'NaN';
因为,根据您链接到的文档,Pg将NaN
视为相等.我对此感到惊讶,因为它正确地将NULL
视为不等于任何其他NULL
,并且期望NaN
= NaN
返回NULL
,但是...嗯,它有效.
as, per the docs you linked to, Pg treats NaN
s as equal. I'm surprised by this, given that it correctly treats NULL
as not equal to any other NULL
, and would've expected NaN
= NaN
to return NULL
, but ... well, it works.
或者如果您需要功能:
create or replace function isnan(double precision)
language sql
immutable
returns boolean as $$
select $1 = double precision 'NaN'
$$;
这篇关于如何检查数字是否为NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!