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,700 of 57,431   
   Richard Harnden to Tim Rentsch   
   Re: A little puzzle.   
   29 Nov 22 15:03:54   
   
   From: richard.nospam@gmail.com   
      
   On 29/11/2022 12:03, Tim Rentsch wrote:   
   > Richard Harnden  writes:   
   >   
   >> On 28/11/2022 15:30, Tim Rentsch wrote:   
   >>   
   >>> Richard Harnden  writes:   
   >>>   
   >>>> On 21/11/2022 20:45, Ben Bacarisse wrote:   
   >>>>   
   >>>>> I wonder if there are any real posters here?  Let's see...   
   >>>>>   
   >>>>> I came across a trivial programming task that must have been   
   >>>>> solved a thousand times by other programmers, but it had never   
   >>>>> crossed my path until yesterday.  I must be feeling my age   
   >>>>> because I made a real hash of tackling it at first.  Anyway, I   
   >>>>> thought it might be of interest.   
   >>>>>   
   >>>>> 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 was specifically concerned with integer values where the   
   >>>>> sub-range includes the start value but excludes the end value.   
   >>>>> [...]   
   >>>>   
   >>>> I think this works ...   
   >>>>   
   >>>> int in_subrange(int range, int start, int end, int check)   
   >>>> {   
   >>>>       check %= range;   
   >>>>   
   >>>>       if (  ( end < start && (   
   >>>>                  (check >= 0 && check <= end)   
   >>>>               || (check >= start && check < range)   
   >>>>               )   
   >>>>             )   
   >>>>          || ( check >= start && check <= end )   
   >>>>          )   
   >>>>               return 1;   
   >>>>   
   >>>>       return 0;   
   >>>> }   
   >>>   
   >>> Have you thought about a case where the values of check, start,   
   >>> and end are chosen from the interval -9000000000 to 9000000000?   
   >>> How about the interval -18000000000 to 18000000000?   
   >>   
   >> Nope, I assume that start and end are between zero and range, and   
   >> that check is greater that zero.   
   >   
   > This assumption doesn't match the problem statement (emphasis   
   > added):   
   >   
   >      Consider >> any << ordered measure that "wraps round"   
   >   
   > What's being asked for is a function that will work with any   
   > ordered measure (that wraps), not just some such measures.  An   
   > example of such a measure is longitude, which goes from -180   
   > to +180 (with one of the two extreme values omitted).  Similarly > there is   
   no reason not to allow a measure that is only positive   
   > integers but does not include zero.  An important part of the   
   > challenge is to come up with a solution that handles these cases   
   > as well as the more obvious ones.   
      
   Okay, so how about this ... ?   
      
   int in_subrange(int_fast64_t range_min, int_fast64_t range_max,   
   int_fast64_t start, int_fast64_t end, int_fast64_t check)   
   {   
        while ( check < range_min )   
            check += range_max - range_min;   
      
        while ( check > range_max )   
            check -= range_max - range_min;   
      
        if (  ( end < start && (   
                   ( check > range_min && check <= end )   
                || (check >= start && check < range_max)   
                )   
              )   
           || ( check >= start && check <= end )   
           )   
                return 1;   
      
        return 0   
   }   
      
   It's okay for check to have 'clocked', range_min, range_max, start and   
   end have sensible values.   
      
   --- 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