On Sun, Aug 24, 2008 at 10:05 AM, Aitor Gonzalez
<aitor69gonzalez at gmail.com>wrote:
> l:[1, 2, 3, 4];
> m:map(lambda([x],x-1.1),l);
> sublist_indices(m,lambda ([x], x=-0.1));
> sublist_indices(m,lambda ([x], x=0.9));
> sublist_indices(m,lambda ([x], x=1.9));
> sublist_indices(m,lambda ([x], x=2.9));
>
> why the first and second sublist_indices cannot find the -0.1 and 0.9
> values,
> whereas the third and fourth sublist_indices can find the 1.9 and 2.9
> values?
Because 1-1.1 is not equal to -0.1. Remember that floating-point numbers
are approximations of the form N*2^k, so cannot represent all decimal
numbers exactly. See below. If you want exact arithmetic, use rational
numbers, e.g. l - 11/10 rather than l - 1.1 and x=-1/10 rather than x=-0.1.
Or perform an approximate comparison.
-s
(%i1) [1,2,3,4]-1.1;
(%o1) [- 0.1, 0.9, 1.9, 2.9]
(%i2) %-[-0.1,0.9,1.9,2.9];
(%o2) [- 8.3266726846886741E-17, - 1.1102230246251565E-16, 0.0, 0.0]
(%i3)