comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Fixed-point question
Date: Fri, 26 Jun 2015 13:25:14 -0700
Date: 2015-06-26T13:25:14-07:00	[thread overview]
Message-ID: <mmkcco$3r1$2@dont-email.me> (raw)
In-Reply-To: <6a104bde-67ea-48a5-a035-6be3f9235d42@googlegroups.com>

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


  parent reply	other threads:[~2015-06-26 20:25 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-26 13:20 Fixed-point question Patrick Noffke
2015-06-26 16:56 ` Jeffrey R. Carter
2015-06-26 18:21   ` Patrick Noffke
2015-06-26 20:21     ` Anh Vo
2015-06-26 20:25     ` Jeffrey R. Carter [this message]
2015-06-27  2:55       ` Patrick Noffke
2015-06-26 23:14     ` Qun-Ying
2015-06-27  3:00       ` Patrick Noffke
2015-06-27  4:44         ` Jeffrey R. Carter
2015-06-27 17:59           ` Patrick Noffke
2015-06-27 18:38             ` Jeffrey R. Carter
2015-06-27 19:20               ` Bob Duff
2015-06-27 19:57                 ` Jeffrey R. Carter
2015-06-27 20:42                   ` Bob Duff
2015-06-28 11:39                     ` Brian Drummond
2015-06-29 13:28                     ` Patrick Noffke
2015-06-29 14:13                       ` Bob Duff
2015-06-27 23:41               ` Anh Vo
  -- strict thread matches above, loose matches on Subject: below --
1995-04-01  0:00 Garlington KE
1995-04-06  0:00 ` Robert Dewar
1995-04-07  0:00   ` Garlington KE
1995-04-07  0:00     ` Robert Dewar
1995-04-07  0:00   ` Robert I. Eachus
1995-04-07  0:00     ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox