dtrnish3637 2017-12-20 10:35
浏览 214

在php中使用NAN

I am currently working on writing a small math related library in php. I know there exists some kind of an analogue of javascript's NaN in php, it is a constant NAN of float type.

So far I have not ever come across this constant apart from php standard functions like asin() and alike, which will return this NAN value if you pass them an invalid parameter (>1 in this exact case). This value is not detected by is_float() and is_numeric() since it is of float type, both of these functions return true and the only way to check it is is_nan(). Also, casting it to (int) will result in 0;

$x = asin(3);   // $x = NAN
is_float($x);   // true
is_numeric($x); // true
$d = (int)$x    // $d = 0;

is_nan(0.5)     // false
is_nan($x)      // true 

But none of existing libs whereof I know uses this behaviour, instead throwing an error is a common choice, as it seems.

So my questions are: which approach ought I to choose? why nowhere, apart from standard php functions, the NAN is used?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器