# How to use numbers with 16 decimal places in arduino?

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 ;
int r =  4 ;
void setup() {

Serial.begin(9600);

}

void loop() {
x= 0.789632145698 ;
for (  int i = 2; i <=10 ; i++) {
x[i] = r * (x[i-1]*(1 - x[i-1]));
Serial.println(x[i]); }
``````

A float is 32 bits.

If you're expecting 16 places of decimals, you're out of luck.

When you print a float you can add a second parameter to say how many decimal places should be printed. The default is 2 decimal places

Which Arduino board are you using, because there is a limitation as to how many decimal places are held by a float in any case

This will show 15 digits after the decimal point... but only the first 6 or 7 significant digits are likely to be correct.

`Serial.println(x[i], 15); }`

i use arduino uno

A 32 bit float has 23 bits of mantissa.

Log base 10 of 2 to the 23rd power is 6.923, so that's how many significant places you can rely on.

(How the Hell do you do subscript and superscript in the new forum's mark-any-way-but-up?)

cus i use arduino uno i have precision 6 or 7

The result is as correct as it is going to be.

Log10(223) = 6.923

`Log<sub>10</sub>(2<sup>23</sup>) = 6.923`

3 Likes

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'.

Maybe this 'double precision on Arduino' library will help:
https://github.com/mmoller2k/Float64

1 Like

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.

thnx .. i use just the uno ... i can't use ARM in my projct

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 ;
int r =  4;

void setup()
{
Serial.begin(115200);
delay(200);
Serial.println();
Serial.println();

// x = f64(0x3fe944aa, 0xa234953c); // 0.789632145698 converted with https://gregstoll.com/~gregstoll/floattohex/
x = atof64("0.789632145698");

x.setDecs(16);  // Set number of decimal places to display
Serial.println(x);

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() {}``````

Results:

0.78963214569799
0.66445288071349
0.89182100010013
0.38590521552211
0.94792952061977
0.19743657822935
0.63382150322574
0.92836722109760
0.26600609555646

1 Like

thank you sir . It works well

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

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.