| 1 |
-- { dg-do run } |
| 2 |
|
| 3 |
with Text_IO; use Text_IO; |
| 4 |
with Ada.Numerics; use Ada.Numerics; |
| 5 |
with Unchecked_Conversion; |
| 6 |
|
| 7 |
procedure Fixedpnt5 is |
| 8 |
-- Test conversions from Floating point to Fixed point types when the |
| 9 |
-- fixed type has a Small that is not a power of two. Verify that the |
| 10 |
-- conversions are reversible, so that: |
| 11 |
-- |
| 12 |
-- Fixed_T (Float_T (Fixed_Var)) = Fixed_Var |
| 13 |
-- |
| 14 |
-- for a range of fixed values, in particular at the boundary of type. |
| 15 |
|
| 16 |
type T_Fixed_Type is delta PI/32768.0 range -PI .. PI - PI/32768.0; |
| 17 |
for T_Fixed_Type'Small use PI/32768.0; |
| 18 |
|
| 19 |
function To_Fix is new Unchecked_Conversion (Short_Integer, T_Fixed_Type); |
| 20 |
Fixed_Point_Var : T_Fixed_Type; |
| 21 |
Float_Var : Float; |
| 22 |
|
| 23 |
begin |
| 24 |
Fixed_Point_Var := -PI; |
| 25 |
Float_Var := Float(Fixed_Point_Var); |
| 26 |
Fixed_Point_Var := T_Fixed_Type (Float_Var); |
| 27 |
|
| 28 |
Fixed_Point_Var := T_Fixed_Type'First; |
| 29 |
Float_Var := Float(Fixed_Point_Var); |
| 30 |
Fixed_Point_Var := T_Fixed_Type (Float_Var); |
| 31 |
|
| 32 |
if Fixed_Point_Var /= T_Fixed_Type'First then |
| 33 |
raise Program_Error; |
| 34 |
end if; |
| 35 |
|
| 36 |
fixed_point_var := t_fixed_type'Last; |
| 37 |
Float_Var := Float(Fixed_Point_Var); |
| 38 |
Fixed_Point_Var := T_Fixed_Type (Float_Var); |
| 39 |
|
| 40 |
if Fixed_Point_Var /= T_Fixed_Type'Last then |
| 41 |
raise Program_Error; |
| 42 |
end if; |
| 43 |
|
| 44 |
for I in -32768 .. 32767 loop |
| 45 |
fixed_Point_Var := To_Fix (Short_Integer (I)); |
| 46 |
Float_Var := Float (Fixed_Point_Var); |
| 47 |
if T_Fixed_Type (Float_Var) /= FIxed_Point_Var then |
| 48 |
Put_Line ("Not reversibloe"); |
| 49 |
Put_Line (Integer'Image (I)); |
| 50 |
raise Program_Error; |
| 51 |
end if; |
| 52 |
end loop; |
| 53 |
|
| 54 |
Fixed_Point_Var := T_Fixed_Type (Float_Var * 2.0); |
| 55 |
raise Program_Error; |
| 56 |
exception |
| 57 |
when others => null; |
| 58 |
end Fixedpnt5; |