From: julio@diegidio.name   
      
   On Monday, 28 November 2022 at 04:23:28 UTC+1, Tim Rentsch wrote:   
   > Julio Di Egidio writes:   
   > > On Monday, 21 November 2022 at 21:45:34 UTC+1, Ben Bacarisse wrote:   
   > >   
   > >> Consider any ordered measure that "wraps round" -- bearings in degrees,   
   > >> minutes in the hour, indeed hours in either the 12 or 24 hour clock.   
   > >> The problem is to determine if a given value is in the sub-range   
   > >> specified by a start and an en value.   
      
      
   > > In terms of remainder (as in JS), MOD looks like this:   
   > >   
   > > ```js   
      
   Should have been "ts", i.e. TypeScript (for the few type annotations:   
   get rid of those to get the plain JS).   
      
   > > ```js   
   > > function MOD(x: number, m: number): number {   
   > > if (x * m === 0) { return 0; } // allow for MOD(x, 0)   
      
   BTW, better not do that multiplication:   
      
   ```js   
    if (m === 0) { return 0; } // allow for m = 0   
    if (x === 0) { return 0; } // a short-cut   
   ```   
      
   Moreover, notice that this MOD fails (in particular, does not   
   return positive) for m <= 0. (!!) But I won't bother with that now.   
      
   > > if (x > 0) { x = x % m; }   
   > > else { x = (m + x % m) % m; }   
   > > return x;   
   > > }   
   > > ```   
   > This proposed function doesn't work. Consider a curfew that   
   > starts at 10 pm (2200) and goes until 5 am (0500). Is 3 am   
   > (0300) a curfew violation? The call to your function would be   
   >   
   > inOpenModRange( 0300, 2200, 0500, 2400 );   
   >   
   > which yields false. But it should yield true.   
      
   Argh! But you should not have snipped that "beware   
   of bugs", that was the most important part!! ;)   
      
   This one should do the trick:   
      
   ```ts   
   /** Returns whether x in [lo, hi[ (mod m). */   
   function inOpenModRange(   
    x: number, lo: number, hi: number, m: number   
   ): boolean {   
    let x_ = MOD(x - lo, m);   
    let hi_ = MOD(hi - lo, m);   
    return x_ < hi_;   
   }   
   ```   
      
   Still untested, I hope I have not botched it again...   
      
   Julio   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|