# Type conversions

• 06-25-2002
sean
Type conversions
Consider the following code (I'd use code tags - but I can't find the button for them):

#include <iostream.h>

int main()
{
int x;
int y = 159; // <- no meaning - just a number
float z = (float)y;
cout >> x >> y >> z;
return 0;
}

x would be essentially random - you have no control over it whatsoever, y is 159, but would z be 159.000 etc... or would it come out with a completely random, something like 159.31415298? I have a lot of questions all for the same project all to do with data types - but I've posted them separately to avoid confusion and complexity.
• 06-25-2002
NANO
Re: Type conversions
Quote:

Originally posted by Sean

Code:

```#include <iostream.h> int main() { int x; int y = 159; // <- no meaning - just a number float z = (float)y; cout >> x >> y >> z; return 0; }```

>>>float z = (float)y;
Do you mean float (y);:confused:
• 06-25-2002
Sorensen
>but would z be 159.000 etc... or would it come out with a completely random, something like 159.31415298?<

From the standard -

Quote:

An rvalue of an integer type can be converted to an rvalue of a floating point type. The result is exact if
possible. Otherwise, it is an unspecified choice of either the next lower or higher representable value. Loss
of precision occurs if the integral value cannot be represented exactly as a value of the floating type.
So it should be 159.000 if possible.
• 06-25-2002
sean
No. That's how you do type conversions - type casting. And thanks for the guy who posted just above me - I think you pressed post while I was still typing it - so I had to edit say thanks.