comp.lang.ada
 help / color / mirror / Atom feed
* Fixed vs float and precision and conversions
@ 2020-07-07 21:10 Björn Lundin
  2020-07-07 21:30 ` Niklas Holsti
  2020-07-07 21:58 ` Shark8
  0 siblings, 2 replies; 20+ messages in thread
From: Björn Lundin @ 2020-07-07 21:10 UTC (permalink / raw)



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

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2020-07-09  7:49 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox