1

I'm trying to check if the increment from one number to the next is the same from the second to it's next and so on but found that it returns "false" when it should say "true". Here is what i found out after an hour of tinkering. Does anyone know why this happens and if it's possible to fix?

(%i1) kill(all)$;
(%i1) pointsx:[0.1,0.2,0.3,0.4,0.65,0.9]$;
(%i2) pointsx[2]-pointsx[1]; pointsx[3]-pointsx[2];
(%o2) 0.1
(%o3) 0.1
(%i4) is(pointsx[2]-pointsx[1]=pointsx[3]-pointsx[2]);
(%o4) false
(%i5) is(pointsx[2]-pointsx[1]>pointsx[3]-pointsx[2]);
(%o5) true
  • 1
    This is actually a consequence of the way floating point numbers behave. When you write 0.1, 0.2, etc., Maxima represents those with IEEE 754 floats (widely used in programming languages), and multiples of 0.1 are *not* exactly representable; actually you'll get 0.099999999999998 or something. When I try `%o2 - %o3`, I get a small number like 2.775e-17, not 0. A web search for "floating point numbers surprising behavior" will probably find some resources. – Robert Dodier Oct 20 '21 at 19:53
  • If it's important to compare numerical values for equality, my advice is to use rational numbers, which in Maxima are represented as 1/10, 2/10, 3/10, etc. Maybe you can say more about the larger problem you are trying to solve. – Robert Dodier Oct 20 '21 at 19:54

0 Answers0