Mathematical way of determining whether a number is an integer

$\begingroup$

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

$\endgroup$ 11

6 Answers

$\begingroup$

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.

$\endgroup$ 4 $\begingroup$

Define

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

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

$\endgroup$ 4 $\begingroup$

Since no one has answered with this debatable solution, I will post it. $$f(x) := \begin{cases}1 \qquad x \in \mathbb{Z}\\ 0 \qquad x \in \mathbb{R} \setminus \mathbb{Z}\end{cases}$$ is a perfectly fine function. Even shorter would be $\chi_{\mathbb{Z}}$ defined on $\mathbb{R}$.

$\endgroup$ $\begingroup$

There is a more fundamental conceptual error in the problem, which comes from not thinking about the way that numbers are actually stored in a modern computer. Most programming languages use double precision floating point, in which a number is stored essentially as an integer with a fixed number of binary digits multiplied by a power of $2$.

This leads to all kinds of rounding errors - one of the main goals of numerical analysis is to understand and cope with these. The fact that this takes an entire subfield of mathematics shows how difficult the problem can be.

The most basic kind of error is that the representation process is not exact. For example, the number $x = 3.6$ is actually stored as approximately $$ x = 3.60000000000000008882 $$ and $y = 10*(x-3)$ is actually stored as approximately $$ y = 6.00000000000000088818. $$

To verify this, or to see what happens on your own computer/compiler combination, you can use the following C program.

#include <stdio.h>
#include <math.h>
int main() { double x = 3.6; printf("x %0.20f\n", x); double y = 10*(x - 3); printf("y %0.20f\n", y); double z = floor(y); if ( y == z ) { printf("yes\n"); } else {printf("no\n");} if ( y == 6.0 ) { printf("yes\n"); } else {printf("no\n");}
}

Now $y$ should be an integer, of course - it should be $6$. But the program will say "no" two times, because the way that $y$ is stored is not the same as the way $6$ is stored. It is almost always a mistake to test whether floating point numbers are exactly equal.

The most common way of working around this is to use an error threshold, and to claim that two numbers are equal if they are closer than the threshold. But this has its own complications. Here are just three of many websites with more information.

$\endgroup$ 1 $\begingroup$

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.

$\endgroup$ 1 $\begingroup$

Subtract $1$ from $x$ until the result is less than $1$. If result equals zero, $x$ is integer.

$\endgroup$

You Might Also Like