简体   繁体   English

为什么is_numeric(NAN)返回TRUE?

[英]Why does is_numeric(NAN) return TRUE?

I have tested the is_numeric function in the NAN constant in PHP and the given result 我已经在PHP的NAN常量中测试了is_numeric函数,并给出了结果

is_numeric(NAN); // TRUE

But NAN means "Not A Number". 但是NAN的意思是“不是数字”。 Why the function is_numeric returns true ? 为什么函数is_numeric返回true

I know that NAN is of float type. 我知道NAN是float型的。 But when testing the two cases below, the results are different: 但是,当测试以下两种情况时,结果是不同的:

is_float(NAN) // true
filter_var(NAN, FILTER_VALIDATE_FLOAT)// false

Why ocurred it? 为什么会发生呢?

Sorry, my english is bad 对不起,我的英语不好

NAN is a special constant. NAN是一个特殊常数。 It has to hold some value, so it holds a float datatype 它必须保存一些值,所以它保存一个float 数据类型

var_dump(NAN); // float(NAN);

The problem is that filter_var isn't comparing data types, it's looking for numbers, which NAN is not . 问题在于filter_var不是在比较数据类型,而是在寻找数字,而NAN 不是

Some numeric operations can result in a value represented by the constant NAN. 一些数字运算可能会导致由常量NAN表示的值。 This result represents an undefined or unrepresentable value in floating-point calculations. 该结果表示浮点计算中的未定义或无法表示的值。 Any loose or strict comparisons of this value against any other value, including itself, will have a result of FALSE. 将该值与任何其他值(包括其自身)进行任何宽松或严格的比较,将得出FALSE的结果。

Because NAN represents any number of different values, NAN should not be compared to other values, including itself, and instead should be checked for using is_nan(). 由于NAN代表任意数量的不同值,因此不应将NAN与其他值(包括其自身)进行比较,而应使用is_nan()进行检查。

NAN is a non-zero floating point value. NAN是一个非零浮点值。 What is NAN, Its a square root of (-1) . 什么是NAN,其平方根是(-1)

So now it comes to complex numbers. 因此,现在涉及到复数。 Square root of (-1) is +i or -i which means NAN can have two values. (-1)平方根是+i-i ,这意味着NAN可以有两个值。 So if NAN were to evaluate to -i or false , NAN == false would become true instead. 因此,如果NAN求值为-ifalse ,则NAN == false将变为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM