3
$\begingroup$

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...

$\endgroup$
  • 2
    $\begingroup$ 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]] ? $\endgroup$ – e.doroskevic Dec 22 '15 at 9:42
  • 1
    $\begingroup$ @E.Doroskevic Ok, I see the problem now. So it might make more sense to use Round[x] instead of IntegerPart, right? $\endgroup$ – skrat Dec 22 '15 at 9:47
  • 1
    $\begingroup$ Your problem is due to finite precision number representation. Take a look at FullForm[.57*100]. Round will see you right. $\endgroup$ – Yves Klett Dec 22 '15 at 9:48
  • 2
    $\begingroup$ No, @E.Doroskevic misread your code and assumed, you're taking the IntegerPart before multiplying by 100. Round may make more sense, of course. $\endgroup$ – LLlAMnYP Dec 22 '15 at 9:49
6
$\begingroup$

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].

$\endgroup$
2
$\begingroup$
IntegerPart[0.57*100]
(* 56 *)
0.57*100
(* 57. *)
InputForm@%
(* 56.99999999999999 *)
$\endgroup$
0
$\begingroup$

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}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.