comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Fixed vs float and precision and conversions
Date: Thu, 9 Jul 2020 09:49:47 +0200	[thread overview]
Message-ID: <re6i6q$78f$1@gioia.aioe.org> (raw)
In-Reply-To: re6glj$bs1$1@dont-email.me

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

  reply	other threads:[~2020-07-09  7:49 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-07 21:10 Fixed vs float and precision and conversions Björn Lundin
2020-07-07 21:30 ` Niklas Holsti
2020-07-08  8:17   ` Björn Lundin
2020-07-07 21:58 ` Shark8
2020-07-08  8:15   ` Björn Lundin
2020-07-08 16:16     ` Shark8
2020-07-08 18:08       ` Björn Lundin
2020-07-08 18:10         ` Björn Lundin
2020-07-08 18:21           ` Niklas Holsti
2020-07-08 19:39             ` Björn Lundin
2020-07-08 20:34               ` Niklas Holsti
2020-07-08 21:24                 ` Björn Lundin
2020-07-09  7:11                   ` Niklas Holsti
2020-07-08 18:36           ` Dmitry A. Kazakov
2020-07-08 19:41             ` Björn Lundin
2020-07-09  6:20               ` Dmitry A. Kazakov
2020-07-09  7:23                 ` Björn Lundin
2020-07-09  7:49                   ` Dmitry A. Kazakov [this message]
2020-07-08 21:16         ` Shark8
2020-07-08 21:47           ` Björn Lundin
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox