如何解决在Python中无法将大整数除以10并舍去小数位数
以下程序遇到错误,因为当我将大整数除以10并截断它时,Python进行了奇怪的数学运算并返回了另一个数字。
# Program to add the digits in an integer
# eg. 463728 would equal 30 ( 4+6+3+7+2+8 )
the_sum = 2**1000
running_tot = 0
sum_length = len(str(the_sum)) # to calculate the length of the integer
i = 0 #initialize iterator
while i < sum_length:
remainder = the_sum % 10 # Find out the last no. in int.
running_tot += remainder # adds the last no. to running total
the_sum = int(the_sum / 10) # truncates the last no. for original number
i += 1 # increment iterator
print(running_tot)
我想知道为什么Python要这么做吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。