From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Natasha Kerensikova Newsgroups: comp.lang.ada Subject: How to round to the nearest fixed-point value? Date: Wed, 22 Jan 2014 16:48:26 +0000 (UTC) Organization: A noiseless patient Spider Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Date: Wed, 22 Jan 2014 16:48:26 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="31d6bde745a337034b005384ef225743"; logging-data="16347"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181ymPMJ2qWBYm7M+wRH17E" User-Agent: slrn/0.9.9p1 (FreeBSD) Cancel-Lock: sha1:ProzmazTZVMBvS/WeZGRT6P4tf0= Xref: news.eternal-september.org comp.lang.ada:18248 Date: 2014-01-22T16:48:26+00:00 List-Id: Hello, from what I understood of the LRM (at least in Ada 2005 mode), Fixed_Point_Type'Round is supposed to be a function returning a Fixed_Point_Type value closest to its argument. So I'm a bit surprised by the behavior of the following code: package Lib is type High is delta 0.001 digits 9; type Low is delta 0.01 digits 9; function Convert (Value : High) return Low; end Lib; package body Lib is function Convert (Value : High) return Low is begin return Low'Round (Value); end Convert; end Lib; with Ada.Text_IO; with Lib; procedure Testcase is Raw_Value : constant Lib.High := 0.999; Shown_Value : Lib.Low; begin Ada.Text_IO.Put_Line (Lib.Low'Image (Lib.Low'Round (Raw_Value))); Shown_Value := Lib.Convert (Raw_Value); Ada.Text_IO.Put_Line (Lib.Low'Image (Shown_Value)); end Testcase; I tried building it with gnat 4.6.3 from debian stable kfreebsd and with gnat-aux 4.7.3 from FreeBSD ports, and in both situations I got the following output: 1.00 0.99 Is there something I'm doing wrong that would trigger the truncation at some point? Or is this a compiler bug? Would anyone know a workaround to get correct results? Thanks in advance for your help, Natasha