/ * fabs功能* / / * fabs功能* / #include" xmath.h" double(fabs)(双x) {开关(_Dtest(& x)) {/ *测试特殊代码* / 案例NAN: errno = EDOM; 返回(x); 案例INF: errno = ERANGE ; 返回(_Inf._D); 案例0:返回(0.0); 默认:/ *有限* / 返回(x } } 这不一定是关于fabs()的问题,而是而是关于*技术的问题* Plauger已经聘用了,如果有一个微妙的参与。 我的问题是:为什么有一个0的情况,它只返回0.0?没有它,默认无论如何情况都会陷阱,并返回0,这将是co避免加倍 - 函数的返回类型。 我不知道'_Dtest'的作用,但如果'x''是 0.0,可能会返回0。如果已知'x''为0.0,则没有理由将再次与0.0进行比较,这只会(稍微)降低 的性能。 此外,我对返回表达式周围多余的括号使用感到困惑。 In Plauger''s THE STANDARD C LIBRARY (1992) there is the source code for fabs.c (p140). /* fabs function */ #include "xmath.h" double (fabs)(double x) { switch (_Dtest(&x)) { /* test for special codes */ case NAN: errno = EDOM; return (x); case INF: errno = ERANGE; return (_Inf._D); case 0: return (0.0); default: /* finite */ return (x < 0.0 ? -x : x); } } This is not necessarily a question about fabs() in particular, but rather a question about the *technique* Plauger has employed, and if there is a subtlety involved. My question is: why is there a case for 0, which simply returns 0.0? Without it, the default case will trap it anyway, and return 0, which will be converted to double - the return type of the function.I don''t know what `_Dtest'' does, but presumably it returns 0 if `x'' is0.0. If it is already known that `x'' is 0.0, there is no reason tocompare it with 0.0 again, which would only (slightly) degrade theperformance. Also, I''m puzzled by the superfluous use of parentheses around the return expressions. 我也是。 Martin So am I. Martin 这篇关于为什么fabs()的零情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 06:55