大花车的Python3停止部。

时间:2021-10-10 07:20:09

I am dividing extremely large integers, so to speak up to 1kb integers and I ran into 2 problems already. Either

我正在分割非常大的整数,所以说到1kb的整数,我已经遇到了两个问题。要么

OverflowError: integer division result too large for a float

溢出错误:整数分割结果对于浮动来说太大了。

or The float is rounded off to some digits and when I try to multiply back I get a slightly different number.

或者浮点数被四舍五入到一些数字当我试着乘回去的时候我得到一个稍微不同的数字。

Is there any way in python to somehow prevent from dividing floats that have more than 20 digits after the decimal point?

在python中有没有什么方法可以防止在小数点后超过20位数的浮动?

smallest_floats = []

n1 = int(input())
n2 = int(input())

while n2 != 1:
  smallest_floats.append(str(n1/n2))
     n2 -= 1
print(min(smallest_floats, key=len))

I am thinking that the possible solutions is to somehow assert division or:

我在想,可能的解决方案是,以某种方式断言部门或:

len(s.split(".")[-1]) > 20

2 个解决方案

#1


1  

For rational number arithmetic without precision loss you can use the fractions.Fraction class from the fractions package. You can divide by another rational number and then multiply by it again in order to obtain the very same rational number you had at the beginning.

对于没有精确损失的有理数算法,你可以使用分数。分数包中的分数类。你可以除以另一个有理数然后再乘以它来得到与开始时相同的理性数。

>>> from fractions import Fraction
>>> n1 = Fraction(large_numerator, denominator)
>>> n2 = n1 / some_rational_number
>>> assert n1 == n2 * some_rational_number

#2


1  

import the decimal module (https://docs.python.org/2/library/decimal.html) it has abritrary precision

导入十进制模块(https://docs.python.org/2/library/decim.html),它具有一定的精度。

you can increase displayed decimal digits with

您可以增加显示的十进制数字。

>>> from decimal import *
>>> getcontext().prec = 100
>>> Decimal(2).sqrt()
Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573') 100 decimal digits

how can i show an irrational number to 100 decimal places in python?

如何在python中显示一个不合理的数字到100个小数?

#1


1  

For rational number arithmetic without precision loss you can use the fractions.Fraction class from the fractions package. You can divide by another rational number and then multiply by it again in order to obtain the very same rational number you had at the beginning.

对于没有精确损失的有理数算法,你可以使用分数。分数包中的分数类。你可以除以另一个有理数然后再乘以它来得到与开始时相同的理性数。

>>> from fractions import Fraction
>>> n1 = Fraction(large_numerator, denominator)
>>> n2 = n1 / some_rational_number
>>> assert n1 == n2 * some_rational_number

#2


1  

import the decimal module (https://docs.python.org/2/library/decimal.html) it has abritrary precision

导入十进制模块(https://docs.python.org/2/library/decim.html),它具有一定的精度。

you can increase displayed decimal digits with

您可以增加显示的十进制数字。

>>> from decimal import *
>>> getcontext().prec = 100
>>> Decimal(2).sqrt()
Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573') 100 decimal digits

how can i show an irrational number to 100 decimal places in python?

如何在python中显示一个不合理的数字到100个小数?