使用双打不正确的VBA溢出(Excel) [英] Incorrect VBA Overflow using Doubles (Excel)

查看:171
本文介绍了使用双打不正确的VBA溢出(Excel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,以下语句的计算结果为零。我认为这是溢出的,但所有的临时价值似乎都在双倍的限度内。

  DiffieHellmanKey =( 43 ^ 47) - (53 * Fix((43 ^ 47)/ 53))

它是溢出的,因为当我执行不同的数字(下面),它的结果是正确的值29。

  DiffieHellmanKey =( 5 ^ 22) - (53 * Fix((5 ^ 22)/ 53))

?现在,回到给我溢出的原始数字。涉及的所有变量都是双倍。如果将其计算为工作表公式而不是VBA,它甚至不起作用:

  =(43 ^ 47) - (53 * ROUNDDOWN(((43 ^ 47)/ 53),0))

如果我在VBA中使用等效的形式(如下)实现上述示例,我得到-1.75357E + 62的错误结果。

  DiffieHellmanKey =(43 ^ 47) - (53 * WorksheetFunction.RoundDown(((43 ^ 47)/ 53),0))


解决方案

你是对的,但你的问题不是溢出,而是有重要的数字。



Microsoft Excel中的数字永远不能超过15位有效数字,但小数可以高达127位。



source: http://msdn.microsoft.com/en-us/library/office/ff838588.aspx



您的原始公式如下:


$ b $ (43 ^ 47) - (53 * Fix((43 ^ 47)/ 53))简化为(43 ^ 47) - 修复(43 ^ 47)
那么(43 ^ 47)有大约76位数字,所以他们都被削减到相同的数量,等于0。



VBA中最大的变量类型是十进制,只能显示29位数字。您将无法使用本机直观的基本数据进行数学运算。


For some reason, the following statement evaluates to zero. I assume it's due to overflow, but all the interim values seem to be well within the limits of a double.

DiffieHellmanKey = (43 ^ 47) - (53 * Fix((43 ^ 47) / 53))

I think it's overflow because when I execute it with different numbers (below), it results in the correct value of 29.

DiffieHellmanKey = (5 ^ 22) - (53 * Fix((5 ^ 22) / 53))

What gives? Now, back to the original numbers that are giving me overflow. All variables involved are Doubles. It doesn't even work if I calculate it as a Worksheet formula instead of in VBA:

=(43 ^ 47) - (53 * ROUNDDOWN(((43 ^ 47) / 53), 0))

And if I implement the above example in VBA using the equivalent form (below), I get an incorrect result of -1.75357E+62.

DiffieHellmanKey = (43 ^ 47) - (53 * WorksheetFunction.RoundDown(((43 ^ 47) / 53), 0))

解决方案

You are kind of right but your problem is not with overflow, but with significant digits.

Numbers in Microsoft Excel can never have more than 15 significant digits, but decimals can be as large as 127.

source: http://msdn.microsoft.com/en-us/library/office/ff838588.aspx

This is what was happening with your original formula:

(43 ^ 47) - (53 * Fix((43 ^ 47) / 53)) simplifies to (43 ^ 47) - fix(43 ^ 47) Then the (43^47) has about 76 digits long so they are both getting cut down to the same amount and equating to 0.

The largest variable type in VBA is 'Decimal' Which only holds 29 digits of significance. You won't be able to perform math this large using visual basic natively.

这篇关于使用双打不正确的VBA溢出(Excel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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