9

There was a similar thread before but it didn't solve my problem. I had an issue with sending GPS data, which is float, and I couldn't receive the float with the same amount of significant figures as I was sending. I always received a number with 2 digits after a decimal point. So I started debugging and wrote this simple code just to check if floats work as intended:

#include <SoftwareSerial.h>
void setup() {
Serial.begin(9600);
}
void loop() {
float number1;
number1=1.45436;
Serial.print(number1);
delay(200);
}

And the serial monitor showed 1.45 as an output. How can increase the precision of the float I am saving in memory?

Chris Stratton
  • 5,390
  • 19
  • 40

3 Answers3

17

By default, the Serial print function shows only 2 decimals, however I'm sure the float has the full (unrounded) value, even if it does not show it.

You can read about it in the official documentation here

With the following code you can use more decimals (fragment from the official documentation):

-Serial.println(1.23456, 0) gives "1" 
-Serial.println(1.23456, 2) gives "1.23" 
-Serial.println(1.23456, 4) gives "1.2346" 

Also, probably the next link (PrintFloat) will help:

PrintFloat

It works roughly by iterating through the digits and printing them.

Michel Keijzers
  • 12,954
  • 7
  • 40
  • 56
  • 2
    Arduino floats are not particularly accurate. They have 6-7 significant digits, counting those on both sides of the decimal point. For higher accuracy a double may be used. – Chris Stratton Jul 24 '17 at 15:16
  • Also note that the Arduino is not really good (i.e. fast) with floats/doubles. – Michel Keijzers Jul 24 '17 at 15:19
  • 3
    @ChrisStratton Regarding the higher accuracy comment: On Arduino, a double is just another name for float. See: https://www.arduino.cc/reference/en/language/variables/data-types/float/ "Unlike other platforms, where you can get more precision by using a double (e.g. up to 15 digits), on the Arduino, double is the same size as float." – statueuphemism Jan 14 '20 at 17:25
  • 1
    Also String() will do the same thing. It takes a second argument so if the number is a float or a double: String(floatVariable, 3) gives you 1.234. – Andrew Jan 24 '23 at 17:06
9

By default, Serial.print() prints floats with two decimal digits.

float num = 7.875;
Serial.println(num, 4);

will print num with 4 decimal digits, thus: 7.8750.

The precision of float is not decreased in the way you think it decreases. See this answer for a better explanation.

Keeley Hoek
  • 103
  • 3
Fauzan
  • 375
  • 1
  • 8
1

Multiply your float by 10000 (or whatever number you wish), send it as an integer, and convert the received integer back to float.

Alternatively change the library or write your own.

dannyf
  • 2,770
  • 10
  • 13
  • Do you mean change serial library? Besides that, that's what I am doing(multiplying by something etc.). The problem is, it seems arduino does not save the numbers with the precision I give them to it. If what you suggested was to work, first arduino should be able to display the float correctly on the serial monitor. Am I wrong? – Anthropomorphous Dodecahedron Jul 24 '17 at 11:36