为什么fabs()的零情况? [英] Why the zero case in fabs()?

查看:136
本文介绍了为什么fabs()的零情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Plauger的THE STANDARD C LIBRARY(1992)中有

fabs.c(p140)的源代码。


/ * fabs功能* /

#include" xmath.h"

double(fabs)(双x)

{

开关(_Dtest(& x))

{/ *特殊代码测试* /

案例NAN:

errno = EDOM;

返回(x);

案例INF:

errno = ERANGE;

return(_Inf。 _D);

案例0:

返回(0.0);

默认:/ *有限* /

返回(x <0.0?-x:x);

}

}


这不一定是关于特别是fabs(),而是一个关于*技术的问题* Plauger已经使用过,如果有一个

微妙的话。

我的问题是:为什么有一个0的情况,它只返回0.0?没有

它,默认情况下会捕获它,并返回0,这将是

转换为double - 函数的返回类型。


另外,我对于回归

表达式周围多余的括号使用感到困惑。


Martin
http://martinobrien.co.uk/

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.

Also, I''m puzzled by the superfluous use of parentheses around the return
expressions.

Martin
http://martinobrien.co.uk/

推荐答案

在< CD ****************** @ fe04.usenetserver.com> "马丁" < martin.o_brien @ [无垃圾邮件] which.net>写道:
In <CD******************@fe04.usenetserver.com> "Martin" <martin.o_brien@[no-spam]which.net> writes:
在Plauger的THE STANDARD C LIBRARY(1992)中有
fabs.c的源代码(p140)。
/ * fabs功能* /
#include" xmath.h"
double(fabs)(双x)
{
开关(_Dtest(& x))
{/ *测试特殊代码* /
案例NAN:
errno = EDOM;
返回(x);
案例INF:
errno = ERANGE ;
返回(_Inf._D);
案例0:
返回(0.0);
默认:/ *有限* /
返回(x <0.0? -x:x);
}
}
这不一定是关于fabs()的问题,而是关于*技术的问题* Plauger已经聘用,如果涉及到微妙的话。

我的问题是:为什么有一个0的情况,它只返回0.0?没有
它,默认情况下无论如何都会捕获它,并返回0,它将被转换为double - 函数的返回类型。


在这种情况下不需要转换为double:

函数将返回x,它已经是double类型。


然而,我不知道为什么Plauger将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.
No conversion to double is needed or would occur in that case: the
function would return x which is already of type double.

However, I don''t know why Plauger handled 0.0 as a special case.
Also, I''m puzzled by the superfluous use of parentheses around the return
expressions.




他们是强制性的,当Plauger开始使用时回来C.老习惯死了

很难......

Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da*****@ifh.de


Martin< martin.o_brien @ [no-spam] which.net>写道:
Martin <martin.o_brien@[no-spam]which.net> wrote:

我的问题是:为什么有一个0的情况,它只返回0.0?没有
它,默认情况下无论如何都会捕获它,并返回0,它将被转换为double - 函数的返回类型。


您遗失的微妙之处是签名为零。关于

有这样的东西的实现,-0.0不是< 0.0,所以默认情况下会返回它

as-is(即-0.0)而不是返回正确的+0.0。

另外,我很困惑返回
表达式周围多余使用括号。

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.
The subtlety you''re missing is signed zeros. On implementations that
have such things, -0.0 is not < 0.0, so the default case would return it
as-is (i.e., -0.0) rather than returning the correct +0.0.
Also, I''m puzzled by the superfluous use of parentheses around the return
expressions.




样式。如果我们正在做出价值判断,或者缺乏它。 :-)


-Larry Jones


我不认为这个问题非常假设。 - Calvin



Style. Or lack thereof, if we''re making value judgments. :-)

-Larry Jones

I don''t think that question was very hypothetical at all. -- Calvin


" Martin" < martin.o_brien @ [无垃圾邮件] which.net>写道:
"Martin" <martin.o_brien@[no-spam]which.net> writes:
在Plauger的THE STANDARD C LIBRARY(1992)中有
fabs.c的源代码(p140)。
/ * fabs功能* /
#include" xmath.h"
double(fabs)(双x)
{
开关(_Dtest(& x))
{/ *测试特殊代码* /
案例NAN:
errno = EDOM;
返回(x);
案例INF:
errno = ERANGE ;
返回(_Inf._D);
案例0:
返回(0.0);
默认:/ *有限* /
返回(x <0.0? -x: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'' is
0.0. If it is already known that `x'' is 0.0, there is no reason to
compare it with 0.0 again, which would only (slightly) degrade the
performance.
Also, I''m puzzled by the superfluous use of parentheses around the
return expressions.




我也是。


Martin



So am I.

Martin


这篇关于为什么fabs()的零情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆