[英]Is there a python module that convert a value and an error to a scientific notation?
[英]Error with Python modulo on scientific notation
希望有人可以帮我解释这个大除数的奇怪结果,或者用一些关键字建议我,这样我可以得到更好的搜索。
>>> m = 1e9+9
>>> n = 1000000009
>>> m == n
True
>>> 2549015908609065 % m
885667930.0
>>> 2549015908609065 % n
885667930
>>> 2549015908609065L % n
885667930L
>>> 2549015908609065L % m
885667930.0
>>> 254901590860906524041412370460835L % m
98506080.0
>>> 254901590860906524041412370460835L % n
327998297L
>>> 254901590860906524041412370460835L % int(m)
327998297L
之所以会看到奇怪的结果,是因为您正在对具有不精确表示形式的浮点数进行模运算。 十进制模块上的文档很好地强调了这一点。
要对很大的数字执行精确的运算,可以使用十进制类,如下所示:
from decimal import *
print Decimal(2549015908609065) % Decimal(1e9) # Gives the answer you expect
m = 1e9+9
将数字存储为浮点数; 但是n = 1000000009
将数字存储为整数。
当您将整数除以浮点数时,python会隐式输出一个浮点数作为结果; 但是,当您将一个整数除以另一个整数时,会得到整数商。 浮点数较大时的精度会降低。
如果您注意到,
>>> 254901590860906524041412370460835L % m
98506080.0
#outputted a float with different precision
>>> 254901590860906524041412370460835L % n
327998297L
#outputted a long int
在http://www.tutorialspoint.com/python/python_numbers.htm上查看有关python数字的基本教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.