i'm sure there must be a function in one of the standard headers, i'm just not sure what it is
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; i'm sure there must be a function in one of the standard headers, i'm just not sure what it is...
i'm sure there must be a function in one of the standard headers, i'm just not sure what it is
What is "something"? What type of data? I assume you mean a string. Use isdigit in a loop. Use sscanf. Use...
Quzah.
Hope is the first step on the road to disappointment.
well i have the following integers: k, t and e
i want to see if (1 + k*t)/e gives an integer value
All integer maths yield other integers.
Quzah.
Hope is the first step on the road to disappointment.
no it does not
both 2 and 3 are integers
however 3/2 is not an integer
it's 1.5
No it isn't.You lose.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.
Last edited by quzah; 01-18-2007 at 06:19 PM.
Hope is the first step on the road to disappointment.
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.
Maybe you should compile that, because you're wrong.Originally Posted by manutd
Quzah.
Hope is the first step on the road to disappointment.
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?
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.
can anyone actually answer my question please, instead of rantingOriginally Posted by e66n06
Whoopsy sorry 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...)
but i what if i am not using integer math, like i am asking
Rubbish. All rounding of integers in C and C++ truncates towards zero.Originally Posted by manutd
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.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;
there are two answers.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;
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 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();
Last edited by grumpy; 01-18-2007 at 06:37 PM.
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.