c++ - How to set returned value of log10(0)? -
it prints me -inf
. fixed every library/compiler i'll use?
code:
#include <iostream> #include <math.h> using namespace std; int main () { double result = log10(0.0); cout << result; }
or change in different platforms? how manage pole error
keeping double
?
according cplusplus, depends on library log10(0)
. however, in general value of log10(0)
not defined (can -inf
if like, not real number). usually, should prevent such undefined results (not undefined in c++ sense of undefined behaviour, in mathematical sense) before happen. e.g.
double x; x = foo(); if ( x <= 0 ) { /* handle case */ else { y = log10(x); }
what value use in case of log10(0)
depends on application. however, think easier check 0
before doing calculation instead of relying on log10(0)
returning particular value (as might -inf
or different).
Comments
Post a Comment