When I use cout in C++ I have floats printed with flexible precision:
std::cout << 0.123; // prints 0.123 std::cout << 0.1234 // prints 0.1234
But I can only print with fixed precision on Arduino:
Serial.print(0.123, 4); // prints 0.1230 Serial.print(0.1234, 4); // prints 0.1234 Serial.print(0.12345, 4); // prints 0.1234
Is it possible to print with flexible precision?
Edits:
Is it possible to call some function with float arg so that it printed this float with maximum possible precision without trailing zeros (without setting the precision in advance):
on an Arduino I use sprintf to format the data into a text array (with field width, precision, etc as required) then use Serial.print() to print the array
The above code says that 4-digit (arg2) will be printed after the decimal point of the float number of arg1.
Because you have only 3-digit after the decimal point of arg1 (0.123), the Serial.print() method appends a 0 (zero) at the end to make it 4-digit. Similar interpretation applies for the following code:
Off topic: how do you do strike-through? Oh--I see from the quote in the edit box that you do strike-through by bracketing the text with double-tildes.
Yeah, a missing semicolon was minor. But the important part of the OP's question was the ragged precision of dropping the trailing zeros.
I would like to print out float without setting precision in advance so that the float printed with maximum precision. Something like Serial.print(0.12345); // prints 0.12345; Serial.print(0.1); // prints 0.1;
You need to understand the functioning of the Serial.print(arg1, arg2) method.
1.Serial.print(0.12345) will show 0.12; because, the 2nd argument is silent which is 2 by default and indicates the number of digits to be printed after the decimal point on the condition that the 1st argument is a floating point number.
2. To see 0.12345 on the Serial Monitor, you must execute the following code:
Serial.print(0.12345, 5);
3. The execution of Serial.print(0.1) will show what? Is it 0.1 or 0.10?
to print to the maximum float precision you need to get the value of FLT_DIG in float.h and print that number of digits
e.g. FLT_DIG: Number of decimal digits that can be rounded into a floating-point type and back again to the same decimal digits, without loss in precision.
the <float.h> header file consists of platform-dependent and implementation specific floating point values
program for PC using GNU C
#include <float.h>
int main(void) {
printf("FLT_DIG %d\n",FLT_DIG);
printf("DBL_DIG %d\n",DBL_DIG);
}
when run gives
FLT_DIG 6
DBL_DIG 15
floats have a precision of 6 digits (4 bytes) and doubles 15 digits (8 bytes)
however, precision does depend of the source of your data - if you are acquiring data from a sensor with three digits of precision it is no use printing six digits
But you are giving 0 a status it does not have. Just because the last digits are 0 does not mean the floating point number is any more, or less, precise.
You are chasing a cosmetic desire, you wanna see it look a certain way. This is understandable.
But floats have 6-7 digits of significance, period. Some of them may be zeroes, some of those may come near the end…
that is how I generally print floats and doubles - consider PI defined to 50 digits
#include <float.h>
int main(void) {
double pi=3.14159265358979323846264338327950288419716939937510;
float x=pi;
printf(" float PI %25.20f\n",x);
printf("double PI %25.20lf\n",pi);
}
on a pc running GNU C gives
float PI 3.14159274101257320000
double PI 3.14159265358979310000
after 6 or 7 digits the float output is meaningless similar double after 15 or 16 digits
I have seen engineers read data from experiments, e.g. from analogue meters or 4 digit digital meters, and perform double calculations and print results with 15 digits
Just two questions out of curiosity: 1. What is the accuracy and precision of float-type data in terms of "number of digits". 2. What is the accuracy and precision of double-type data in terms of "number of digits".