comp.lang.ada
 help / color / mirror / Atom feed
From: Ada novice <ycalleecharan@gmx.com>
Subject: Re: understanding floating point types
Date: Sun, 22 Aug 2010 12:05:33 -0700 (PDT)
Date: 2010-08-22T12:05:33-07:00	[thread overview]
Message-ID: <2d2a622b-6c4a-45b7-9e3c-a565d5dbc9e3@p3g2000yqp.googlegroups.com> (raw)
In-Reply-To: toekjoyml5tx$.4xvgjm3annj4$.dlg@40tude.net

On Aug 22, 8:16 pm, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Sun, 22 Aug 2010 10:15:24 -0700 (PDT), Ada novice wrote:
> > I didn't quite get this part. In my previous example with say a = 2.33
> > and b = 3.45. Does Multiplication of a with b mean converting each of
> > a and b in model number first and then do the multiplication and
> > afterwards approximating the result to the nearest model number? Or
> > the multiplication will just be performed with the two numbers as 2.33
> > X 3.45 = 8.0385 and then approximating this result to the nearest
> > model number?
>
> No, the result is 8.0385 (assuming 32-bit machine numbers). Try this:
>
> with Ada.Text_IO; use Ada.Text_IO;
> procedure Test_Float is
>    type My_Float is digits 3;
>    X : My_Float := 2.33;
>    Y : My_Float := 3.45;
> begin
>    Put_Line ("Model view:" & My_Float'Image (X*Y));
>    Put_Line ("The reality:" & Float'Image (Float (X*Y)));
> end Test_Float;
>
> It should print:
>
> Model view: 8.04E+00
> The reality: 8.03850E+00
>
> When you print the number using My_Float'Image that rounds the machine
> number to the nearest model number. In computations and conversions
> machine numbers are left untouched.
>
> --
> Regards,
> Dmitry A. Kazakovhttp://www.dmitry-kazakov.de


Thanks for all this information. I have 2 small queries:

1. You used: type My_Float is digits 3; I tend to confuse the fact
that digits 3 means 3 digits of precision after the decimal point? So
when I consider 2.33 or 3.45, then both numbers are with digits 2 in
my mind. Or does digits 3 mean 3 significant digits which will include
the leading 2 in 2.33 and the leading 3 in 3.45 as well?

2. In your example, you have:

Model view: 8.04E+00
The reality: 8.03850E+00

If I understand correctly, model view represents the number that is
exactly representable by the computer. In our case here, we can get
the reality number since Float has higher precision digits than
My_Float. However if My_float used the same precision digits as Float
(i.e. 6) then this would also represent the number that could be
represented by the computer. If both a and b had a hypothetical 30
digits of precision, then neither Float (6 digits) nor Long_Float (15
digits), nor Long_Long_Float (18 digits) would give the right value
that would represent the model number of the multiplication result
a*b. I mentioned the word hypothetical, as we would not able to use a
number with 30 digits of precision anyway on a computer. But my point
was to say that Model view doesn't have to have less precision digits
as the reality number. But in our example, since My_Float has fewer
digits than Float, in this specific case we can form the reality
number with more precision digits than the model number.


Thanks,
YC



  reply	other threads:[~2010-08-22 19:05 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-22  9:11 understanding floating point types Ada novice
2010-08-22  9:51 ` Dmitry A. Kazakov
2010-08-22 10:37   ` Ada novice
2010-08-22 10:39     ` Ada novice
2010-08-22 13:57     ` Dmitry A. Kazakov
2010-08-22 17:15       ` Ada novice
2010-08-22 18:16         ` Dmitry A. Kazakov
2010-08-22 19:05           ` Ada novice [this message]
2010-08-22 19:34             ` Dmitry A. Kazakov
2010-08-23  6:29               ` Ada novice
2010-08-23  6:40                 ` J-P. Rosen
2010-08-23  7:13                 ` Dmitry A. Kazakov
2010-08-23  7:15             ` Martin
2010-08-23 11:42               ` Ada novice
2010-08-24 12:14                 ` Ada novice
2010-08-24 14:05                   ` Jacob Sparre Andersen
2010-08-24 14:36                     ` Ada novice
2010-08-22 17:22       ` Yannick Duchêne (Hibou57)
2010-08-22 18:49         ` Ada novice
replies disabled

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