Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.programming Subject: Re: A little puzzle. Date: Tue, 29 Nov 2022 17:37:35 +0000 Organization: A noiseless patient Spider Lines: 33 Message-ID: <87wn7dd5ls.fsf@bsb.me.uk> References: <875yf8nijb.fsf@bsb.me.uk> <86pmd76qrg.fsf@linuxsc.com> <86lenu6k7x.fsf@linuxsc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Info: reader01.eternal-september.org; posting-host="a3d5feb3dd0d972ffe3744e5624321cc"; logging-data="2433944"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bWPBFx4+1LmkyXbrf6feGsm6Rr9PL3lQ=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) Cancel-Lock: sha1:ACHBt/IgGagiz74QCn0+VDc9jDM= sha1:9czAUyiC03QdeP8mmAYr9pAiM/E= X-BSB-Auth: 1.a8bc65181569d9b495a3.20221129173735GMT.87wn7dd5ls.fsf@bsb.me.uk Xref: csiph.com comp.programming:15998 "Dmitry A. Kazakov" writes: > On 2022-11-29 16:03, Richard Harnden wrote: > >> 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; > > Ring modulo would be range_max - range_min + 1 > >> It's okay for check to have 'clocked', range_min, range_max, start >> and end have sensible values. > > If the language supports modular and ranged types there is no need to > check/normalize arguments. Part of the specification was that the inputs were intended to be valid, though maybe that was not very clear. In the case where this cropped up, everything was in minutes coming from internal clocks and calendars where the result were already in the 0-59 range. In my view, if you need to check the arguments, that's a separate function because what to do with bad data is so often application-specific. Just normalising and carrying on is only one strategy, and one that can delay detecting bugs. In your solution, you normalised the degrees, minutes and seconds into a longitude beforehand, which is all that was needed in that example. -- Ben.