# Mathematical way of determining whether a number is an integer

I'm developing a computer program, and I've run into a mathematical problem. This isn't specific to any programming language, so it isn't really appropriate to ask on stackoverflow. Is there any way to determine whether a number is an integer using a mathematical function, from which a boolean response is given.

For example:

let x equal 159 let y equal 12.5

f(x) returns 1 and f(y) returns 0

Please get back to me if you can. If it isn't possible, is there a similar way to determine whether a number is odd or even?

EDIT:

I found a solution to the problem thats to Karolis Juodelė. I'll use a floor function to round the integer down, and then subtract the output from the original number. If the output is zero, then the function returns 0.

I just need to make sure that floor is a purely mathematical function. Does anyone know?

Thanks

-
I don't think this question belongs here. Such a function must take into consideration the machine representation of the number. For determining whether a given (integer) number n is even of odd, you may compute n - 2 * int(n/2); if n is even it returns 0, otherwise 1. – mau Mar 24 '13 at 10:56
Let's begin with something 'simpler' : recognize that a number is $0$. See for example Richardson's paper 'How to Recognize Zero'. – Raymond Manzoni Mar 24 '13 at 11:51
If it was not about viewing from computer science and was about purely mathematical sense, to me, it will be a more interesting question (and hence answers) that how we can conclude that a given number is an integer or not. – Metin Y. Mar 24 '13 at 12:51
@Jack what is a purely mathematical function for you ? – Dominic Michaelis Mar 25 '13 at 6:13
Stackoverflow is for things like "practical, answerable problems that are unique to the programming profession" or "a specific programming problem"; your question is appropriate. In fact, with questions like this, you will almost always get better answers from programming sites than from math sites. – Hurkyl Mar 25 '13 at 6:21

The most basic thing you could do is check if $x = \text{floor}(x)$. Here $\text{floor}$ returns the integer part of a number (rounds down). It is present in standard libraries of most languages.

-
The floor function is not computable too – Dominic Michaelis Mar 24 '13 at 11:07
but since the poster must develop a computing program, that's not a problem - he only uses machine numbers :-) – mau Mar 24 '13 at 11:08

Define

$$f(x)=e^{2\pi\iota x}$$

If f(x) for any given x is 1 then x is an integer.

-
Well... Computing this would require quite a number of approximations. – G. Sassatelli Jan 3 at 17:36
@G.Sassatelli, yeah but the question was how can you mathematically say that a given number is integer or not. If you are using a computer then answers given earlier in this post are good enough. – Abhinav Soni Jan 3 at 17:53

I want to add this answer to provide something more mathematical. I'm also going to simplify the process by using the shortcuts available in programming. Any examples I use will be Java-ish code, because it's the easiest language for me to explain it.

It is correct to say that x is an integer if the 'floor(x)' is equal to x. Although this is all you'd need in any computing environment, to prove that it is mathematical requires defining properly what a 'floor' really is:

Floor(x) = max { n ∈ Z | n ≤ x }

For anyone reading this who read that and felt light-headed, it basically means the 'floor' of a value is found by cycling through the integers ( n ∈ Z ) given that ( | ) that integer is less than or equal to x. And it's simply looking for the biggest (max) integer that satisfies this.

You can program this by starting at integer value 0, seeing if it is greater than 'x', and if not, incrementing it by 1 and checking again. When the largest integer has been found, the function's boolean is answered by if said integer is equal to x. Of course, if the value you're checking is negative you will want to change this.

Have a look at this in Java for example (a 'double' data type supports decimals; an 'int' does not):

public static int floor(double x){

  int a = 0;

while( a + 1 <= x ) a++; // (a++ is the same as 'a = a + 1')

return a;


}

public static boolean isInteger(double x){

  return (Class.floor(x) == x);


}

You can quote all the symbols and such when explaining the functions, and this is how you'd implement it. The method by which you get an answer isn't very efficient or useful for big numbers, but this shows at least that it can be done with the maths intact.

This isn't relevant to the Maths forum but you might be interested in it anyway. When converting a decimal data type to an integer data type, a compiler will normally 'floor' the decimal to get the integer value anyway. To simplify your code (and save a lot of time) you may want to slip this in:

public static boolean cheatMethod(double x){

  return ((int)x == x);


}

Note: this is neither mathematically sound, nor likely to work in every language, nor relevant to this forum. It's there because it's a simple method in general for checking if a number is an integer.

By quoting the reasoning behind the 'floor()' function and adapting it for larger / negative numbers you can show that the function has a mathematical meaning. The cheatMethod() won't get you any brownie points but will also work in some languages.

-
Most programming languages/implementations don't in fact use decimals for floating-point values, but binary fractions. Also, your proposed implementation of floor() works only for positive arguments. And your description of the implied rounding when a floating-point value is converted to an integral type suffers from the same program -- most programming languages including Java will round towards 0 (that is, negative non-integers are rounded up), whereas floor always rounds down (away from 0). – Henning Makholm Dec 7 '14 at 14:44