Issue with precision using days function

I have a varriable x (this x was generated by reading an excel file). Using variable explorer I can see: x=0.465277777777778.
If I do the following:
y = days(0.465277777777778);
y.Format = 'hh:mm:ss'
I get:
11:10:00
Which is correct, as it is in the excel file. However, when instead I use the variable, like this
y=days(x) =
y.Format 'hh:mm:ss'
I get:
11:09:59
As you see there is a second of difference. How can I fix this so that I get the correct value (11:10:00) using the variable x?
Obs: when I get do x - 0.465277777777778, I get -2.775557561562891e-16. So I think it has to do with precision.
I appreciate any hint. Thanks

 Réponse acceptée

Adam Danz
Adam Danz le 19 Jan 2020
Modifié(e) : Adam Danz le 19 Jan 2020
0.465277777777778<---this 8 is rounded up.
Look at how the following 2 values are converted
x=0.465277777777778 % = 11:10:00
x=0.4652777777777777 % = 11:09:59
The difference between Matlab and Excel can be explained either by 1) a different level of precision between the two programs or 2) due to round-off error associated with floating point decimals when the value was imported into matlab.
If you want to round to the nearest second,
x=0.4652777777777777;
y = days(round(x,5));
y.Format = 'hh:mm:ss' % = 11:10:00

3 commentaires

Stephen23
Stephen23 le 19 Jan 2020
Modifié(e) : Stephen23 le 19 Jan 2020
A time of 11:10:00 is stored in .xlsx as this exact string:
Compare those values:
0.46527777777777773 % string in XLSX
0.465277777777778 % displayed by MATLAB
0.46527777777777773 % string in XLSX
0.465277777777778 % displayed by MATLAB
sprintf('%0.55f',x) % long format
0.4652777777777777346024379312439123168587684631347656250
and I thought the displayed value in "variable explorer" was the "true" value.
Adam Danz
Adam Danz le 19 Jan 2020
Modifié(e) : Adam Danz le 19 Jan 2020
If the excel represents 11:10:00 precisely as 0.46527777777777773, why does it come out out to 11:09:59.999 in Matlab?
x = 0.46527777777777773
y = days(x);
y.Format = 'hh:mm:ss.SSS' % = 11:09:59.999
In the floating point decimal below, the 15th digit is underlined and in bold.
0.46527777777777773460
When they are converted to durations in Matlab,
x1 = 0.465277777777778; % 15 digits, rounded
x2 = 0.4652777777777777; % 16 digits, rounded
y1 = days(x1); y1.Format = 'hh:mm:ss.SSS' % = 11:10:00.000
y2 = days(x2); y2.Format = 'hh:mm:ss.SSS' % = 11:09:59.999
*I'm no expert in floating decimal point representation, just an enthusiast. So perhaps someone more familiar with this topic will confirm or correct my explanation.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by