Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction.
It does if it dosen’t have a decimal. If it has decimal then it automatically isn’t and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy.
Just in case anyone was looking for a decent way to do it…
if (((number/2) - round(number/2)) == 0) return true; return false;
Or whatever the rounding function is in your language of choice.
EDIT: removed unnecessary else.
Modulo operator my dude.
Or modulo %
Huh?
return number % 2 == 0
That’s the only sane solution.
Do note how I said “a decent” way, not “the best” way. Get that huh outta here.
number % 2 == 0 and (number & 0b1) == 0
Are the only sane ways to do this. No need to floor. Although If its C and you can’t modulo floats then (number/2 == floor(number/2))
If you are using floats, you really do not want to have an isEven function …
Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction.
No. The alternative is to not use a float. Testing if a float is even simply does not make sense.
Even testing two floats for equality rarely makes sense.
What is the correct output of isEven((.2 + .4) ×10)
Hint: (.2 + .4) x 10 != 6
It does if it dosen’t have a decimal. If it has decimal then it automatically isn’t and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy.
Take out the
else
and I’m inValid point.