I have two big numbers a
and b
of length around 10000, such that a <= b
.
Now, I have to find c = a / b
, upto 10 places of decimal, how do I do it without loosing precision?
The decimal
module should work. As seen in TigerhawkT3's link, you can choose the number of decimal places your quotient should be.
from decimal import *
getcontext().prec = 6
a = float(raw_input('The first number:')) #Can be int() if needed
b = float(raw_input('The second number:'))
c = Decimal(a) / Decimal(b)
print float(c)
You could use the decimal
module:
from decimal import localcontext, Decimal
def foo(a, b):
with localcontext() as ctx:
ctx.prec = 10 # Sets precision to 10 places temporarily
c = Decimal(a) / Decimal(b) # Not sure if this is precise if a and b are floats,
# str(a) and str(b) instead'd ensure precision i think.
return float(c)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With