All is in the title. How to check a possible overflow when using the two functions exp() and log()?
To expand the answer of @TheOtherGuy, you can cancel the operation if overflow occurs.
|
|||||||||||||||||||||
|
When an oferflow occurs, then errno is set to ERANGE. Next time, do your homework before asking. Googling: "c++ exp" returned this as the first result http://www.cplusplus.com/reference/cmath/exp/
|
|||||||||||||||||
|
The best way to check for overflow beforehand is to do so intelligently on a case-by-case basis. Using your knowledge of logarithms and exponents, you should be able to identify potential overflows using properties like I threw a rough sample c++ execution together, assuming you know beforehand what limits you are attempting to follow.
If you're looking to catch the errors post mortem, examine errno in range. |
||||
|
For the exp() handling: Just compare against a variable which you assign to log(FLT_MAX). FLT_MAX is biggest float. You can do this before calculating an exp(). Because log() is inverse of exp() .
For the log() handling: 1)You cannot everflow log(x) before overflowing x. (for the upper bound) 2)Float's/Double's (x) precision is not enough to overflow to negative-infinity for log(x). 3)Make sure x is bigger than zero. |
|||||||||||
|
Better than prevent, you can catch the exception:
|
|||||||
|