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=-0.9 required=3.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Received: by 2002:a0c:eec5:: with SMTP id h5mr52250617qvs.69.1594159120562; Tue, 07 Jul 2020 14:58:40 -0700 (PDT) X-Received: by 2002:a9d:554d:: with SMTP id h13mr44467325oti.329.1594159120297; Tue, 07 Jul 2020 14:58:40 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!aioe.org!peer03.ams4!peer.am4.highwinds-media.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 7 Jul 2020 14:58:40 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=146.5.2.231; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 146.5.2.231 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <756885db-118a-4d76-b90d-e547a3cabf28o@googlegroups.com> Subject: Re: Fixed vs float and precision and conversions From: Shark8 Injection-Date: Tue, 07 Jul 2020 21:58:40 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 2991 X-Received-Body-CRC: 1584664139 Xref: reader01.eternal-september.org comp.lang.ada:59391 List-Id: On Tuesday, July 7, 2020 at 3:10:22 PM UTC-6, bj=C3=B6rn lundin wrote: >=20 > I have a fixed type > type Fixed_Type is delta 0.001 digits 18; >=20 > but JSON does not support that. So I get floats instead. This is a limitation of JSON, IIUC: all numeric are IEE754 floats -- see: h= ttps://www.json.org/json-en.html If you have access to both sides of the serialization, you could define an = intermediate serialization say a string of "Fixed_Type#value#" where 'value= ' is the string-representation you need. -- You can extract the value by in= dexing on the '#' characters, extracting the portion in between, and feedin= g that via Fixed_Type'Value( EXTRACTED_SUBSTRING ), and produce it via "Fix= ed_Type#" & Fixed_Type'Image( fp_value ) & '#'. >=20 >=20 > I use gnatcoll.json as parser by the way >=20 >=20 > procedure Get_Value(Container: in Json_Value; > Field : in String; > Target : in out Fixed_Type; > Found : out Boolean ) is > Tmp : Float :=3D 0.0; > begin > if Container.Has_Field(Field) then > Tmp :=3D Container.Get(Field); > Found :=3D True; > Target :=3D Fixed_Type(Tmp); > else > Found :=3D False; > end if; > end Get_Value; >=20 >=20 > The message (JSON) contains a value 5.10 (in a float), but that is=20 > converted (sometimes I think) to the fixed_type variable with value=20 > 5.099. This gets me into trouble further down in the code. >=20 > So - What should I do instead? >=20 > should I express my Fixed_Type in another way? >=20 > 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; >=20 >=20 >=20 >=20 > --=20 > Bj=C3=B6rn