Go Down

Topic: Cast (Read 271 times) previous topic - next topic

tinexd

Nov 01, 2018, 11:28 am Last Edit: Nov 01, 2018, 11:29 am by tinexd
Hello, can somebody explain the difference between cast and conversion, please?

cast e.g.   int x; float y;  y = (float) x;

conversion e.g.   int x; float y; y = float(x);

Thank you :)

PaulS

Quote
Hello, can somebody explain the difference between cast and conversion, please?
The "conversion" is creating a float from the value in the parentheses. Since you have not given x a value, you haven't a clue what will be in y.

The cast is telling the compiler to treat some number of bytes (4 in the case of a float cast) as though they were a float. The first byte is the first byte of the variable x. The second byte is the second half of  x. The 3rd and 4th bytes are whatever bytes follow x in memory.

The result of that cast will be garbage in y, too.
The art of getting good answers lies in asking good questions.

tinexd

#2
Nov 01, 2018, 12:11 pm Last Edit: Nov 01, 2018, 12:14 pm by tinexd
Ok, thank you.
Can you give some examples where i have to use conversion and where i have to use cast, please?

Whandall

The cast is telling the compiler to treat some number of bytes (4 in the case of a float cast) as though they were a float. The first byte is the first byte of the variable x. The second byte is the second half of  x. The 3rd and 4th bytes are whatever bytes follow x in memory.
That is only true for pointers to float / int.

Functional there should be no difference in value between (float)x and float(x).

Code: [Select]
int x = 1234;

void setup() {
  Serial.begin(250000);
  float y = (float)x;
  Serial.print(F("(float)x      = "));
  Serial.println(y);
  y = float(x);
  Serial.print(F("float(x)      = "));
  Serial.println(y);
  y = *((float*)&x);
  Serial.print(F("*((float*)&x) = "));
  Serial.println(y);
}
void loop() {}
Code: [Select]
(float)x      = 1234.00
float(x)      = 1234.00
*((float*)&x) = 0.00
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

PaulS

Quote
Can you give some examples where i have to use conversion and where i have to use cast, please?
You NEVER have to use "conversion".

Code: [Select]
float y = 1234;
does EXACTLY the same thing as
Code: [Select]
float y = float(1234);

You rarely need to cast one type to another, unless the types are pointers. When you DO need to cast, you'll know it, and know what cast is required.
The art of getting good answers lies in asking good questions.

GolamMostafa

Functional there should be no difference in value between (float)x and float(x).
Exactly!

Code: [Select]
void setup()
{
  Serial.begin(9600);
  int x = 1234;
  float y;
  y = (float)x;
  Serial.println(y); //shows: 1234.00

  long *p;
  p = (long*) &y;
  long m = *p;
  Serial.println(m, HEX); //shows: binary32 formatted value 449A4000

  Serial.println("//--------------------------------");
  float y1;
  y1 = float(x);
  Serial.println(y1); //shows: 1234.00

  long *p1;
  p1 = (long*) &y1;
  long m1 = *p1;
  Serial.println(m1, HEX); //shows: binary32 formatted value 449A4000
}

void loop()
{

}

Go Up