整数数学问题 [英] Integer math question
问题描述
有人可以解决以下问题吗?
C ++
i = 5/10和
i = -5 / 10都有相同的结果0.
在python中
i = 5/10按预期给出0,但是
i = -5 / 10得到-1。
这是一个特征还是一个错误?我记得Delphi给了我同样的结果
C ++。
TIA,
Frank
Hi,
can anybody help with the following problem?
In C++
i = 5 / 10 and
i = -5 / 10 both have the same result 0.
In python
i = 5 / 10 gives me 0 as expected, but
i = -5 / 10 gives -1 as result.
Is this a feature or a bug? I remember Delphi gave me the same result as
C++.
TIA,
Frank
推荐答案
" Frank" <我****** @ szut.uni-bremen.de>在消息中写道
news:39 ************************** @ posting.google.c om ...
"Frank" <me******@szut.uni-bremen.de> wrote in message
news:39**************************@posting.google.c om...
任何人都可以帮助解决以下问题吗?
在C ++中
i = 5/10
i = -5 / 10都有相同的结果0.
在python
i = 5/10给我0按预期,但
i = -5 / 10结果为-1。
这是一个功能还是一个错误?我记得Delphi给了我与G ++相同的结果。
这是一个功能。整数除法在
Python中明确定义。
基本要记住的是*正确*
将一个整数除以
的数学结果另一个整数是理性的。 Python并没有
有一个合理的数据类型,所以它必须选择一个
的众多可能的方法来舍入
非积分结果为整数。
这个过程没有普遍正确的答案:
正确回答任何四舍五入的问题是
客户想要的是什么。
John Roth
TIA,
Frank
Hi,
can anybody help with the following problem?
In C++
i = 5 / 10 and
i = -5 / 10 both have the same result 0.
In python
i = 5 / 10 gives me 0 as expected, but
i = -5 / 10 gives -1 as result.
Is this a feature or a bug? I remember Delphi gave me the same result as
C++.
That''s a feature. Integer division is explicitly defined in
Python to do exactly that.
The basic thing to remember is that the *correct*
mathematical result of dividing one integer by
another integer is a rational. Python does not
have a rational data type, so it has to pick one
of the multitude of possible ways of rounding a
non-integral result to an integer.
There is no universally right answer to this process:
the "right" answer to any rounding problem is
what the customer wants it to be.
John Roth
TIA,
Frank
" Frank" <我****** @ szut.uni-bremen.de>在消息中写道
news:39 ************************** @ posting.google.c om ...
"Frank" <me******@szut.uni-bremen.de> wrote in message
news:39**************************@posting.google.c om...
任何人都可以帮助解决以下问题吗?
在C ++中
i = 5/10
i = -5 / 10都有相同的结果0.
在python
i = 5/10给我0按预期,但
i = -5 / 10结果为-1。
这是一个功能还是一个错误?我记得Delphi给了我与G ++相同的结果。
TIA,
Frank
Hi,
can anybody help with the following problem?
In C++
i = 5 / 10 and
i = -5 / 10 both have the same result 0.
In python
i = 5 / 10 gives me 0 as expected, but
i = -5 / 10 gives -1 as result.
Is this a feature or a bug? I remember Delphi gave me the same result as
C++.
TIA,
Frank
使用除法算法:
设a,b为b> 0的整数。然后存在唯一的整数q和r这样的
:
a = bq + r和0< = r< b [1]
如果我们让a = 5且b = 10,那么a / b由[1]表示为
a = bq + r
5 =(10)q + r
....跳过一些步骤,我们发现q = 0且r = 5
5 = 10(0)+ 5
5 = 0 + 5
5 = 5
LHS = RHS
所以,5/10 = 0整数除法(没有余数)。
现在,让a = -5和b = 10
>
-5 =(10)q + r
如果我们有q = -1,那么
-5 = (10)( - 1)+ r
-5 = -10 + r
召回[1],0< = r<湾r必须是非负的。
在这种情况下r = 5,
-5 = -10 + 5
-5 = -5
LHS = RHS
因此,q = -1,r = 5,在这种情况下-5/10 = -1整数除法(没有
余额)。
假设我们让q = 0来解决上面的第二个问题。然后
-5 =(10)(0)+ r
-5 = 0 + r
-5 = r
或
r = -5
但是,按[1],r必须是非负的。所以,我们有一个矛盾。其中
的情况我们可以说q在上面的算法中不能等于0。
所以,我想你的问题的答案是,
这是一个功能 - Python做得很好,其他人没有。
HTH
肖恩
Using the division algorithm:
Let a,b be integers with b>0. Then there exist unique integers q and r such
that:
a = bq + r and 0<=r<b [1]
If we let a=5 and b=10, then a/b is represented by [1] as
a = bq + r
5 = (10) q + r
.... skipping some steps, we find that q=0 and r=5
5 = 10(0) + 5
5 = 0 + 5
5 = 5
LHS = RHS
so, 5/10 = 0 in integer division (with no remainder).
Now, let a = -5 and b = 10
-5 = (10)q + r
If we have q = -1, then
-5 = (10)(-1) + r
-5 = -10 + r
Recall [1] above, 0 <= r < b. r must be nonnegative.
In this case r=5,
-5 = -10 + 5
-5 = -5
LHS = RHS
So, q = -1, and r=5, in which case -5/10 = -1 in integer division (without
remainder).
Suppose we let q=0 for the second problem above. Then
-5 = (10)(0) + r
-5 = 0 + r
-5 = r
or
r = -5
But, by [1], r must be nonnegative. So, we have a contradiction. In which
case we can say that q cannot equal 0 in the algorithm above.
So, I suppose the answer to your question would be,
"This is a feature - Python does it properly, where the others do not."
HTH
Sean
" Sean Ross" < SR *** @ connectmail.carleton.ca>在消息中写道
新闻:11 ********************* @ news20.bellglobal.com ...
"Sean Ross" <sr***@connectmail.carleton.ca> wrote in message
news:11*********************@news20.bellglobal.com ...
然后a / b由[1]表示为
then a/b is represented by [1] as
'''''在这里是错误的词,但希望你能得到这个想法......
''represented'' is the wrong word here, but hopefully, you get the idea ...
这篇关于整数数学问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!