判断一个数字是负数还是正数? [英] Decide if a number is negative or positive?

查看:219
本文介绍了判断一个数字是负数还是正数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果''a''和'b''都是正数或

都是负数,我需要采取一些特殊行动。是否有一些内置功能来检查这个?


Johs

I need to make some special action if ''a'' and ''b'' are both positive or
both negative. Is there some inbuilt function to check this?

Johs

推荐答案

Johs说:
Johs said:

如果''a''和'b''都是正数或

都是负数,我需要做一些特别的动作。是否有一些内置功能来检查这个?
I need to make some special action if ''a'' and ''b'' are both positive or
both negative. Is there some inbuilt function to check this?



零如何,既不是正面的也不是负面的?


有了这个警告,请考虑这个:


#define SAME_SIGN(a,b)(((((a)< 0)==((b)< 0))&&(((a)> 0) ==((b)> 0)))


-

Richard Heathfield

" Usenet是一个奇怪的放置" - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

How about zero, which is neither positive or negative?

With that caveat, consider this:

#define SAME_SIGN(a, b) ((((a)<0)==((b)<0))&&(((a)>0)==((b)>0)))

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


在文章< Yc ******* ***********************@bt.com>,

Richard Heathfield< rj*@see.sig.invalidwrote:
In article <Yc******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:

>如果a和b都是正面的,我需要采取一些特殊行动或<两者都是消极的。是否有一些内置功能来检查这个?
>I need to make some special action if ''a'' and ''b'' are both positive or
both negative. Is there some inbuilt function to check this?


>零如何,既不是正面的,也不是负面的?
>How about zero, which is neither positive or negative?



如果其中任何一个值可能是一个浮点数,你需要

来考虑你想要的-0(这是表现不同于某些

浮点系统中的+0。


- Richard

-

考虑在一些字母表中需要多达32个字符

- X3.4,1963。

And if either of the values may be a floating-point number, you need
to consider how you want -0 (which is distinct from +0 in some
floating point systems) to behave.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


Johs写道:
Johs wrote:

我需要做一些特殊的动作,如果' 'a''和'b''均为正数或

均为负数。是否有一些内置功能来检查这个?


Johs
I need to make some special action if ''a'' and ''b'' are both positive or
both negative. Is there some inbuilt function to check this?

Johs



我不确定是否有内置功能或不。 AFAIK它

将花费更少的时间来编码,而不是找到特殊功能,如果

它可用。


无论如何这是代码,但未经过测试:


/ *两个正数或两个负数的乘积总是正数

*和零平等值得关注如果((a * b)> = 0)

{

/ *数字是正数还是两者都是负数

* /

}


这也会删除+0和-0之间的冲突,它们是b $ b类型的签名0,它们都必须具有相同的符号。


有时我们有和非感觉一样生活,比如+0或-0。


HTH

-


问候

Taran

I am not really sure if there is an inbuilt function or not. AFAIK it
would take less time to code it than to find that special function if
it was available.

Anyways here''s the code, not tested though:

/* product of two positive or two negative numbers is always positive
* and equality with zero takes care of the special case 0.
*/
if ( (a* b) >= 0 )
{
/* the numbers are either both positive or both negative
*/
}

This will also remove the the conflict between +0 and -0, which ever
type of signed 0 it is, they both must be of the same sign.

Sometimes we have to live with non-sense like +0 or -0.

HTH
--

Regards
Taran


这篇关于判断一个数字是负数还是正数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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