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,689 of 57,431   
   Tim Rentsch to Julio Di Egidio   
   Re: A little puzzle.   
   27 Nov 22 19:23:21   
   
   From: tr.17687@z991.linuxsc.com   
      
   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.   
   >   
   > 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;   
   > }   
   > ```   
      
   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.   
      
   --- 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