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