负整数除法 [英] negative integer division

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

问题描述

整数除法和模数在c和python中给出不同的结果,

当涉及负数

时。把gdb作为一个广泛使用的c语言翻译

打印-2 / 3

0表示c,-1表示python。

更多惊人,模数负数给出负值! (在c)。

从代数的角度来看,python似乎是正确的,但我认为

python符合底层c编译器是一个强有力的承诺,

显然不在这里,我在python doc中找不到任何解释。

我这里没有实际的编程挑战,我刚刚遇到这个bug,

自信地从python转换为c。

integer division and modulo gives different results in c and python,
when negative numbers
are involved. take gdb as a widely available c interpreter
print -2 /3
0 for c, -1 for python.
more amazing, modulos of negative number give negative values! (in c).
from an algebraic point of view, python seems right, but I thought
python conformity to the underlying c compiler was a strong commitment,
obviously not here, and I found no explanation whatsoever in python doc.
no actual programming challenge for me here, I just had this bug, after
confidently translating from python to c.

推荐答案



Imbaud>整数除法和模数在c和/或
Imbaud>中给出不同的结果。 python,当涉及负数时。

http://www.python.org/doc/faq/progra...22-10-return-3


Skip

Imbaud> integer division and modulo gives different results in c and
Imbaud> python, when negative numbers are involved.

http://www.python.org/doc/faq/progra...22-10-return-3

Skip


Imbaud Pierre写道:
Imbaud Pierre wrote:
整数除法和modulo在c和python中给出不同的结果,
当负数时
参与其中。把gdb作为一个广泛使用的c语言翻译器打印-2 / 3
0表示c,-1表示python。
更多惊人,负数的模数给出负值! (在c)。从代数的角度来看,python似乎是正确的,但我认为python符合底层c编译器是一个强有力的承诺,
显然不在这里,我发现在python doc中没有任何解释。
对我来说没有实际的编程挑战,我只是有了这个bug,在
自信地从python转换为c。
integer division and modulo gives different results in c and python,
when negative numbers
are involved. take gdb as a widely available c interpreter
print -2 /3
0 for c, -1 for python.
more amazing, modulos of negative number give negative values! (in c).
from an algebraic point of view, python seems right, but I thought
python conformity to the underlying c compiler was a strong commitment,
obviously not here, and I found no explanation whatsoever in python doc.
no actual programming challenge for me here, I just had this bug, after
confidently translating from python to c.




我不认为有很多*承诺*来遵循你的
C编译器的行为。当问题变得太复杂时,这更像是一个默认的立场,而且很难让Python实现自己。因此,大多数浮动的

点行为默认为您的平台的行为,因为

浮点数学很难并且在很多上获得了一致的行为

平台真的很难。


相对而言,整数是小菜一碟,而且我认为这是值得修补的价值来自C行为的疣。


至于文档,请参阅
http://www.python.org/doc/current/lib/typesnumeric.html


-

Robert Kern
rk***@ucsd.edu

在地狱的地方,草地长得很高

梦想的坟墓是否能够死亡。

- Richard Harter



I don''t think there''s much *commitment* to follow the behaviour of your
C compiler. It''s more of a default stance when the issues get too
complicated for Python to implement itself. Thus, much of the floating
point behaviour defaults to the behaviour of your platform because
floating point math is hard and getting consistent behaviour on lots of
platforms is really hard.

Integers are a piece of cake, relatively speaking, and I think that it''s
worth fixing up a few warts from the C behaviour.

As for documentation, see
http://www.python.org/doc/current/lib/typesnumeric.html

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


Imbaud Pierre写道:
Imbaud Pierre wrote:
整数除法和模数在c和p中给出不同的结果ython,
当涉及负数时。把gdb作为一个广泛使用的c语言翻译器打印-2 / 3
0表示c,-1表示python。
更多惊人,负数的模数给出负值! (在c)。从代数的角度来看,python似乎是正确的,但我认为python符合底层c编译器是一个强有力的承诺,
显然不在这里,我发现在python doc中没有任何解释。
对我来说没有真正的编程挑战,我只是有了这个bug,在
自信地从python转换为c之后。
integer division and modulo gives different results in c and python,
when negative numbers
are involved. take gdb as a widely available c interpreter
print -2 /3
0 for c, -1 for python.
more amazing, modulos of negative number give negative values! (in c).
from an algebraic point of view, python seems right, but I thought
python conformity to the underlying c compiler was a strong commitment,
obviously not here, and I found no explanation whatsoever in python doc.
no actual programming challenge for me here, I just had this bug, after
confidently translating from python to c.



如果你阅读了C标准,你可以看到C编译器可以自由地使用
使-1 / 3为-1或0(但它必须描述它的作用)。

的决定通常是做底层CPU所做的事情。

的基本原理是,通常你会将积极的积极分开,

并且你不希望编译器在很多测试代码周围散布。


虽然编译C中的差异是一个相当大的时间损失

(DIVIDE vs. DIVIDE; COMPARE; JUMPGE),在Python中权衡取舍是

为许多人添加两条指令(50?100?)。 Python并没有真正支付一些额外的指令来获得规范价值,

并且保证简化了用户的代码。


--Scott David Daniels
Sc ******* ****@Acm.Org


这篇关于负整数除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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