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


Solution 1:

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.

Solution 2:

Define

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

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

Solution 3:

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}$.

Solution 4:

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.

  • https://bitbashing.io/comparing-floats.html

  • https://blogs.sas.com/content/iml/2012/06/25/programming-tip-avoid-testing-floating-point-values-for-equality.html

  • https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/