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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.224.49.72 with SMTP id u8mr816728qaf.3.1390430723554; Wed, 22 Jan 2014 14:45:23 -0800 (PST) X-Received: by 10.50.153.109 with SMTP id vf13mr118093igb.3.1390430723299; Wed, 22 Jan 2014 14:45:23 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.glorb.com!k15no144685qaq.0!news-out.google.com!vg8ni2igb.0!nntp.google.com!uq10no2017930igb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 22 Jan 2014 14:45:22 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=KSa2aQoAAACOxnC0usBJYX8NE3x3a1Xq NNTP-Posting-Host: 66.126.103.122 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: How to round to the nearest fixed-point value? From: adambeneschan@gmail.com Injection-Date: Wed, 22 Jan 2014 22:45:23 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: news.eternal-september.org comp.lang.ada:18253 Date: 2014-01-22T14:45:22-08:00 List-Id: On Wednesday, January 22, 2014 8:48:26 AM UTC-8, Natasha Kerensikova wrote: > 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; > 1.00 > 0.99 I'm pretty sure this is a compiler bug. Since Low'Small is a multiple of High'Small, you can work around it by changing the definition of Convert to return Low (Value + Low'Small / 2); (Low'Small is a universal real and therefore Low'Small/2 will be interpreted as a value of type High. The type conversion to Low will truncate.) -- Adam