checking if a number is int

Dan Bishop danb_83 at yahoo.com
Sun Apr 7 00:27:40 EST 2002


Paul Rubin <phr-n2002a at nightsong.com> wrote in message news:<7xvgb4to7h.fsf at ruckus.brouhaha.com>...
> Johannes Gijsbers <taradino at softhome.net> writes:
> > That's not really a good idea, as math.sqrt() returns a float. I'd say:
> > 
> > >>> import math
> > >>> def isInt(number):
> > ...     return int(number) == number
> > ...
> > >>> isInt(math.sqrt(289))
>  1
> > >>> isInt(math.sqrt(315))
> > 0
> 
> isInt(math.sqrt(1e100)) doesn't work so well.

As it shouldn't.  In CPython 2.2, math.sqrt(1e100) is 7 629 769 841
091 887 003 294 964 970 946 560 higher than its actual value.  With an
absolute error this high, who cares about whether there's a fractional
part?



More information about the Python-list mailing list