[Tutor] Count for loops
Mats Wichmann
mats at wichmann.us
Tue Apr 11 14:44:42 EDT 2017
On 04/11/2017 10:48 AM, Rafael Knuth wrote:
> Thanks for the clarification.
> I tested this approach, and I noticed one weird thing:
>
> Pi_Number = str(3.14159265358979323846264338327950288419716939)
> Pi_Number = "3" + Pi_Number[2:]
> print(Pi_Number)
>
> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\CODING\Python Code\PPC_56.py ==
> 3141592653589793
>>>>
>
> How come that not the entire string is being printed, but only the
> first 16 digits?
Believe it or not, this is one of the Mysteries of Life (Monty Python
reference since that's what the language is named after... sorry).
To get what you're expecting, you could do this:
import decimal
Pi_Number =
str(decimal.Decimal(3.14159265358979323846264338327950288419716939))
in general, (binary) floats and all other things don't interact the way
we mere mortals might expect, and there's a ton of writing on that
topic. The decimal module documentation contains this pithy comment near
the top:
Decimal “is based on a floating-point model which was designed with
people in mind, and necessarily has a paramount guiding principle –
computers must provide an arithmetic that works in the same way as the
arithmetic that people learn at school.”
More information about the Tutor
mailing list