在 VBA/VB6 中测试 NaN [英] Testing for NaN in VBA/VB6

查看:32
本文介绍了在 VBA/VB6 中测试 NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 VBA,我将一个 8 字节的浮点数从一个字节数组加载到一个 Double 中.一些数字将是 IEEE 754 NaN(即,如果您尝试使用 Debug.Print 打印它,您将看到 1.#QNAN).我的问题是,如何测试 Double 中包含的数据是否为 ​​NaN 而不是常规数字?

Using VBA I am loading an 8-byte floating point number from an array of bytes into a Double. Some numbers will be IEEE 754 NaN (i.e. if you try to print it with Debug.Print you will see 1.#QNAN). My question is, how can I test whether the data contained in the Double is an NaN as opposed to a regular number?

谢谢.

推荐答案

NaN 在指数中有一个模式,您可以在它们仍在字节数组中时进行识别.具体来说,任何 NaN 都会有一个全为 1 的指数,任何无穷大也是如此,您可能也应该捕获它.

NaN's have a pattern in the exponent that you can identify while they're still in the byte array. Specifically, any NaN will have an exponent of all 1's, as will any Infinity, which you probably also should trap.

在 double 中,指数位于最高位的两个字节中:

In a double, the exponent is in the highest-order two bytes:

 SEEEEEEE EEEEMMMM MMM....

假设它们是 b(0) 和 b(1):

Assume those are b(0) and b(1):

  Is_A_Nan = ((b(0) And &H7F) = &H7F) And ((b(1) And &HF0) = &HF0)

这是空中代码,但你懂的.

That's air code, but you get the idea.

如果您需要区分 SNaN、QNaN 和 Infinity,您需要更深入地研究,但这对您来说似乎不是问题.

If you need to distinguish between SNaN, QNaN, and Infinity you'll need to look deeper, but it doesn't sound like that's an issue for you.

这篇关于在 VBA/VB6 中测试 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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