This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Squaring floating point values

Hello Everybody,

Iam facing a strange problem with floats and their mathematical operations

i recive a string of data through serial port( 20bytes i.e 5 float values)

so to do mathematical operation on these floating values i needto see the raw bytes in floating values to do that i created a union like below

union {
unsigned char FB[4];
float value;
long value1;
}convert1;

now i use convert1 to use the raw bytes for my calculations like below


float val1 = 0;
float LAST_val1 = 0;
float val2 = 0;
float LAST_val2 = 0;
float val3 = 0;
float val4 = 0;
float DIFFERANCE = 0;
Get_Difference(){

convert1.FB[0] = BUFFER[0];
convert1.FB[1] = BUFFER[1];
convert1.FB[2] = BUFFER[2];
convert1.FB[3] = BUFFER[3];
val1 = convert1.value;
val1 = (val1*180)/PI;
val4 = val1 - LAST_val1;
LAST_val1 = (convert1.value*180)/PI;


convert1.FB[0] = BUFFER[4];
convert1.FB[1] = BUFFER[5];
convert1.FB[2] = BUFFER[6];
convert1.FB[3] = BUFFER[7];
val2 = convert1.value;
val2 = (val2*180)/PI;
val3 = val2 - LAST_val2;
LAST_val2 = (convert1.value*180)/PI;

val4 = sqrt(val4*val4 + val3*val3);

if( val4 < 0.01) DIFFERANCE = DIFFERANCE + val4;

}

i am calling the Get_Differance(); function many times in my application

The problem i am facing is that the val4 = sqrt(val4*val4 + val3*val3)
is some time not comming correct

i am also monetring the values on my PC ie.the whole buffer is sent to an application on pc which shows the expected values but not inside the micro

is there some thing wrong with squaring of floating point number?
should i use a function for that?

pls help

Parents
  • yes the whole system involves avr and 8051
    so the message is on other forum

    some things to clear

    -: The compiler used is c51

    -: Application is basically decoding the Latitude and longitude comming inside the micro in four byte format representing a single precesion float

    -: I had no other option for micro as the hardware is fixed and miro is also fixed for the customer and i am just trying to solve their problems(this one among thelast one).
    worst they have done all the manufacturing of PCB for production !!

Reply
  • yes the whole system involves avr and 8051
    so the message is on other forum

    some things to clear

    -: The compiler used is c51

    -: Application is basically decoding the Latitude and longitude comming inside the micro in four byte format representing a single precesion float

    -: I had no other option for micro as the hardware is fixed and miro is also fixed for the customer and i am just trying to solve their problems(this one among thelast one).
    worst they have done all the manufacturing of PCB for production !!

Children