pi not exactly representable as floating Python (same as C double platform type). The closest representable approximation is used.
Here's the exact approximation in my drawer (probably the same as on your drawer):
>>> import math >>> (math.pi / 2).as_integer_ratio() (884279719003555, 562949953421312)
To find the tangent of this relationship, I will now switch to wxMaxima:
(%i1) fpprec: 32; (%o1) 32 (%i2) tan(bfloat(884279719003555) / 562949953421312); (%o2) 1.6331239353195369755967737041529b16
Thus, in essence, it is identical to what you received. The binary approximation used to pi/2 slightly less than the mathematical value ("infinite accuracy") pi/2 . This way you get a very large tangent instead of infinity . The computed tan() is suitable for actual input!
For exactly the same reasons, for example,
>>> math.sin(math.pi) 1.2246467991473532e-16
does not return 0. The approximation of math.pi slightly smaller than pi , and the displayed result is true with the truth.
OTHER VISIONS OF VISION math.pi
There are several ways to see the exact approximation:
>>> import math >>> math.pi.as_integer_ratio() (884279719003555, 281474976710656)
math.pi exactly matches the mathematical ("infinite precision") value of this relation.
Or as an exact float in hex notation:
>>> math.pi.hex() '0x1.921fb54442d18p+1'
Or in the way that is most easily understood by almost everyone:
>>> import decimal >>> decimal.Decimal(math.pi) Decimal('3.141592653589793115997963468544185161590576171875')
While this may not be immediately obvious, each trailing binary float is accurately represented as a trailing decimal float (the opposite is not true, for example, decimal 0.1 not exactly representable as a trailing binary float), and Decimal(some_float) creates the exact equivalent.
Here's the true pi value, followed by the exact decimal value math.pi , and the carriage on the third line indicates the first digit in which they differ:
true 3.14159265358979323846264338327950288419716939937510... math.pi 3.141592653589793115997963468544185161590576171875 ^
math.pi now the same for "almost everyone" because almost all mailboxes now use the same binary floating-point format (IEEE 754 double precision). You can use any of the above methods to confirm what is in your field, or find an exact approximation in use if your box is an exception.