From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED.2uCIJahv+a4XEBqttj5Vkw.user.gioia.aioe.org!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Fixed vs float and precision and conversions Date: Thu, 9 Jul 2020 09:49:47 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <756885db-118a-4d76-b90d-e547a3cabf28o@googlegroups.com> <518ff68b-1017-4775-8248-c40048b4fe67o@googlegroups.com> NNTP-Posting-Host: 2uCIJahv+a4XEBqttj5Vkw.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:59412 List-Id: On 09/07/2020 09:23, Björn Lundin wrote: > Den 2020-07-09 kl. 08:20, skrev Dmitry A. Kazakov: >> On 08/07/2020 21:41, Björn Lundin wrote: >> >>> As I replied to Niklas: >>> It is at least not certain that the number expressed in fixed_type is >>> one of them in the array below. >> >> The array has varying step 0.01 to 10.0, your test has step 0.001. >> What do you expect? Of course it must fail at a very high rate from >> 9/1 to 999/1, practically always. >> > > welll, in practice not always. Then your test is wrong. > It only fails when - like my example - > > Fixed_Type(float(5.10)) becomes 5.099 It must, because the conversion truncates and because 5.10 is not the machine number stored. You can try this: Long_Float'Image (Long_Float (Float (5.10))) You will not give you 5.10 either. Remember that T'Image rounds the last decimal place, so Float'Image (5.10) = " 5.10000E+00" is a lie. " 5.09999E+00" is a lie too. The choice between these two lies is more or less arbitrary (depends on the application). > and its been running with that construct since 2014, > and I only noticed now. So - not always, actually very seldom. You did not say *what* is seldom. Truncating is no worse than rounding. Actually when dealing with integers (fixed-point is a scaled integer) truncating is more natural. I had a several serious bugs in the past with Ada's rounding integer conversions. You must be very careful about these when, for example, splitting clock readings into hours, minutes, seconds. A naive implementation would seldomly produce wrong results. Truncating fixed-point conversion is a safer choice. But if you need rounding it is there. > I did expect floating comparison error to go away using fixed type, > but I forgot about 'garbage in -> garbage out' It goes away. But you need to eliminate the error first. Eliminating error is what conversion does. You move the choice between left and right margins of the error interval from comparison into conversion. > I don't have that kind of rounding errors once the values are converted > to fixed_type but I did not realize that I had them in the conversion. Yes. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de