Hi all,
I've got the book "Practical C Programming" by Steve Qualline and it has a small demo program to test floating point accuracy. I copied the code and modified it for the Arduino (i.e. replacing "printf" with "Serial.print", etc) and got this result:
8 digits accuracy in calculations
8 digits accuracy in storage
Running the same code compiled with GCC in Linux yields the same results (8 and 8).
So my question is, what does this MEAN?
I know what "accuracy is storage" means (8 bytes of storage for a floating point number, right?), but what about "accuracy in calculations"? Does it mean 8 decimal places?
FYI, this is the code I ran on the Arduino:
// Floating point accuracy test - from the cow book page 271
void setup (void)
{
Serial.begin(115200);
char buffer[64];
const char *mask1 = PSTR("%2d digits accuracy in calculations\r\n");
const char *mask2 = PSTR("%2d digits accuracy in storage\r\n");
int counter;
float number1, number2, result;
number1 = 1.0;
number2 = 1.0;
counter = 0;
while (number1 + number2 != number1) {
counter++;
number2 /= 10.0;
}
sprintf_P(buffer, mask1, counter);
Serial.print(buffer);
number2 = 1.0;
counter = 0;
while (1) {
result = number1 + number2;
if (result == number1) {
break;
}
counter++;
number2 /= 10.0;
}
sprintf_P(buffer, mask2, counter);
Serial.print(buffer);
}
void loop (void) { ; }
Here's the same thing for GCC (the code I tested the Arduino version against):
// Floating point accuracy test - from the cow book page 271
#include <stdio.h>
int main(void)
{
char buffer[64];
const char *mask1 = "%2d digits accuracy in calculations\r\n";
const char *mask2 = "%2d digits accuracy in storage\r\n";
int counter;
float number1, number2, result;
number1 = 1.0;
number2 = 1.0;
counter = 0;
while(number1 + number2 != number1) {
counter++;
number2 /= 10.0;
}
sprintf(buffer, mask1, counter);
fprintf(stdout, "%s", buffer);
number2 = 1.0;
counter = 0;
while(1) {
result = number1 + number2;
if(result == number1) {
break;
}
counter++;
number2 /= 10.0;
}
sprintf(buffer, mask2, counter);
fprintf(stdout, "%s", buffer);
}
If anyone can explain to me what this all means, I'd appreciate it.
Thanks!
-- Roger