i'm sure there must be a function in one of the standard headers, i'm just not sure what it is

Printable View

- 01-18-2007e66n06How 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-2007quzah
What is "something"? What type of data? I assume you mean a string. Use isdigit in a loop. Use sscanf. Use...

Quzah. - 01-18-2007e66n06
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-2007quzah
All integer maths yield other integers.

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

both 2 and 3 are integers

however 3/2 is not an integer

it's 1.5 - 01-18-2007quzah
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;

}

All integer math produce integers in C.

Quzah. - 01-18-2007manutd
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-2007quzahQuote:

Originally Posted by**manutd**

Quzah. - 01-18-2007e66n06
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-2007Wraithan
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-2007e66n06Quote:

Originally Posted by**e66n06**

- 01-18-2007manutd
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-2007e66n06
but i what if i am not using integer math, like i am asking

- 01-18-2007grumpyQuote:

Originally Posted by**manutd**

Quote:

Originally Posted by**manutd**

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**

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();

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-2007Wraithan
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.