Code:
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
double result = log10(0.0);
cout << result;
}
When I execute log10(0) in C++, It prints to me -inf
.
Is it fixed for every library/compiler I'll use?
Or could it change in different platforms?
How would you manage the pole error
keeping double
?
According to cplusplus, it depends on the library what you get for log10(0)
. However, in general the value of log10(0)
is not defined (can be -inf
if you like, but it is not a real number). Usually, you should prevent such undefined results (not undefined in the C++ sense of Undefined Behaviour, but in a mathematical sense) before they happen. E.g.
double x;
x = foo();
if ( x <= 0 ) {
/* handle this case extra */
else {
y = log10(x);
}
What value you use in the case of log10(0)
depends very much on your application. However, I think it is easier to check for 0
before doing the calculation instead of relying on log10(0)
returning some particular value (as it might be -inf
or something completely different).