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 16:41:57 +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: 7bit Injection-Info: gioia.aioe.org; logging-data="31502"; 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 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: csiph.com comp.programming:16083 On 2022-12-14 16:18, Richard Heathfield wrote: >> BTW, averaging floats is a nasty problem too. A naive implementation >> quickly loses precision. > > We're dealing with 'o'clock' and "HH:MM", and nowadays we have 64-bit > integer types and there are even 128-bit integers mooching around > looking for a reason to exist. You'd have to average a hell of a lot of > times even to /need/ floats, let alone lose significant precision. I never suggested float for averaging time stamps, I pointed out that averaging is not a simple problem. E.g. try this one: function Average (X : Float; N : Positive) return Float is Sum : Float := 0.0; begin for Index in 1..N loop Sum := Sum + X; end loop; return Sum / Float (N); end Average; The function does naive averaging. For simplicity it just sums up the same number X N times and divides by N. Average (1.0, 17_000_000) = 0.986895 Average (1.0, 100_000_000) = 0.167772 Average (1.0, 200_000_000) = 0.838861 The issue is catastrophic precision loss of addition Sum + X when Sum >> X. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de