Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent overflow when using usual math functions exp() log()?

All is in the title. How to check a possible overflow when using the two functions exp() and log()?

like image 763
WildThing Avatar asked Jul 11 '13 10:07

WildThing


People also ask

How can overflow errors be prevented?

Use 64-bits integers. One very good way to prevent integer overflows is to use int64_t to implement integers. In most case, 64-bits ints will not commit overflow, unlike their 32-bits counterparts. There is actually very few downsides in using int64_t instead of int32_t .

What is the difference between exp () and pow () in C?

exp(Math. log(base) * exponent) is faster than Math. pow(base, exponent) .

How do you avoid arithmetic overflow in C++?

You can prevent this loss by casting the value to a wider type before the arithmetic operation. In the corrected source, the left operand was cast to a wider type for the result of the arithmetic operation to be wider.

How do you prevent integer underflow?

Avoiding Integer Overflows and Underflows Applications should be designed to perform value checks before explicit typecasts and to avoid implicit typecasts – such as comparing variables of different types or passing a signed variable to a function expecting an unsigned one – when possible.


1 Answers

#include <errno.h>

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/
In the middle of the page, there is EXACTLY what you're looking for.

like image 185
gifnoc-gkp Avatar answered Sep 24 '22 06:09

gifnoc-gkp