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

I suggest a system based on generic functions: for each type, you define a function iround(x) to convert instances to the nearest integer, and a function equals_integer(x, n) which tells if instance x should be considered equal to the integer n. Then isinteger() is true if x must be considered equal to its nearest integer. The advantage of this system is that new implementations can be added on demand for new types

In python 2, I use pkgutil to implement my generic functions.

from fractions import Fraction
from pkgutil import simplegeneric
import sys

@simplegeneric
def iround(x):
    """Round to nearest integer"""
    raise NotImplementedError

def isinteger(x):
    return equals_integer(x, iround(x))

@simplegeneric
def equals_integer(x, n):
    """Indicate if the first argument must be considered equal to the integer second argument"""
    assert isinstance(n, (int, long))
    return x == n

@iround.register(int)
@iround.register(long)
def f(x):
    return x

@iround.register(float)
def f(x):
    return int(x + (0.5 if x > 0 else -0.5))
    
@iround.register(Fraction)
def f(x):
    return x.limit_denominator(1).numerator

@equals_integer.register(float)
def f(x, n):
    assert isinstance(n, (int, long))
    return abs(x-n) < 1e-16

def main(opts, args):
    for num in [3.0, 3.14, -5.0, 7, 222222222222222222222222222222222222222222222222,
                4.0e25, 4e-25, 1+4e-25, 4e25+.5, Fraction(-21, 3), Fraction(22, 3)]:
        print repr(num), isinteger(num)
    print "end of iround.py"

""" my output -->
3.0 True
3.14 False
-5.0 True
7 True
222222222222222222222222222222222222222222222222L True
4e+25 True
4e-25 True
1.0 True
4e+25 True
Fraction(-7, 1) True
Fraction(22, 3) False
end of iround.py
"""

Interesting technique, monsieur!

Sorry mix up in terminology in my code, I mean in this part of code of course Gaussian integer, not prime. If I read up correctly for Gaussian prime I must check, if it is rational prime number of form 4 * n + 3 or if it's norm (real**2 + imag**2) is such a prime. Also that those numbers are complex factors for rational integers. To factor all numbers with Gaussian primes I need list of highest factors of each integer which I can traverse to extract complete factoring considering all assosiated values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.