Computer Science
Computer Science
ways of, implicitly or explicitly, changing an entity of one data type into another.
This is done to take advantage of certain features of type hierarchies or type
representations. One example would be small integers, which can be stored in a
compact format and converted to a larger representation when used in arithmetic
computations. In object-oriented programming, type conversion allows programs
to treat objects of one type as one of their ancestor types to simplify interacting
with them.
Each programming language has its own rules on how types can be converted. In
general, both objects and fundamental data types can be converted. In most
languages, the word coercion is used to denote an implicit conversion, either
during compilation or during run time. A typical example would be an expression
mixing integer and floating point numbers (like 5 + 0.1), where the integers are
normally converted into the latter. Explicit type conversions can either be
performed via built-in routines (or a special syntax) or via separately defined
conversion routines such as an overloaded object constructor.
In the C family of languages, the word cast typically refers to an explicit type
conversion (as opposed to an implicit conversion), regardless of whether this is a
re-interpretaion of a bit-pattern or a real conversion.
[edit] Implicit type conversion in C-like languages
double d;
long l;
int i;
if (d > i) d = i;
if (i > l) l = i;
if (d == l) d *= 2;
#include "stdio.h"
int main() {
int i_value = 16777217;
float f_value = 16777217.0;
printf("The integer is: %i\n", i_value);
printf("The float is: %f\n", f_value);
printf("Their equality: %i\n", i_value ==
f_value);
}
On compilers that implement floats as IEEE single precision, and ints as at least 32
bits, this code will give the peculiar result of printing out "The integer is:
16777217", followed by "The float is: 16777217.000000", then "Their equality: 0"
(where 1 represents equal). This odd behavior is caused by an implicit cast of
i_value to float when it is compared with f_value; a cast which loses precision,
making both values being compared equal.
One special case of implicit type conversion is type promotion, where the
compiler automatically expands the binary representation of objects of integer or
floating-point types. Promotions are commonly used with types smaller than the
native type of the target platform's ALU prior to arithmetic and logical operations
in order to make such operations possible, or more efficient if the ALU can work
with more than one type. C and C++ perform such promotion for objects of
boolean, character, wide character, enumeration, and short integer types which are
promoted to int, and for objects of type float, which are promoted to double.
Unlike some other type conversions, promotions never lose precision or modify the
value stored in the object.
double da = 5.5;
double db = 5.5;
int result = (int)da + (int)db;
//Result would be equal to 10 instead of 11.
checked
Before the conversion is performed, a runtime check is done to see if the
destination type can hold the source value. If not, an error condition is
raised.
unchecked
No check is performed. If the destination type cannot hold the source value,
the result is undefined.
bit pattern
The raw bit representation of the source is copied verbatim, and it is re-
interpreted according to the destination type. This can also be achieved via
aliasing.
class Myclass {
public:
double myD;
Myclass(double d) : myD(d) {};
};