我认为这是一个简单的问题,但我很难找到答案:
我了解 Single 和 Double 数据类型存储浮点数。但我不明白他们可以代表的最大数字?
例如,如果我在这个简单的宏中使用 Single 数据类型:
Sub Float()
Dim Result As Single
Result = 10 / 3
MsgBox Result
End Sub
结果是 3.333333
有7位数。但是,当您查看 Single 可以容纳的范围时 - 它表明它可以容纳更多位数。
与 Double 类型相同。我将变量更改为 Double,然后结果是 3.33333333333333(15 位)但 Double 可以容纳更多位?
我很困惑。
最佳答案
精度限制为最多 15 个有效数字。其他答案提到Single 7。
如果您改为打印到工作表,您将看到以下内容:
Public Sub test()
Dim s As Single
Dim s2 As Double
s = 10 / 3
s2 = 10 / 3
[A1] = "Single"
[B1] = s
[A2] = "Double"
[B2] = s2
[B1:B2].NumberFormat = "0.000000000000000000"
End Sub
关于你后来关于精度决定的问题;有一个国际标准来管理这一点。
IEEE 754
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in the diverse floating point implementations that made them difficult to use reliably and portably. Many hardware floating point units now use the IEEE 754 standard.
有人可能会纠正我,但我相信这是普遍的标准。
为你评论单精度wiki有一个很好的解释here .
您可以访问标准 here .
关于Excel VBA Single VS Double 数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023795/