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
There are two reasons I can think of: a) Are you sure that the data is sent in the right order (LSB first)? b) Are you sure that val4*val4 + val3*val3 is not exceeding the float range? And what about rounding errors when val4 or val3 are very small? Did you try to monitor the received values, too? Are they identical on PC and micro? Regards Thomas
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 Which toolset are you using (ARM/C51/C166) ? What is the width of the floating point numbers on your uC and on the PC ? In what way are the numbers "not correct" ? Are they off by a small amount (rounding/precision errors) or totally bogus ?
Yes the data is sent in correct order because i have simulated it. and there are few values which are not computed correctly the dat i recive from one module is sent as its to the pc as well as to this function so data is comming via micro i am using c51 and micro i am using is 89c52 i have also disabled interrupts in this function thanks and regards
Yes the data is sent in correct order because i have simulated it. and there are few values which are not computed correctly Can you give an example ? i.e. what are the input values, what is the result, and what result did you expect ?
"Yes the data is sent in correct order because i have simulated it." Are you certain that your simulation is correct? "i am using c51 and micro i am using is 89c52" A rather strange choice for a number-cruncher!
"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" It is quite likely (almost certain?) that a PC application will use a different floating-point representation from C51. If you are transferring the values byte-by-byte between PC and 8051, you will almost certainly have to do some format conversion. Have you done this? Are you sure that it is correct?
It's been a long time since I've seen a system that didn't support IEEE 754 format. As I recall, endianness is up to the implementation, though.
"endianness is up to the implementation, though." Yes, that's what I was thinking of! A PC might well use more bits, too...
The problem i am facing is that the val4 = sqrt(val4*val4 + val3*val3) is some time not comming correct It couldn't be that you expect a PC and a '51 to come up with identical (+-0.000%) values? could it? Erik
This "Problem" has also been posted on the AVRfreaks forum: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=240980#240980 So either the system actually includes an AVR, a PC, and some other Keil-supported processor (8051, ARM, or C166), or the OP is just randomly spamming forums...
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 !!
I posted: "It couldn't be that you expect a PC and a '51 to come up with identical (+-0.000%) values? could it?" Give some examples of "wrong" and what it should be Erik