python判断用户输入的是正数还是负数,判断一个数字是负数还是正数?

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 said:

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?

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.

In article ,

Richard Heathfield

>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?

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 wrote:

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值