在C90 / C89中可靠地确定双倍的符号值 [英] Reliably determine sign value of double in C90/C89
问题描述
您好。
我希望能够轻松判断给定的双倍
值是否为负值。
>
在C99下,我这样做:
if(signbit(d))negative = 1;
或者,如果实现没有提供signbit(),我这样做:
union real {
unsigned long long n;
double d;
};
if((real.n> 63)& 1)negative = 1;
但是,如果我支持只提供
C89的实现(不长,没有signbit())怎么办?
任何人有一个可靠,便携的方法处理这个吗?
谢谢,
mc
Hello.
I want to be able to portably determine whether or not a given double
value is negative.
Under C99, I do:
if (signbit(d)) negative = 1;
Or, if the implementation doesn''t provide signbit(), I do:
union real {
unsigned long long n;
double d;
};
if ((real.n >63) & 1) negative = 1;
However, what if I am to support an implementation that only provides
C89 (no long long, no signbit())?
Anybody got a reliable, portable method of handling this?
thanks,
mc
推荐答案
ar ********** @ googlemail.com 写道,2007年4月27日21:48:
ar**********@googlemail.com wrote, On 27/04/07 21:48:
您好。
我希望能够移植确定是否ag iven double
值是负数。
在C99下,我这样做:
if(signbit(d) )negative = 1;
Hello.
I want to be able to portably determine whether or not a given double
value is negative.
Under C99, I do:
if (signbit(d)) negative = 1;
< snip>
<snip>
但是,如果我支持仅提供<的实现,该怎么办? br />
C89(没多久,没有signbit())?
任何人都有可靠,便携的方法来处理这个问题?
However, what if I am to support an implementation that only provides
C89 (no long long, no signbit())?
Anybody got a reliable, portable method of handling this?
有什么问题如果(d <0)
?
它不会检测到负0,但对你来说这很重要吗?
-
Flash Gordon
What is wrong with
if (d < 0)
?
It won''t detect negative 0, but is that important to you?
--
Flash Gordon
4月27日,9:53 pm,Flash Gordon< s ... @ flash-gordon.me.ukwrote:
On Apr 27, 9:53 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
> ;
>
任何人都有可靠,便携的方法来处理这个问题?
Anybody got a reliable, portable method of handling this?
有什么问题如果(d <0)
?
它不会检测到负0,但对你来说这很重要吗?
What is wrong with
if (d < 0)
?
It won''t detect negative 0, but is that important to you?
是的,不幸的是。
mc
Yes, unfortunately it is.
mc
>
在文章< 11 ********************** @ b40g2000prd.googlegroups .com> ;,
< ar **********@googlemail.com写的:
In article <11**********************@b40g2000prd.googlegroups .com>,
<ar**********@googlemail.comwrote:
>你好。
我希望能够移植确定给定的双重值是否为负值。
>Hello.
I want to be able to portably determine whether or not a given double
value is negative.
[...]
[...]
>任何人都有可靠,便携的方法来处理这个问题?
>Anybody got a reliable, portable method of handling this?
为什么不是'double_val< 0",可能与魔术值的测试相结合
就像NaN那样会赢或者赢'当他们不应该或不应该通过这个测试时,
足够可靠或便携?
(我不是数值分析师;可能有一个有效的理由不这样做。
但是如果你问的是那些不是数值分析师的C程序员如何做到这一点,你应该能够告诉我们为什么显而易见的解决方案不符合你的标准。)
dave
-
Dave Vandervies dj******@csclub.uwaterloo.ca
>
惊喜你的编译器。编写比它更好的代码。
- comp.lang.c中的Keith Thompson
Why isn''t "double_val<0", possibly combined with tests for magic values
like NaN that will or won''t pass this test when they shoudn''t or should,
reliable or portable enough?
(I am not a numerical analyst; there may be a valid reason why not.
But if you''re asking C programmers who aren''t numerical analysts how to
do this, you should be able to tell us why the obvious solution doesn''t
meet your standards.)
dave
--
Dave Vandervies dj******@csclub.uwaterloo.ca
Surprise your compiler. Write better code than it asks you to.
--Keith Thompson in comp.lang.c
这篇关于在C90 / C89中可靠地确定双倍的符号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!