# EvenQ not working properly on IntegerPart[real number]

Something fascinating is happening at the moment.

force[[All, 6]]={0.5, 0.55, 0.56, 0.57, 0.58, 0.76, 0.76, 0.76, 0.76, 0.76, \
0.76, 0.76, 0.76, 0.76, 0.76, 0.79, 0.79, 0.79, 0.79, 0.79, 0.8, 0.8, \
0.8, 0.9, 0.96, 0.97, 0.98, 0.99}

Above you will find a table of my numbers. I want to check if the number on i th place is even or not! I tried to do that with the following code:

i = 2;
EvenQ[IntegerPart[force[[i, 6]]*100]]
Out[164]= False

and the output is ok, since the second number in the table is 0.55 which is odd and not even. However, 4th number in the table is also odd, yet not according to mathematica:

i = 4;
EvenQ[IntegerPart[force[[i, 6]]*100]]

Out[162]= True

Dear god! I am a bit lost what went wrong here and what to do...

• All of them are true since your code would multiply all 0 by 100 and then return 0 as an output. For all of the numbers in your collection force. What do you mean by force[[All,6]] ? – e.doroskevic Dec 22 '15 at 9:42
• @E.Doroskevic Ok, I see the problem now. So it might make more sense to use Round[x] instead of IntegerPart, right? – skrat Dec 22 '15 at 9:47
• Your problem is due to finite precision number representation. Take a look at FullForm[.57*100]. Round will see you right. – Yves Klett Dec 22 '15 at 9:48
• No, @E.Doroskevic misread your code and assumed, you're taking the IntegerPart before multiplying by 100. Round may make more sense, of course. – LLlAMnYP Dec 22 '15 at 9:49

This is because you are working with floating point numbers. Welcome to the world of floating point arithmetic!

The computer represents floating point numbers in binary, not decimal. The actual number stored for 0.57 is not precisely 0.57, but slightly smaller. Why? Because 0.57 cannot be represented exactly in binary, just like 1/3 = 0.33333333... cannot be represented exactly in decimal. 0.33333333 is smaller than 1/3, right?

Thus IntegerPart[0.57*100] is 56 and not 57.

You can get the exact binary representation using RealDigits[0.57, 2].

IntegerPart[0.57*100]
(* 56 *)
0.57*100
(* 57. *)
InputForm@%
(* 56.99999999999999 *)

Input:

(*Where t is your list*)
Flatten[EvenQ @ IntegerPart @ {t[[#]]*100} & /@ Range @ Length @ t, 1]

Output:

{True, False, True, True, False, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False, True, True, True, True, True, False, True, False}

• But he is having IntegerPart[x*100], not IntegerPart[x]*100 ... – Szabolcs Dec 22 '15 at 9:54
• @Szabolcs I just realised that I have misread the code. Thanks for pointing out. – e.doroskevic Dec 22 '15 at 9:57
• Too many [ and ]! :) It's too easy to misread all those [[s ... – Szabolcs Dec 22 '15 at 9:58
• @Szabolcs I think the above looks better now! :D – e.doroskevic Dec 22 '15 at 10:02