comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: How to put 200 into an integer sub-type of 16 bits (code included)
Date: Tue, 13 Jan 2009 13:01:51 -0800 (PST)
Date: 2009-01-13T13:01:51-08:00	[thread overview]
Message-ID: <62451c87-a63a-4ba5-9bf7-90439b9e4e2b@z27g2000prd.googlegroups.com> (raw)
In-Reply-To: 6c7964a6-1733-434b-b1b1-962baa4ebba2@p23g2000prp.googlegroups.com

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.



       reply	other threads:[~2009-01-13 21:01 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <6c7964a6-1733-434b-b1b1-962baa4ebba2@p23g2000prp.googlegroups.com>
2009-01-13 21:01 ` Ludovic Brenta [this message]
     [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
2009-01-14 21:17           ` sjw
     [not found]           ` <139961e9-bae6-4e60-8ff7-4f4779b27481@z6g2000pre.googlegroups.com>
2009-01-14 20:41             ` Adam Beneschan
     [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]               ` <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]               ` <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 18:49     ` Jeffrey R. Carter
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
replies disabled

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