开发者

VBA Exp() Overflow

开发者 https://www.devze.com 2023-03-12 07:08 出处:网络
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)

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?)

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.

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

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 h the 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.

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.


Not surprising it will overflow given that Exp(28,000) is 1.8x1012160, the maximum value you can pass to Exp is ~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 ....


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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消