2

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.

4

2 回答 2

2

Exp(28,000)考虑到1.8x10 12160 ,它会溢出并不奇怪,你可以传递的最大值Exp是 ~709。

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

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 ....
于 2011-06-13T16:11:03.017 回答
0

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

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

于 2011-06-13T14:29:45.720 回答