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

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    54

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    well i have the following integers: k, t and e

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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All integer maths yield other integers.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    no it does not

    both 2 and 3 are integers

    however 3/2 is not an integer

    it's 1.5

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 01-18-2007 at 06:19 PM.
    Hope is the first step on the road to disappointment.

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    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.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    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?

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    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.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    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?
    can anyone actually answer my question please, instead of ranting

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    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...)
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    54
    but i what if i am not using integer math, like i am asking

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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
    there are two answers.

    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();
    Last edited by grumpy; 01-18-2007 at 06:37 PM.

  15. #15
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  4. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM