How to prevent an exponential function to return infinity
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Clemens
el 2 de Ag. de 2014
Comentada: Walter Roberson
el 7 de Abr. de 2021
Hello everybody
I currently try to simulate the performance of a portfolio and have - at some points - to deal with very high numbers. This works fine as long as I dont feed an exponential function since the return value is then Inf.
e.g. exp(x) where x = 2.9678e+03.
Can I somehow chance the configuration of the exp function such that it returns real numbers even if I feed the function with high values?
Best, Clemens
2 comentarios
Walter Roberson
el 7 de Abr. de 2021
you do not need expm if you are passing in a symbolic scalar; just use exp()
Respuesta aceptada
John D'Errico
el 2 de Ag. de 2014
And, what exactly do you want it to return, when you pass it an argument that is larger than the maximum value it can handle?
A double can store nothing larger than realmax. Any larger value is inf as far as that format can handle.
My suggestion is that you see where you can use logs in your computations. Most of the time large intermediate values only that - intermediates. Logs allow these big numbers to be dealt with properly. It really is very rare that properly written computations need exceed the limit of a double.
As a VERY last resort, you might consider use of a tool that will allow a larger dynamic range than a double. Such a tool is my own HPF (found on the file exchange), or the symbolic toolbox. These tools will be dramatically slower than use of a double, and they have their own limits too. It really is a rare case where you cannot do better by simply learning the techniques one needs to deal with these problems in the range afforded by a double. Your code will be the better for it, and your problems will be less in the future as your own numerical coding skills improve.
4 comentarios
Talon Townley
el 25 de Sept. de 2018
Hadn't thought of logs thankyou (problem was in one equation getting a rounded inf x 0 returning nan when should have been reasonable in the end)
Más respuestas (1)
Sigitas Babraitis
el 5 de En. de 2021
exp^inf
1 comentario
Walter Roberson
el 7 de Abr. de 2021
not valid in MATLAB as exp must be passed an argument.
It is also not obvious how this would solve the problem?
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!