Interesting. It could quite easily be a fault in the floating point software. Embedded systems often include floating point as an option so as to keep the code size down.
I'm not sure that's the problem here though since you're first statement works.
What happens with:
result = 0.005 - 0.001;
result = -result;
result = 0.002 - 0.001;
result = 0.002 - 0.002;
result = 0.002 - 0.003;
result = 0.001 - 0.002;
result = 0.001 - 0.003;
result = 0.001 - 0.004;
The idea here is to collect useful information as to what could be causing it, a common thing to do in forensics. The results of those calculations are likely to be helpful in determining the actual problem.
Update 1:
Based on your results in the comments:
result = 0.005 - 0.001; // 0.004
result = -result; // 0.000
result = 0.002 - 0.001; // 0.001
result = 0.002 - 0.002; // 0.000
result = 0.002 - 0.003; // 0.000
result = 0.001 - 0.002; // 0.000
result = 0.001 - 0.003; // 0.000
result = 0.001 - 0.004; // 0.000
It looks like your floating point library has a serious shortcoming. Two more questions:
- How are you printing out the results (show us the actual code)?
- Which micro-controller and development environment are you using?
There may be some problem with the way you're printing or it may be a limitation of your environment.
Update 2:
Ajit, I think you're really going to have to give us some code to help you out. Not necessarily your real code (your concern about releasing the real code is understood), just some that demonstrates the problem.
Based on some of your comments, to wit:
Adriaan, the datatype for "result" is of float i.e. 32 bit representation (single). I have CAN as the system interface and hence I am multiplying the result by 1000 to send ti over the CAN. If it happens to be a negative number like -0.003 then I am expecting FF FD in the CAN message. I do not have a debugger.
I'm not sure I totally understand, but I'll give it a shot.
You have a 32-bit float, e.g., -0.003 and you multiply it by 1000 and put it in an integer (0xfffd is the 16-bit 2s complement representation of -3). So what happens when you run something like the following code:
int main(void) {
float w = -0.003;
int x = (int)(w * 1000);
int y = -3;
int z = -32768;
// show us you code here for printing x, y and z.
return 0;
}
The reason I want you to test an integer is that it may have nothing to do with floats at all. It may be that the float value is perfectly correct but there's some problem with the way you're printing it (the CAN method).
If "CAN" is some sort of serial interface, it may be there's a restriction on the bytes you're allowed to send across it. I can envisage a scenario where the high bytes are used as packet markers so that FF may actually end the message prematurely. That's why I also want you to test -32768 (0x8000).
It's hard to believe that STMicroelectronics would produce such a braindead runtime system that it couldn't handle negative floats. It seems much more likely to me that the information is being corrupted somewhere else (e.g., the "printing" process, whatever that may be).