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

C51 - Compiler Problem?

#include <REGX52.h>

unsigned char i;
float x;

void main()
{
x=0;
for(i=1;i<=30;i++)
{x+=1.3;}
}

This should ideally produce an output of x=39.

But in debugger the value is shown as x=38.999999. What could be the issue?

Parents
  • The big issue here is to assume that floating point allows exact results.

    Try to spend some time browsing the net about how floating point works and how the majority of values will be approximated in the float representation. A floating point number can only manage the large range of magnitude by approximating values.

    You can not do 30*1.3 and think that is the same as 1.3+1.3+1.3+... since the errors will accumulate.

Reply
  • The big issue here is to assume that floating point allows exact results.

    Try to spend some time browsing the net about how floating point works and how the majority of values will be approximated in the float representation. A floating point number can only manage the large range of magnitude by approximating values.

    You can not do 30*1.3 and think that is the same as 1.3+1.3+1.3+... since the errors will accumulate.

Children
No data