Path: csiph.com!aioe.org!W4+pUJJ+LMQSnRdpBvjvmw.user.46.165.242.91.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.programming Subject: Re: Another little puzzle Date: Wed, 14 Dec 2022 15:58:45 +0100 Organization: Aioe.org NNTP Server Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: gioia.aioe.org; logging-data="57693"; posting-host="W4+pUJJ+LMQSnRdpBvjvmw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.5.1 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.9.2 Xref: csiph.com comp.programming:16081 On 2022-12-14 15:10, Richard Heathfield wrote: > On 14/12/2022 1:35 pm, Dmitry A. Kazakov wrote: >> On 2022-12-14 14:10, Richard Heathfield wrote: >>> On 14/12/2022 1:06 pm, Dmitry A. Kazakov wrote: >>>> On 2022-12-14 13:24, Stefan Ram wrote: >>>>>    Given n times of the 24-hour day, print their average. >>>>> >>>>>    For example, the average of "eight o'clock" and >>>>>    "ten o'clock" (n=2) would be "nine o'clock". >>>> >>>> You probably missed to require the interesting part: doing all that >>>> in the modular type (modulo 24) arithmetic: >>>> >>>>     20 + 5 = 1 (mod 24) >>> >>> ...which will give you the wrong answer. Chase that goose! >> >> Right, you must count the wrap-ups. [...] > So why do you need mod? As I said, the challenge is only interesting in modulo arithmetic (24 or 24*60 or 24*60*60). E.g. let type Hour is mod 24; Average a series of Hour without conversion to another integer type. (Otherwise it is trivial: convert to a wider type, average, convert back) P.S. This problem may actually arise in programming a microcontroller when you have some large series and narrow 16-bit types. Close cases are computations with color channels and grayscale levels when conversions to wider types are too expensive etc. The general case is some f(X1,X2,..,Xn) such that max{Xi} >= f() >= min{Xi}, but intermediates are not. As it is in the case with averaging Xi. BTW, averaging floats is a nasty problem too. A naive implementation quickly loses precision. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de