Issue with precision using days function

조회 수: 1 (최근 30일)
Rub Ron
Rub Ron 2020년 1월 19일
편집: Adam Danz 2020년 1월 19일
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

채택된 답변

Adam Danz
Adam Danz 2020년 1월 19일
편집: Adam Danz 2020년 1월 19일
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
Rub Ron
Rub Ron 2020년 1월 19일
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 2020년 1월 19일
편집: Adam Danz 2020년 1월 19일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by