datetime values appear to be equal but return not equal

1 回表示 (過去 30 日間)
Dan Klisiewicz
Dan Klisiewicz 2021 年 4 月 1 日
コメント済み: Steven Lord 2021 年 4 月 1 日
I have two datetime variables that appear to be the same (even using format long), but matlab says they are not equal:
Any idea what's going on here? I discovered this because the setxor function told me these values were mutually exclusive.
K>> T1(1)
ans =
datetime
24-Aug-2020 18:00:01.730
K>> T2(2)
ans =
datetime
24-Aug-2020 18:00:01.730
K>> datenum(T1(1))
ans =
7.380277500200346e+05
K>> datenum(T2(2))
ans =
7.380277500200346e+05
K>> isequal(T2(2),T1(1))
ans =
logical
0

採用された回答

John D'Errico
John D'Errico 2021 年 4 月 1 日
編集済み: John D'Errico 2021 年 4 月 1 日
format long does NOT tell you every single digit in the number!!!!
format long does NOT tell you every single digit in the number!!!!
format long does NOT tell you every single digit in the number!!!!
There, I've said it three times, so it must be true.
format long
X = pi
X =
3.141592653589793
Y = X - 3.e-16
Y =
3.141592653589793
Do they appear to be identical? Yes. Down to the last digit, I think. At least, format long tells me so. But are they?
X == Y
ans = logical
0
Numbers are stored with a 52 bit mantissa in MATLAB. However, 52 binary bits is how many decimal digits? There is not an integer relationship between binary bits and decimal digits. So while format long gives you DECIMAL digits, you don't see the exact bits.

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 4 月 1 日
What does this show you?
seconds(T2(2)-T1(1))
That will likely be small enough to make no difference in how those datetime values are displayed but large enough to make them not return true for the equals calculation.
  2 件のコメント
Dan Klisiewicz
Dan Klisiewicz 2021 年 4 月 1 日
K>> T1(1)-T2(2)
ans =
duration
00:00:00
Steven Lord
Steven Lord 2021 年 4 月 1 日
You forgot the seconds call.
T1 = datetime('now')
T1 = datetime
01-Apr-2021 18:10:05
T2 = T1 + seconds(1e-4)
T2 = datetime
01-Apr-2021 18:10:05
T2-T1
ans = duration
00:00:00
seconds(T2-T1)
ans = 1.0000e-04

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by