[Bug?] for step on float

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

[Bug?] for step on float

Post 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
>
-- (define? (Cornflakes))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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...! ;-)

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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 ;-)
-- (define? (Cornflakes))

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

..Thanks..

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

when comparing floats you always should compare the difference of the numbers against some other small quantity. Google for: comparing floats

Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

for your last example there is a nice piece here:

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

Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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.
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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.
-- (define? (Cornflakes))

Locked