comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.Lhota.adarose@verizon.net>
Subject: Re: How unchecked conversion works?
Date: Thu, 13 Jan 2005 21:22:22 GMT
Date: 2005-01-13T21:22:22+00:00	[thread overview]
Message-ID: <iEBFd.612$qu2.222@trndny08> (raw)
In-Reply-To: MtzFd.82780$C94.78656@tornado.fastwebnet.it

You would not use Unchecked_Conversion to convert a float to an integer. 
Basically, Unchecked_Conversion takes an object S of type Source and pretend 
it is an object of type Target, simply by assuming that an object of type 
Target resides at the same address as S. In other words, if we define

    function Convert is new Ada.Unchecked_Conversion( Source, Target );

    S : Source;

then the Ada call

    Convert( S )

does basically the same thing as the following C code:

    *( (Target *) &S )

Needless to say, the result of unchecked conversion depends heavily on the 
internal representation of the Source and Target types. This type of 
conversion is inherently risky in either C or Ada, which is why Ada makes it 
difficult to do unchecked conversions.

You almost certainly do not want to do an unchecked conversion to change a 
float into an integer. The integer resulting from such a conversion would 
have little to do with the original floating point number. Most likely you 
want to do a straighforward type conversion. For example:

    type My_Float is digits 6;
    type My_Integer is range 1 .. 10;

    S : My_Float;

Then we can convert S into a My_Integer value by writing

    My_Integer( S )

That's it!





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

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-13 18:54 How unchecked conversion works? None
2005-01-13 21:22 ` Frank J. Lhota [this message]
2005-01-13 22:28 ` Keith Thompson
2005-01-14  0:17   ` Larry Kilgallen
2005-01-14 15:23 ` None
2005-01-14 15:55   ` Marius Amado Alves
2005-01-14 22:29     ` Keith Thompson
2005-01-14 23:15       ` Marius Amado Alves
2005-01-15  1:27         ` Keith Thompson
2005-01-15  2:15         ` Larry Kilgallen
2005-01-15 10:24           ` Marius Amado Alves
2005-01-15 12:02             ` Larry Kilgallen
2005-01-15 16:54               ` Nick Roberts
2005-01-16 16:14                 ` Larry Kilgallen
2005-01-16 16:41                   ` Jeffrey Carter
2005-01-16 20:52                     ` TCSEC security levels [was: How unchecked conversion works?] Nick Roberts
2005-01-17 15:57                       ` Larry Kilgallen
2005-01-17  0:34                     ` How unchecked conversion works? Larry Kilgallen
2005-01-17  1:29                       ` Jeffrey Carter
2005-01-17  4:20                         ` Larry Kilgallen
2005-01-15 18:24             ` Jeffrey Carter
2005-01-14 16:52   ` Mark H Johnson
2005-01-14 22:27     ` Keith Thompson
2005-01-14 22:49   ` Stephen Leake
replies disabled

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