Page 1 of 1

[Bug?] for step on float

Posted: Wed Sep 05, 2007 3:10 pm
by newdep
Hi Lutz,

I dont understand the result below... do you have a clue?

Norman.



> (for (t 1 0.0 0.1) (println t))
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
-5.551115123e-17
-5.551115123e-17
>

Posted: Wed Sep 05, 2007 3:15 pm
by cormullion
On my Mac it's

Code: Select all

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
so I expect your platform (Linux?) is giving you an optimistic view of zero!

And you know better than I do why you get it twice...! ;-)

Posted: Wed Sep 05, 2007 3:21 pm
by newdep
Heee odd... seems to be a bug indeed... (linux newlisp 9.2.0 , 32 bits)


double screen vision, I have that also since yesterday evening ;-)

Posted: Wed Sep 05, 2007 3:24 pm
by pjot
It may be the same bug with (sequence) mentioned before:

http://www.alh.net/newlisp/phpbb/viewto ... c&start=25


Option [8].

Peter

Posted: Wed Sep 05, 2007 3:26 pm
by newdep
..Thanks..

But unacceptable..
We are unable to ever reach 0.0 under linux using floats... Handy ! ;-)

Posted: Wed Sep 05, 2007 3:29 pm
by Lutz
yes, on Windows too, but fine on Mac OS X, other BSD's and Solaris. Use format to output the numbers.

Lutz

Ps: There is no precise way to convert from decimal to binary and back. On any OS you can find numbers showing remainders on division.

Posted: Wed Sep 05, 2007 3:32 pm
by Lutz
when comparing floats you always should compare the difference of the numbers against some other small quantity. Google for: comparing floats

Lutz

Posted: Wed Sep 05, 2007 3:43 pm
by newdep
Im realy lost...

Seems newlisp is simply "Ignoring" ZERO ;-)

> (setq t 1)
1
> (for (x 10 0) (println (dec 't 0.1)))
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
1.387778781e-16
-0.1
-0.1

Posted: Wed Sep 05, 2007 4:02 pm
by Lutz
This is the same effect you where showing before. Again, there is no precise way to convert between decimal to binary and back, use formatting when displaying.

Lutz

Posted: Wed Sep 05, 2007 4:07 pm
by Lutz
for your last example there is a nice piece here:

http://www.petebecker.com/js/js200006.html

Lutz

Posted: Wed Sep 05, 2007 4:20 pm
by newdep
..yes..yes i understand the issue.. ;-)

(for (x 1.0 0.01 0.01) (println x))

That assumes almost zero for my purpose ;-)

Thanks..

Norman.

Posted: Wed Sep 05, 2007 5:19 pm
by Lutz
The difference between:

Code: Select all

(for (i 1 10) (println (dec 'x 0.1)))
and

Code: Select all

(for (i 1 0.0 0.1) (println i))
is, that newLISP when doing for loops and sequences tries to avoid accumulating the rounding error by calculating the total number of iterations first, then calculates the loop variable i as the product of 'step * count' and not as a repeated increment as in the first example. This way newLISP will never over/under run a loop, the way JavaScript would do, as shown in Peter Becker's article.

Your last example with decrementing x multiple times will produce the rounding error in pretty much any programming language after enough iterations depending on the floating point format and precision. Here the example in Python:

Code: Select all

x = 1
for i in range(1, 11):
    x -= 0.1
    print x

0.9
...
0.1
1.38777878078e-16
Lutz

newLISP shows 1.387778781e-16 because it uses only 9 digits when displaying unformatted.

Posted: Wed Sep 05, 2007 9:12 pm
by newdep
Ooo its very good that newlisp does NOT round it (visualy)
by default to 0.0 because that is even worse...

I understand the idea, no problem ..thanks for explaining..

Norman.