From: sjw <simon.j.wright@mac.com>
Subject: Re: conversions between fixed-point types
Date: Sun, 20 Sep 2009 01:22:07 -0700 (PDT)
Date: 2009-09-20T01:22:07-07:00 [thread overview]
Message-ID: <183ee354-79d7-4373-8acb-c7d43f017d52@p9g2000vbl.googlegroups.com> (raw)
In-Reply-To: h90uf3$bdo$2@news.albasani.net
On Sep 18, 10:35 pm, Dirk Herrmann <fight_s...@invalid.invalid> wrote:
> I realized that with GNAT for the following example types
> type FpA is delta 0.5 range -10.0 .. +10.0;
> for FpA'Small use 0.5;
> type FpB is delta 0.4 range -10.0 .. +10.0;
> for FpB'Small use 0.4;
> conversions from FpA to FpB work as follows:
>
> FpA FpB
> -1.5 ---> -1.2
> -1.0 ---> -0.8
> -0.5 ---> -0.4
> -0.0 ---> -0.0
> +0.5 ---> +0.4
> +1.0 ---> +0.8
> +1.5 ---> +1.2
>
> That is, the conversion is performed similar to a truncation (always
> choosing the closest value towards zero).
I don't see this; GNAT rounds ..
with Ada.Text_IO; use Ada.Text_IO;
procedure Fixed
is
type FpA is delta 0.5 range -10.0 .. +10.0;
for FpA'Small use 0.5;
type FpB is delta 0.4 range -10.0 .. +10.0;
for FpB'Small use 0.4;
F : FpA'Base;
G : FpB'Base;
begin
F := -3.0;
loop
exit when F > 3.0;
Put_Line
(FpA'Image (F)
& " "
& FpB'Image (FpB (F))
& " "
& FpA'Image (FpA (FpB (F))));
F := FpA'Succ (F);
end loop;
New_Line;
G := -2.8;
loop
exit when G > 2.8;
Put_Line
(FpB'Image (G)
& " "
& FpA'Image (FpA (G))
& " "
& FpB'Image (FpB (FpA (G))));
G := FpB'Succ (G);
end loop;
end Fixed;
The output (Mac OS X, gcc-4.3.3 and GNAT-GPL-2009) is below, I've
noted the differences with ?'s:
-3.0 -3.2 -3.0 rounded away from 0
-2.5 -2.4 -2.5
-2.0 -2.0 -2.0
-1.5 -1.6 -1.5 " ?
-1.0 -1.2 -1.0 " ?
-0.5 -0.4 -0.5
0.0 0.0 0.0
0.5 0.4 0.5
1.0 1.2 1.0 " ?
1.5 1.6 1.5 " ?
2.0 2.0 2.0
2.5 2.4 2.5
3.0 3.2 3.0 "
-2.8 -3.0 -3.2 " and final value /= initial value
-2.4 -2.5 -2.4 "
-2.0 -2.0 -2.0
-1.6 -1.5 -1.6
-1.2 -1.0 -1.2
-0.8 -1.0 -1.2 " "
-0.4 -0.5 -0.4 "
0.0 0.0 0.0
0.4 0.5 0.4
0.8 1.0 1.2 " "
1.2 1.0 1.2
1.6 1.5 1.6
2.0 2.0 2.0
2.4 2.5 2.4 "
2.8 3.0 3.2 " "
prev parent reply other threads:[~2009-09-20 8:22 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-18 21:35 conversions between fixed-point types Dirk Herrmann
2009-09-18 22:42 ` Adam Beneschan
2009-09-19 12:41 ` Dirk Herrmann
2009-09-19 14:48 ` John B. Matthews
2009-09-20 8:15 ` Dirk Herrmann
2009-09-20 14:22 ` Robert A Duff
2009-09-20 18:55 ` Dirk Herrmann
2009-09-20 20:34 ` Simon Clubley
2009-09-23 20:46 ` Dirk Herrmann
2009-09-27 17:15 ` Simon Clubley
2009-09-27 19:22 ` sjw
2009-09-28 20:18 ` Dirk Herrmann
2009-09-28 18:37 ` Robert A Duff
2009-09-28 20:50 ` Dirk Herrmann
2009-09-20 15:18 ` John B. Matthews
2009-09-20 19:13 ` Dirk Herrmann
2009-09-20 20:09 ` tmoran
2009-09-21 17:24 ` Jeffrey R. Carter
2009-09-23 20:57 ` Dirk Herrmann
2009-09-23 22:28 ` Jeffrey R. Carter
2009-09-24 1:05 ` Adam Beneschan
2009-09-24 3:57 ` Jeffrey R. Carter
2009-09-25 8:47 ` Stuart
2009-09-25 20:41 ` sjw
2009-09-25 21:58 ` Jeffrey R. Carter
2009-09-28 13:40 ` Stuart
2009-09-26 14:43 ` Dirk Herrmann
2009-09-28 15:15 ` Adam Beneschan
2009-09-26 14:31 ` Dirk Herrmann
2009-09-19 18:38 ` tmoran
2009-09-20 8:22 ` sjw [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox