Excel VBA Single VS Double 数据类型

标签 excel vba

我认为这是一个简单的问题,但我很难找到答案:

我了解 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

Data

关于你后来关于精度决定的问题;有一个国际标准来管理这一点。

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/

相关文章:

excel - 查找单元格中一起出现的 n 个数字的公式

VBA 监视新文件的文件夹

c# - 为什么我对 AccountBalance 的引用返回 0 金额?

arrays - VBA检查多维变量的整行是否为空而不循环

excel - Outlook 项目更改重复

excel - VBA - 如何测试是否提供了可选字符串参数?

excel - 差异直方图 Excel/Matlab

java - 将数组插入 Excel

python - 如何使用 Python 将 csv 文件转换为 xlsb?

excel - 使用 VBA 防止 Excel 中的单元格重叠