i'm sure there must be a function in one of the standard headers, i'm just not sure what it is
Printable View
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.
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.
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.
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. :)Quote:
Originally Posted by manutd
Quzah.
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 rantingQuote:
Originally Posted by e66n06
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...)
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.Quote:
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.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;
there are two answers.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;
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();
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.