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!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Fixed vs float and precision and conversions Date: Wed, 8 Jul 2020 00:30:29 +0300 Organization: Tidorum Ltd Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net lrSIZyhsX5yQZnfDx4UnrgVjIfGZKzJVNEmbxBJBThkn/xYnuH Cancel-Lock: sha1:4ZjgnJZA9FM+YJPw9y+UXHo3NLs= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 In-Reply-To: Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:59389 List-Id: 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 . @ .