Hi everyone I am beginner in arduino. I have doubt in floating point conversion. Below I attached my program,
float floatValue=12.15;
void setup(){
Serial.begin(9600);
Serial.print("The given float value is =");
Serial.println(floatValue);
Serial.print("The hex value is =");
Serial.println(floatValue,HEX);
Serial.print("The bin value is =");
Serial.println(floatValue,BIN);
}
void loop(){
}
Output will be like this,
The given float value is =12.15
The hex value is =12.1499996185302734
The bin value is =12.15
I don't know the reason why my hexadecimal and binary value coming like this. At the same time while executing this program by means of int value it will work fine.
typedef union
{
float v;
uint32_t as_int;
}
cracked_float_t;
cracked_float_t floatValue;
void setup()
{
floatValue.v = 12.15;
Serial.begin(9600);
Serial.print("The given float value is =");
Serial.println(floatValue.v);
Serial.print("The hex value is =");
Serial.println(floatValue.as_int,HEX);
Serial.print("The bin value is =");
Serial.println(floatValue.as_int,BIN);
}
void loop() {}
I believe such things are increasingly likely to run into aliasing trouble (undefined behaviour, no longer legal). A reinterpret_cast may save the day (either by working correctly or flagging the trouble).
You can have exact bit pattern for 12.25 if you ask for binary32 format. If you wish, you can use paper -pencil method to get binary value of 12.15 (1100.001000111111...).
Thank you guys for guiding me. I have small doubt in this below program,
typedef union{
float v;
int16_t loWord,hiWord;
byte b[sizeof(float)];
}
cracked_float_t;
cracked_float_t floatValue;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
floatValue.v=263.3;
Serial.print("The float value is =");
Serial.println(floatValue.v);
Serial.print("The byte value of given float is =");
for(int i=0;i<4;i++){
Serial.print(floatValue.b*,HEX);*
Serial.println(" ");* } floatValue.loWord=word(floatValue.b[1],floatValue.b[0]); floatValue.hiWord=word(floatValue.b[3],floatValue.b[2]); Serial.print("The 16 bit low byte is ="); Serial.println(floatValue.loWord,HEX); Serial.print("THE 16 bit high byte is ="); Serial.println(floatValue.hiWord,HEX); } void loop() {
// put your main code here, to run repeatedly:* } OUTPUT is, The float value is =263.30 The byte value of given float is =66 A6 83 43 The 16 bit low byte is =4383 THE 16 bit high byte is =4383 My Excepted Output is, The float value is =263.30 The byte value of given float is =66 A6 83 43 The 16 bit low byte is =4383 THE 16 bit high byte is =A666 I want a output will be 16 bit high byte = A666 but I get a output as 16 bit high byte = 4383.Can anyone providing a solution for this problem.
OUTPUT is,
The float value is =263.30
The byte value of given float is =66
A6
83
43
The 16 bit low byte is =4383
THE 16 bit high byte is =4383
My Excepted Output is,
The float value is =263.30
The byte value of given float is =66
A6
83
43
The 16 bit low byte is =4383
THE 16 bit high byte is =A666
My Excepted Output is,
The float value is =263.30
The byte value of given float is =66
A6
83
43
The 16 bit low byte is =A666
THE 16 bit high byte is =4383
My excepted output will be 16 bit low byte = A666 but I get a output as 16 bit low byte = 4383.Can anyone providing a solution for this problem.
Have a look at reply #15 again. And note, it's a union. EVERY definition inside it has the same memory address. Even loWord and hiWord. Do you still expect them to differ?