Prev: Emacs and long file names
Next: Improving the first contact with Ada (was: GCC conflict on Ubuntu for mixed Ada/C++ project)
From: ChristopherL on 24 May 2010 18:29 Can you modify the below code to convert an integer variable of 32 bits to a variable of type float. type Unknown_integer_type is range -(2**16) ..(2**16) -1; -- 32 Bit Integer An_Unknown_Integer: Unknown_integer_type; An_Integer: Integer; A_Float: Float; -- Hoping this float is usually 32 bits An_Integer := 1234; An_Unknown_Integer := An_Integer; A_Float := Float (An_Unknown_Integer);
From: Ludovic Brenta on 24 May 2010 18:53 ChristopherL <clusardi2k(a)aol.com> writes: > Can you modify the below code to convert an integer variable of 32 > bits to a variable of type float. Why does it matter that the integer variable is 32 bits? How many bits does the float type use? > type Unknown_integer_type is range -(2**16) ..(2**16) -1; -- 32 Bit > Integer This looks like a 17-bit integer type to me, not 32-bit. > An_Unknown_Integer: Unknown_integer_type; > An_Integer: Integer; > A_Float: Float; -- Hoping this float is usually 32 bits Then it cannot hold a 32-bit integer without losing some precision, since some of the float's 32 bits will be reserved for the exponent. But since the integer type is only 17-bit wide, that's OK; the mantissa of a 32-bit float is very probably wider than 17 bits. > An_Integer := 1234; > > An_Unknown_Integer := An_Integer; > > A_Float := Float (An_Unknown_Integer); That's a type conversion, it assigns the value 1234.0 to A_Float. Since you ask a question, I can only assume that that's now what you wanted. So what is it that you want, exactly? -- Ludovic Brenta.
From: Jeffrey R. Carter on 24 May 2010 19:05 ChristopherL wrote: > > type Unknown_integer_type is range -(2**16) ..(2**16) -1; -- 32 Bit > Integer If you want an unconstrained, 32-bit, signed integer type, something from Interfaces (such as Integer_32) is usually the way to go. > An_Unknown_Integer: Unknown_integer_type; > An_Integer: Integer; > A_Float: Float; -- Hoping this float is usually 32 bits Why hope? If you need a 32-bit floating-point type, again something from Interfaces might be in order, or declare an appropriate type for your application: type Real is digits 6 [range Low .. High]; for Real'Size use 32; In any case, the size of Float should be documented for your compiler. > An_Integer := 1234; > > An_Unknown_Integer := An_Integer; This shouldn't compile, as An_Integer has a different type than An_Unknown_Integer; you need a type conversion. > A_Float := Float (An_Unknown_Integer); What is the point of Unknown_Integer_Type and An_Unknown_Integer? What's wrong with A_Float := Float (An_Integer); ? -- Jeff Carter "You can never forget too much about C++." 115
From: Stephen Leake on 24 May 2010 22:47 ChristopherL <clusardi2k(a)aol.com> writes: > Can you modify the below code to convert an integer variable of 32 > bits to a variable of type float. > > > > A_Float := Float (An_Unknown_Integer); This answers your question as asked; it converts any integer type to the Float type. Perhaps you want to ask something else? -- -- Stephe
From: mockturtle on 25 May 2010 12:34
On May 25, 4:47 am, Stephen Leake <stephen_le...(a)stephe-leake.org> wrote: > ChristopherL <clusard...(a)aol.com> writes: > > Can you modify the below code to convert an integer variable of 32 > > bits to a variable of type float. > > > A_Float := Float (An_Unknown_Integer); > > This answers your question as asked; it converts any integer type to the > Float type. > > Perhaps you want to ask something else? My theory: maybe the OP wanted to do a "bitwise copy" of the 4 octets of the Integer into the 4 octets of the Float? Something like an Unchecked_Conversion? (This would explain the "Hoping this float is usually 32 bits" remark) > > -- > -- Stephe |