Please I need your help. I have to visualise numbers of vector x with 16 values after comma. But with this code, I just have 2 numbers.
x(2)=0.66.....But I want to see all x(2)=0.664452880713490 (was calculated by matlab)
float x[10] ;
int r = 4 ;
void setup() {
Serial.begin(9600);
}
void loop() {
x[1]= 0.789632145698 ;
for ( int i = 2; i <=10 ; i++) {
x[i] = r * (x[i-1]*(1 - x[i-1]));
Serial.println(x[i]); }
You need IEEE 'binary64' (aka 'double') to get 53 bits of precision (15.95 decimal digits). Unfortunately, the avr-gcc runtime library doesn't support 'double' and uses 'float'.
If you use an Arduino with an ARM processor, type double is supported. The Teensy 3.x and 4.x series is much faster and has more memory than the Uno, too.
I tried the Float64 library for you. It has a bug in "toString()" and was hanging in Serial.print(x[i]); on the second iteration. Commenting out a 'printf()' call in the library's 'toString()' function eliminated the hang.
#include <Float64.h>
f64 x[10] ;
int r = 4;
void setup()
{
Serial.begin(115200);
delay(200);
Serial.println();
Serial.println();
// x[1] = f64(0x3fe944aa, 0xa234953c); // 0.789632145698 converted with https://gregstoll.com/~gregstoll/floattohex/
x[1] = atof64("0.789632145698");
x[1].setDecs(16); // Set number of decimal places to display
Serial.println(x[1]);
for (int i = 2; i < 10 ; i++)
{
// Serial.println("Calculating...");
// Serial.flush();
x[i] = f64(r) * (x[i - 1] * (f64(1) - x[i - 1]));
// Serial.println("Calculated.");
// Serial.flush();
x[i].setDecs(16);
Serial.println(x[i]);
// Serial.println("Printed.");
// Serial.flush();
}
}
void loop() {}
To help anyone searching for help on the same subject can I suggest that you revise the title of this topic. It is not help with 16 bits you need but rather 16 decimal places