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=1.8 required=3.0 tests=BAYES_50,FORGED_GMAIL_RCVD, FREEMAIL_FROM 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: Re: Fixed vs float and precision and conversions Date: Wed, 8 Jul 2020 10:15:39 +0200 Organization: A noiseless patient Spider Message-ID: References: <756885db-118a-4d76-b90d-e547a3cabf28o@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 8 Jul 2020 08:15:39 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="bbc4443e166da1163e3df9d7300a854c"; logging-data="20874"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+07n5wKR3tdp9tt1Sf4b3i" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 Cancel-Lock: sha1:IsiYVTlmN+zMzcdhsWzCzDEIz8E= In-Reply-To: <756885db-118a-4d76-b90d-e547a3cabf28o@googlegroups.com> Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:59392 List-Id: Den 2020-07-07 kl. 23:58, skrev Shark8: > 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 Yes. > > 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 ) & '#'. Interesting - but for another project. Here, I do not own the other side. Perhaps I should have stated that the other side is Betfair, and in this case I get odds of horse races via their API. The only valid odds are in a fixed set of numbers like they have small increments in the beginning, which increase as the odds go up like this partial array shows where type Tics_Type is new integer range 1 .. 350; Global_Odds_Table : array(Tics_Type'Range) of Fixed_Type := ( 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); so I get 5.1 from the JSON, which is occasionaly converted to 5.099, which I have trouble to find in the array above. I am now thinking of traversing it and make an absolute diff like abs(myval - arrayval) and pick the one with smallest diff. 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 get the error, while the rest will work nice. Hmm, I need to do some testing Thanks for the reply -- Björn