I read that for the Arduino, there is no differene between float and double precision. Does this mean that double and float occupy the same number of bytes? If not, and there is no other significant difference between them, it seems the advice should always be to use flot. Is that a reasonable conclusion?
Secondly, I am having problems performing calculations on some big numbers (> 10^^6) where there are significant changes in the 3d decimal place and onwards. The values to the left of the dp remain unchanged; only the values to the right change.
Even when I perform two separate calculations (integer part and frction part) and combine the result, the answer is no different from performing the operation on the original number. And this is a bit of a stumbling block.
To illustrate, the following sketch illustrates the problem:
#include <math.h>
double big = 10;
double small = 0.1;
void setup () {
// fire up the serial interface for the monitor
Serial.begin(9600); // slower than we need but sound enough
float t;
double u;
Serial.println("Double Precision");
Serial.println();
t=1234.1234;
u=1234.1234;
Serial.print("This is a big float number: 1234.1234 : "); Serial.println(t,9);
Serial.print("This is a big double number: 1234.1234 : "); Serial.println(u,9);
t=12345.12345;
u=12345.12345;
Serial.print("This is a big float number: 12345.12345 : "); Serial.println(t,9);
Serial.print("This is a big double number: 12345.12345 : "); Serial.println(u,9);
t=123456.123456;
u=123456.123456;
Serial.print("This is a big float number: 123456.123456 : "); Serial.println(t,9);
Serial.print("This is a big double number: 123456.123456 : "); Serial.println(u,9);
t=1234567.1234567;
u=1234567.1234567;
Serial.print("This is a big float number: 1234567.1234567 : "); Serial.println(t,9);
Serial.print("This is a big double number: 1234567.1234567 : "); Serial.println(u,9);
t=12345678.12345678;
u=12345678.12345678;
Serial.print("This is a big float number: 12345678.12345678 : "); Serial.println(t,9);
Serial.print("This is a big double number: 12345678.12345678 : "); Serial.println(u,9);
t=123456789.123456789;
u=123456789.123456789;
Serial.print("This is a big float number: 123456789.123456789 : "); Serial.println(t,9);
Serial.print("This is a big double number: 123456789.123456789 : "); Serial.println(u,9);
Serial.println();
}
void loop () {
double j;
// do some maths
for (int i = 1; i < 5; i++) {
leadingZero(i); Serial.print(". ");
//Serial.print("big: "); Serial.print(big,6); Serial.print("\tsmall: "); Serial.println(small,6);
big = big*(i);
small = small/(sqrt(i));
j = combine(big, small);
Serial.print("big: "); Serial.print(big,2); Serial.print("\t\tsmall: "); Serial.print(small,8); Serial.print("\tsum: "); Serial.println(j,8);
}
for (int i = 5; i < 11; i++) {
leadingZero(i); Serial.print(". ");
//Serial.print("big: "); Serial.print(big,6); Serial.print("\tsmall: "); Serial.println(small,6);
big = big*(i);
small = small/(sqrt(i));
j = combine(big, small);
Serial.print("big: "); Serial.print(big,2); Serial.print("\tsmall: "); Serial.print(small,8); Serial.print("\tsum: "); Serial.println(j,8);
}
while (1);
}
double combine (double s, double b) {
double c;
//Serial.print("small: "); Serial.print(s, 6); Serial.print (" ");
//Serial.print("BIG : "); Serial.print(b, 6); Serial.print (" ");
c = s + b;
//Serial.print("Sum : "); Serial.print(c, 6); Serial.print (" ");
return c;
}
void leadingZero(int digits) {
if(digits < 10) Serial.print('0');
Serial.print(digits);
}
Here is the output:
Double Precision
This is a big float number : 1234.1234 : 1234.123413085
This is a big double number : 1234.1234 : 1234.123413085
This is a big float number : 12345.12345 : 12345.123046875
This is a big double number : 12345.12345 : 12345.123046875
This is a big float number : 123456.123456 : 123456.125000000
This is a big double number : 123456.123456 : 123456.125000000
This is a big float number : 1234567.1234567 : 1234567.125000000
This is a big double number : 1234567.1234567 : 1234567.125000000
This is a big float number : 12345678.12345678 : 12345678.000000000
This is a big double number : 12345678.12345678 : 12345678.000000000
This is a big float number : 123456789.123456789 : 123456792.000000000
This is a big double number : 123456789.123456789 : 123456792.000000000
- big: 10.00 small: 0.10000001 sum: 10.10000038
- big: 20.00 small: 0.07071068 sum: 20.07071113
- big: 60.00 small: 0.04082483 sum: 60.04082489
- big: 240.00 small: 0.02041242 sum: 240.02041625
- big: 1200.00 small: 0.00912871 sum: 1200.00915527
- big: 7200.00 small: 0.00372678 sum: 7200.00390625
- big: 50400.00 small: 0.00140859 sum: 50400.00000000
- big: 403200.00 small: 0.00049801 sum: 403200.00000000
- big: 3628800.00 small: 0.00016600 sum: 3628800.00000000
- big: 36288000.00 small: 0.00005250 sum: 36288000.00000000
The first part of the output simply holds an increasingly big number with an increasingly higher precision in both float and double variables and prints the result. Even the number "12345.12345" has problems in the 4th dp.
The second part of the output illustrates the huge loss of significance by iteration 7.
Is there any way of increasing the precision? An alternative library?
Ric