0

I’m trying to get a more precise result from this code:

a = str(10**20+10**-20)
b = eval(a)

I want to obtain around 42 decimal places so Python doesn’t round it to 1e+20.

1

1 Answer 1

1

You actually 'only' need 41 to accomplish that. Use the decimal module:

import decimal
decimal.getcontext().prec=41
a = 10**20+10**decimal.Decimal(-20)

Python's floating-point types are just as inaccurate as in any other language. The decimal module contains the Decimal data structure, capable of handling arbitrarily long decimal numbers instead. You can read more about it here: check it out.

Sign up to request clarification or add additional context in comments.

1 Comment

Your comment on my post is correct. I removed it to deter potential confusion and upvoted yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.