Arduino vs Math precision

Good morning
Is my 1st post on this forum.
I'm portuguese and I'm giving the first steps using Arduino Mega and also on C/C++ language after several years using VB.

My project have some math... and I need some precision...
It happens that I don't have it...

Eg:
1.471 gives me 1.50... using a 'float' var.

Is possible to have more precision?

I don't need to have it on display... but as internal, because have influence on other kind of calculations...

I'll appreciate your help
Thanks on advance
Best regards

Pedro Ferrer

This is far too vague a question to answer without seeing some specific code.

1.471 gives me 1.50... using a 'float' var.

Where did the 1.471 come from? What type is the variable? Where does the 1.50 appear? Using what command to make it appear?

If you are using Serial.print() to print the value of a float variable, the default is to round the value to two decimal places (for display purposes only). Internally, the value is stored as accurately as possible, given the limitations of the Arduino.

Good morning Paul

First of all, let me thanks.

The value is this one...Julian Day.
2455398.427

char buffer[20];
char *result;
float dJulianDate;

result = dtostrf(dJulianDate, 2, 5, buffer);
Serial.println(result);
lcd.printIn(result)

and on 'Screen IDE' and on 'LCD 4 bits', I have 2455398.50

Perhaps I miss wrong.

Thanks on advance
Best regards
Pedro Ferrer

When printing float variables using Serial.print, which derives from the Print class, the default behavior is to print float values using two decimal places. This is what you are seeing.

You can specify an optional argument to the Serial.print function, when the first argument is a float, to specify the number of decimal places to print.

Try using

Serial.print("Julian date: ");
Serial.print(result, 4);
Serial.println();

This should print the value with 4 decimal places.

Good morning Paul

Thanks once again by the answer.
I'll try later at night, at home.

So, you are telling me that I'm visualizing at wrong way, and Arduino have 4 bytes precision, so I shouldn't have problems with math.

Best regards
Pedro Ferrer

A float variable is 4 bytes. The way the bits are used to store the number affects the precision of the value, but it is more than 4 decimal places.

The "loss-of-precision" that you are seeing is entirely a function of how the number is being formatted for display. You have control, by specifying the number of digits after the decimal point, of how that formatting occurs.

The value is this one...Julian Day. 2455398.427

When using float variables, precision is limited to about 7 decimal digits which is less than you have in your Julian Day example. As such this precision issue you observed is to be expected.

One possible workaround is to store days and time in separate variables. You can use one variable of type "long" for days and another "long" variable for seconds (rather than using days and fractions of a day combined in a float).

Another approach is to offset the Julian day number to an epoch closer to real time (e.g. January first 2010). Julian day numbers would then be smaller and so possibly not subject to the precision limitation of floats. This will only work however for a limited range of dates (you need to consider min and max date for your application).

The "loss-of-precision" that you are seeing is entirely a function of how the number is being formatted for display.

No this is not true. Precision is limited to a total of 7+ digits irrespective of decimal position.

Details on the IEE-754 float number format can be found here:

Good afternoon

Thanks Paul and Ben by your comments.
They are very helpful.

I remember now that I have a value 'decimal day' that is 12.25 and the serial shows me 12.25 without round to 12.30.

Thanks once again
Best regards
Pedro Ferrer