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,692 of 57,431   
   Tim Rentsch to Julio Di Egidio   
   Re: A little puzzle.   
   28 Nov 22 07:22:33   
   
   From: tr.17687@z991.linuxsc.com   
      
   Julio Di Egidio  writes:   
      
   > On Monday, 28 November 2022 at 04:23:28 UTC+1, Tim Rentsch wrote:   
   >   
   >> Julio Di Egidio  writes:   
      
   I recommend switching away from google groups, to a posting service   
   that is more well-behaved.  Try eternal-september.org, which offers   
   free accounts after registering, if you can't find anything else   
   more to your liking.   
      
   >>> 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!!   ;)   
      
   It's your job to beware of bugs, not mine.   
      
   > 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_;   
   > }   
   > ```   
      
   This scheme looks like it will work, as long as the values given   
   don't get too near the edges of representation.  JavaScript   
   represents numeric values using floating point, and that choice   
   leads to some unexpected results when working with large numbers.   
      
   However, this approach is more complicated than it needs to be.   
   Have you tried looking at the other answers?   
      
   --- 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