precision problem when creating a time vector
1 view (last 30 days)
Benjamin Brammer on 29 Nov 2022
Ia have come across a very strange phenomena:
When you create the following vectors (time vectors) there seems to be a precision problem, allthough I don't understand why:
t1 = 0:.00001:0.0010; %time vector for a pulse 10µs step
t2 = 0.00101:.00001:0.015; % time vector for a discharge 10µs step
t3 = [t1, t2]; %resulting time vector for plot
Normally, I would expect, that if you try to find the index of t3 at the time of 4.5ms (0.0045) this should be no problem. But if you run the code
find(t3 == 0.0045)
you will get no return, as for some reason matlab is not hitting the exact 4.5ms in the vector.
Furthermore if you try to find the deviation by the following code line:
t3(451) - 0.0045 % 451 is the index for 4.5ms
you can see that there is a very small deviation from the exact value. But this should normally not be the case. At least for my understanding. Or where is the mistake here?
Stephen23 on 29 Nov 2022
Moved: Cris LaPierre on 29 Nov 2022
"But this should normally not be the case."
What you show is the completely normal and expected behavior of binary floating point numbers:
This is worth reading as well: