# Test if number represents an integer

I can't figure out how to test if a number is an integer.

I have found IntegerQ, but this function only checks if the type of the input is an integer, not whether the number it represents actually is one (IntegerQ[3.0] returns False).

Element[3.0, Integers] also does not return True.

I'm probably missing something obvious. There must be an easy way to do this.

• check if FractionalPart is zero, or smaller than some epsilon Jun 4 '17 at 12:13
• Closely related: (7463) Jun 4 '17 at 12:42

you can try this:

integerQ[x_] := x == Round[x]

• One has to be aware of the approximations that the Equal function makes, though. 1.000000000000001 == Round[1.000000000000001] is true, but 0.000000000000001 == Round[0.000000000000001] is false. Jun 4 '17 at 14:50
• Round@Root[1 + 3 # + 4 #^2 + 2 #^3 + #^4 &, 1, 0] runs into "Internal precision limit". The use of Round makes near-half-integers an issue, which they need not be as they are clearly non-integers.
– Blrp
Jun 27 at 15:00
• Although Round@N@Root[1 + 3 # + 4 #^2 + 2 #^3 + #^4 &, 1, 0] does not run into this issue.
– Blrp
Jun 27 at 18:46
on suggestion of @george2079
checkInteger[num_] := (# === 0. || # === 0) &@FractionalPart[num]

• Can you explain why you didn't just use # == 0 instead of (# === 0. || # === 0)? Jun 4 '17 at 12:38
• @mistercake for your problem it does not matter. you can choose to use == (Equal) instead of === (SameQ). SameQ is usually used to check if two expressions are identical in form. for example 3.0 == 3 will yield True whereas 3.0===3 will yield False Jun 4 '17 at 12:46
• @mistercake also checkInteger[num_] := FractionalPart[num] === 0 will not work properly if you use checkInteger[3.0] because 0. === 0 returns False. You can use checkInteger[num_] := FractionalPart[num] == 0 instead Jun 4 '17 at 12:49
• Does not always, work, for example N[E^(-I Pi/4) + E^(I Pi/4) + E^(-3 I Pi/4) + E^(3 I Pi/4)] yields 2.22045*10^-16 + 0. I and FractionalPart yields the same, so this will not be found to be identical to zero.
– Blrp
Jun 27 at 18:55

Define

intQ = IntegerQ@Rationalize@# &;


Then

intQ@3.0


True

and

intQ@2.2


False

• I like using the equivalent Composition version: intQ = IntegerQ @* Rationalize Jun 4 '17 at 16:29

You can also check if your number mod 1 is zero

• This method has a significant bias in its false positives though. Using f[x_] := (Mod[x, 1] == 0), f[100000000000.000000001] is True while f[99999999999.999999999]. While this isn't likely to be an actual issue in most applications, it is worth remembering that values only slightly above an integer may be reported as integers using this method, while values that are slightly below will almost certainly not be. Every other answer here reports at least one false positive on those 2 numbers though. Jul 10 '19 at 19:37