[Tutor] float.is_integer()

Richard Damon Richard at Damon-Family.org
Fri Oct 2 20:14:59 EDT 2020


On 10/2/20 1:07 PM, Manprit Singh wrote:
> Dear Sir,
>
> Kindly look at the below experiment done on python IDLE Shell :
>
> (2.0).is_integer()
> True
> (2.4).is_integer()
> False
> (234.77777777777777).is_integer()
> False
> (23444444444444444.7777).is_integer()
> True
> (234444444444444.73).is_integer()
> False
> (23444444444444444444444.73).is_integer()
> True
>
> Just need to know why the 4th and 6th lines return True, It should be
> False. Is
> there any limitation on using this float.is_integer() method ?
>
> Regards
> Manprit singh

The basic answer is that floating point numbers have only finite
precision, so numbers of large enough magnitude will no longer have any
fractional bits.

The number 23444444444444444.7777 is big enough that the 0.777 can't be
represented, so the number you actually get is just 23444444444444444.0
which is of course is the value of an integer.

-- 
Richard Damon



More information about the Tutor mailing list