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

Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last
• 01-18-2007
e66n06
How can I test if something is an integer or not?
i'm sure there must be a function in one of the standard headers, i'm just not sure what it is
• 01-18-2007
quzah
What is "something"? What type of data? I assume you mean a string. Use isdigit in a loop. Use sscanf. Use...

Quzah.
• 01-18-2007
e66n06
well i have the following integers: k, t and e

i want to see if (1 + k*t)/e gives an integer value
• 01-18-2007
quzah
All integer maths yield other integers.

Quzah.
• 01-18-2007
e66n06
no it does not

both 2 and 3 are integers

however 3/2 is not an integer

it's 1.5
• 01-18-2007
quzah
No it isn't.
Code:

```#include<stdio.h> int main( void ) {     int a = 2, b = 3, c = b / a;     printf( "c is %d\n", c );     return 0; }```
You lose.

All integer math produce integers in C.

Quzah.
• 01-18-2007
manutd
Keep in mind (so you don't have to compile) that this will be 2. In C++, every floating point number computed using integer math is rounded up. So, 61/5 (12.2) would be 13 using integer math.
• 01-18-2007
quzah
Quote:

Originally Posted by manutd
Keep in mind (so you don't have to compile) that this will be 2.

Maybe you should compile that, because you're wrong. :)

Quzah.
• 01-18-2007
e66n06
why are you getting so angry?

lets say k, t and e are floating point numbers then

how can i test if the result of (1 + k*t)/e gives an integer value please?
• 01-18-2007
Wraithan
There is no rounding. It is truncation, plus it is a valuable lesson for the OP to compile that and try and figure it out (or better yet get a book that tells you that integer math always results in a truncated integer.

Quzah is not angry, he is just tired of the same questions over and over again.
• 01-18-2007
e66n06
Quote:

Originally Posted by e66n06
why are you getting so angry?

lets say k, t and e are floating point numbers then

how can i test if the result of (1 + k*t)/e gives an integer value please?

• 01-18-2007
manutd
Whoopsy sorry :o I should have said: Keep in mind (so you don't have to compile) that this will be 1. In C++, every floating point number computed using integer math is truncated. So, 61/5 (12.2) would be 12 using integer math. And the answer has been given. IT ALWAYS WILL BE AN INTEGER! (sorry for yelling, but, well...)
• 01-18-2007
e66n06
but i what if i am not using integer math, like i am asking
• 01-18-2007
grumpy
Quote:

Originally Posted by manutd
Keep in mind (so you don't have to compile) that this will be 2.

Rubbish. All rounding of integers in C and C++ truncates towards zero.
Quote:

Originally Posted by manutd
In C++, every floating point number computed using integer math is rounded up. So, 61/5 (12.2) would be 13 using integer math.

This shows how deep your misunderstanding is. Division of two integer values (by which I mean values that are of integral type) does not involve anything being computed as a floating point value. For two integers a and b, computing a/b is performed using integer arithmetic and there is never an intermediate value calculated that is of a floating point type.

Now, to come back to the problem which seems to be e66n06's mind, even if he worded it poorly (and because he doesn't think he is doing integer maths, but he is;
Quote:

Originally Posted by e66n06
well i have the following integers: k, t and e

i want to see if (1 + k*t)/e gives an integer value

The first is that, as others have said, using integer arithmetic, the result (1+k*t)/e will always yield a result that is an integer, as long as no overflow or division by zero occurs (an example would be if k*t is too large to be stored in an int on the machine).

The second is to compute the remainder. For example;
Code:

```/*  Assume we have values k, t, and e and all are non-zero */     int result = (1+k*t)/e;    /* assume no overflow */     int e_times_result = e*result;     int remainder = e_times_result - (1 + k*t);     if (remainder != 0)         there_is_a_nonzero_remainder();```
A more practical way is to avoid doing the division in the first place. For example, and simply compute the remainder directly.
Code:

```/*  Assume we have values k, t, and e and all are non-zero */     int remainder = (1+k*t) % e;     if (remainder != 0)         there_is_a_nonzero_remainder();```
• 01-18-2007
Wraithan
Ok, we are not ranting, simply telling you the truth of things from the first question.

You can cast into an int then compare, I just Googled there are plenty of solutions to your problem, perhaps reading the link in my signature and becoming more enlightened will show you the true path to the answers you seek.
Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last