[Tutor] float.is_integer()

Alan Gauld alan.gauld at yahoo.co.uk
Sun Oct 4 07:02:12 EDT 2020


On 03/10/2020 13:53, Richard Damon wrote:
> On 10/3/20 5:51 AM, Alan Gauld via Tutor wrote:
>> On 03/10/2020 01:08, Zachary Ware wrote:
>>> Have a look at the repr of 23444444444444444.7777; a 64 bit floating point
>>> number cannot represent that number exactly.
>> So basically the float is just too big to include the fractional part.
>> Hmm, I wonder where the break off point is? Time to do some math...
>> Or play with the interpreter! :-)
>>
> Python floating point numbers have 52 significant bits + implied leading
> 1 + sign bit. Numbers less than 2^52 have fractional bits, 2^52 and
> bigger can not.


And just to prove it:

>>> 2**52
4503599627370496
>>> 4503599627370496.7.is_integer()
True
>>> 4503599627370495.7.is_integer()
False
>>>



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list