如何检查数字是否为NaN [英] How to check if number is NaN

查看:83
本文介绍了如何检查数字是否为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 NaNs?

推荐答案

有没有更好的方法来测试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 NaNs 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆