Reputation: 21
Python newbie here. I was curious as to how I would print a float longer than 13 characters to my terminal window (Mac user, run Python programs in terminal). I created a program to calculate Pi using the formula:
π^4/90 = 1^-4 + 2^-4 + 3^-4 etc.
Yet no matter how long I loop the program to get a more accurate calculation of Pi, it still only prints 3.14159265359 then stops, is there a way to get the program to extend this?
Upvotes: 0
Views: 149
Reputation: 280380
You can use
print repr(pi)
to see more precision. str
, which is the default used for printing, truncates floats to 12 significant digits. However, there is a limit to how precisely you can approximate pi with Python floats, because the binary64 floating point format only has 53 bits of precision. If you need more precision, you can use decimal.Decimal
with a high-precision context.
Upvotes: 2