Path: csiph.com!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.programming Subject: Re: A little puzzle. Date: Tue, 22 Nov 2022 05:48:17 -0800 Organization: A noiseless patient Spider Lines: 32 Message-ID: <86wn7n85i6.fsf@linuxsc.com> References: <875yf8nijb.fsf@bsb.me.uk> <87leo3kv55.fsf@bsb.me.uk> <4f03335d-63ca-447b-b538-dd8a59c11fa9n@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader01.eternal-september.org; posting-host="0b8f1b00ad451c4da87815129da31114"; logging-data="119594"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jlytV0jOt1kHWEN5jUN3f8oGH64lPQcs=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:j0AK6gXKWpDmHE6bxR1TQ5TdPDI= sha1:8SjXV1b0aVXMDScrn8Bw8gOFfIE= Xref: csiph.com comp.programming:15938 Paul N writes: > On Tuesday, November 22, 2022 at 12:53:47 PM UTC, Ben Bacarisse wrote: > >> Richard Heathfield writes: [...] >>> although of course in C the problem would be far better solved as: >>> >>> int inrange(int lo, int hi, int k) >>> { >>> return (lo <= k) && (k < hi); >>> } >>> >>> or even as a macro. >>> >>> I must confess I'm not entirely certain I have correctly interpreted >>> your puzzle, which I have taken to mean "is this a given value in the >>> given range", but this seems just a bit too easy for you to make a >>> hash of, but I'm sure I've made hashes of worse. I've missed >>> something, haven't I? >> >> The circular wrapping. On a clock, 55 is in the range of minutes that >> starts at 45 and ends at 5. > > What's wrong with - subtract start from both end and value, add the > modulus if either is negative, and compare? Suppose start is 9223372036854775800 and end is -9223372036854775800 (or the corresponding values for type 'int', those values are for a 64-bit signed integer type). The subtraction gives undefined behavior.