Testing if a result n is an Integer, for large n.
I have a number x of size 2^160, and I perform the operation n = x*(x+1.5) and want to test if the result is an integer.
In python I would normally try, n.is_integer() or use an isinstance(), but this doesn't work in sage, I assume due to the fact sage integers are set up quite differently.
Upon scouring this site, I found someone recommend n is in ZZ, but this doesn't work (sage seems to loose accuracy at this level) Example:
n = 2^160
n in ZZ
Result: False
The only (terribly inefficient) way I can get this to work is to call n.divisors() and if I get the error "AttributeError: 'sage.rings.real_mpfr.RealNumber' object has no attribute 'divisors' " I know it was indeed not an integer.
There must be a better way?
n.is_integer()
should work in Sage but not in Python, I think.If you do the operation x*(x+1.5) on a number of size 2^160, the result will be a float, because you add 1.5 to it. Furthermore, it would be a standard float, so you'd only have 53 bits worth of significant digits. That's not even enough to recover the integer if it were one, let alone enough precision to determine with any degree of reliability if it might be one.
You could do instead something like
It will be up to you to decide how small the latter must be for you to believe that it's an integer.
If x is an integer to begin with then x*(x+3/2) is in fact an integer, but by computing it this way you'd get it represented as a rational ...(more)
Please provide the actual code you are running including defining x and n and your attempts to test if n is integer.