Lua检查数字/值是否为nan [英] Lua check if a number/value is nan

查看:1464
本文介绍了Lua检查数字/值是否为nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,在经过一些计算后打印出矩阵,我得到了所有元素的nan输出.我想在矩阵的第一个元素变为nan后立即中断for循环,以了解该问题.我怎样才能做到这一点?在终端中,我打印了包含nan作为所有元素的矩阵a,并分别键入了a[1][1]=="nan"a[{{1},{1}}]=="nan",这两个均返回false.他们为什么不返回false,我应该改用什么语句?

I have written a program to print a matrix after some computations and I am getting an output of nan for all elements. I want to break a for loop as soon as the matrix's first element becomes nan to understand the problem. How can I do this? In the terminal, I have printed the matrix a containing nan as all elements and typed a[1][1]=="nan" and a[{{1},{1}}]=="nan" both of which return false. Why are they not returning false and what statement should I use instead?

推荐答案

您的测试失败,因为您正在将数字与字符串"nan"进行比较.

Your test fails because you are comparing a number with a string, "nan".

如果确定是数字,最简单的方法是:

If you are sure it's a number, the easiest way is:

if a[1][1] ~= a[1][1] then

因为根据IEEE 754,nan值被认为不等于任何值,包括其自身.

because according to IEEE 754, a nan value is considered not equal to any value, including itself.

这篇关于Lua检查数字/值是否为nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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