VBA Exp() Overflow - excel

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.

Related

Making the number of executions of a for-loop depending on a variable

As you probably already figured by my question, I am pretty new to programming which is why this question is so basic. But I couldn't really find a question that had an answer to my problem so I thought I might just ask. I have a list that contains integers that I wanted to compare to each other so they can be sorted from highest to lowest. Now for that I wanted to use a for-loop that is executed exactly as many times as there are integers in my list. How do I do that? I tried:
for len(a): # (a is my list)
*code*
Thought it is returning me an error, saying that it couldn't be assigned to the function call. I am not sure why :/ . I hope you can help me.
for i in range(len(a)):
is the usual way; the official Python tutorial is a good place to start for understanding range(), and the fact that the loop is executed as many times as there are elements in your list when you first entered the loop

Most efficient "next-day" function?

I want to write a function that takes 'yyyymmdd' as its input and generates the next day (same 'yyyymmdd' format output). This function can be applied to solve problems like next-week or next-t-days.
I know that this task can be easily done using the time.delta approach. However, what is inside the time library? I mean, what sort of computation did it do to solve this "next-day" problem?
In Python, I wrote my own next-day function using a long if-elif-..-else statement on the day-level when int(dd) >= 28. It worked, but looks extremely ugly and inefficient... So I start to be curious about what's written inside the time library...
In particular, I want to know what is the most efficient way of performing such next-day task, and can we prove its optimality of efficiency?
Thank you very much for your help!

Does Julia have a way to solve for unknown variables

Is there a function in Julia that is similar to the solver function in Excel where I can provide and equation, and it will solve for the unknown variable? If not, does anybody know the math behind Excel's solver function?
I am not expecting anybody to solve the equation, but if it helps:
Price = (Earnings_1/(1+r)^1)+(Earnings_2/(1+r)^2)++(Earnings_3/(1+r)^3)+(Earnings_4/(1+r)^4)+(Earnings_5/(1+r)^5)+(((Earnings_5)(RiskFreeRate))/((1+r)^5)(1-RiskFreeRate))
The known variables are: Price, All Earnings, and RiskFreeRate. I am just trying to figure out how to solve for r.
Write this instead as an expression f(r) = 0 by subtracting Price over to the other side. Now it's a rootfinding problem. If you only have one variable you're solving for (looks to be the case), then Roots.jl is a good choice.
fzero(f, a::Real, b::Real)
will search for a solution between a and b for example, and the docs have more choices for algorithms when you don't know a range to start with and only give an initial condition for example.
In addition, KINSOL in Sundials.jl is good when you know you're starting close to a multidimensional root. For multidimensional and needing some robustness to the initial condition, I'd recommend using NLsolve.jl.
There's nothing out of the box no. Root finding is a science in itself.
Luckily for you, your function has an analytic first derivative with respect to r. That means that you can use Newton Raphson, which will be extremely stable for your function.
I'm sure you're aware your function behaves badly around r = -1.

Shorter version of IF(ISERROR(...))

I'm wondering if there is a way to check if a formula returns an error, and if not, use the value found, without doing the following:
=IF(ISERROR(A1/B1), 0, A1/B1)
The syntax I'm looking for is something like this:
=EQ([value_if_not_error], [value_if_error])
The solution might contain more stuff, IF and ISERROR is perfectly fine, as long as I avoid having the main function several times in each cell. The reason why I want to do this is that my equations are quite long and the readability is drastically reduced when I have to write the equation twice, or even more (if several ifs).
Is there a simple solution to this?
Try
=IFERROR([formula], [value_if_error])

Is there a reason to prefer a switch over an if statement with only one condition?

I found the following code in my team's project:
Public Shared Function isRemoteDisconnectMessage(ByRef m As Message)
isRemoteDisconnectMessage = False
Select Case (m.Msg)
Case WM_WTSSESSION_CHANGE
Select Case (m.WParam.ToInt32)
Case WTS_REMOTE_DISCONNECT
isRemoteDisconnectMessage = True
End Select
End Select
End Function
Never mind that the function doesn't have a return type (I can easily add 'As Boolean'); what I'm wondering is, could there be any reason to prefer the above over the following (to me, much more readable) code?
Public Shared Function isRemoteDisconnectMessage(ByRef m As Message) As Boolean
Return m.Msg = WM_WTSSESSION_CHANGE AndAlso _
m.WParam.ToInt32() = WTS_REMOTE_DISCONNECT
End Function
To put the question in general terms: Does it make sense to use a switch (or, in this case, Select Case) block--and/or nested blocks--to test a single condition? Is this possibly faster than a straightforward if?
If you're worried about performance...profile. Otherwise you can't go wrong erring on the side of readability...
I don't believe it actually matters in terms of speed, the compiler should be able to optimize it.
I think it would just be a matter of preference.
My rule of thumb is to use a switch statement when the number of if/else conditions is greater than three. I don't have any data behind why this makes sense other than readability/maintainability seems to decrease as the number of if/else conditions increases.
I think the answer in the specific case you've given is no - it doesn't make sense, as suggested in other answers one would hope that the compilers would optimise away any practical differences.
I'd put money on this being a bit of cut, paste and delete coding - taking a generalised set of nested case statements and extracting that one bit that gives you the yes/no result you need.
If this were something similar in-line and/or there was a function call where the return flag is set then one might, possibly, be at a point where one could start to justify it but not as it is.

Resources