Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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

share|improve this question
6  
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
3  
$-4$, really? I've seen way worse questions which haven't gotten a single downvote. –  Git Gud Mar 24 '13 at 11:00
2  
@GitGud I gave an sympathy upvote, he said why he think that it belongs here, i only see 4 downvotes, no close vote. Guys tell him what he is doing wrong, he is new here how shall he know it ? –  Dominic Michaelis Mar 24 '13 at 11:01
1  
@Jack what is a purely mathematical function for you ? –  Dominic Michaelis Mar 25 '13 at 6:13
2  
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
show 3 more comments

2 Answers

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.