for / while else doesn't make sense

Mark Dickinson mdickinson at enthought.com
Mon May 23 16:17:44 EDT 2016


Ben Bacarisse <ben.usenet <at> bsb.me.uk> writes:
> [1] Not being a Python expert I don't know how you show that actual
> value of a float.  What is the Pythonic way to do that?

I don't know about Pythonic, but here are some options.

1. Convert the float to Decimal, and print the result. This shows
   the exact binary value that's stored, but displays it in decimal.
   Be aware that the result will be hundreds of digits long for
   very large or very small floats.

   >>> print(Decimal(pi))
   3.141592653589793115997963468544185161590576171875

2. If you're happy with a hexadecimal representation, use the
   float.hex method. Again, this shows the exact value stored.

   >>> print(pi.hex())
   0x1.921fb54442d18p+1

3. To get an equivalent fraction, convert to the fractions.Fraction
   type or use the as_integer_ratio method.

   >>> from fractions import Fraction
   >>> print(Fraction(pi))
   884279719003555/281474976710656
   >>> print(pi.as_integer_ratio())
   (884279719003555, 281474976710656)

-- 
Mark (who should probably take a numerical methods class someday)




More information about the Python-list mailing list