From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Fixed vs float and precision and conversions
Date: Wed, 8 Jul 2020 00:30:29 +0300 [thread overview]
Message-ID: <hmk7rlF4nfjU1@mid.individual.net> (raw)
In-Reply-To: <re2obs$jus$1@dont-email.me>
On 2020-07-08 0:10, Björn Lundin wrote:
>
> Hi !
>
> I've for years run an interface towards extenal part on a raspberry pi
> that communicates with JSON over http (JSONRPC2)
>
> the versions are
>
> bnl@pibetbot:~ $ gnatls -v
>
> GNATLS 6.3.0
> Copyright (C) 1997-2016, Free Software Foundation, Inc.
> ...
>
> bnl@pibetbot:~ $ uname -a
> Linux pibetbot 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l
> GNU/Linux
> bnl@pibetbot:~ $ cat /etc/deb
> debconf.conf debian_version
> bnl@pibetbot:~ $ cat /etc/deb
> debconf.conf debian_version
> bnl@pibetbot:~ $ cat /etc/debian_version
> 9.4
>
>
> I have found this reliable but suddenly I have got some rounding
> troubles. Or I perhaps just discovered it now.
>
>
> I have a fixed type
> type Fixed_Type is delta 0.001 digits 18;
>
> but JSON does not support that. So I get floats instead.
>
>
> I use gnatcoll.json as parser by the way
>
>
> procedure Get_Value(Container: in Json_Value;
> Field : in String;
> Target : in out Fixed_Type;
> Found : out Boolean ) is
> Tmp : Float := 0.0;
> begin
> if Container.Has_Field(Field) then
> Tmp := Container.Get(Field);
> Found := True;
> Target := Fixed_Type(Tmp);
> else
> Found := False;
> end if;
> end Get_Value;
>
>
> The message (JSON) contains a value 5.10 (in a float), but that is
> converted (sometimes I think) to the fixed_type variable with value
> 5.099. This gets me into trouble further down in the code.
>
> So - What should I do instead?
According to RM 4.6(31), conversion to a decimal fixed-point type does
not round, but truncates toward zero if the operand is not a multiple of
the "small" of the target type, which is usually the case here if Floats
are base-two.
You should perhaps change the conversion (Target := Fixed_Type(Tmp)) to
round, by doing Target := Fixed_Point'Round (Tmp).
Note, I haven't tried it.
--
Niklas Holsti
niklas holsti tidorum fi
. @ .
next prev parent reply other threads:[~2020-07-07 21:30 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 [this message]
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
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