负整数 [英] Negative integers
问题描述
我正在试图弄清楚如何测试两个数字是否相同
符号(均为正数或均为负数)。我已经尝试了
abs(x)/ x == abs(y)/ y
但是当其中一个数字是我确定有一个简单的方法可以做到这一点。有什么建议吗?
谢谢
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried
abs(x) / x == abs(y) / y
but that fails when one of the numbers is 0. I''m sure that there is
an easy way to do this. Any suggestions?
Thanks
推荐答案
johnewing写道:
johnewing wrote:
我试图弄清楚如何测试两个数字是否相同
符号(均为正数或均为负数)。我已经尝试了
abs(x)/ x == abs(y)/ y
但是当其中一个数字是我确定有一个简单的方法可以做到这一点。有什么建议?
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried
abs(x) / x == abs(y) / y
but that fails when one of the numbers is 0. I''m sure that there is
an easy way to do this. Any suggestions?
(a< 0)==(b< 0)
2008年8月20日星期三14:38:11 -0700,johnewing写道:
On Wed, 20 Aug 2008 14:38:11 -0700, johnewing wrote:
I我试图弄清楚如何测试两个数字是否相同
符号(均为正数或均为负数)。我已经尝试了
abs(x)/ x == abs(y)/ y
但是当其中一个数字是我确定有一个简单的方法可以做到这一点。有什么建议吗?
谢谢
I am trying to figure out how to test if two numbers are of the same
sign (both positive or both negative). I have tried
abs(x) / x == abs(y) / y
but that fails when one of the numbers is 0. I''m sure that there is an
easy way to do this. Any suggestions?
Thanks
不要过早使用内联技巧。以非常简单的方式编写程序(可能会注意算法选择或
功能样式),只有在速度太慢的情况下才开始优化。如果它是
太慢了,你可能应该在使用不寻常的python代码之前看看pyrex或类似的东西。
直接写作的一部分,意味着有一个函数或对象
封装程序所做的每个决定 - 所以如果那个决定后来需要更改
,它可以改变在一个地方。
我最近一直经常参加comp.unix.shell,而且这个小组被怪异的小技巧所困扰了。
,使用:作为true的别名,1作为打印等的
别名。我宁愿看不到comp.lang.python变成
那种组。 (也就是说,我经常用bash编程 - 选择)
Don''t resort to inline tricks too soon. Write your program in a very
straightforward way (perhaps paying attention to algorithm choice or a
functional style), and only start optimizing if it''s too slow. And if it
is too slow, you probably should look at pyrex or similar before
resorting to unusual python code.
Part of writing straightforwardly, means having a function or object that
encapsulates each decision the program makes - so that if that decision
later needs to be changed, it can be changed in one place.
I''ve been frequenting comp.unix.shell lately, and the group is plagued
with weird little tricks - EG, using : as an alias for true, 1 as an
alias for print, etc. I''d rather not see comp.lang.python turned into
that sort of group. (That said, I program in bash frequently - by choice)
cat / tmp / sign
#!/ usr / bin / env python
def sign(x):
返回cmp(x,0)
打印标志(-5)
打印标志(0)
打印标志(33)
如果标志(-5)= =符号(-2):
打印''相同符号''
cat /tmp/sign
#!/usr/bin/env python
def sign(x):
return cmp(x,0)
print sign(-5)
print sign(0)
print sign(33)
if sign(-5) == sign(-2):
print ''Same sign''
这篇关于负整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!