8 messages
Open this post in threaded view
|

Open this post in threaded view
|

## Re: [NoW] About numeric for-loop

Open this post in threaded view
|

## Re: [NoW] About numeric for-loop

 > On Jan 17, 2019, at 12:44 AM, Dirk Laurie <[hidden email]> wrote: > > Put all the clever tricks in a function 'loop' and use the generic for: > >   for v in loop(e1,e2,e3) do Like this ? > function loop(e1,e2,e3) >>     local i, n = -1, math.floor((e2-e1)/e3) >>     if e1 + n * e3 == e2 then n = n+1 end >>     return function() >>         i = i + 1 >>         if i < n then return e1 + i*e3 end >>     end >> end > > for k in loop(1,2,1/6) do print(k) end 1.0 1.1666666666667 1.3333333333333 1.5 1.6666666666667 1.8333333333333 2.0 Avoiding accumulated steps rounding error also work: > for k = 6,12 do print(k/6) end 1.0 1.1666666666667 1.3333333333333 1.5 1.6666666666667 1.8333333333333 2.0 >
Open this post in threaded view
|

## Re: [NoW] About numeric for-loop

 >> function loop(e1,e2,e3) >>>    local i, n = -1, math.floor((e2-e1)/e3) >>>    if e1 + n * e3 == e2 then n = n+1 end >>>    return function() >>>        i = i + 1 >>>        if i < n then return e1 + i*e3 end >>>    end >>> end Uh ... above can be simplified: function loop(e1, e2, e3)     local i, n = -1, (e2-e1)/e3     return function()         i = i + 1         if i <= n then return e1 + i*e3 end     end end
Open this post in threaded view
|

## Re: [NoW] About numeric for-loop

 On 2019-01-17 11:52 a.m., Albert Chan wrote: >>> function loop(e1,e2,e3) >>>>     local i, n = -1, math.floor((e2-e1)/e3) >>>>     if e1 + n * e3 == e2 then n = n+1 end >>>>     return function() >>>>         i = i + 1 >>>>         if i < n then return e1 + i*e3 end >>>>     end >>>> end > Uh ... above can be simplified: > > function loop(e1, e2, e3) >      local i, n = -1, (e2-e1)/e3 >      return function() >          i = i + 1 >          if i <= n then return e1 + i*e3 end >      end > end > See also the implementation details of https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
Open this post in threaded view
|

## Re: [NoW] About numeric for-loop

 In reply to this post by Dirk Laurie-2 On Thu, Jan 17, 2019 at 8:45 AM Dirk Laurie wrote:This is just disguised version of hardy perennial on this list: (a) relying on equality testing involving floating-point arithmetic (b) blaming Lua when it does not work as expected.  But all this will still not solve the problem that the following loop does not work as expected: > for k=1,2,1/6 do print(k) end 1.0 1.1666666666667 1.3333333333333 1.5 1.6666666666667 1.8333333333333 > IMO, this loop works "as expected". 6*(1/6) ~= 1, and everybody reading this list is aware of it  ;-) Float-comparison is a red herring which prevents you from seeing the main idea of the post.  I assume that user knows EVERYTHING about Lua numeric datatypes (floats, integers). But anyway, Lua could make a surprise due to weird internal logic of numeric "for"-loop.   But it actually makes matters worse that the objectionable behaviour is not an implementation detail but part of the specification. Exactly! This strange behavior should not be a part of the language specification! My suggestion is to add some more LOC in Lua source and make numeric "for"-loop more correct.  "More correct" means "to satisfy user's expectations more closely". For example, the loop "for x = N, N do" should work correctly whatever N is, including the case N==math.huge   Put all the clever tricks in a function 'loop' and use the generic for:    for v in loop(e1,e2,e3) doIt's too slow (to call a Lua function on every iteration).