comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: Fixed vs float and precision and conversions
Date: Tue, 7 Jul 2020 14:58:40 -0700 (PDT)	[thread overview]
Message-ID: <756885db-118a-4d76-b90d-e547a3cabf28o@googlegroups.com> (raw)
In-Reply-To: <re2obs$jus$1@dont-email.me>

On Tuesday, July 7, 2020 at 3:10:22 PM UTC-6, björn lundin wrote:
> 
> 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.
This is a limitation of JSON, IIUC: all numeric are IEE754 floats -- see: https://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 indexing on the '#' characters, extracting the portion in between, and feeding that via Fixed_Type'Value( EXTRACTED_SUBSTRING ), and produce it via "Fixed_Type#" & Fixed_Type'Image( fp_value ) & '#'.

> 
> 
> 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

  parent reply	other threads:[~2020-07-07 21:58 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 [this message]
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