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 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Fixed-point question Date: Fri, 26 Jun 2015 13:25:14 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <249bcd57-3074-4566-b8f2-03e6923bfbce@googlegroups.com> <6a104bde-67ea-48a5-a035-6be3f9235d42@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Fri, 26 Jun 2015 20:23:53 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="a3855fbfe1a666be9aefba0563039ed5"; logging-data="3937"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Gm//iVHpmxAT6DIve/5R+2eZd9V9TZ4s=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: <6a104bde-67ea-48a5-a035-6be3f9235d42@googlegroups.com> Cancel-Lock: sha1:1JVhZc/Ys+7tmGyRQ9dAmKVCxuA= Xref: news.eternal-september.org comp.lang.ada:26488 Date: 2015-06-26T13:25:14-07:00 List-Id: On 06/26/2015 11:21 AM, Patrick Noffke wrote: > On Friday, June 26, 2015 at 11:56:44 AM UTC-5, Jeffrey R. Carter wrote: >> On 06/26/2015 06:20 AM, Patrick Noffke wrote: >>> >>> type Fixed_Type is delta 1.0 / 180.0 range 0.0 .. 65535.0 / 180.0; >> >> What is Fixed_Type'Small? > > It is 1/180. Not as you wrote it. By default, the small of an ordinary fixed-point type is a power of two <= the delta. In this case, that would be 2**(-8) or less [ARM 3.5.9 (8/2)]. The introduction to this I got (in Ada 83) was Cent : constant := 0.01; type Money is delta Cent range -10_000.0 .. 10_000.0; Amount : Money := 0.0; Add_5 : for I in 1 .. 5 loop Amount := Amount + Cent; end loop Add_5; Text_IO.Put_Line (Item => Money'Image (Amount) ); which output 0.04, to most people's consternation. This is because the small was 2**(-7), or 1/128. The multiple of that nearest to 0.01 is 1/128, so Amount ends up containing 5/128, or 0.0390625. Adding for Money'Small use Cent; gives the expected result. -- Jeff Carter "I did not rob a bank. If I'd robbed a bank, everything would be great. I tried to rob a bank, is what happened, and they got me." Take the Money and Run 139