Question

Given 64 bits which represent a number in IEEE 754 double precision format, what is the algorithm for converting them to 32 bits which represent the same number, but in IEEE 754 single precision format (perhaps with loss of precision)?

Also, is there an easy way to perform this conversion in Ada, without having to manipulate the bits?

Was it helpful?

Solution

There is indeed an easy way to do this in Ada (quite similar to the way you would do the same thing in C!):

with Ada.Numerics;
with Ada.Text_IO; use Ada.Text_IO;
procedure Floating_Conversion is
   D : constant Long_Float := Ada.Numerics.Pi;
   F : Float;
begin
   F := Float (D);                                    -- The conversion!
   Put_Line ("double:" & Long_Float'Image (D));
   Put_Line ("single:" & Float'Image (F));
end Floating_Conversion;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top