Odd or Even?

This is a discussion on Odd or Even? within the C Programming forums, part of the General Programming Boards category; Hello How can I test if a float is an odd or even number without using any header files? And ...

  1. #1
    Unregistered
    Guest

    Question Odd or Even?

    Hello

    How can I test if a float is an odd or even number without using any header files?

    And is it the same to test an int or is there a different way?

    TIA

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I'd say to use the same thing. You know,

    Code:
    if ((num %2) == 0)
    {
        printf("An even number\n");
    }
    else
    {
        printf("An odd number\n");
    }
    1978 Silver Anniversary Corvette

  3. #3
    Unregistered
    Guest

    Cool

    Cool Thanks!

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    That will not work with floating point numbers. You cannot use floats or doubles with %. Illegal use of floating point.

    You will have to use a header and one of the floating point functions provided in it - either math.h or complex.h.

    Many people treat floats and doubles as though they are integers. When you compare floating point numbers the result may not be what you expect due to precision and loss. Floats and doubles compare, add, subtract, multiply, and divide totally different than integers. If you look at the topic in depth it can really turn into a nightmare. That's where the FPU comes in which does very fast and very accurate (there is loss, but not until you are trying to get very precise numbers). In your compiler have it generate floating point code and uncheck emulation. That will cause those functions which do FP operations to generate FPU opcodes instead of trying to emulate them.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    You can just use an (int) typecast. You don't need to know the exact .xxxx of the floating to tell if it is even/odd. You just need what is before the decimal.
    1978 Silver Anniversary Corvette

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int isOddFloat( double d )
    {
        char buffer[256]={0};
        int x;
        sprintf( buf, "%f", d );
        x = buffer[strlen( buffer ) -1] - '0';
        return x % 2;
    }
    Enjoy.

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

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Yes, that is true and you know that and I know that, but I did not want him to get into the habit of typecasting every double or float to an int because eventually it will not work like he wants. The typecast will cause a lot of loss and if you are coding any 3D stuff it's not a good idea.

    But, you are correct, in this case it will work.



    Maybe I was getting too technical.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, that is true and you know that and I know that, but I did not want him to get into the habit of typecasting every double or float to an int because eventually it will not work like he wants. The typecast will cause a lot of loss and if you are coding any 3D stuff it's not a good idea.
    What are you talking about? There is no typecast involved.

    The value of the character '0' subtracted from the value of a character '0' through '9' results in an integer. No typecast at all.

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

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Code:
    /* Returns 1 if odd else 0 */
    int IsOdd (float val)
    {
        int temp;
    
        temp = (int) val;
    
        if ((temp % 2) == 1)
            return 1;
        else
            return 0;
    }
    There is a typecast involved, but since this doesn't affect the value of val there is no information lost.

  10. #10
    Unregistered
    Guest
    > There is a typecast involved, but since this doesn't affect the
    > value of val there is no information lost.

    Oh really? Where? Show me the typecast:

    int x = '5' - '0';

    Even with maximum warnings you will get no message regarding the typecast becase THER IS NO TYPECAST.

    If you ust 'x', you are getting the value of a single character. This value is an integer.

    Quzah.

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Are you sure a floating point number can be classified as an even number? That doesn't seem right. What are floats called in math lingo terms, irrational numbers? I don't see any problem with the function although I'd return a bool rather than an int, but I just never heard of testing a floating point number for odd or evenness. I think it should just be done with natural numbers. If you want to test a negative value than you can make it an absolute value, but even that is not right because a negative number is an integer.

  12. #12
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >Oh really? Where? Show me the typecast

    I was talking about my own piece code.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Originally posted by Deckard on the thread "What's the use of bitwise operators?" on January 12th 2002:
    There are real world reasons for using bitwise operators, such as converting ASCII to base64 encoding (which is used in MIME).

    Using a bitwise operator to see if a number is even or odd is much more efficient than using the modulus operator.
    Code:
    if ( number & 1 )
      printf( "odd\n" );
    else
      printf( "even\n");
    klausi
    When I close my eyes nobody can see me...

  14. #14
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Garfield's example suggested that he typecast the float to int. I'm only pointing out that in doing this you will lose precision. Quzah, your code did not have a typecast. I posted soon after reading Garfield's post, but we must have posted at or near the same time cuz you beat me to the punch. So, I was not talking about your example, but about Garfield's. I can see how the order of the posts would look as though I'm referring to your code.

    When I hit the submit reply button I noticed that it took longer than usual - u must have been posting at that time as well. Anyways, we all have valid points.

    I'm with Troll_King on this one, though. Classifying a float or double as odd or even is definitely strange and mathematically incorrect unless the numbers do not have anything after the decimal point, thus eliminating the need for using floating point numbers. This should be done with integers. Floats and doubles (floating point) are totally different from integers in a computer.

    2 and 4 are even but not 2.3212368 and 4.9510732. Like comparing apples and oranges.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help C program that reads Odd numbers and evens
    By Cyberman86 in forum C Programming
    Replies: 4
    Last Post: 02-27-2009, 10:59 AM
  2. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  3. new type: even or odd
    By nbo10 in forum C++ Programming
    Replies: 7
    Last Post: 09-05-2003, 11:17 AM
  4. Replies: 4
    Last Post: 03-03-2003, 02:52 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21