如何设置log10(0)的返回值? [英] How to set returned value of log10(0)?
问题描述
它会向我显示 -inf
.我要使用的每个库/编译器是否都已修复?
It prints to me -inf
. Is it fixed for every library/compiler I'll use?
代码:
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
double result = log10(0.0);
cout << result;
}
还是可以在不同的平台上进行更改?您如何管理 pole错误
并保持 double
?
Or could it change in different platforms? How would you manage the pole error
keeping double
?
推荐答案
根据 cplusplus ,这取决于您从库中获得的 log10(0)
.但是,通常未定义 log10(0)
的值(如果愿意,可以是 -inf
,但它不是实数).通常,您应该在发生这种不确定的结果之前 防止发生这种不确定的结果(从C ++的未定义的行为"的角度来看不是未定义的,而是从数学的意义上来说是未定义的).例如.
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);
}
在 log10(0)
情况下使用什么值在很大程度上取决于您的应用程序.但是,我认为在进行计算之前检查 0
更容易,而不是依赖 log10(0)
返回某些特定值(因为它可能是-inf
或完全不同的东西).
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).
这篇关于如何设置log10(0)的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!