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:ad4:5844:: with SMTP id de4mr16578363qvb.241.1594224998060; Wed, 08 Jul 2020 09:16:38 -0700 (PDT) X-Received: by 2002:aca:48d3:: with SMTP id v202mr7528906oia.78.1594224997627; Wed, 08 Jul 2020 09:16:37 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 8 Jul 2020 09:16:37 -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: <756885db-118a-4d76-b90d-e547a3cabf28o@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <518ff68b-1017-4775-8248-c40048b4fe67o@googlegroups.com> Subject: Re: Fixed vs float and precision and conversions From: Shark8 Injection-Date: Wed, 08 Jul 2020 16:16:38 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader01.eternal-september.org comp.lang.ada:59397 List-Id: On Wednesday, July 8, 2020 at 2:15:43 AM UTC-6, bj=C3=B6rn lundin wrote: > Den 2020-07-07 kl. 23:58, skrev Shark8: > > On Tuesday, July 7, 2020 at 3:10:22 PM UTC-6, bj=C3=B6rn 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 -- se= e: https://www.json.org/json-en.html >=20 > Yes. >=20 > >=20 > > If you have access to both sides of the serialization, you could define= an intermediate serialization say a string of "Fixed_Type#value#" where 'v= alue' is the string-representation you need. -- You can extract the value b= y indexing on the '#' characters, extracting the portion in between, and fe= eding that via Fixed_Type'Value( EXTRACTED_SUBSTRING ), and produce it via = "Fixed_Type#" & Fixed_Type'Image( fp_value ) & '#'. >=20 > Interesting - but for another project. > Here, I do not own the other side. >=20 > Perhaps I should have stated that the other side is Betfair, and in this= =20 > case I get odds of horse races via their API. > The only valid odds are in a fixed set of numbers like >=20 > they have small increments in the beginning, which increase as the odds= =20 > go up > like this partial array shows where > type Tics_Type is new integer range 1 .. 350; >=20 > Global_Odds_Table : array(Tics_Type'Range) of Fixed_Type :=3D ( > 1.01, 1.02, 1.03, 1.04, 1.05, > ... > 2.00, 2.02, 2.04, 2.06, 2.08, 2.10, > ... > 3.00, 3.05, 3.10, 3.15, 3.20, 3.25, > ... > 4.00, 4.10, 4.20, 4.30, 4.40, 4.50, > ... > 5.00, 5.10, 5.20, 5.30, 5.40, 5.50, > ... > 10.00, 10.50, 11.00, 11.50, 12.00, 12.50, > ... > 20.00, 21.00, 22.00, 23.00, 24.00, 25.00, > ... > 50.00, 55.00, 60.00, 65.00, 70.00, 75.00, > ... > 100.00, 110.00, 120.00, 130.00, 140.00, 150.00, > ... > 960.00, 970.00, 980.00, 990.00,1000.00); >=20 >=20 > so I get 5.1 from the JSON, which is occasionaly converted to 5.099,=20 > which I have trouble to find in the array above. > I am now thinking of traversing it and make an absolute diff like=20 > abs(myval - arrayval) and pick the one with smallest diff. >=20 > But my idea of using fixed was to escape the rounding errors. > But hmm, here it is about feeding data _into_ the system, and here I'll= =20 > get the error, while the rest will work nice. >=20 > Hmm, I need to do some testing >=20 > Thanks for the reply >=20 >=20 > --=20 > Bj=C3=B6rn Ok, so one thing to keep in mind here is that type-wise you're dealing not = with two types, but *three* -- the type on the other side, the values in th= e Global_Odds_Table; the IEEE754 transport, via JSON serialization and dese= rialization; and the type on your side, Fixed_Type. So then, the correct way to set up your end of things is to have a Fixed_Ty= pe which only has the values of Global_Odds_Table for your general-purpose/= internal-usage, and have a conversion function for handling the IEEE754; he= re you are: Lowest_Odds : Constant :=3D 1.01; Highest_Odds : Constant :=3D 1_000.00; type Odds_Base is delta 0.001 digits 18 range Lowest_Odds..Highest_Odds= ; Subtype Odds is Odds_Base -- range Lowest_Odds..Hihgest_Odds with Predicate_Failure =3D> raise Constraint_Error with Odds_Base'Image(Odds) & " is not a valid value.", Static_Predicate =3D> Odds in -- Global_Odds_Table 1.01 | 1.02 | 1.03 | 1.04 | 1.05 | 2.00 | 2.02 | 2.04 | 2.06 | 2.08 | 2.10 | 3.00 | 3.05 | 3.10 | 3.15 | 3.20 | 3.25 | 4.00 | 4.10 | 4.20 | 4.30 | 4.40 | 4.50 | 5.00 | 5.10 | 5.20 | 5.30 | 5.40 | 5.50 | 10.00 | 10.50 | 11.00 | 11.50 | 12.00 | 12.50 | 20.00 | 21.00 | 22.00 | 23.00 | 24.00 | 25.00 | 50.00 | 55.00 | 60.00 | 65.00 | 70.00 | 75.00 | 100.00 | 110.00 | 120.00 | 130.00 | 140.00 | 150.00 | 960.00 | 970.00 | 980.00 | 990.00 |1000.00 ; =20 Function Convert( Input : Interfaces.IEEE_Float_32 ) return Odds is use Interfaces; Begin Ada.Text_IO.Put_Line( "Input:" & Input'Image ); Ada.Text_IO.Put_Line( "OB:" & Odds_Base'Round(Input)'Image ); Return Odds'Round( Input ); Exception When Constraint_Error =3D> -- Handle out-of-range errors. if Input < Lowest_Odds then return Lowest_Odds; elsif Input > Highest_Odds then return Highest_Odds; else declare -- Global_Odds_Table Table : Constant Array (Positive range <>) of IEEE_Floa= t_32 :=3D ( 1.01, 1.02, 1.03, 1.04, 1.05, 2.00, 2.02, 2.04, 2.06, 2.08, 2.10, 3.00, 3.05, 3.10, 3.15, 3.20, 3.25, 4.00, 4.10, 4.20, 4.30, 4.40, 4.50, 5.00, 5.10, 5.20, 5.30, 5.40, 5.50, 10.00, 10.50, 11.00, 11.50, 12.00, 12.50, 20.00, 21.00, 22.00, 23.00, 24.00, 25.00, 50.00, 55.00, 60.00, 65.00, 70.00, 75.00, 100.00, 110.00, 120.00, 130.00, 140.00, 150.00, 960.00, 970.00, 980.00, 990.00,1000.00 ); Closest_Index : Positive :=3D 1; Closest_Value : IEEE_Float_32 :=3D IEEE_Float_32'Last; begin For X in Table'Range loop if abs (Input - Table(X)) < Closest_Value then Closest_Value:=3D abs (Input - Table(X)); Closest_Index:=3D X; end if; end loop; =20 Return Odds'Round( Table(Closest_Index) ); end; end if; End Convert; The bad thing here is that you have to repeat Global_Odds_Table, though thi= s isn't so bad as the second is fully contained within the exception-handle= r; further, searching the table is done only when 'Round cannot be used dir= ectly. Also, this version does catch out of bounds errors and coerce them t= o the nearest bound. -- You might want to write a one-off program to genera= te the Predicate-form and array-value form of Global_Odds_Table, since ther= e's 350 values.