[Bug?] for step on float
[Bug?] for step on float
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
>
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))
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
On my Mac it's
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...! ;-)
Code: Select all
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
And you know better than I do why you get it twice...! ;-)
It may be the same bug with (sequence) mentioned before:
http://www.alh.net/newlisp/phpbb/viewto ... c&start=25
Option [8].
Peter
http://www.alh.net/newlisp/phpbb/viewto ... c&start=25
Option [8].
Peter
The difference between:
and
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:
Lutz
newLISP shows 1.387778781e-16 because it uses only 9 digits when displaying unformatted.
Code: Select all
(for (i 1 10) (println (dec 'x 0.1)))
Code: Select all
(for (i 1 0.0 0.1) (println i))
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
newLISP shows 1.387778781e-16 because it uses only 9 digits when displaying unformatted.