VBA Exp() 溢出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6331416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:28:04  来源:igfitidea点击:

VBA Exp() Overflow

excelvbaexp

提问by TNi

Here's a very easy question for the VBA guys. I'm running Newton's method on some functions, and occasionally I find a guess that I can only assume overflows the Exp() function (and stops the code). What suggestions do you guys have to simply handle this case? (Maybe some sort of error handling?)

对于 VBA 人员来说,这是一个非常简单的问题。我在某些函数上运行 Newton 方法,偶尔我会发现我只能假设溢出 Exp() 函数(并停止代码)。你们有什么建议可以简单地处理这个案子?(也许是某种错误处理?)

If Newton's method fails because of this or any sort of blowup, I would like to proceed onto my bisection code below that point.

如果牛顿的方法因为这个或任何类型的爆炸而失败,我想继续我在那个点以下的二分代码。

By the way, I have thought about maybe taking logs to make this situation less likely, but to be honest I am working with some math I do not yet completely understand, and I would like to handle the case of Newton's method failing in any case first.

顺便说一句,我曾考虑过使用日志来减少这种情况的可能性,但老实说我正在研究一些我还没有完全理解的数学,并且我想处理牛顿方法在任何情况下失败的情况第一的。

Disclaimer: I'm a complete VBA beginner, so any suggestions would be read and appreciated. Thanks in advance.

免责声明:我是一个完整的 VBA 初学者,所以任何建议都会被阅读和赞赏。提前致谢。

Edit: I've been asked to post the code. First, thanks for reading. Unfortunately, I can't post the entire code due to business reasons, but I can give the very barebones outline. I've created a module and a function. Inside this function, I have:

编辑:我被要求发布代码。首先,感谢阅读。不幸的是,由于业务原因,我无法发布整个代码,但我可以给出非常简单的大纲。我创建了一个模块和一个函数。在这个函数中,我有:

Newtons Method Loop

Newtons Method Loop

Bisection Loop

Bisection Loop

Inside the Newton's method loop, I've traced to a point where I have a next guess of something around 28,000 or so, and I am assigning to a variable hthe value Exp(28,000) or roundabouts. The debugger breaks at that point; my code essentially exits, and whatever value my function should be returning produces #VALUE! in my cell.

在 Newton 的方法循环中,我已经追踪到我有一个大约 28,000 左右的下一个猜测的点,并且我将值分配给变量h值 Exp(28,000) 或回旋处。调试器在那时中断;我的代码基本上退出了,我的函数应该返回的任何值都会产生#VALUE!在我的牢房里。

I know this is not a lot of information, but I hope (and think) it should be enough. Correct me if I am wrong.

我知道这不是很多信息,但我希望(并认为)应该足够了。如果我错了,请纠正我。

Edit 2: If all else fails, I'm going to explicitly catch too large values, but I wonder if there is a more robust and elegant solution.

编辑 2:如果所有其他方法都失败了,我将明确捕获太大的值,但我想知道是否有更健壮和优雅的解决方案。

采纳答案by Alex K.

Not surprising it will overflow given that Exp(28,000)is 1.8x1012160, the maximum value you can pass to Expis ~709.

鉴于Exp(28,000)1.8x10 12160会溢出并不奇怪,您可以传递的最大值Exp是 ~709。

If you want to exit your loop if you encounter a value that is too large, just check the value before passing it;

如果遇到过大的值而想退出循环,只需在传递之前检查该值即可;

function Newton
   const MAX_EXP_ARGUMENT as double = 709.782712893#

   do ....
      if (abs(var) <= MAX_EXP_ARGUMENT) then
         r = exp(var)
      else
         exit do '// exit the loop
      end if
      '//use r
   loop

   do ....

回答by ray

There was a SO issue posted a while back dealing with numbers larger than Long in VBA.

不久前发布了一个 SO 问题,用于处理VBA 中大于 Long 的数字

The accepted answer pointed to a link called Large Number Arithmetic. I just tried to implement your example of exp(28000) using that example, but received a "Type Mismatch" error after processing a few loops. However, it may be the fault of my hasty implementation. If you've got no leads so far, I would start there.

接受的答案指向一个名为Large Number Arithmetic的链接。我只是尝试使用该示例实现您的 exp(28000) 示例,但在处理了几个循环后收到了“类型不匹配”错误。然而,这可能是我草率实施的错。如果到目前为止你没有线索,我会从那里开始。