While looking into Gaussian primes I came to think additional ways to check for is number integer or not, what you think is best way?
epsilon = 1e-11
def is_integer(n):
""" traditional abs, epsilon check for near integer """
return abs(n-round(n)) < epsilon
def is_integer2(n):
return (float(n)- int(n)).as_integer_ratio()[1] == 1
def is_integer3(n):
s = str(float(n))
return 'e-' not in s and '.' not in s.rstrip('0.')
for number in (.00001, .000001, 1 + .000001 * .000001, 1.0000000001,
2.99999999999, 123123342345.000001,
3423141+0j, 123123 + 2342.001j,
263812638621836812736182631872361):
print 'Number to test = %s' % number
for fun in is_integer2, is_integer3, is_integer:
try:
print fun.__name__, fun(number)
except TypeError as e:
print e
print 'Is Gaussian prime:', fun(number.real) and fun(number.imag)
print
is_integer2 False
is_integer3 False
is_integer False
Number to test = 1e-06
is_integer2 False
is_integer3 False
is_integer False
Number to test = 1.0
is_integer2 False
is_integer3 True
is_integer True
Number to test = 1.0000000001
is_integer2 False
is_integer3 False
is_integer False
Number to test = 2.99999999999
is_integer2 False
is_integer3 False
is_integer False
Number to test = 1.23123342345e+11
is_integer2 True
is_integer3 False
is_integer True
Number to test = (3423141+0j)
is_integer2 can't convert complex to float
Is Gaussian prime: True
is_integer3 can't convert complex to float
Is Gaussian prime: True
is_integer can't convert complex to float
Is Gaussian prime: True
Number to test = (123123+2342.001j)
is_integer2 can't convert complex to float
Is Gaussian prime: False
is_integer3 can't convert complex to float
Is Gaussian prime: False
is_integer can't convert complex to float
Is Gaussian prime: False
Number to test = 263812638621836812736182631872361
is_integer2 True
is_integer3 False
is_integer True