comp.lang.ada
 help / color / mirror / Atom feed
* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found] <6c7964a6-1733-434b-b1b1-962baa4ebba2@p23g2000prp.googlegroups.com>
@ 2009-01-13 21:01 ` Ludovic Brenta
  0 siblings, 0 replies; 50+ messages in thread
From: Ludovic Brenta @ 2009-01-13 21:01 UTC (permalink / raw)


ChristopherL wrote on comp.lang.ada:
> (1)The below code compiles but I get a run time exception.
>
> If I do not modify the definitions of Arg or Result how can I
> make Arg's value fit into Result's 16 bits.
>
> Result is a subrange of integer going from -128 to 127 which is
> 16 bits.
>
> Arg has more than 16 bits.
>
> -- Code --
>
> procedure test is
>    subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );

Since you don't specify the size of this type, there is no guarantee
that it is 16 bits. It may as well be 32 bits which is the default for
Integer on most compilers. But that's a detail; what matters is the
range you specified.

>    Result:Shrt;
>
>    Arg:Float;
>
> Begin
>    Arg := 200.0;
>
>    Result := Shrt(Arg + 0.5);  -- type conversion
> End test;
>
> -- Run Time Error --
> Unhandled exception:
> Constraint Error raised in Main
> range check failed

You get this constraint_error because the value 200 is not in the
range -128..127. Surprised?

> ==========================
> (2) The following code will put 201 in test when I want it to be 200,
> and I get the same error message.
>
> -- Code --
>
> procedure test is
>    type Unsigned_short is range 0..(2**8)-1;
>    temp: Unsigned_short;
> Begin
>   Arg := 200.0;
>   temp := Unsigned_short( Arg + 0.5 );
>
>   Result := Shrt(temp);  -- type conversion
> End test;
>
> -- Run Time Error --
> Unhandled exception:
> Constraint Error raised in Main
> range check failed
>
> ==========================
>
> How can I put this 200 into Result.

You can't.

But I suspect you're not asking the right questions. Maybe a better
question would be: since my requirement is to hold the value 200 in an
integer variable, what range should that variable be? More generally,
what values is my variable required to support?

> Besides Arg and Result, can I do it without defining any more types
> or variables.

You probably can, but since you have not stated what your problem is
(you only explained what your non-working solutions were), there is
little anyone can do to help.

Your best option is to explain what you are trying to achieve.

--
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found] <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com>
@ 2009-01-14  1:33 ` Brian Drummond
       [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
  2009-01-14 21:09   ` sjw
  0 siblings, 2 replies; 50+ messages in thread
From: Brian Drummond @ 2009-01-14  1:33 UTC (permalink / raw)


On Tue, 13 Jan 2009 12:26:22 -0800 (PST), ChristopherL
<clusardi2k@aol.com> wrote:

>(1)The below code compiles but I get a run time exception.
>
>If I do not modify the definitions of Arg or Result how can I
>make Arg's value fit into Result's 16 bits.
>
>Result is a subrange of integer going from -128 to 127 which is
>16 bits.

Actually that's 8 bits...
what are you really trying to do?

If you need to put 200 in an 8-bit type, why not use 
Natural range 0 to 255?

- Brian




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
@ 2009-01-14  9:06     ` Ludovic Brenta
  2009-01-14 10:08     ` Georg Bauhaus
                       ` (3 subsequent siblings)
  4 siblings, 0 replies; 50+ messages in thread
From: Ludovic Brenta @ 2009-01-14  9:06 UTC (permalink / raw)


ChristopherL wrote on comp.lang.ada:
> What I want to do is this. I have a variable holding the base 10 value
> 200. It has a certain bit representation. If the variable was an
> integer of 16 bits it's representation would be something such as
> 0000000011001000. For give me if I'm wrong. Anyway, what I'd like to
> do is put that exact bit representation in another variable of a
> different type.
>
> How do I proceed?

The way to do it is with Unchecked_Conversion, as your example shows.
Beware, however, that interpreting the bit representation in another
type will probably yield a different value.

   Size_Of_Float : constant := Float'Size;
   type Modular_Type_With_Same_Size_As_Float is mod 2 **
Size_Of_Float;
   for Modular_Type_With_Same_Size_As_Float'Size use Float'Size;
   Two_Hundred : constant Modular_Type_With_Same_Size_As_Float := 200;
   function To_Float is new Ada.Unchecked_Conversion
(Modular_Type_With_Same_Size_As_Float, Float);
   F : constant Float := To_Float (Two_Hundred);

You can substitute any type you want for Float. The bit representation
of F will be the same as the bit representation of Two_Hundred, but
the value of F is *not* going to be 200.  It is even possible that the
bit pattern is illegal for a Float (or your chosen type); you would
then get a Constraint_Error. Is that what you want?

--
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
  2009-01-14  9:06     ` Ludovic Brenta
@ 2009-01-14 10:08     ` Georg Bauhaus
  2009-01-14 10:29       ` Georg Bauhaus
  2009-01-14 12:47     ` Brian Drummond
                       ` (2 subsequent siblings)
  4 siblings, 1 reply; 50+ messages in thread
From: Georg Bauhaus @ 2009-01-14 10:08 UTC (permalink / raw)


ChristopherL schrieb:
> On Jan 13, 9:33 pm, Brian Drummond <brian_drumm...@btconnect.com>
> wrote:
>> what are you really trying to do?
>>
>> - Brian
> 
> 
> What I want to do is this. I have a variable holding the base 10 value
> 200. It has a certain bit representation. If the variable was an
> integer of 16 bits it's representation would be something such as
> 0000000011001000. For give me if I'm wrong. Anyway, what I'd like to
> do is put that exact bit representation in another variable of a
> different type.
> 
> How do I proceed?

You use an instance of Unchecked_Conversion, as explained.

Consider choosing your types (by declaring them, preferably)
just like you need them. Avoid predefined types like the plague.
Yes. There is an exception: there are type that are intended for
directly manipulating machine words or machine registers with
no other intention than to manipulate bits in ways that are
neither numeric nor reflecting Boolean logic.

Forgive me if I'm wrong, but you types' names sound like C's,
and the approach also reminds me of what might be a reasonable
way to do it in C. It is worth while giving Ada's fundamental
type system a chance. It is never necessary to make assumptions
about the bit size of a number.  Give the range of numbers
that your(!) data type needs, exactly, in a subtype constraint.
If needed, request a size for this type, as explained
in another post.

> I was thinking of trying to do it with code such as the below. The
> below code is not mine and came from this group. So, tomorrow I am
> planning on modifying my first example above to use the below code.
> What are my chances of doing it?
> 
> Thanks,
> Chris L.
> 
> with TEXT_IO;
> with UNCHECKED_CONVERSION;
> procedure UNCHECK_DEMO is
> package INT_IO is new TEXT_IO.INTEGER_IO(LONG_LONG_INTEGER);
> function INT_TO_FLT is
> new UNCHECKED_CONVERSION(
> SOURCE => LONG_LONG_INTEGER,
> TARGET => FLOAT);
> INT : LONG_LONG_INTEGER;

Beware that type Long_Long_Integer is neither a standard
Ada type (in that an implementation need not provide it,
see RM 3.5.4 Integer Types), nor are its objects guaranteed to
have the size you'd expect them to have.  Use representation
clauses as outlined by Ludovic if you really need tight control
over bit representation.

> FLT : FLOAT;

Your compiler might warn you in case the two types
you pass to Unchecked_Conversion do not match.
See:
http://en.wikibooks.org/wiki/Ada_Programming/Type_System#Unchecked_conversion

> begin
> INT:=2#11111111100000000000000000000000#;

This value might be inacceptable all Ada compilers;
the non-standard name

> FLT:=INT_TO_FLT(INT);
> INT_IO.put(INT,0,2);
> end;



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-14 10:08     ` Georg Bauhaus
@ 2009-01-14 10:29       ` Georg Bauhaus
  0 siblings, 0 replies; 50+ messages in thread
From: Georg Bauhaus @ 2009-01-14 10:29 UTC (permalink / raw)


Georg Bauhaus schrieb:

>> begin
>> INT:=2#11111111100000000000000000000000#;
> 
> This value might be inacceptable all Ada compilers;
                   ^not be accepted by some^
> the non-standard name


--
It's too misty today.



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
  2009-01-14  9:06     ` Ludovic Brenta
  2009-01-14 10:08     ` Georg Bauhaus
@ 2009-01-14 12:47     ` Brian Drummond
  2009-01-14 12:53     ` Brian Drummond
  2009-01-14 18:49     ` Jeffrey R. Carter
  4 siblings, 0 replies; 50+ messages in thread
From: Brian Drummond @ 2009-01-14 12:47 UTC (permalink / raw)


On Tue, 13 Jan 2009 20:33:19 -0800 (PST), ChristopherL
<clusardi2k@aol.com> wrote:

>On Jan 13, 9:33�pm, Brian Drummond <brian_drumm...@btconnect.com>
>wrote:
>> what are you really trying to do?
>>
>> - Brian
>
>
>What I want to do is this. I have a variable holding the base 10 value
>200. It has a certain bit representation. If the variable was an
>integer of 16 bits it's representation would be something such as
>0000000011001000. For give me if I'm wrong. Anyway, what I'd like to
>do is put that exact bit representation in another variable of a
>different type.
>
>How do I proceed?

UNCHECKED_CONVERSION is central to this.
But unless you are trying to write C in Ada, take Ludovic's suggestions
seriously.

You may not have LONG_LONG_INTEGER, and FLOAT may not be the size you
expect. If you want specific representations, then specify what you
want.

Making a couple of unjustified wild assumptions about what you want to
do:

(a) by FLOAT you mean IEEE P754 32-bit single precision (or something
closely compatible with it; there are sloppy implementations out there)

(b) you need to translate from your 16 bit integer input to the
identical number represented as 32-bit integer as a first step; there
may be endian issues to worry about here; THEN to FLOAT

(c) you don't want to translate 200 (16#C8#) to 200.0 (otherwise you'd
simply convert it!) but to some very very small denormalised number
(16#000000C8# is in the region below 10^-37)
(Aside to Ludovic: it's a legal FLOAT value in any FP representation
I've seen; but may be invalid for obscure or prehistoric machines)

(d) All your 16 bit inputs translate to denorms, which makes me wonder
if this is really what you want. But if you want the inputs in the most
significant bits of the 32-bit type you have to say so...

>I was thinking of trying to do it with code such as the below. The
>below code is not mine and came from this group. So, tomorrow I am
>planning on modifying my first example above to use the below code.
>What are my chances of doing it?

>package INT_IO is new TEXT_IO.INTEGER_IO(LONG_LONG_INTEGER);


Type Float_32 is new digits 7;
-- hack warning: most systems will adopt IEEE 754 SP for this
-- I think "Float" could be 64 bit in some implementations
-- Either way CHECK the size you get...
pragma Assert(Float'Size = 32);
-- you need to use Ada.Assertions

Type Integer_32_bit is -- ... see Ludovic's code
package INT_32_IO is new TEXT_IO.INTEGER_IO(Integer_32_bit);

>INT:=2#11111111100000000000000000000000#;

Unless you are better at counting zeroes than I am,

INT := 16#3f800000#;		-- known 1.0 in P754 SP

and test this independently of your 16-bit to 32-bit translation

Disclaimer: I am new to Ada, but not to FP bit wrangling in the closely
related language VHDL.

- Brian



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
                       ` (2 preceding siblings ...)
  2009-01-14 12:47     ` Brian Drummond
@ 2009-01-14 12:53     ` Brian Drummond
       [not found]       ` <f4894476-851e-493f-93a2-168976bd97fb@s1g2000prg.googlegroups.com>
  2009-01-14 18:49     ` Jeffrey R. Carter
  4 siblings, 1 reply; 50+ messages in thread
From: Brian Drummond @ 2009-01-14 12:53 UTC (permalink / raw)


On Tue, 13 Jan 2009 20:33:19 -0800 (PST), ChristopherL
<clusardi2k@aol.com> wrote:

>On Jan 13, 9:33�pm, Brian Drummond <brian_drumm...@btconnect.com>
>wrote:
>> what are you really trying to do?
>>
>> - Brian
>
>
>What I want to do is this. I have a variable holding the base 10 value
>200. It has a certain bit representation. If the variable was an
>integer of 16 bits it's representation would be something such as
>0000000011001000. For give me if I'm wrong. Anyway, what I'd like to
>do is put that exact bit representation in another variable of a
>different type.
>
>How do I proceed?

UNCHECKED_CONVERSION is central to this.
But unless you are trying to write C in Ada, take Ludovic's suggestions
seriously.

You may not have LONG_LONG_INTEGER, and FLOAT may not be the size you
expect. If you want specific representations, then specify what you
want.

Making a couple of unjustified wild assumptions about what you want to
do:

(a) by FLOAT you mean IEEE P754 32-bit single precision (or something
closely compatible with it; there are sloppy implementations out there)

(b) you need to translate from your 16 bit integer input to the
identical number represented as 32-bit integer as a first step; there
may be endian issues to worry about here; THEN to FLOAT

(c) you don't want to translate 200 (16#C8#) to 200.0 (otherwise you'd
simply convert it!) but to some very very small denormalised number
(16#000000C8# is in the region below 10^-37)
(Aside to Ludovic: it's a legal FLOAT value in any FP representation
I've seen; but may be invalid for obscure or prehistoric machines)

(d) All your 16 bit inputs translate to denorms, which makes me wonder
if this is really what you want. But if you want the inputs in the most
significant bits of the 32-bit type you have to say so...

>I was thinking of trying to do it with code such as the below. The
>below code is not mine and came from this group. So, tomorrow I am
>planning on modifying my first example above to use the below code.
>What are my chances of doing it?

>package INT_IO is new TEXT_IO.INTEGER_IO(LONG_LONG_INTEGER);


Type Float_32 is new digits 7;
-- hack warning: most systems will adopt IEEE 754 SP for this
-- I think "Float" could be 64 bit in some implementations
-- Either way CHECK the size you get...
pragma Assert(Float'Size = 32);
-- you need to use Ada.Assertions

Type Integer_32_bit is -- ... see Ludovic's code
package INT_32_IO is new TEXT_IO.INTEGER_IO(Integer_32_bit);

>INT:=2#11111111100000000000000000000000#;

Unless you are better at counting zeroes than I am,
>INT:=2#1111_1111_1000_0000_0000_0000_0000_0000#;
(aha! positive infinity...)
or better
INT := 16#3f800000#;		-- known 1.0 in P754 SP

and TEST this independently of your 16-bit to 32-bit translation

Disclaimer: I am new to Ada, but slightly familiar with FP bit wrangling
in the closely related language VHDL.

- Brian



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]       ` <f4894476-851e-493f-93a2-168976bd97fb@s1g2000prg.googlegroups.com>
@ 2009-01-14 16:08         ` Adam Beneschan
       [not found]           ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
  2009-01-14 21:17           ` sjw
  0 siblings, 2 replies; 50+ messages in thread
From: Adam Beneschan @ 2009-01-14 16:08 UTC (permalink / raw)


On Jan 14, 6:59 am, ChristopherL <clusard...@aol.com> wrote:
> Hello again,
>
> Trying twice, I seem not to be able to get the "Unchecked_Conversion"
> to compile. See below, and explain this to me.
>
> Thanks,
> Chris L.
>
> -- Program (1) --
>
> with Ada.Unchecked_Conversion;
>
> procedure test is
>    subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
>    Result:Shrt;
>
>    Arg:Float;
>
>    function To_Bits is new
>       Ada.Unchecked_Conversion (Source => Float, Target => Shrt);
>
> Begin
>    Arg := 200.0;
>
>    Result := To_Bits(Arg + 0.5);  -- crude round then type conversion
> End test;
>
> -- Error Message Received
>
>     210       Ada.Unchecked_Conversion (Source => Float, Target =>
> Shrt);
>
> *** 387E-0: Identifier ADA is not directly visible
>
>     236       Result := To_Bits( Arg + 0.5 )
>
> *** 387E-0: Identifier TO_BITS is not directly visible
>
> ==================================================================
>
> -- Program (2) --
>
> with Unchecked_Conversion;
>
> procedure test is
>    subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
>    Result:Shrt;
>
>    Arg:Float;
>
>    function To_Bits is new
>       Unchecked_Conversion (Source => Float, Target => Shrt);
>
> Begin
>    Arg := 200.0;
>
>    Result := To_Bits(Arg + 0.5);  -- crude round then type conversion
> End test;

(1) The error messages don't make any sense to me.  If you say "with
Ada.Unchecked_Conversion", then Ada and Ada.Unchecked_Conversion
should both be visible and you shouldn't be getting "not directly
visible" errors.  You did include the "with" statement in your source
file, right, rather than putting it in another source?  I'd check your
compiler manual here; maybe you didn't configure things properly and
you need to do something special in order to access the Ada standard
library.  I'm just guessing here.  I have no idea what compiler you're
using.

(2) Even if you get past that, your program will not work as
intended.  Unchecked_Conversion simply copies bit representations from
one type to another.  A floating-point type will contain bits in a
floating-point format, and that format won't look like an integer
format.  If Float is an IEEE-standard 32-bit float, the representation
of 200.0 looks like

   01000011010010000000000000000000

If you convert this to Shrt, which is probably 8 bits, the language
doesn't really tell you what happens when you Unchecked_Conversion a
32-bit value to an 8-bit value.  But most likely you'll either get the
upper 8 bits (01000011) or the lower 8 bits (00000000), neither of
which is what you want.

You need a regular type conversion to convert the float to an integer,
not an Unchecked_Conversion.

(3) You still haven't been clear on what you want to do, but it now
looks like you're trying to take an 8-bit integer in the range 0..255
and convert it to an 8-bit Shrt (in the range -128..127) with the same
bit representation.  Did I guess right?  If that's the case, it might
be helpful to do this the simple way:

   type Signed_Short is range -128 .. 127;
     -- more readable than "Shrt", which could be mistaken for
     -- "shirt", or some other word with four letters
   for Signed_Short'Size use 8;

   type Unsigned_Short is range 0 .. 255;
   for Unsigned_Short'Size use 8;

   Arg : Float;
   Unsigned_Result : Unsigned_Short;
   Result : Signed_Short;
begin
   Arg := 200.0;
   Unsigned_Result := Unsigned_Short (Arg + 0.5);
       -- will raise exception if Arg is out of the range 0.0 ..
255.0,
       -- after rounding

   if Unsigned_Result >= 128
      then Result := Signed_Short (Integer(Unsigned_Result) - 256)
      else Result := Signed_Short (Unsigned_Result);
   end if;

(You may have to convert to "Integer" before subtracting 256 because
otherwise Ada may try to use 8-bit integers to do the arithmetic and
then
things will be out of range.)

Or maybe you don't need the Unsigned_Short type:

   if Arg >= 128.0
      then Result := Signed_Short (Arg + 0.5 - 256.0)
      else Result := Signed_Short (Arg + 0.5);
   end if;

Or you can use Unchecked_Conversion if you want:

   function To_Signed_Short is new Unchecked_Conversion
       (Source => Unsigned_Short, Target => Signed_Short);
   ...
begin
   ...
   Unsigned_Result := Unsigned_Short (Arg + 0.5);
   Result := To_Signed_Short (Unsigned_Result);

This seems to be what you want... copying bits from an unsigned
integer to a signed integer.  Not copying bits from a float to an
integer---that is not at all what you're trying to accomplish.

Hope this gives you some idea of how this language works.  It's quite
simple, really.

                              -- Adam





^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
                       ` (3 preceding siblings ...)
  2009-01-14 12:53     ` Brian Drummond
@ 2009-01-14 18:49     ` Jeffrey R. Carter
  2009-01-15 10:12       ` Quote of the Day (Re: " Peter Hermann
  4 siblings, 1 reply; 50+ messages in thread
From: Jeffrey R. Carter @ 2009-01-14 18:49 UTC (permalink / raw)


ChristopherL wrote:
> 
> What I want to do is this. I have a variable holding the base 10 value
> 200. It has a certain bit representation. If the variable was an
> integer of 16 bits it's representation would be something such as
> 0000000011001000. For give me if I'm wrong. Anyway, what I'd like to
> do is put that exact bit representation in another variable of a
> different type.

Why do you want to do that?

You'll find that you get better responses if you describe the problem you're 
trying to solve, not what you think the solution to it is. Ada works best when 
it's used the way it was intended, and that's sometimes different from the 
low-level approaches required in other languages.

-- 
Jeff Carter
"I've got to stay here, but there's no reason
why you folks shouldn't go out into the lobby
until this thing blows over."
Horse Feathers
50



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]           ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
@ 2009-01-14 20:41             ` Adam Beneschan
       [not found]               ` <c265ffb7-6159-4d85-b259-78b830e115f9@v18g2000pro.googlegroups.com>
                                 ` (3 more replies)
  2009-01-14 21:32             ` sjw
  2009-01-14 21:51             ` Brian Drummond
  2 siblings, 4 replies; 50+ messages in thread
From: Adam Beneschan @ 2009-01-14 20:41 UTC (permalink / raw)


On Jan 14, 12:13 pm, ChristopherL <clusard...@aol.com> wrote:

> I want to copy bits from an Ada float to a user defined short integer
> maintaining the same
> bit representation.

OK, why?  Depending on your hardware, a float is likely 32 or 64 bits,
maybe 80.  I've occasionally seen different sizes, but I don't recall
ever seeing a 16-bit float, and definitely not an 8-bit float.

Your short integer is an 8-bit integer.  (If you want it to be a 16-
bit integer, why would you give it a range of -128 .. 127, rather than
-32768 .. 32767?)

OK, assuming that want to copy bits from an "Ada float", which is
actually a hardware float supported by your processor and has nothing
to do with your programming language, to a short integer: You want to
copy bits from a 32-bit float, perhaps, to an 8-bit integer.  Which
bits do you want to copy?  The ones containing the exponent or part of
it, or the low-order mantissa bits, or some other sequence of bits?
Why don't you want to copy the other 24 bits?

If you have no idea what I'm talking about, then you have certainly
stated your problem incorrectly, and you need to rethink your problem
and restate what you want to accomplish, at a high level, without
referring to "bits".  Actually, I already suspect that you don't want
what you said you want (copying bits from a float), because if you
really did want to copy bits from a floating-point representation, you
wouldn't be concerned with rounding.

                                -- Adam




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-14  1:33 ` How to put 200 into an integer sub-type of 16 bits (code included) Brian Drummond
       [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
@ 2009-01-14 21:09   ` sjw
  2009-01-14 21:16     ` Adam Beneschan
  2009-01-15 14:44     ` Brian Drummond
  1 sibling, 2 replies; 50+ messages in thread
From: sjw @ 2009-01-14 21:09 UTC (permalink / raw)


On Jan 14, 1:33 am, Brian Drummond <brian_drumm...@btconnect.com>
wrote:

> If you need to put 200 in an 8-bit type, why not use
> Natural range 0 to 255?

Natural range <whatever> has the same number of bits as Integer, which
has to be at least 16 and is very likely to be 32 (or maybe 64 on a 64-
bit machine).

type Short is mod 256;
for Short'Size use 8;  -- a confirming representation clause

For those of a C background, Short is a bad name here. Byte?
Interfaces.Unsigned_8?

--S





^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-14 21:09   ` sjw
@ 2009-01-14 21:16     ` Adam Beneschan
  2009-01-14 23:09       ` Martin
  2009-01-15 14:44     ` Brian Drummond
  1 sibling, 1 reply; 50+ messages in thread
From: Adam Beneschan @ 2009-01-14 21:16 UTC (permalink / raw)


On Jan 14, 1:09 pm, sjw <simon.j.wri...@mac.com> wrote:
> On Jan 14, 1:33 am, Brian Drummond <brian_drumm...@btconnect.com>
> wrote:
>
> > If you need to put 200 in an 8-bit type, why not use
> > Natural range 0 to 255?
>
> Natural range <whatever> has the same number of bits as Integer, which
> has to be at least 16 and is very likely to be 32 (or maybe 64 on a 64-
> bit machine).

Not necessarily.  You can apply a 'Size clause to a subtype:

   subtype Byte is Natural range 0 .. 255;
   for Byte'Size use 8;

                            -- Adam




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-14 16:08         ` Adam Beneschan
       [not found]           ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
@ 2009-01-14 21:17           ` sjw
  1 sibling, 0 replies; 50+ messages in thread
From: sjw @ 2009-01-14 21:17 UTC (permalink / raw)


On Jan 14, 4:08 pm, Adam Beneschan <a...@irvine.com> wrote:
> On Jan 14, 6:59 am, ChristopherL <clusard...@aol.com> wrote:

>
> > with Ada.Unchecked_Conversion;
>
> > procedure test is
> >    subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
> >    Result:Shrt;
>
> >    Arg:Float;
>
> >    function To_Bits is new
> >       Ada.Unchecked_Conversion (Source => Float, Target => Shrt);
>
> > Begin
> >    Arg := 200.0;
>
> >    Result := To_Bits(Arg + 0.5);  -- crude round then type conversion
> > End test;
>
> > -- Error Message Received
>
> >     210       Ada.Unchecked_Conversion (Source => Float, Target =>
> > Shrt);
>
> > *** 387E-0: Identifier ADA is not directly visible
>
> >     236       Result := To_Bits( Arg + 0.5 )
>
> > *** 387E-0: Identifier TO_BITS is not directly visible

> (1) The error messages don't make any sense to me.  If you say "with
> Ada.Unchecked_Conversion", then Ada and Ada.Unchecked_Conversion
> should both be visible and you shouldn't be getting "not directly
> visible" errors.

The error messages in OP's listing appear to refer to lines 210 and
236; there aren't nearly that many lines in the quoted code...perhaps
they come from different places?



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]           ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
  2009-01-14 20:41             ` Adam Beneschan
@ 2009-01-14 21:32             ` sjw
  2009-01-14 21:51             ` Brian Drummond
  2 siblings, 0 replies; 50+ messages in thread
From: sjw @ 2009-01-14 21:32 UTC (permalink / raw)


On Jan 14, 8:13 pm, ChristopherL <clusard...@aol.com> wrote:
> On Jan 14, 8:08 am, Adam Beneschan <a...@irvine.com> wrote:
>
>
>
> > On Jan 14, 6:59 am, ChristopherL <clusard...@aol.com> wrote:
>
> > > Hello again,
>
> > > Trying twice, I seem not to be able to get the "Unchecked_Conversion"
> > > to compile. See below, and explain this to me.
>
> > > Thanks,
> > > Chris L.
>
> > > -- Program (1) --
>
> > > with Ada.Unchecked_Conversion;
>
> > > procedure test is
> > >    subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
> > >    Result:Shrt;
>
> > >    Arg:Float;
>
> > >    function To_Bits is new
> > >       Ada.Unchecked_Conversion (Source => Float, Target => Shrt);
>
> > > Begin
> > >    Arg := 200.0;
>
> > >    Result := To_Bits(Arg + 0.5);  -- crude round then type conversion
> > > End test;
>
> > > -- Error Message Received
>
> > >     210       Ada.Unchecked_Conversion (Source => Float, Target =>
> > > Shrt);
>
> > > *** 387E-0: Identifier ADA is not directly visible
>
> > >     236       Result := To_Bits( Arg + 0.5 )
>
> > > *** 387E-0: Identifier TO_BITS is not directly visible
>
> > > ==================================================================
>
> > > -- Program (2) --
>
> > > with Unchecked_Conversion;
>
> > > procedure test is
> > >    subtype Shrt is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
> > >    Result:Shrt;
>
> > >    Arg:Float;
>
> > >    function To_Bits is new
> > >       Unchecked_Conversion (Source => Float, Target => Shrt);
>
> > > Begin
> > >    Arg := 200.0;
>
> > >    Result := To_Bits(Arg + 0.5);  -- crude round then type conversion
> > > End test;
>
> > (1) The error messages don't make any sense to me.  If you say "with
> > Ada.Unchecked_Conversion", then Ada and Ada.Unchecked_Conversion
> > should both be visible and you shouldn't be getting "not directly
> > visible" errors.  You did include the "with" statement in your source
> > file, right, rather than putting it in another source?  I'd check your
> > compiler manual here; maybe you didn't configure things properly and
> > you need to do something special in order to access the Ada standard
> > library.  I'm just guessing here.  I have no idea what compiler you're
> > using.
>
> > (2) Even if you get past that, your program will not work as
> > intended.  Unchecked_Conversion simply copies bit representations from
> > one type to another.  A floating-point type will contain bits in a
> > floating-point format, and that format won't look like an integer
> > format.  If Float is an IEEE-standard 32-bit float, the representation
> > of 200.0 looks like
>
> >    01000011010010000000000000000000
>
> > If you convert this to Shrt, which is probably 8 bits, the language
> > doesn't really tell you what happens when you Unchecked_Conversion a
> > 32-bit value to an 8-bit value.  But most likely you'll either get the
> > upper 8 bits (01000011) or the lower 8 bits (00000000), neither of
> > which is what you want.
>
> > You need a regular type conversion to convert the float to an integer,
> > not an Unchecked_Conversion.
>
> > (3) You still haven't been clear on what you want to do, but it now
> > looks like you're trying to take an 8-bit integer in the range 0..255
> > and convert it to an 8-bit Shrt (in the range -128..127) with the same
> > bit representation.  Did I guess right?  If that's the case, it might
> > be helpful to do this the simple way:
>
> >    type Signed_Short is range -128 .. 127;
> >      -- more readable than "Shrt", which could be mistaken for
> >      -- "shirt", or some other word with four letters
> >    for Signed_Short'Size use 8;
>
> >    type Unsigned_Short is range 0 .. 255;
> >    for Unsigned_Short'Size use 8;
>
> >    Arg : Float;
> >    Unsigned_Result : Unsigned_Short;
> >    Result : Signed_Short;
> > begin
> >    Arg := 200.0;
> >    Unsigned_Result := Unsigned_Short (Arg + 0.5);
> >        -- will raise exception if Arg is out of the range 0.0 ..
> > 255.0,
> >        -- after rounding
>
> >    if Unsigned_Result >= 128
> >       then Result := Signed_Short (Integer(Unsigned_Result) - 256)
> >       else Result := Signed_Short (Unsigned_Result);
> >    end if;
>
> > (You may have to convert to "Integer" before subtracting 256 because
> > otherwise Ada may try to use 8-bit integers to do the arithmetic and
> > then
> > things will be out of range.)
>
> > Or maybe you don't need the Unsigned_Short type:
>
> >    if Arg >= 128.0
> >       then Result := Signed_Short (Arg + 0.5 - 256.0)
> >       else Result := Signed_Short (Arg + 0.5);
> >    end if;
>
> > Or you can use Unchecked_Conversion if you want:
>
> >    function To_Signed_Short is new Unchecked_Conversion
> >        (Source => Unsigned_Short, Target => Signed_Short);
> >    ...
> > begin
> >    ...
> >    Unsigned_Result := Unsigned_Short (Arg + 0.5);
> >    Result := To_Signed_Short (Unsigned_Result);
>
> > This seems to be what you want... copying bits from an unsigned
> > integer to a signed integer.  Not copying bits from a float to an
> > integer---that is not at all what you're trying to accomplish.
>
> I want to copy bits from an Ada float to a user defined short integer
> maintaining the same
> bit representation.
>
> Arg:Float;  -- From this with a value of 200
> subtype Short_integer is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
> Result: Short_integer; --to this, and I want to round Arg

Chris,

Never mind the rounding; tell us what you need the value in Result to
be for Arg = 200?

It clearly can't be 200, because that's outside the range you've
specified.


Note that the numeric range you've specified for Short_Integer will
fit into 8 bits, but because you've made it a subtype of Integer,
variables will occupy the same space as an Integer (32 bits usually;
it will be at least 16 bits).


It'd also help to know what compiler you're using.




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]           ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
  2009-01-14 20:41             ` Adam Beneschan
  2009-01-14 21:32             ` sjw
@ 2009-01-14 21:51             ` Brian Drummond
  2 siblings, 0 replies; 50+ messages in thread
From: Brian Drummond @ 2009-01-14 21:51 UTC (permalink / raw)


On Wed, 14 Jan 2009 12:13:29 -0800 (PST), ChristopherL
<clusardi2k@aol.com> wrote:


>
>I want to copy bits from an Ada float to a user defined short integer
>maintaining the same
>bit representation.

FROM a Float? This is the first time you have said that.

Now - assuming SP float, which bits? You have 32 to choose from, and you
alternate between wanting 16 bits (last time) and 8 bits (as here).

But you still  ha you want:
(a) the top 8 bits?
(b) the bottom 8 bits?
(c) the exponent?
(d) the top 8 bits of the mantissa,
(e) an integer representation (200) of the input Float _value_ (200.0)
instead of its _bit_pattern_
(f) something else altogether
>
>Arg:Float;  -- From this with a value of 200
>subtype Short_integer is Integer range -(2 ** 7) .. ((2 ** 7) - 1 );
>Result: Short_integer; --to this, and I want to round Arg

If (e) you have 2 options:

(1) for 8 bits you need an unsigned integer, e.g. 
subtype Short_integer is Natural range 0 .. ((2 ** 8) - 1 );

(2) for 16 bits, either signed or unsigned will work.
subtype Short_integer is Integer range -(2 ** 15) .. ((2 ** 15) - 1 );

In either case, all you need is
Result: Short_integer := Short_integer(Arg + 0.5);
Of course it may well round up to 201...

- Brian



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]               ` <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com>
@ 2009-01-14 22:47                 ` Adam Beneschan
  2009-01-14 23:11                 ` Ludovic Brenta
                                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 50+ messages in thread
From: Adam Beneschan @ 2009-01-14 22:47 UTC (permalink / raw)


On Jan 14, 1:23 pm, ChristopherL <clusard...@aol.com> wrote:
> On Jan 14, 12:41 pm, Adam Beneschan <a...@irvine.com> wrote:
>
>
>
> > On Jan 14, 12:13 pm, ChristopherL <clusard...@aol.com> wrote:
>
> > > I want to copy bits from an Ada float to a user defined short integer
> > > maintaining the same
> > > bit representation.
>
> > OK, why?  Depending on your hardware, a float is likely 32 or 64 bits,
> > maybe 80.  I've occasionally seen different sizes, but I don't recall
> > ever seeing a 16-bit float, and definitely not an 8-bit float.
>
> > Your short integer is an 8-bit integer.  (If you want it to be a 16-
> > bit integer, why would you give it a range of -128 .. 127, rather than
> > -32768 .. 32767?)
>
> > OK, assuming that want to copy bits from an "Ada float", which is
> > actually a hardware float supported by your processor and has nothing
> > to do with your programming language, to a short integer: You want to
> > copy bits from a 32-bit float, perhaps, to an 8-bit integer.  Which
> > bits do you want to copy?  The ones containing the exponent or part of
> > it, or the low-order mantissa bits, or some other sequence of bits?
> > Why don't you want to copy the other 24 bits?
>
> > If you have no idea what I'm talking about, then you have certainly
> > stated your problem incorrectly, and you need to rethink your problem
> > and restate what you want to accomplish, at a high level, without
> > referring to "bits".  Actually, I already suspect that you don't want
> > what you said you want (copying bits from a float), because if you
> > really did want to copy bits from a floating-point representation, you
> > wouldn't be concerned with rounding.
>
> >                                 -- Adam
>
> Please remove the rounding operation for now from this discussion.
>
> This problem was given to me to try to solve who said it's probably
> impossible!
>
> The size of floating point number is 32 bits on my system, and my
> float will
> always be positive.
>
> General information about floating point numbers in general:
>
> Floating-point numbers are typically packed into a computer datum as
> the sign bit, the exponent field, and the significand (mantissa).
>
>   Mantissa  Exponent  Value
>   71        0           71
>   71        1          710
>   71        2         7100
>   71        -1           7.1
>   2         2          200
>
> For example, mathematical PI , rounded to 24 bits of precision, has:
>
> sign = 0 ; e = 1 ; s = 110010010000111111011011 (including the hidden
> bit)
>
> The sum of the exponent bias (127) and the exponent (1) is 128, so
> this is represented in single precision format as
>
> 0 10000000 10010010000111111011011 (excluding the hidden bit).
>
> So, what is the proper way to store a number (never being greater
> than 200.5) in a 8 bit short number as outlined above?

It's pretty clear that you're very confused about something.  Earlier
you wanted to store a bit representation; now you want to store a
number.  Not the same thing.  A number is a mathematical concept, and
a bit representation is a sequence of 1's and 0's.

If you want an 8-bit short integer to hold the value 200, then declare
it with a range 0..255, and assign 200 into it:

  type Short_Integer is range 0 .. (2**8) - 1; -- same as 0..255
  Result : Short_Integer;

  Result := 200;
--or something like--
  Result := Short_Integer (Arg + 0.5);
--this is a type conversion, not an Unchecked_Conversion

Don't declare Short_Integer as -(2**7) .. (2**7)-1.  2**7 is 128, so
that gives you a range of -128 .. 127, and 200 is not in this range.
That should be obvious, but perhaps you're used to programming
languages that don't work in the obvious way.  Ada isn't like that.
If you tell it that a number's type is in the range -128 .. 127, then
it only allows numbers in the range -128 .. 127 to be put in there.
Too simple?

Note also that the bit representation, 11001000, is *not* a sequence
of bits that exists anywhere in the 32-bit floating-point
representation of 200.0.  So if you still think you want to copy
*bits* from a float, or from the representation of a floating-point
number, "maintaining the same representation" as you said earlier,
you're very confused.  You can't maintain the representation of a 32-
bit float in an 8-bit integer.  There aren't enough bits.

                              -- Adam









^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-14 21:16     ` Adam Beneschan
@ 2009-01-14 23:09       ` Martin
  2009-01-15  0:07         ` Adam Beneschan
  0 siblings, 1 reply; 50+ messages in thread
From: Martin @ 2009-01-14 23:09 UTC (permalink / raw)


On Jan 14, 9:16 pm, Adam Beneschan <a...@irvine.com> wrote:
> Not necessarily.  You can apply a 'Size clause to a subtype:
>
>    subtype Byte is Natural range 0 .. 255;
>    for Byte'Size use 8;
>
>                             -- Adam

Since when???

Cheers
-- Martin



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]               ` <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com>
  2009-01-14 22:47                 ` Adam Beneschan
@ 2009-01-14 23:11                 ` Ludovic Brenta
  2009-01-15  9:56                 ` Stuart
                                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 50+ messages in thread
From: Ludovic Brenta @ 2009-01-14 23:11 UTC (permalink / raw)


ChristopherL wrote:
> Please remove the rounding operation for now from this discussion.
>
> This problem was given to me to try to solve who said it's probably
> impossible!
>
> The size of floating point number is 32 bits on my system, and my
> float will always be positive.
>
> General information about floating point numbers in general:
>
> Floating-point numbers are typically packed into a computer datum as
> the sign bit, the exponent field, and the significand (mantissa).
>
>   Mantissa  Exponent  Value
>   71        0           71
>   71        1          710
>   71        2         7100
>   71        -1           7.1
>   2         2          200
>
> For example, mathematical PI , rounded to 24 bits of precision, has:
>
> sign = 0 ; e = 1 ; s = 110010010000111111011011 (including the hidden
> bit)
>
> The sum of the exponent bias (127) and the exponent (1) is 128, so
> this is represented in single precision format as
>
> 0 10000000 10010010000111111011011 (excluding the hidden bit).
>
> So, what is the proper way to store a number (never being greater
> than 200.5) in a 8 bit short number as outlined above?
>
> Chris L.

Are you talking about an 8-bit floating-point number? One that uses
e.g. 1 bit for sign, 3 for exponent and 4 for mantissa?

An exact floating-point representation for 200.5 requires more than 8
bits. At a minimum you'd need:

0 1000 10010001

i.e. 13 bits. Is that what you were thinking about when you first
mentioned 16-bit types? And since your hardware probably supports
neither 8-bit nor 16-bit floating-point types anyway, do you plan to
implement floating-point arithmetic in software?

--
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-14 23:09       ` Martin
@ 2009-01-15  0:07         ` Adam Beneschan
  2009-01-15  3:09           ` Randy Brukardt
  0 siblings, 1 reply; 50+ messages in thread
From: Adam Beneschan @ 2009-01-15  0:07 UTC (permalink / raw)


On Jan 14, 3:09 pm, Martin <martin.do...@btopenworld.com> wrote:
> On Jan 14, 9:16 pm, Adam Beneschan <a...@irvine.com> wrote:
>
> > Not necessarily.  You can apply a 'Size clause to a subtype:
>
> >    subtype Byte is Natural range 0 .. 255;
> >    for Byte'Size use 8;
>
> Since when???

Since Ada 95 (I don't think this was allowed in Ada 83).  See 13.1
(8).  GNAT may not allow this, but 13.1(20) gives implementations the
right to put their own restrictions on representation items.

                                  -- Adam



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]               ` <c265ffb7-6159-4d85-b259-78b830e115f9@v18g2000pro.googlegroups.com>
@ 2009-01-15  2:39                 ` SteveD@teranews.com
  0 siblings, 0 replies; 50+ messages in thread
From: SteveD@teranews.com @ 2009-01-15  2:39 UTC (permalink / raw)


"ChristopherL" <clusardi2k@aol.com> wrote in message 
news:c265ffb7-6159-4d85-b259-78b830e115f9@v18g2000pro.googlegroups.com...
[snip]
>
>So, what is the proper way to store a number (never being greater than
>200.5) in a 8 bit
>short number as outlined above?
>
>
>Chris L.

I've been watching this thread trying to figure out what it is you're trying 
to do and I still don't get it.

It appears that you are trying to store values in the range of 0 to 200.5 in 
8 bits.  Are you trying to store a bunch of data values in a small amount of 
memory?  Are you trying to minimize bandwidth for communication?

What it comes down to is this:
  With an 8 bit number you have the ability to distinguish 256 values.  If 
you want to get the best resolution possible for values in the range of 0.0 
to 200.5 that you can store in 8 bits, create a linear mapping such that 0.0 
maps to 0 and 200.5 maps to 255.
  8 bit value = 255.0/200.5 * floatValue

  To retrieve the value, do the reverse mapping:
   floatValue = 200.5/255.0 * 8 bit value

  You probably want to use a lookup table if you're processing a bunch of 
data.

But then again, I'm still unclear on what you're trying to do.

Regards,
Steve




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15  0:07         ` Adam Beneschan
@ 2009-01-15  3:09           ` Randy Brukardt
  2009-01-15 16:28             ` Adam Beneschan
  0 siblings, 1 reply; 50+ messages in thread
From: Randy Brukardt @ 2009-01-15  3:09 UTC (permalink / raw)


"Adam Beneschan" <adam@irvine.com> wrote in message 
news:12837a65-dd6e-493f-91e8-4140d67237c2@z28g2000prd.googlegroups.com...
> On Jan 14, 3:09 pm, Martin <martin.do...@btopenworld.com> wrote:
>> On Jan 14, 9:16 pm, Adam Beneschan <a...@irvine.com> wrote:
>>
>> > Not necessarily.  You can apply a 'Size clause to a subtype:
>>
>> >    subtype Byte is Natural range 0 .. 255;
>> >    for Byte'Size use 8;
>>
>> Since when???
>
> Since Ada 95 (I don't think this was allowed in Ada 83).  See 13.1
> (8).  GNAT may not allow this, but 13.1(20) gives implementations the
> right to put their own restrictions on representation items.

No, 13.3(48) says "Size may be specified for *first subtypes* ..." (emphasis 
mine). Nothing about specifying it for non-first subtypes. (There has been 
quite a bit of discussion about changing that, but it leads to uncomfortable 
issues with legality rules depending on representation - not enough people 
can live with that.)

                                                Randy.





^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]               ` <3625f980-4406-4f51-b494-dd4a48cab840@p36g2000prp.googlegroups.com>
@ 2009-01-15  6:53                 ` Michael Bode
  2009-01-15  8:57                   ` Martin
  0 siblings, 1 reply; 50+ messages in thread
From: Michael Bode @ 2009-01-15  6:53 UTC (permalink / raw)


ChristopherL <clusardi2k@aol.com> writes:

> The size of floating point number is 32 bits on my system, and my
> float will
> always be positive.
>
> General information about floating point numbers in general:
>
> Floating-point numbers are typically packed into a computer datum as
> the sign bit, the exponent field, and the significand (mantissa).
>
>   Mantissa  Exponent  Value
>   71        0           71
>   71        1          710
>   71        2         7100
>   71        -1           7.1
>   2         2          200
>
> For example, it was shown above that π, rounded to 24 bits of
> precision, has:
>
> sign = 0 ; e = 1 ; s = 110010010000111111011011 (including the hidden
> bit)
> The sum of the exponent bias (127) and the exponent (1) is 128, so
> this is represented in single precision format as
>
> 0 10000000 10010010000111111011011 (excluding the hidden bit).
>
> So, what is the proper way to store a number (never being greater
> than
> 200.5) in a 8 bit short number as outlined above?

Try compressing it with gzip? Your question is how to store 4 bytes in
1 byte. The answer is, you don't. You use 4 8-bit numbers to store 1
32-bit number. In Ada you can do it like this:

with Ada.Unchecked_Conversion;
with Ada.Text_Io;

procedure Convert is
   type SByte is range -128 .. 127;
   for SByte'Size use 8;
   type Float_Bytes is array (0 .. 3) of SByte;

   Arg : Float := 200.5;
   Result : Float_Bytes;

   function To_Bytes is new Ada.Unchecked_Conversion (Source => Float,
                                                      Target => Float_Bytes);
begin
   Result := To_Bytes (Arg);
   for I in Result'Range loop
      Ada.Text_Io.Put (Sbyte'Image (Result(I)) & " ");
   end loop;
   Ada.Text_Io.New_Line;
end Convert;

Output:

 0 -128  72  67 



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15  6:53                 ` Michael Bode
@ 2009-01-15  8:57                   ` Martin
  0 siblings, 0 replies; 50+ messages in thread
From: Martin @ 2009-01-15  8:57 UTC (permalink / raw)


On Jan 15, 6:53 am, Michael Bode <m.g.b...@web.de> wrote:
> ChristopherL <clusard...@aol.com> writes:
> > The size of floating point number is 32 bits on my system, and my
> > float will
> > always be positive.
>
> > General information about floating point numbers in general:
>
> > Floating-point numbers are typically packed into a computer datum as
> > the sign bit, the exponent field, and the significand (mantissa).
>
> >   Mantissa  Exponent  Value
> >   71        0           71
> >   71        1          710
> >   71        2         7100
> >   71        -1           7.1
> >   2         2          200
>
> > For example, it was shown above that π, rounded to 24 bits of
> > precision, has:
>
> > sign = 0 ; e = 1 ; s = 110010010000111111011011 (including the hidden
> > bit)
> > The sum of the exponent bias (127) and the exponent (1) is 128, so
> > this is represented in single precision format as
>
> > 0 10000000 10010010000111111011011 (excluding the hidden bit).
>
> > So, what is the proper way to store a number (never being greater
> > than
> > 200.5) in a 8 bit short number as outlined above?
>
> Try compressing it with gzip? Your question is how to store 4 bytes in
> 1 byte. The answer is, you don't. You use 4 8-bit numbers to store 1
> 32-bit number. In Ada you can do it like this:
>
> with Ada.Unchecked_Conversion;
> with Ada.Text_Io;
>
> procedure Convert is
>    type SByte is range -128 .. 127;
>    for SByte'Size use 8;
>    type Float_Bytes is array (0 .. 3) of SByte;
>
>    Arg : Float := 200.5;
>    Result : Float_Bytes;
>
>    function To_Bytes is new Ada.Unchecked_Conversion (Source => Float,
>                                                       Target => Float_Bytes);
> begin
>    Result := To_Bytes (Arg);
>    for I in Result'Range loop
>       Ada.Text_Io.Put (Sbyte'Image (Result(I)) & " ");
>    end loop;
>    Ada.Text_Io.New_Line;
> end Convert;
>
> Output:
>
>  0 -128  72  67

For the insanely paranoid, you might want to add:

pragma Assert (Float'Size = 32);



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]               ` <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com>
  2009-01-14 22:47                 ` Adam Beneschan
  2009-01-14 23:11                 ` Ludovic Brenta
@ 2009-01-15  9:56                 ` Stuart
  2009-01-15 12:40                 ` Stuart
  2009-01-15 14:02                 ` Stephen Leake
  4 siblings, 0 replies; 50+ messages in thread
From: Stuart @ 2009-01-15  9:56 UTC (permalink / raw)



"ChristopherL" <clusardi2k@aol.com> wrote in message 
news:1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com...

<snip much>

> Please remove the rounding operation for now from this discussion.

OK.

> This problem was given to me to try to solve who said it's probably
> impossible!

Not being funny or anything but it might be as well to describe the problem 
as given.  I think the exchanges elsewhere in this thread demonstrate that 
there is a great deal of confusion.

> The size of floating point number is 32 bits on my system, and my
> float will always be positive.
>
> General information about floating point numbers in general:

<snip description of sign + biased exponent + mantissa representation>

> So, what is the proper way to store a number (never being greater
> than 200.5) in a 8 bit short number as outlined above?

OK - there is no 'proper' way, there are several ways, each of which has 
advantages and disadvantages.

I get the impression that you have a 'real' input in the range: 0.0 .. 200.5 
and you want to have this value held in an 8-bit variable.

You do not say to what precision you need this value, or how this precision 
might vary over the value range.  Floating point representations provide a 
relative precision, so while values around 200.0 might only be represented 
with a precision of 1.0, values around 20.0 would be represented with a 
precision of 0.1, and those around 2.0 with a precision of 0.01.  Fixed 
point representations provide a fixed precision, so all values might be 
represented with a precision of 1.0 whether they be 200.0, 20.0 or 2.0.

You need to clarify what your requirements are!

If fixed point precision is adequate to your needs then you should find that 
Ada fixed point types will meet your needs most adequately (you may need to 
specify the 'small attribute to extract the maximum amount of precision) and 
you will be able to use a simple type conversion.

If you need floating point precision things are going to be more difficult 
because you are going to have to roll your own representation.  You seem to 
have done some research on floating point representations so I won't go into 
detail now, but I would note that:
   a) You do not need a sign bit as all values are positive.
   b) You might want to consider something other than base 2 representation 
to eke out a little more precision given your data range (200.5).

On the subject of extracting 'values' out of  floating point representations 
I should like to suggest you track down a copy of the PowerPC Compiler 
Writers guide - this shows a process for injecting/extracting values by 
adding/subtracting a bias to move the values of interest to a more useful 
part of the representation and exclude the effects of the biased exponent. 
(The method may be described elsewhere - but this is one that I am familiar 
with and the guide is available on-line (or was).

On the subject of range you may need to think very carefully about whether 
200.5 is or isn't in your number range.  It may be the case that when you 
look at alternative number bases it will become a moot point, but in your 
earlier examples you were running into a constraint problem because 200.5 
was being rounded up to 201.  (Read a bit more about rounding if you are 
unclear about this).

Regards
   Stuart




Chris L. 





^ permalink raw reply	[flat|nested] 50+ messages in thread

* Quote of the Day (Re: How to put 200 into an integer sub-type of 16 bits (code  included)
  2009-01-14 18:49     ` Jeffrey R. Carter
@ 2009-01-15 10:12       ` Peter Hermann
  0 siblings, 0 replies; 50+ messages in thread
From: Peter Hermann @ 2009-01-15 10:12 UTC (permalink / raw)


Jeffrey R. Carter <spam.jrcarter.not@spam.acm.org> wrote:
> trying to solve, not what you think the solution to it is. Ada works best when 
> it's used the way it was intended, and that's sometimes different from the 
> low-level approaches required in other languages.

found its way to the quote of the day (-:
www.ihr.uni-stuttgart.de/forschung/ada/resources_on_ada/



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]               ` <fc154f52-7168-447a-bcd3-6ece9066ebf7@r37g2000prr.googlegroups.com>
@ 2009-01-15 10:29                 ` Georg Bauhaus
  0 siblings, 0 replies; 50+ messages in thread
From: Georg Bauhaus @ 2009-01-15 10:29 UTC (permalink / raw)


ChristopherL schrieb:
> On Jan 14, 12:41 pm, Adam Beneschan <a...@irvine.com> wrote:
>>  Which
>> bits do you want to copy? 
> 
> The ones containing the mantissa.

OK, here is some code to extract the exponent.

BTW,  consider giving the reference when you
quote from Wikipedia.
In this case the "general info" seems to have been pasted from
http://en.wikipedia.org/wiki/Floating_point#Internal_representation


with Interfaces;
with Ada.Unchecked_Conversion, Ada.Text_IO;

procedure Th is

   use Ada;

   Arg: Float;
   pragma Assert(Float'Size = 32);

   The_Bits : Interfaces.Unsigned_32;

   function Just_The_Bits is
      new Unchecked_Conversion (Source => Float,
                                Target => Interfaces.Unsigned_32);

   Result: Interfaces.Unsigned_8;

   package Bit_IO_8 is new Text_IO.Modular_IO (Interfaces.Unsigned_8);
   package Bit_IO_32 is new Text_IO.Modular_IO (Interfaces.Unsigned_32);

begin
   Arg := 200.0;
   The_Bits := Just_The_Bits(Arg);

   Text_IO.Put("starting from float ");
   Bit_IO_32.Put(The_Bits, Base => 2);
   Text_IO.Put_Line(" of bit size" & Natural'Image(Float'Size) & ",");
   Text_IO.Put_Line("for " & Float'Image(Arg) & ", bits 24 .. 31 give");

   The_Bits := Interfaces.Shift_Right(The_Bits, 23);
   Result := Interfaces.Unsigned_8(The_Bits);

   declare
      use type Interfaces.Unsigned_8;
   begin
      Bit_IO_8.Default_Width := 0;

      Text_IO.Put("decimal "); Bit_IO_8.Put(Result);
      Text_IO.Put(", that's "); Bit_IO_8.Put(Result - 127);
      Text_IO.Put_Line(" without bias");

      Text_IO.Put("binary "); Bit_IO_8.Put(Result, Base => 2);
      Text_IO.Put(", or ");  Bit_IO_8.Put(Result - 127, Base => 2);
      Text_IO.Put_Line(" without bias.");
   end;
end Th;



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]               ` <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com>
                                   ` (2 preceding siblings ...)
  2009-01-15  9:56                 ` Stuart
@ 2009-01-15 12:40                 ` Stuart
  2009-01-15 14:02                 ` Stephen Leake
  4 siblings, 0 replies; 50+ messages in thread
From: Stuart @ 2009-01-15 12:40 UTC (permalink / raw)


"ChristopherL" <clusardi2k@aol.com> wrote in message 
news:1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com...
<snip>
> So, what is the proper way to store a number (never being greater
> than 200.5) in a 8 bit short number as outlined above?

then

"Stuart" <stuart@0.0> wrote in message 
news:496f03e2$1_1@glkas0286.greenlnk.net...
> OK - there is no 'proper' way, there are several ways, each of which has
> advantages and disadvantages.
<snip>

To add to what I wrote before, and depending on what you are trying to 
achieve, you might find the following Ada code of interest:

with Ada.Unchecked_Conversion;
procedure ada_main is
   type Flt is digits 7 range -1000.0 .. 1000.0;  -- !!! or whatever!

   -- The condition we need to allow the range 0.0..200.0 to be stored with
   -- most precision in 8 bits is:
   --      255 * small > 200.0 - small/2
   --   => 255.5 * small > 200.0
   --   => small > 200.0 / 255.5
   Fxd_Small : constant := 0.782778865;
      -- !!! There seem to be some issues with GNAT debugger using an 
expression here!

   type Fxd is delta Fxd_Small range 0.0 .. 200.0;
   for  Fxd'small use Fxd_Small;
   for  Fxd'size use 8;

   type Int is mod 256;  -- In case you really want the underlying 'integer' 
representation
   for  Int'size use 8;

   function to_Int is new Ada.Unchecked_Conversion(Source => Fxd, Target => 
Int);

   -- Create some volatile variables to force code to be generated.
   X : Flt;
   pragma volatile(X);
   Y : Fxd;
   for Y'size use 8;
   pragma volatile(Y);

   Z : Int;
   for Z'size use 8;
   pragma volatile(Z);

begin
   X := 200.0;
   Y := Fxd(X);    -- 199.6086
   -- Note: Read up about model numbers !!!
   -- This is the closest model number to 200.0 for Fxd.
   Z := to_Int(Y);  -- 255
end ada_main;

Regards
   Stuart 





^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
       [not found]               ` <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com>
                                   ` (3 preceding siblings ...)
  2009-01-15 12:40                 ` Stuart
@ 2009-01-15 14:02                 ` Stephen Leake
  2009-01-15 15:54                   ` Dmitry A. Kazakov
  4 siblings, 1 reply; 50+ messages in thread
From: Stephen Leake @ 2009-01-15 14:02 UTC (permalink / raw)


ChristopherL <clusardi2k@aol.com> writes:

> On Jan 14, 12:41 pm, Adam Beneschan <a...@irvine.com> wrote:
>> On Jan 14, 12:13 pm, ChristopherL <clusard...@aol.com> wrote:
>>
>> > I want to copy bits from an Ada float to a user defined short integer
>> > maintaining the same
>> > bit representation.
>>
>
> This problem was given to me to try to solve who said it's probably
> impossible!

Ok. This could eventually be used to verify the floating point format
used by your machine, for example.

The right way to proceed is to write a bit-level record clause for the
floating point type on your hardware; something like:

type unsigned_24 is ...;
type usigned_7 is ...;

type IEEE_32_Float_Rep_Type is record
    sign : boolean;
    exponent : unsigned_7;
    mantissa_1 : Interfaces.unsigned_8;
    mantissa_2 : Interfaces.unsigned_8;
    mantissa_3 : Interfaces.unsigned_8;
end record;

for IEEE_32_Float_Rep_Type use record
    sign : at 0 range 0 .. 0;
    exponent : at 0 range 1 .. 8;
    mantissa_1 : at 0 range 9 .. 15;
    mantissa_2 : at 0 range 16 .. 23;
    mantissa_3 : at 0 range 24 .. 31;
end record;

Note that I broke the mantissa down into three parts, each fitting in
8 bits.

I probably have the details wrong for IEEE 32 bit float; look it up
somewhere.

Especially note that the 'range' values need to have the correct
endianness for your machine (sign will be at 31 on some machines, 0 on
others).

Then use unchecked conversion from float to IEEE_32_Float_Rep_type,
then simple assignment (possibly with an integer type conversion) from
any field to an Unsigned_8.

--
-- Stephe



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-14 21:09   ` sjw
  2009-01-14 21:16     ` Adam Beneschan
@ 2009-01-15 14:44     ` Brian Drummond
       [not found]       ` <3f1f2f67-5d69-4baf-8e8c-0d2b5f68475f@p36g2000prp.googlegroups.com>
  1 sibling, 1 reply; 50+ messages in thread
From: Brian Drummond @ 2009-01-15 14:44 UTC (permalink / raw)


On Wed, 14 Jan 2009 13:09:50 -0800 (PST), sjw <simon.j.wright@mac.com>
wrote:

>On Jan 14, 1:33�am, Brian Drummond <brian_drumm...@btconnect.com>
>wrote:
>
>> If you need to put 200 in an 8-bit type, why not use
>> Natural range 0 to 255?
>
>Natural range <whatever> has the same number of bits as Integer, which
>has to be at least 16 and is very likely to be 32 (or maybe 64 on a 64-
>bit machine).

OK, I mistakenly thought you could apply 'size to a subtype of natural.
Perhaps you can to a new type derived from it? I'll check later.

Otherwise your modular type suggestion is best.

>type Short is mod 256;
>for Short'Size use 8;  -- a confirming representation clause

- Brian



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15 14:02                 ` Stephen Leake
@ 2009-01-15 15:54                   ` Dmitry A. Kazakov
  2009-01-15 16:29                     ` Hyman Rosen
  0 siblings, 1 reply; 50+ messages in thread
From: Dmitry A. Kazakov @ 2009-01-15 15:54 UTC (permalink / raw)


On Thu, 15 Jan 2009 09:02:34 -0500, Stephen Leake wrote:

> ChristopherL <clusardi2k@aol.com> writes:
> 
>> On Jan 14, 12:41�pm, Adam Beneschan <a...@irvine.com> wrote:
>>> On Jan 14, 12:13 pm, ChristopherL <clusard...@aol.com> wrote:
>>>
>>> > I want to copy bits from an Ada float to a user defined short integer
>>> > maintaining the same
>>> > bit representation.
>>
>> This problem was given to me to try to solve who said it's probably
>> impossible!
> 
> Ok. This could eventually be used to verify the floating point format
> used by your machine, for example.
>
> The right way to proceed is to write a bit-level record clause for the
> floating point type on your hardware; something like:
> 
> type unsigned_24 is ...;
> type usigned_7 is ...;
> 
> type IEEE_32_Float_Rep_Type is record
>     sign : boolean;
>     exponent : unsigned_7;
>     mantissa_1 : Interfaces.unsigned_8;
>     mantissa_2 : Interfaces.unsigned_8;
>     mantissa_3 : Interfaces.unsigned_8;
> end record;
> 
> for IEEE_32_Float_Rep_Type use record
>     sign : at 0 range 0 .. 0;
>     exponent : at 0 range 1 .. 8;
>     mantissa_1 : at 0 range 9 .. 15;
>     mantissa_2 : at 0 range 16 .. 23;
>     mantissa_3 : at 0 range 24 .. 31;
> end record;
> 
> Note that I broke the mantissa down into three parts, each fitting in
> 8 bits.
> 
> I probably have the details wrong for IEEE 32 bit float; look it up
> somewhere.

Well, it is not that simple, because there are special patterns for ideals
like NaN and also denormalized representations. Also there is an issue of
octet endianness.

Anyway here is an implementation that handles IEEE 754 representations:

http://www.dmitry-kazakov.de/ada/components.htm#IEEE_754

> Especially note that the 'range' values need to have the correct
> endianness for your machine (sign will be at 31 on some machines, 0 on
> others).

Or even somewhere in the middle, provided that the machine numbers are
indeed IEEE... (:-))

> Then use unchecked conversion from float to IEEE_32_Float_Rep_type,
> then simple assignment (possibly with an integer type conversion) from
> any field to an Unsigned_8.

Well, that is IMO a different task, and there are floating point attributes
like S'Exponent and S'Fraction for this, which do the job properly.

I am afraid that the OP's question does not make any sense. It looks like a
C-ism...

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15  3:09           ` Randy Brukardt
@ 2009-01-15 16:28             ` Adam Beneschan
  2009-01-15 21:21               ` Robert A Duff
  0 siblings, 1 reply; 50+ messages in thread
From: Adam Beneschan @ 2009-01-15 16:28 UTC (permalink / raw)


On Jan 14, 7:09 pm, "Randy Brukardt" <ra...@rrsoftware.com> wrote:
> "Adam Beneschan" <a...@irvine.com> wrote in message
>
> news:12837a65-dd6e-493f-91e8-4140d67237c2@z28g2000prd.googlegroups.com...
>
> > On Jan 14, 3:09 pm, Martin <martin.do...@btopenworld.com> wrote:
> >> On Jan 14, 9:16 pm, Adam Beneschan <a...@irvine.com> wrote:
>
> >> > Not necessarily.  You can apply a 'Size clause to a subtype:
>
> >> >    subtype Byte is Natural range 0 .. 255;
> >> >    for Byte'Size use 8;
>
> >> Since when???
>
> > Since Ada 95 (I don't think this was allowed in Ada 83).  See 13.1
> > (8).  GNAT may not allow this, but 13.1(20) gives implementations the
> > right to put their own restrictions on representation items.
>
> No, 13.3(48) says "Size may be specified for *first subtypes* ..." (emphasis
> mine). Nothing about specifying it for non-first subtypes. (There has been
> quite a bit of discussion about changing that, but it leads to uncomfortable
> issues with legality rules depending on representation - not enough people
> can live with that.)

Wow, that's confusing when looking at 13.1(8).  That says, "A
representation item that names a subtype is either subtype-specific
(Size and Alignment clauses) or type-related (all others).  Subtype-
specific aspects may differ for different subtypes of the same type".
The clear implication of that seems to be that you can use a subtype-
specific representation item to change certain aspects of subtypes so
that they differ from those aspects of different subtypes of the same
type.  Yet the RM says for both Size and Alignment that they may be
specified for "first subtypes", and nothing says they may be specified
for other subtypes.

What's the point of defining the term "subtype-specific representation
item" if you can't make it subtype-specific?  I'll accept that 13.3
(48) [and 13.3(25)] override any implications I may have read into 13.1
(8); but if that's true, then the whole concept of a "subtype-specific
representation item", and probably the last two sentences of 13.1(8),
appear to be a crock that ought to be purged from the RM.  [Yes, there
may still be subtype-specific *aspects*, but the second sentence of
13.1(8) clearly talks about subtype-specific *representation items*.]

Anyway, I apologize to anybody for publicly disseminating
misinformation about the language, but I hope you all understand how I
got misled.  Sigh.......

                                   -- Adam





^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15 15:54                   ` Dmitry A. Kazakov
@ 2009-01-15 16:29                     ` Hyman Rosen
  2009-01-15 22:15                       ` Dmitry A. Kazakov
  0 siblings, 1 reply; 50+ messages in thread
From: Hyman Rosen @ 2009-01-15 16:29 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> I am afraid that the OP's question does not make any sense.
 > It looks like a C-ism...

I suspect such deep confusion is independent of programming
language. Look at the latest posting from the OP. He doesn't
seem to understand why he's getting an exception when trying
to assign 200 to a variable declared as -128 .. 127. At that
point, what can you do except throw up your hands?



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]       ` <3f1f2f67-5d69-4baf-8e8c-0d2b5f68475f@p36g2000prp.googlegroups.com>
@ 2009-01-15 16:36         ` Adam Beneschan
       [not found]         ` <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com>
       [not found]         ` <d3b7ad53-af51-4ac5-9167-7cb99e61b2b1@v5g2000pre.googlegroups.com>
  2 siblings, 0 replies; 50+ messages in thread
From: Adam Beneschan @ 2009-01-15 16:36 UTC (permalink / raw)


On Jan 15, 7:55 am, ChristopherL <clusard...@aol.com> wrote:
> After some size investigation here is what I found.
>
> Here is a program that impliments everything that I have been trying
> to do.
>
> I want to put the value of variable "Arg" into variable "Result"
> without changing
> the definitions of these two variables.
>
> Arg is a variable with 32 bits and will never be negative or greater
> than 200.
> Result is a variable of 10 bits.
>
> When I try to do it using the below program, I get the below error
> message.

The reason why the CONSTRAINT_ERROR/Range_Check error message is
happening has been explained to you, clearly, multiple times.  If you
still don't understand it, you're not going to understand it if we
tell you again.

                               -- Adam

>
> Chris L.
>
> --with Unchecked_Conversion;
>
> procedure test is
>   subtype Short_integer1 is INTEGER range -(2 ** 7)..((2 ** 7) - 1);
>   subtype A_Float is Float;
>
>   subtype Short_integer2 is Integer range 0.. ((2 ** 8) - 1);
>   subtype A_Natural is Natural range 0..((2 ** 8) - 1);
>
>   --FYI1
>   Size_Of_Float : integer := Float'Size;                  -- 32
>   Size_Of_short_int: integer := Short_integer1'Size;      --
> 10 !!!!!!! Size is 10
>
>   --FYI2
>   Size_Of_short_integer: integer := Short_integer2'Size;  -- 8
>   Size_Of_Natural: integer := A_Natural'Size;             -- 8
>
>   Arg    : A_Float;
>   Result : Short_integer1;
> Begin
>
> Arg := 200.0;
>
> Result := Short_integer1(Arg);
>
> End test;
>
> --Run Time Error Message Follows
> Unhandled exception:
>
> CONSTRAINT_ERROR raised in MAIN
>
> Range_Check_Failed
>
> Exception raised at code address: 0x412494
> +++ Program completed with exit code 1 at 01/15/09 07:37:44
> +++
> --------------------------------------------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]         ` <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com>
@ 2009-01-15 17:09           ` christoph.grein
  2009-01-15 17:17             ` Adam Beneschan
       [not found]             ` <a61abb30-bc60-4d13-b298-f369ddc8f741@z6g2000pre.googlegroups.com>
  2009-01-15 17:23           ` Georg Bauhaus
  1 sibling, 2 replies; 50+ messages in thread
From: christoph.grein @ 2009-01-15 17:09 UTC (permalink / raw)


On 15 Jan., 17:46, ChristopherL <clusard...@aol.com> wrote:
> Can some the Ada intelligent person modify this program to make it
> correctly
> put the value in variable "Arg" into the variable "Result".

Can anybody please help me to seat 200 people in a bus with 127 seats.



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
  2009-01-15 17:09           ` christoph.grein
@ 2009-01-15 17:17             ` Adam Beneschan
  2009-01-15 17:29               ` Georg Bauhaus
       [not found]             ` <a61abb30-bc60-4d13-b298-f369ddc8f741@z6g2000pre.googlegroups.com>
  1 sibling, 1 reply; 50+ messages in thread
From: Adam Beneschan @ 2009-01-15 17:17 UTC (permalink / raw)


On Jan 15, 9:09 am, christoph.gr...@eurocopter.com wrote:
> On 15 Jan., 17:46, ChristopherL <clusard...@aol.com> wrote:
>
> > Can some the Ada intelligent person modify this program to make it
> > correctly
> > put the value in variable "Arg" into the variable "Result".
>
> Can anybody please help me to seat 200 people in a bus with 127 seats.

Vaseline?

Actually, on second thought, maybe just get some bungee cords and tie
73 people to the roof.

                                -- Adam




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]         ` <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com>
  2009-01-15 17:09           ` christoph.grein
@ 2009-01-15 17:23           ` Georg Bauhaus
  1 sibling, 0 replies; 50+ messages in thread
From: Georg Bauhaus @ 2009-01-15 17:23 UTC (permalink / raw)


ChristopherL schrieb:
> Can some the Ada intelligent person modify this program to make it
> correctly
> put the value in variable "Arg" into the variable "Result".

Yes, anyone here can.  A possibly hint is this:
Of what type is your Result variable (look closely)?
Note the values that can be assigned to
variable Result, accordingly.
Is the converted value of floating point value 200.0
in this range of values?



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
  2009-01-15 17:17             ` Adam Beneschan
@ 2009-01-15 17:29               ` Georg Bauhaus
       [not found]                 ` <09b4a056-688d-49c8-b935-fa0b30f1ae84@w1g2000prk.googlegroups.com>
       [not found]                 ` <97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com>
  0 siblings, 2 replies; 50+ messages in thread
From: Georg Bauhaus @ 2009-01-15 17:29 UTC (permalink / raw)


Adam Beneschan schrieb:
> On Jan 15, 9:09 am, christoph.gr...@eurocopter.com wrote:
>> On 15 Jan., 17:46, ChristopherL <clusard...@aol.com> wrote:
>>
>>> Can some the Ada intelligent person modify this program to make it
>>> correctly
>>> put the value in variable "Arg" into the variable "Result".
>> Can anybody please help me to seat 200 people in a bus with 127 seats.
> 
> Vaseline?
> 
> Actually, on second thought, maybe just get some bungee cords and tie
> 73 people to the roof.


http://www.tropicalisland.de/india/rajasthan/karauli/pages/JAI%20-%20local%20bus%20truck%20with%20people%20on%20the%20roof%20on%20the%20road%20from%20Ranthambore%20National%20Park%20to%20Karauli%203008x2000.html



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]         ` <d3b7ad53-af51-4ac5-9167-7cb99e61b2b1@v5g2000pre.googlegroups.com>
@ 2009-01-15 17:50           ` Stuart
  0 siblings, 0 replies; 50+ messages in thread
From: Stuart @ 2009-01-15 17:50 UTC (permalink / raw)


"ChristopherL" <clusardi2k@aol.com> wrote in message 
news:d3b7ad53-af51-4ac5-9167-7cb99e61b2b1@v5g2000pre.googlegroups.com...
On Jan 15, 7:55 am, ChristopherL <clusard...@aol.com> wrote:
> After some size investigation here is what I found.

<snip>

> I want to put the value of variable "Arg" into variable "Result"
> without changing the definitions of these two variables.
>
> Arg is a variable with 32 bits and will never be negative or greater
> than 200.
> Result is a variable of 10 bits.

This is not really relevant, but to anything you have defined as a 
requirement (because you do not explicitly specify you want these sizes, 
they just happen to be what the system chose for you).

> When I try to do it using the below program, I get the below error
> message.
...
> procedure test is
> subtype Short_integer1 is INTEGER range -(2 ** 7)..((2 ** 7) - 1);

rewriting this
   subtype Short_integer1 is integer range -128..127;

> subtype A_Float is Float;

<snip>

> Arg : A_Float;
> Result : Short_integer1;
> Begin
>
> Arg := 200.0;
>
> Result := Short_integer1(Arg);

Given that you have declared Short_integer1 to have a range of -128..127 
what value do you think should be put in Result when you provide a value of 
200.0?

Are you, perhaps expecting wrap-around semantics?

   if (Arg > A_float(Short_integer1'last)) then
      Result := Short_integer1(A_float(Short_integer1'last) - Arg);
  else
      Result := Short_integer1(Arg);
  end if;

> End test;


> Can Ada intelligent person modify this program to make it correctly
> put the value in variable "Arg" into the variable "Result".

At the moment I would say no as we do not know how you think the 
contradiction of Result being in the -128..127 and you wanting the value 200 
should be resolved.

Ada is doing what it is meant to do and pointing out to you that the current 
behaviour is in error because you are exceeding the constraints (range of 
legal values) of Result.

As there seems to be some difficulty communicating requirements in terms 
that both parties can understand, perhaps you can clarify your requirement 
by indicating what values you expect for a number of cases bearing in mind 
that with your current definition (which you don't want to change) Result 
must be an integer value in the range -128..127:

    Arg       Result
    0.00
    0.49
    0.50
    0.51
    1.00
    1.49
    1.50
    1.51
126.50
126.51
127.00
127.01
127.49
127.50
127.51
128.00
199.49
199.50
199.51
199.99
200.00

-- 
Stuart 





^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]             ` <a61abb30-bc60-4d13-b298-f369ddc8f741@z6g2000pre.googlegroups.com>
@ 2009-01-15 18:15               ` Martin
  2009-01-15 18:34               ` Stuart
  1 sibling, 0 replies; 50+ messages in thread
From: Martin @ 2009-01-15 18:15 UTC (permalink / raw)


On Jan 15, 5:21 pm, ChristopherL <clusard...@aol.com> wrote:
> On Jan 15, 9:09 am, christoph.gr...@eurocopter.com wrote:
>
> > On 15 Jan., 17:46, ChristopherL <clusard...@aol.com> wrote:
>
> > > Can some the Ada intelligent person modify this program to make it
> > > correctly
> > > put the value in variable "Arg" into the variable "Result".
>
> > Can anybody please help me to seat 200 people in a bus with 127 seats.
>
> But, isn't 2^10 greater than 200! So, is there some way to put 200.0
> into the variable "Result".
>
> Ideally, if I achieve my ultimate goal I would like to later look at
> "Result" and be able to reconstruct "Arg" (200.0) from what is in
> "Result".
>
> Chris L.

2^10 is but that's not what you asked for!!!! At least not the 1st
post I saw from you!!!

E.g. from your 'test' procedure:

subtype Short_integer1 is INTEGER range -(2 ** 7)..((2 ** 7) - 1);

That's the range -128 to 127. "200.0" isn't in that range =>
exception.

But this new 'requirement' to be able to go between a float and a 10-
bit integer representation without loosing information is just pie in
the sky!

Float value    => 10.12312
As 10-bit int  => 10
Back to float  => 10.00000

That why computer have different data types and why programming
languages have different types to make use of them.

Or are you saying that all the floating point values will have ".0" as
their fractional part?

Is this any closer to what you want?

with Ada.Text_IO; use Ada.Text_IO;
procedure Test is
   type UInt10 is mod 2 ** 10;
   for UInt10'Size use 10;

   type Float32 is new Float;
   pragma Assert (Float'Size = 32);

   function To_Uint10 (F : Float32) return UInt10 is
   begin
      pragma Assert (F in 0.0 .. 200.0);
      return UInt10 (F);
   end To_Uint10;

   function To_Float32 (U : UInt10) return Float32 is
   begin
      return Float32 (U);
   end To_Float32;

   procedure Test_Value (Arg : Float32) is
      package Float32_IO is new Float_IO (Float32);
      package UInt10_IO  is new Modular_IO (UInt10);
      Result     : constant UInt10  := To_Uint10 (Arg);
      Back_Again : constant Float32 := To_Float32 (Result);
   begin
      Put ("Arg        =");
      Float32_IO.Put (Arg, Fore => 4, Aft => 3);
      New_Line;
      Put ("Result     =");
      UInt10_IO.Put (Result, Width => 4);
      New_Line;
      Put ("Back_Again =");
      Float32_IO.Put (Back_Again, Fore => 4, Aft => 3);
      New_Line;
   end Test_Value;
begin
   Put_Line ("UInt10'Size  =" & Integer'Image (UInt10'Size));
   Put_Line ("Float32'Size =" & Integer'Image (Float32'Size));
   Test_Value (200.0);
   Test_Value (200.5);
end Test;

C:\Ada\test\lib\test
UInt10'Size  = 10
Float32'Size = 32
Arg        =   2.000E+02
Result     = 200
Back_Again =   2.000E+02
Arg        =   2.005E+02
Result     = 201
Back_Again =   2.010E+02

Cheers
-- Martin



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]             ` <a61abb30-bc60-4d13-b298-f369ddc8f741@z6g2000pre.googlegroups.com>
  2009-01-15 18:15               ` Martin
@ 2009-01-15 18:34               ` Stuart
  1 sibling, 0 replies; 50+ messages in thread
From: Stuart @ 2009-01-15 18:34 UTC (permalink / raw)


"ChristopherL" <clusardi2k@aol.com> wrote in message 
news:a61abb30-bc60-4d13-b298-f369ddc8f741@z6g2000pre.googlegroups.com...
> On Jan 15, 9:09 am, christoph.gr...@eurocopter.com wrote:
> > On 15 Jan., 17:46, ChristopherL <clusard...@aol.com> wrote:
> >
> > > Can some the Ada intelligent person modify this program to make it
> > > correctly
> > > put the value in variable "Arg" into the variable "Result".
> >
> > Can anybody please help me to seat 200 people in a bus with 127 seats.
>
> But, isn't 2^10 greater than 200!

Yes it is.

>  So, is there some way to put 200.0 into the variable "Result".

Not in a way that means Result has the value 200 because the declared type 
of Result (Short_integer1) is specified as having the range  -128..127.  If 
you want Result to hold values in the range 0..200 you need to declare its 
type accordingly.

> Ideally, if I achieve my ultimate goal I would like to later look at
> "Result" and be able to reconstruct "Arg" (200.0) from what is in
> "Result".

You don't say to what precision you want Arg to be restored - it would seem 
that you are only expecting to get whole number values.  In which case, if 
you are wanting to keep all your definitions as they are, you might best be 
doing wrap around during the conversions.  For example:
     if (Arg > 127.0) then
         Result := Short_integer1(127.0 - Arg);
     else
         Result := Short_integer1(Arg);
    end if:

    if (Result < 0) then
       Arg := A_float(127 - Result);
          -- I think the calculation will be done in the base type of Result 
(integer),
          -- if not you can use integer'(result) to be exlicit about it.
    else
       Arg := A_float(Result);
   end if;

Of course, you should comment heavily to explain this odd-ball coding and 
why you don't simply declare the type to be in the range 0..200.

Regards
-- 
Stuart 





^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]                 ` <09b4a056-688d-49c8-b935-fa0b30f1ae84@w1g2000prk.googlegroups.com>
@ 2009-01-15 18:40                   ` Mike H
       [not found]                     ` <be26729d-9458-42fa-8c8c-004ca33b790d@f33g2000vbf.googlegroups.com>
  0 siblings, 1 reply; 50+ messages in thread
From: Mike H @ 2009-01-15 18:40 UTC (permalink / raw)


In message 
<09b4a056-688d-49c8-b935-fa0b30f1ae84@w1g2000prk.googlegroups.com>, 
ChristopherL <clusardi2k@aol.com> writes
>Well since I do not seem to be able to achieve what I want.
>
>Can someone tell me how to set a high order bit of a 10 number.
>
>if (arg > 128.0) then
>   arg := arg - 128.0;
>   -- set high order bit of arg maintaining all other bits
>end if;
>
>Chris L.
type Ten_Bit_Type is array (1..10) of Boolean;
for Ten_Bit_Type'Size use 10;

Ten_Bits : Ten_Bit_Type;

-- use Unchecked_Conversions to convert Arg to Ten Bits

Ten_Bits(1) := True;

-- use Unchecked_Conversions to convert Ten Bits back to Arg

-- 
Mike Hopkins
Project Rheilffordd Eryri
WHR <http://www.welshhighlandrailway.net/>




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]                     ` <be26729d-9458-42fa-8c8c-004ca33b790d@f33g2000vbf.googlegroups.com>
@ 2009-01-15 20:57                       ` Niklas Holsti
  0 siblings, 0 replies; 50+ messages in thread
From: Niklas Holsti @ 2009-01-15 20:57 UTC (permalink / raw)


ChristopherL wrote:
> How can I be getting the below error message with
> "Unchecked_Conversion".
> Isn't Unchecked_conversion just suppose to copy bits?

Perhaps because your Ada compiler applies the Ada 95 rules, in 
which a call of (an instance of) Unchecked_Conversion is 
"erroneous" if the result is a scalar with an "invalid 
representation" (RM95 13.9.1(12)). In your program, the assignment 
statement

    Result2 := Get_Bits (Result1);

applies Get_Bits to Result1, which has the value 201, so Get_Bits 
tries to return a Short_integer2 with the value 201, which is 
outside the range of Short_integer2 and so is an invalid 
representation. Your compiler may signal this error by raising the 
exception (but "erroneous" execution can have any result, in 
principle).

Under the Ada 2005 rules, you can get away with this assignment -- 
it is not "erroneous" -- but the only thing you can then do with 
Result2 is to check if it is 'Valid (RM05 13.9.1(12/2)); all other 
uses of Result2 are "erroneous" (until it is assigned some valid 
value).

One way to avoid this error is to remove the range constraints on 
Short_integer2, or widen them to include 201. However, even after 
reading all of this thread I don't understand what you want to 
achieve, so I don't know what to suggest -- except to ask your boss 
for a clearer statement of the problem.

> 
> Chris L.
> with Unchecked_Conversion;
> 
> procedure test is
>   subtype Short_integer1 is Natural range 0.. ((2 ** 10) - 1);
>   subtype Short_integer2 is Integer range -(2 ** 7)..((2 ** 7) - 1);
> 
>   subtype A_Float is Float;
> 
>   subtype A_Natural is Natural range 0..((2 ** 8) - 1);
>   -- The next line (if used) gives compilation error message:
> A_NATURAL does not denote a first subtype
>   --for A_Natural'Size use Short_integer1'Size;
> 
>   Size_Of_Float : integer := Float'Size;                  -- 32
> 
>   Size_Of_short_integer: integer := Short_integer1'Size;  -- 10 bits
> long
>   Size_Of_short_int: integer := Short_integer2'Size;      -- 10 bits
> long
> 
>   Size_Of_Natural: integer := A_Natural'Size;             -- 8
> 
>   Arg     : A_Float;
>   Result2 : Short_integer2;
>   Result1 : Short_integer1;
> 
>   function Get_Bits is new Unchecked_Conversion (Source =>
> Short_integer1, Target => Short_integer2);
> 
> Begin
> 
> Arg := 200.0;
> 
> Result1 := Short_integer1(Arg + 0.5);  -- Result1 becomes 201
> Result2 := Get_Bits (Result1);
> 
> End test;
> 
> -- Error Message
> +++ Program  started at 01/15/09 12:14:02
> 
> Unhandled exception:
> 
> CONSTRAINT_ERROR raised in MAIN
> 
> Range_Check_Failed
> 
> Exception raised at code address: 0x413516
> +++ Program completed with exit code 1 at 01/15/09 12:14:02
> +++
> --------------------------------------------------------------------------------------------------------------

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15 16:28             ` Adam Beneschan
@ 2009-01-15 21:21               ` Robert A Duff
  2009-01-16  1:17                 ` Adam Beneschan
  0 siblings, 1 reply; 50+ messages in thread
From: Robert A Duff @ 2009-01-15 21:21 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> Wow, that's confusing when looking at 13.1(8).  That says, "A
> representation item that names a subtype is either subtype-specific
> (Size and Alignment clauses) or type-related (all others).  Subtype-
> specific aspects may differ for different subtypes of the same type".

It says the "aspects may differ".  That is, different subtypes can have
different sizes and alignments.  It does not say that you can specify
different sizes and alignments.

I agree that it's confusing!

For:

    subtype Byte is Natural range 0 .. 255;

Byte'Size = 8.  You can write "pragma Assert(Byte'Size = 8);"
if you want to clarify that fact.

- Bob



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15 16:29                     ` Hyman Rosen
@ 2009-01-15 22:15                       ` Dmitry A. Kazakov
  2009-01-16  9:11                         ` Jacob Sparre Andersen
  0 siblings, 1 reply; 50+ messages in thread
From: Dmitry A. Kazakov @ 2009-01-15 22:15 UTC (permalink / raw)


On Thu, 15 Jan 2009 11:29:36 -0500, Hyman Rosen wrote:

> Dmitry A. Kazakov wrote:
>> I am afraid that the OP's question does not make any sense.
>  > It looks like a C-ism...
> 
> I suspect such deep confusion is independent of programming
> language.

The problem is that people get prematurely exposed to C, which is a very
difficult language, requiring a lot of self discipline and thinking. Maybe
this is why it is so popular among good programmers. But many, not so
lucky, just fail to understanding some basic concepts that are not
articulated in the language. For example, the concept that encoding and the
thing being encoded are not same. I call it C-ism, a persistent damage
caused by a premature exposure to C.

> Look at the latest posting from the OP. He doesn't
> seem to understand why he's getting an exception when trying
> to assign 200 to a variable declared as -128 .. 127.

But 200 fits into one byte! - cries his poor mind...

> At that point, what can you do except throw up your hands?

Guys here keep on trying to help, good Samaritans... (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15 21:21               ` Robert A Duff
@ 2009-01-16  1:17                 ` Adam Beneschan
  2009-01-16 14:55                   ` Robert A Duff
  0 siblings, 1 reply; 50+ messages in thread
From: Adam Beneschan @ 2009-01-16  1:17 UTC (permalink / raw)


On Jan 15, 1:21 pm, Robert A Duff <bobd...@shell01.TheWorld.com>
wrote:
> Adam Beneschan <a...@irvine.com> writes:
> > Wow, that's confusing when looking at 13.1(8).  That says, "A
> > representation item that names a subtype is either subtype-specific
> > (Size and Alignment clauses) or type-related (all others).  Subtype-
> > specific aspects may differ for different subtypes of the same type".
>
> It says the "aspects may differ".  That is, different subtypes can have
> different sizes and alignments.  It does not say that you can specify
> different sizes and alignments.

Yeah, but coming right after, and in the same paragraph as, a sentence
that says that "Size and Alignment clauses" are subtype-specific
clauses while all others are type-related, how the heck else is one
supposed to interpret it?  I mean, if the RM goes to all the trouble
of distinguishing "subtype-specific rep items" from "type-related rep
items", wouldn't one assume that there's some actual reason for making
this distinction---and that the sentence that immediately follows it
provides that reason?  Furthermore, the first two sentences of the
paragraph are about representation items, not about aspects; it would
seem odd to throw in a sentence about aspects that doesn't have any
relevance for representation items.

To sum up, I believe that anyone who reads 13.1(8) would think it's
100% clear that subtype-specific representation items can be used to
make subtype-specific aspects different for different subtypes of the
same type, UNLESS you interpret the paragraph in a VERY odd way.  I
now see that other clauses in the RM require 13.1(8) to be interpreted
in this odd way.  But to me, that's an indication that some rewording
would be useful.

                                -- Adam



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included)
       [not found]                 ` <97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com>
@ 2009-01-16  3:17                   ` Steve D
  0 siblings, 0 replies; 50+ messages in thread
From: Steve D @ 2009-01-16  3:17 UTC (permalink / raw)


"ChristopherL" <clusardi2k@aol.com> wrote in message 
news:97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com...
> If it is not possible to do what I want.
>
> Can someone modify the below program to show me how to set a high
> order bit of a
> 10 number number, and also maintain all other bits.
>
> procedure test2 is
>
> subtype Short_integer1 is INTEGER range -(2 ** 7)..((2 ** 7) - 1);
>
> arg:float;
> Result:Short_integer1;
>
> begin
>
> arg := 200.0
>
> if (arg > 128.0) then
>
>   arg := arg - 128.0;
>   --Result := ?;
>
> else
>
>   Result := Short_integer1(arg);
>
> end if;
>
> end test2;
>
> In this example I would like Result to have a bit representation such
> as:
>
> 10  0100  1000
>
> If you have a simplier algorithm (implemented) please share it with
> me.
>
> Thanks,
> Chris L.
>
>

See if this does what you want:

with Interfaces;
use Interfaces;
with Ada.Unchecked_Conversion;
with Ada.Text_IO;
with Ada.Float_Text_IO;

procedure TestSteve is

    subtype Short_Integer is Integer range -128..127;

    package Short_Io is new Ada.Text_Io.Integer_IO( Short_Integer );
    package Unsigned_8_Io is new Ada.Text_Io.Modular_IO( Unsigned_8 );

    function Float_To_Short_Integer( value : float ) return Short_Integer is
        function Conv is new Ada.Unchecked_Conversion( Unsigned_8, 
Short_Integer );
    begin
 return Conv( Unsigned_8( value ) );
    end Float_To_Short_Integer;

    procedure TestValue( value : float ) is
        result : Short_Integer;
 function Conv is new Ada.Unchecked_Conversion( Short_Integer, Unsigned_8 );
    begin
      result := Float_To_Short_Integer( value );
      Ada.Float_Text_IO.Put( value, 3, 1, 0 );
      Ada.Text_IO.Put( " => ");
      Short_IO.Put( Float_To_Short_Integer( value ) );
      Ada.Text_IO.Put( " => ");
      Unsigned_8_IO.Put( Conv( Float_To_Short_Integer( value ) ), 4, 2 );
      Ada.Text_IO.New_Line;
    end TestValue;

begin
    TestValue( 0.0 );
    TestValue( 127.0);
    TestValue( 200.5 );
end TestSteve;

Output:

  0.0 =>    0 => 2#0#
127.0 =>  127 => 2#1111111#
200.5 =>  -55 => 2#11001001#

Regards,
Steve 




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-15 22:15                       ` Dmitry A. Kazakov
@ 2009-01-16  9:11                         ` Jacob Sparre Andersen
  2009-01-16 11:03                           ` Mike H
  2009-01-16 22:16                           ` Brian Drummond
  0 siblings, 2 replies; 50+ messages in thread
From: Jacob Sparre Andersen @ 2009-01-16  9:11 UTC (permalink / raw)


Dmitry A. Kazakov wrote:

> But 200 fits into one byte! - cries his poor mind...

Actually it fits in one bit - using a suitable declaration of the type.

More seriously; I still haven't been able to figure out exactly what
problem the OP really is trying to solve.

Greetings,

Jacob
-- 
"There is nothing worse than having only one drunk head."



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-16  9:11                         ` Jacob Sparre Andersen
@ 2009-01-16 11:03                           ` Mike H
  2009-01-16 22:16                           ` Brian Drummond
  1 sibling, 0 replies; 50+ messages in thread
From: Mike H @ 2009-01-16 11:03 UTC (permalink / raw)


In message <87d4en4nu4.fsf@nbi.dk>, Jacob Sparre Andersen 
<sparre@nbi.dk> writes
>More seriously; I still haven't been able to figure out exactly what
>problem the OP really is trying to solve.
>
At one stage he seemed to mention changing a particular bit in a ten bit 
word while leaving the other bits undisturbed. To my mind, not only a 
very different problem but also relatively simple.

-- 
Mike Hopkins
Project Rheilffordd Eryri
WHR <http://www.welshhighlandrailway.net/>




^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-16  1:17                 ` Adam Beneschan
@ 2009-01-16 14:55                   ` Robert A Duff
  0 siblings, 0 replies; 50+ messages in thread
From: Robert A Duff @ 2009-01-16 14:55 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> Yeah, but coming right after, and in the same paragraph as, a sentence
> that says that "Size and Alignment clauses" are subtype-specific
> clauses while all others are type-related, how the heck else is one
> supposed to interpret it?  I mean, if the RM goes to all the trouble
> of distinguishing "subtype-specific rep items" from "type-related rep
> items", wouldn't one assume that there's some actual reason for making
> this distinction---and that the sentence that immediately follows it
> provides that reason?

I already admitted that it is confusing!

Here's an example that illustrates the reason for having these
definitions:

    type Root_Color is (Red, Orange, Yellow, Green);
    type Color is new Root_Color Red..Orange;

    for Color use (Red => 0, Orange => 1, Yellow => 2, Green => 4);
    for Color'Size use 1;
    subtype Mumble is Color'Base range Red..Yellow;

The enum rep clause is type-related -- it's setting an aspect of the
type, and this does not differ among different subtypes.

The Size clause is subtype-related -- it's setting Color'Size to 1,
but other subtypes of the same type have different sizes.
Color'Base'Size is 3, and Mumble'Size is 2.

I repeat: I agree with you that the wording is confusing.

- Bob



^ permalink raw reply	[flat|nested] 50+ messages in thread

* Re: How to put 200 into an integer sub-type of 16 bits (code included)
  2009-01-16  9:11                         ` Jacob Sparre Andersen
  2009-01-16 11:03                           ` Mike H
@ 2009-01-16 22:16                           ` Brian Drummond
  1 sibling, 0 replies; 50+ messages in thread
From: Brian Drummond @ 2009-01-16 22:16 UTC (permalink / raw)


On 16 Jan 2009 10:11:47 +0100, Jacob Sparre Andersen <sparre@nbi.dk>
wrote:

>Dmitry A. Kazakov wrote:
>
>> But 200 fits into one byte! - cries his poor mind...
>
>Actually it fits in one bit - using a suitable declaration of the type.
>
>More seriously; I still haven't been able to figure out exactly what
>problem the OP really is trying to solve.

I suspect it may be a software implementation of a "long stand"

- Brian



^ permalink raw reply	[flat|nested] 50+ messages in thread

end of thread, other threads:[~2009-01-16 22:16 UTC | newest]

Thread overview: 50+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <407ae64d-3cb3-4310-b59e-f1bbae9910a5@t39g2000prh.googlegroups.com>
2009-01-14  1:33 ` How to put 200 into an integer sub-type of 16 bits (code included) Brian Drummond
     [not found]   ` <3d3719f4-355c-4094-9902-495d612d46fe@n33g2000pri.googlegroups.com>
2009-01-14  9:06     ` Ludovic Brenta
2009-01-14 10:08     ` Georg Bauhaus
2009-01-14 10:29       ` Georg Bauhaus
2009-01-14 12:47     ` Brian Drummond
2009-01-14 12:53     ` Brian Drummond
     [not found]       ` <f4894476-851e-493f-93a2-168976bd97fb@s1g2000prg.googlegroups.com>
2009-01-14 16:08         ` Adam Beneschan
     [not found]           ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
2009-01-14 20:41             ` Adam Beneschan
     [not found]               ` <c265ffb7-6159-4d85-b259-78b830e115f9@v18g2000pro.googlegroups.com>
2009-01-15  2:39                 ` SteveD@teranews.com
     [not found]               ` <3625f980-4406-4f51-b494-dd4a48cab840@p36g2000prp.googlegroups.com>
2009-01-15  6:53                 ` Michael Bode
2009-01-15  8:57                   ` Martin
     [not found]               ` <1a2b31ac-cf6b-44e3-85b7-04594460db87@d36g2000prf.googlegroups.com>
2009-01-14 22:47                 ` Adam Beneschan
2009-01-14 23:11                 ` Ludovic Brenta
2009-01-15  9:56                 ` Stuart
2009-01-15 12:40                 ` Stuart
2009-01-15 14:02                 ` Stephen Leake
2009-01-15 15:54                   ` Dmitry A. Kazakov
2009-01-15 16:29                     ` Hyman Rosen
2009-01-15 22:15                       ` Dmitry A. Kazakov
2009-01-16  9:11                         ` Jacob Sparre Andersen
2009-01-16 11:03                           ` Mike H
2009-01-16 22:16                           ` Brian Drummond
     [not found]               ` <fc154f52-7168-447a-bcd3-6ece9066ebf7@r37g2000prr.googlegroups.com>
2009-01-15 10:29                 ` Georg Bauhaus
2009-01-14 21:32             ` sjw
2009-01-14 21:51             ` Brian Drummond
2009-01-14 21:17           ` sjw
2009-01-14 18:49     ` Jeffrey R. Carter
2009-01-15 10:12       ` Quote of the Day (Re: " Peter Hermann
2009-01-14 21:09   ` sjw
2009-01-14 21:16     ` Adam Beneschan
2009-01-14 23:09       ` Martin
2009-01-15  0:07         ` Adam Beneschan
2009-01-15  3:09           ` Randy Brukardt
2009-01-15 16:28             ` Adam Beneschan
2009-01-15 21:21               ` Robert A Duff
2009-01-16  1:17                 ` Adam Beneschan
2009-01-16 14:55                   ` Robert A Duff
2009-01-15 14:44     ` Brian Drummond
     [not found]       ` <3f1f2f67-5d69-4baf-8e8c-0d2b5f68475f@p36g2000prp.googlegroups.com>
2009-01-15 16:36         ` How to put 200.0 (is float variable of 32 bits) into an integer sub-type of 10 bits (complete program included) Adam Beneschan
     [not found]         ` <8e64f509-f6fe-4d86-ae1a-fe0b1c88555a@v5g2000pre.googlegroups.com>
2009-01-15 17:09           ` christoph.grein
2009-01-15 17:17             ` Adam Beneschan
2009-01-15 17:29               ` Georg Bauhaus
     [not found]                 ` <09b4a056-688d-49c8-b935-fa0b30f1ae84@w1g2000prk.googlegroups.com>
2009-01-15 18:40                   ` Mike H
     [not found]                     ` <be26729d-9458-42fa-8c8c-004ca33b790d@f33g2000vbf.googlegroups.com>
2009-01-15 20:57                       ` Niklas Holsti
     [not found]                 ` <97231951-54a0-4df7-bb73-04261b34287f@e6g2000vbe.googlegroups.com>
2009-01-16  3:17                   ` Steve D
     [not found]             ` <a61abb30-bc60-4d13-b298-f369ddc8f741@z6g2000pre.googlegroups.com>
2009-01-15 18:15               ` Martin
2009-01-15 18:34               ` Stuart
2009-01-15 17:23           ` Georg Bauhaus
     [not found]         ` <d3b7ad53-af51-4ac5-9167-7cb99e61b2b1@v5g2000pre.googlegroups.com>
2009-01-15 17:50           ` Stuart
     [not found] <6c7964a6-1733-434b-b1b1-962baa4ebba2@p23g2000prp.googlegroups.com>
2009-01-13 21:01 ` How to put 200 into an integer sub-type of 16 bits (code included) Ludovic Brenta

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