[英]Python digit sum function returns wrong digits on large numbers
The function I wrote is fairly basic: It walks through every digit of num
and prints it out.我写的 function 相当基本:它遍历
num
的每个数字并打印出来。 The remainder is then run again until the sample code is finished.然后再次运行剩余部分,直到示例代码完成。
num = 1000000000000000000000000000001
while num > 0:
curr = num % 10
num = (num - curr) / 10
print(curr)
The expected output of this would be 100000000000000000000000000001
(with linebreaks in between).预期的 output 将是
100000000000000000000000000001
(中间有换行符)。 However, it returns the following on the example above:但是,它在上面的示例中返回以下内容:
1622442800000000000000000000001
I suspect this has something to do with the limitations of the long datatype.我怀疑这与长数据类型的限制有关。
How could I handle this problem?我该如何处理这个问题? Thanks!
谢谢!
It's happening because in Python 3, /
means "floating-point" division.这是因为在 Python 3 中,
/
表示“浮点”除法。 Try //
instead.试试
//
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.