-1

It is not a problem to check a small number for whether it is a float or an integer

>>> 4.0.is_integer()
True
>>> 4.123.is_integer()
False
if a - int(a) == 0:
    print('Integer')
else:
    print('Not Integer')

But when I have a large number, it does not work anymore:

>>> 31231242354234534534534534534534534534534534535434645755453543543453534534534534534535345346756423423.111.is_integer()
True

I would like to check very many and very large numbers, and the results of my calculations are floating-point numbers. I want to check if the result is an integer. For large numbers, the conventional methods do not work.

wim
  • 338,267
  • 99
  • 616
  • 750
Mr J
  • 57
  • 1
  • 1
  • 6
  • Check whether the modulo 1 division remainder is zero. Alternatively, integer-divide by 1 and check if it makes a difference. – Ulrich Eckhardt Dec 23 '18 at 10:45
  • 5
    For your purpose, is `4.00` an `int` or a `float`? – Mortz Dec 23 '18 at 10:50
  • 2
    I'm unsure, what your input format is. If it is already a Python variable all proposed ways work. If it is text a search for a decimal separator may help. – guidot Dec 23 '18 at 10:57
  • @guidot `a - int(a) == 0` is `True` for large floats. It's certainly true for the `b` in the question. – Tomalak Dec 23 '18 at 11:09
  • Why do you say it `.is_integer()` does not work for "laaaarge" numbers? For the given example it worked flawlessly. Can you provide an example that shows the contrary? – Lukasz Tracewski Dec 23 '18 at 11:11
  • 3
    Please don't add "Edit" sections to your question. Overwork it to make it clearer what you want. Also, your latest edit claims that you want `5.243` to be considered as integer!? – Ulrich Eckhardt Dec 23 '18 at 11:26
  • 3
    Where do these large values come from, and how are you storing them? If you are using the standard Python `float` (which is IEEE 754 64 bit floating point), then any value with magnitude greater than `2**52` is an integer. There are only finitely many numbers representable as 64 bit floating point. The subset of those numbers greater than 2**52 are integers. – Warren Weckesser Dec 23 '18 at 11:45

6 Answers6

2

Using floats, it's not possible with numbers of the magnitude shown in your example, because eventually the precision of floating point becomes too coarse to distinguish the integer from the float. For example:

>>> 100000000000000000000000.5 == \ 
... 100000000000000000000000.0                                                                                                                                        
False
>>> 1000000000000000000000000.5 == \ 
... 1000000000000000000000000.0                                                                                                                                       
True

Both inputs were parsed to identical numbers. If you need the granularity to distinguish such values, parse them from strings into a different type such as Decimal.

For high precision calculations, you may also be interested in using a multiple-precision arithmetic library such as gmpy2.

wim
  • 338,267
  • 99
  • 616
  • 750
  • 1
    And to be clear, "get in before the numbers have been evaluated and access the original source" should usually mean "rewrite your source code to stop using floats for this", not "kludge together something with source inspection". – user2357112 Dec 23 '18 at 11:18
0

Simply checking type might be misleading. If you're trying to solve a mathematical problem where your function spills continous range of numbers and you want to check which are integers, then type(a) likely won't work for you.

Solution: use is_integer instead.

a = 31231242354234534534534534534534534534534534535434645755453543543453534534534534534535345346756423423.0
print('Type: ',type(a))
print('Is integer: ',a.is_integer()) 

The first check gives you float, while the other tells the number is integer.

Lukasz Tracewski
  • 10,794
  • 3
  • 34
  • 53
0

You can use isinstance. Here is example:

a = 100
print(isinstance(a, int))

It will print True or False values depends of value type for a variable (True in this case above).

print(isinstance(a, float))

In example above it will print False

0

The length = (int) (math.log10(a) + 1); will return the length of integer only.

Try it here: https://onlinegdb.com/Sk1jmeTxV

import math
a= 31231242354234534534534534534534534534534534535434645755453543543453534534534534534535345346756423423

b = 31231242354234534534534534534534534534534534535434645755453543543453534534534534534535345346756423423.111

length = (int) (math.log10(a) + 1);

if length ==len(str(abs(a))): 
   print ('is a perfect int');
I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37
0

Likely you get the number as string from the input or file, and you can use the decimal module:

    import decimal

    ctx=decimal.Context(prec=200)

    b=decimal.Decimal("31231242354234534534534534534534534534534534535434645755453543543453534534534534534535345346756423423.111")                                                                                                            

    print( ctx.divmod(b,1) ) 
    Out: Decimal('31231242354234534534534534534534534534534534535434645755453543543453534534534534534535345346756423423'), Decimal('0.111'))

    print(ctx.divmod(b,1)[1].is_zero())    # integer?                                                                               
    Out: False
kantal
  • 2,331
  • 2
  • 8
  • 15
0

I want to check if the cube root of a number "x" is an integer.

The result of a root calculation is a floating-point number.

For example, the following numbers "y" should be checked:

x = 12341254534XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
y = x**(1/3)  # y = cube root of x

y = 6456535XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.2146544753325
or
y = 6456535XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.0
or 
y = 6456535XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.99999999999646546547

This does not work for these large numbers.

Mr J
  • 57
  • 1
  • 1
  • 6