# How can I test if something is an integer or not?

This is a discussion on How can I test if something is an integer or not? within the C Programming forums, part of the General Programming Boards category; all i want to do is test if something like: (1+ 6.0*7.0)/8.0 gives me an integer or not is there ...

1. all i want to do is test if something like: (1+ 6.0*7.0)/8.0 gives me an integer or not

is there a function for it?

2. I think for what the OP is actually trying to do, all his variable should be floats, that way his expression would be a float.

If this is the case, after your reach your float result, you can see if it is an integer by subtracting its floor value and seeing if the result is zero or not.

3. grumpy - cool thanks ;D

4. Originally Posted by e66n06

While you're at it, learn some math, and you'll have your answer.

2 = 10 / 5
5 = 10 / 2
10 = 2 * 5

Now, given your bit of math, create yourself a test scenario to see if the values you get wind up the same. If they are, then all of your math was perfect little integers, and you didn't have any decimals.

Quzah.

5. Originally Posted by e66n06
well i have the following integers: k, t and e
And you're not using integer math?
Originally Posted by grumpy
Rubbish. All rounding of integers in C and C++ truncates towards zero.
I made a mistake, since corrected.
Originally Posted by grumpy
does not involve anything being computed as a floating point value
Bad choice of words on my part. I meant "everything that would be a floating point number using normal math is truncated using integer math."

6. Originally Posted by manutd
Bad choice of words on my part. I meant "everything that would be a floating point number using normal math is truncated using integer math."

"Normal mathematics" has nothing to do with floating point numbers. The mathematics for division of real values has some different characteristics from division of integers. Mainly because integers are discrete values, but real values can take any value on a continuum.

Consider the problem : we have two values a and c, with a > c and need to find a value b such that a>b>c. With a and b being integers, no result is possible if c = a+1 as there is no integer between a and c With real values, is it guaranteed a value b will exist, as (a+c)/2 will always satisfy the required a>b>c.

Floating point types are just a means by which computers approximately represent real numbers. One characteristic is that they cannot represent all real values. So it is possible to have floating point values a and c with a > c, and for it to be impossible to find any floating point value b that is between them.

7. Originally Posted by manutd
And you're not using integer math?

no, i was not using integer math, sorry for not making that clear XD

8. Originally Posted by e66n06
no, i was not using integer math, sorry for not making that clear XD
If my first post in this thread helped you, you are using integer math.

9. Please stop nitpicking. You know what I mean.

10. Originally Posted by e66n06
no, i was not using integer math, sorry for not making that clear XD
in that case you cannot use ==
use a-b < delta && a- b > - delta

where delta is small enough (like 0.00001)

11. If you are wanting to know if the value of something will be evenly divisible by another, try % in place of /. % is the mod operator and returns the remainder of a divide.

However, your question is worded in a strange, ambiguous manner. What everyone is telling you is that in C, any int / int = int PERIOD -- there is no such thing as int / int = float, unless you did (float)((float) int / (float) int) = float. I believe, however, that your question is not int but whole number, this is what I have answered.

12. Bear in mind that you can not use the modulus on floating point numbers.

13. Originally Posted by grumpy
If my first post in this thread helped you, you are using integer math.

you post helped cos i was using integer maths to solve the problem. but i was not using integer maths in the sense quzah mentioned them as i am interested in the remainder.

14. Computing the remainder on dividing two integers involves only integer mathematics.

15. Originally Posted by vart
in that case you cannot use ==
use a-b < delta && a- b > - delta

where delta is small enough (like 0.00001)
I believe an integer of sufficiently small absolute value can be represented exactly as a floating-point type of given size. For example, an IEEE 754 32-bit float uses 23 bits for the mantissa (and 8 for the exponent, and 1 for the sign bit), and 2^23 is roughly 8 million, so I believe integers between roughly plus and minus 4 million can be represented exactly as such a float. In this case, it would be okay to use ==. If that's not enough precision, an IEEE 754 64-bit double uses (52, 11, 1) bits for the 3 fields, so should be able to represent integers between roughly plus and minus 2^51.

Page 2 of 4 First 1234 Last