# floating point bug

(1)

float f1;
Once every second, the following is executed
f1 = f1 + (1/3600);

Once f1 reaches to 4096.0, f1 stops incrementing.

How to work around this. I need is a variable that can hold a bigger number forever. So I choose 'float'

(2)

unsigned long L1;
float f2;
L1 = 4000000000; // (four billion)
f2 = L1;

When I print f2, it shows -4000000000 (Negative four billion).

Any idea would be appreciated.

BTW, I am using Dynamic C9.21 compiler with RCM3100.
asked Oct 23, 2014

Hello,

I suggest that, please always use the latest version of Dynamic C 9.Currently digi has DC9.62. You can download it from the below link

http://www.digi.com/support/productdetail?pid=5053&type=documentation

At the above link you can find the DC download link under "Development Software and Sample Code "
Also install patches.

With the latest version please check the above two cases.

In the case 1, please explain the issue more clear.
May I know,How are you incrementing the f1?

please provide a piece of code that creates the problem.

in the case 2, please provide piece of code that creates the problem. So that I can suggest you where would be the problem.

Thank you.
answered Oct 24, 2014 by Veteran of the Digi Community (655 points)
Hi cpigilam,

(1)
I am incrementing f1 every second adding fraction of "second in hours" to the existing value and store in the same register used for tracking the running time of an equipment. Do remember that I want to store this value forever and must be preserved between board or power reset.

void main()
{
float f1;
int i;
f1 = 4096;
// Assume that I am simulating a one second through each iteration of the following loop
for(i = 0; i < 4096; i++)
{
f1 += (1/3600); // f1 does not change from 4096
printf("\ni: %d f1: %.06f", i, f1);
}

(2)
To overcome the above, I thought that storing my number in an unsigned long integer and storing in float-point register might help...but it did not:

void main()
{
unsigned long L1;
int temp;
float f1;

L1 = 4000000000; // four billion
f1 = (float)L1;

// Now print both
// You see that L1: 4000000000 while f1: -4000000000.000000
printf("\nL1: %lu f1:%.6f", L1, f1);
}

Why does the f1 show up as negative?
You should have shown the code you are using to print out the float. From a sample program demonstrating how to print various variables:

printf("\nPRINTING FLOATS\n");
//print float
printf("%f\n",some_float);
//print float with one character and no decimals
printf("%1.0f\n",some_float);
//print float with up to 2 characters and 0 past the decimal point
printf("%2.0f\n",some_float);
//print float with up to 3 characters and 0 past the decimal point
printf("%3.0f\n",some_float);
//print float with up to 3 characters and 1 past the decimal point
printf("%3.1f\n",some_float);
//print float with up to 4 characters and 2 past the decimal point
printf("%4.2f\n",some_float);
//print float with up to 5 characters and 3 past the decimal point
printf("%5.3f\n",some_float);
//print float with up to 6 characters and 4 past the decimal point
printf("%6.4f\n",some_float);
//print float with up to 7 characters and 5 past the decimal point
printf("%7.5f\n",some_float);
//print float with up to 8 characters and 6 past the decimal point
printf("%8.6f\n",some_float);
//print float with up to 9 characters and 7 past the decimal point
//NOTE: The value has been rounded and we have hit the limit of the
//float's precision.
printf("%9.7f\n",some_float);
//ERROR: print float with up to 10 characters and 8 past the decimal point
//This will print random data at the end of the float because it exceeds
//the float's possible precision.
printf("ERROR: %10.8f\n",some_float);
//ERROR: print float with up to 3 characters and 0 past the decimal point
printf("ERROR: %11.9f\n",some_float);
//ERROR: print float
printf("ERROR: %25.23f\n",some_float);
answered Oct 25, 2014 by Veteran of the Digi Community (589 points)