Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Ruby和Python长除法不同于GMP和Java BigInteger?_Java_Python_Ruby_Gmp_Bignum - Fatal编程技术网

为什么Ruby和Python长除法不同于GMP和Java BigInteger?

为什么Ruby和Python长除法不同于GMP和Java BigInteger?,java,python,ruby,gmp,bignum,Java,Python,Ruby,Gmp,Bignum,我正在开发一个大的整数类(教学目的),我一直在使用Ruby生成测试用例。我的班级在以下测试中失败: a = -48197174570431531987668852939807674377435188974148779416366905274642031729688518691 b = 4322669160730708444058642850762359547515258361061655693150034467061 a / b = -11149864303351921 # Ruby an

我正在开发一个大的整数类(教学目的),我一直在使用Ruby生成测试用例。我的班级在以下测试中失败:

a = -48197174570431531987668852939807674377435188974148779416366905274642031729688518691
b = 4322669160730708444058642850762359547515258361061655693150034467061
a / b = -11149864303351921    # Ruby answer
我在我的代码中找不到bug,所以我尝试用其他工具验证结果,结果出乎意料:o

GMP、Java BigInteger和my类与以下结果一致:

11149864303351920
-11149864303351920
但Ruby和Python与此一致:

-11149864303351921
11149864303351920

有人能解释一下为什么会有这种行为吗?

问题是整数除法。Python2.x(我假设还有ruby,虽然我不是那里的专家)默认情况下进行整数除法。如果在python中执行此操作:

from __future__ import division

a = -48197174570431531987668852939807674377435188974148779416366905274642031729688518691
b = 4322669160730708444058642850762359547515258361061655693150034467061

print int(a/b)
你会看到你期待的答案

请注意,这种行为在python 3+中是默认的,并且来自未来的导入仅在python 2.2+中可用

这里有更多的信息。关于整数除法,由以下人员提供:)

编辑:


正如Steve Rumbalski指出的,显著的区别在于四舍五入的发生方式。Python的整数除法向负无穷大舍入,而不是向零舍入(这样-0.2变为-1)。强制浮点(“true”)除法,然后在最后强制转换为int,正如我上面所做的,这意味着舍入的方式不同,这就是为什么我上面的例子得到了“正确”的答案。

当整数除法的参数都不是正数时,必须决定商的四舍五入和余数的符号。GMP支持地板分区(f_分区…)、天花板分区(c_分区…)和截断分区(t_分区…)

使用gmpy2通过Python访问GMP

>>> import gmpy2
>>> a = -48197174570431531987668852939807674377435188974148779416366905274642031729688518691
>>> b = 4322669160730708444058642850762359547515258361061655693150034467061
>>> gmpy2.f_divmod(a,b)
(mpz(-11149864303351921), mpz(1542354793066875276328139562907995977816446564586050094773477055490))
>>> gmpy2.c_divmod(a,b)
(mpz(-11149864303351920), mpz(-2780314367663833167730503287854363569698811796475605598376557411571))
>>> gmpy2.t_divmod(a,b)
(mpz(-11149864303351920), mpz(-2780314367663833167730503287854363569698811796475605598376557411571))
>>> help(gmpy2.f_divmod)
f_divmod(x, y) -> (quotient, remainder)

Return the quotient and remainder of x divided by y. The quotient
is rounded towards -Inf (floor rounding) and the remainder will
have the same sign as y. x and y must be integers.

>>> help(gmpy2.c_divmod)
c_divmod(x, y) -> (quotient, remainder)

Return the quotient and remainder of x divided by y. The quotient
is rounded towards +Inf (ceiling rounding) and the remainder will
have the opposite sign of y. x and y must be integers.

>>> help(gmpy2.t_divmod)
t_divmod(x, y) -> (quotient, remainder)

Return the quotient and remainder of x divided by y. The quotient
is rounded towards zero (truncation) and the remainder will have
the same sign as x. x and y must be integers.

我想你是在问为什么Python和Ruby中的负面结果少了一个。在Python中,整数除法层,@StevenRumbalski,是的,这就是我要找的。看起来他不是在寻找浮点答案。如果这是真的,那么你的答案是不正确的。@StevenRumbalski——我以为OP是在寻找解释GMP、Java和他自己的类与Python和Ruby测试之间的差异。正确答案正如我所指出的:Python和Ruby示例使用整数除法。是吗?看看他给出的结果。他对这个整数结果很满意。如果你看java的BigInteger,除法的结果是另一个BigInteger。@StevenRumbalski——我现在明白你的意思了,你说得很对。我更新了我的答案,仍然不正确。Python3的整数除法与Python2的整数除法相同,只是拼写不同(
/
vs
/
)。