home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.programming      Programming issues that transcend langua      57,431 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 56,686 of 57,431   
   Julio Di Egidio to Ben Bacarisse   
   Re: A little puzzle.   
   27 Nov 22 02:27:06   
   
   From: julio@diegidio.name   
      
   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.   
      
   I don't think much better can be done of the obvious implementation:   
      
   ```js   
   function inOpenModRange(x: number, lo: number, hi: number, m: number): boolean   
   {   
       let x_ = MOD(x, m);   
       let lo_ = MOD(lo, m);   
       let hi_ = MOD(hi, m);   
       return lo_ <= x_ && x_ < hi_;   
   }   
   ```   
      
   where MOD is modulo, not remainder.   
      
   In terms of remainder (as in JS), MOD looks like this:   
      
   ```js   
   function MOD(x: number, m: number): number {   
       if (x * m === 0) { return 0; }  // allow for MOD(x, 0)   
       if (x > 0) { x = x % m; }   
       else { x = (m + x % m) % m; }   
       return x;   
   }   
   ```   
      
   Beware of bugs.   
      
   HTH,   
      
   Julio   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca