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
Printable View
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
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");
}
Cool Thanks!
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.
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.
Enjoy.Code:int isOddFloat( double d )
{
char buffer[256]={0};
int x;
sprintf( buf, "%f", d );
x = buffer[strlen( buffer ) -1] - '0';
return x % 2;
}
Quzah.
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.
What are you talking about? There is no typecast involved.Quote:
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.
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.
There is a typecast involved, but since this doesn't affect the value of val there is no information lost.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.
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.
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.
>Oh really? Where? Show me the typecast
I was talking about my own piece code.
Originally posted by Deckard on the thread "What's the use of bitwise operators?" on January 12th 2002:
klausiQuote:
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");
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.