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=2.0 required=3.0 tests=BAYES_40,FORGED_GMAIL_RCVD, FREEMAIL_FROM,TO_NO_BRKTS_PCNT autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: =?UTF-8?Q?Bj=c3=b6rn_Lundin?= Newsgroups: comp.lang.ada Subject: Fixed vs float and precision and conversions Date: Tue, 7 Jul 2020 23:10:20 +0200 Organization: A noiseless patient Spider Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 7 Jul 2020 21:10:20 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="c0ab1a6df2c416baf379afd98bd00208"; logging-data="20444"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Yx/fYczRpXhq3tbe6NtCH" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 Cancel-Lock: sha1:KJcGIR2XHt0fR2mq7wCzScN9ncA= Content-Language: en-US X-Mozilla-News-Host: snews://news.eternal-september.org:563 Xref: reader01.eternal-september.org comp.lang.ada:59388 List-Id: 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? should I express my Fixed_Type in another way? I need to be able to express 6.5 % (0.065) which I could not with type Fixed_Type is delta 0.01 digits 18; -- Björn