1
votes

Going on understanding of these datatypes as primitives

(int) char, and (char) int are intepretations of data. (int) c gives the integer value of that character, and (char) 14 gives you back the character encoded by 14.

I've always understood this as being a "memory parse", such that it just takes the value at that position and then applies a type filter to it.

Given that floating points are stored as some version of scientific notation, what is stored in memory should be garbage as an integer. Looking into this utility http://www.h-schmidt.net/FloatConverter/IEEE754.html it appears that the whole number portion is separated.

However, since this is in the higher portion of memory, how does the int cast know to "reformat"? Does the compiler identify that it was a float and apply special handling, or what's going on?

2
Don't question that low-level code. Trying to understand how your compiler works is like trying to learn binary: You're so used to thinking in terms of higher-level languages you won't get anywhere. - Majora320
That's one of the compiler's jobs: to generate a code sequence that really does convert from the integer representation to the analogous (but inevitably more complex) float or double encoding. Similarly, the conversion between float and double is not trivial either. - Jonathan Leffler
Given that you've tagged this as C, the question title should really be fixed to be more C like. There isn't a float.truncate, and you can't just write (int) float either. - Jonathan Leffler
Are all primitive types handled for cross conversion? - Captain Prinny
@Captain Prinny "all primitive types handled for cross conversion?" Many are, but not all. e,g, double to/from void *is not defined. - chux - Reinstate Monica

2 Answers

7
votes

Your understanding of casts is completely wrong. Casts are nothing but explicit requests for a value conversion from one type to another. They do not reinterpret the representation of one type as if it had a different type. The source code:

float f = 42.5;
int x;
x = (int)f;

simply instructs the compiler to produce code that truncates the floating point value of the expression f to an integer and store the result in the object x.

7
votes

I've always understood this as being a "memory parse", such that it just takes the value at that position and then applies a type filter to it.

That is an incorrect understanding.

The language specifies conversions between the fundamental arithmetic types. Lookup "Usual Arithmetic Conversions" on the web. You will find a lot of links that describe that. For converting a floating point type to an integral type, this is what the C99 Standard has to say:

6.3.1.4 Real floating and integer

1 When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

float f = 4.5;
int i = (int); // i is 4

f = -6.3;
i = (int)f;    // i is -6