Regarding Unchecked_Conversion in ada

220 views Asked by At

Could anyone please explain me the operation of ada.unchecked_coversion(float, UnsignedInteger). What this operation will be performed ? Could anyone please clarify this with example. Thanks a lot in advance.

2

There are 2 answers

1
Timur Samkharadze On

From here

An unchecked conversion is a bit-for-bit copy without regard to the meanings attached to those bits and bit positions by either the source or the destination type. The source bit pattern can easily be meaningless in the context of the destination type. Unchecked conversions can create values that violate type constraints on subsequent operations. Unchecked conversion of objects mismatched in size has implementation-dependent results.

So it should be used with care and only for types that have "same" bit representation. e.g it wouldn't probably work to cast float to int as those types could be represented differently in memory.

Here are two variables

  • A: source float designated by address 0x249fd8c with value 1.0
  • B: target Integer designated by address 0x249fd88 (unitialized)

before conversion

enter image description here

and after calling unchecked conversion

enter image description here

As you can see whole operation is just bit by bit copy of source variable to target variable

Hope this answers your questions

2
Jeffrey R. Carter On

Normally an unchecked conversion has no effect at run time. It does not result in any code being generated. It is just permission to the compiler to interpret the bytes of the source value as the representation of a value of the target type. Most importantly, it does not cause a copy.