5

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

  • 8
    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
  • 1
    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
  • 4
    @Jack what is a purely mathematical function for you ? – Dominic Michaelis Mar 25 '13 at 06:13
  • Yes, floor is a purely mathematical function. – xylon97 Mar 25 '13 at 06:16
  • 4
    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. –  Mar 25 '13 at 06:21
  • 2
    You are thinking about this the wrong way. Very few floating point numbers are "actually" integers. For example, in double floating point, if we take $x = 3.6$ then $10(x-3)$ is not equal to $6$ and is not equal to floor($10(x-3)$). There is no easy way to tell if a floating point is "supposed" to be an integer. In practice, you would pick a small positive $\epsilon$ and then see if your number is within $\epsilon$ of an integer, rather than trying to tell if the number is exactly an integer. This kind of practice is taught in numerical analysis courses - avoid testing doubles for equality. – Carl Mummert May 02 '18 at 14:23
  • You haven't defined how you are given the argument to your function. You seem to be assuming it is a floating point computer number, but never say so. Mathematicians have no problem with functions on the reals that recognize integers, which some of the answers show. – Ross Millikan May 02 '18 at 14:52
  • @xylon97, can you please, point to floor math representation? note, it has to be differentiable. – Serg Kryvonos May 17 '18 at 19:08
  • Also look at: https://math.stackexchange.com/a/3097244/444908 – Peter Chikov Feb 05 '19 at 16:30
  • Please, specify acceptable operators, functions. – Serg Kryvonos Mar 05 '19 at 19:42

6 Answers6

9

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.

Karolis Juodelė
  • 9,702
  • 1
  • 25
  • 39
8

Define

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

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

  • 1
    Well... Computing this would require quite a number of approximations. –  Jan 03 '16 at 17:36
  • 3
    @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 03 '16 at 17:53
  • @G.Sassatelli, maybe polymorphic typing can help to handle it w/o approximations https://github.com/ohhmm/openmind/tree/master/omnn/math – Serg Kryvonos May 17 '18 at 19:02
  • If I understand the answer correctly, you've to blindly search for an i value, right? Wouldn't it be easier to say f(x)=x mod 1, or f(x)=cos(2PIx), and the math would be easier? – Alexandre Gomes Jan 21 '19 at 18:07
4

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

TheGeekGreek
  • 7,869
2

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.

Carl Mummert
  • 81,604
0

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

0

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.

MMJZ
  • 109
  • 1
    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). – hmakholm left over Monica Dec 07 '14 at 14:44