dlmread adds low precision digits
이전 댓글 표시
Hello all,
I'm using dlmread to import datas and when the datas contains integers and float numbers, dlmread sometimes makes an error on the last digit of the output precision.
I'm not sure I am clear, so here is an example :
If my text file ('test.rpt') contains only the row :
1 10.8000 31. 10.5001
the command
A = dlmread('test.rpt')
gives back :
A =
1.000000000000000 10.800000000000001 31.000000000000000 10.500100000000000
I don't understand why this 10e-15 is added on the second number. Ok, it's not a big error, but it prevent me from easly comparing two values...
Does anyone know how to prevent this behaviour ?
댓글 수: 6
dpb
2019년 8월 27일
In short, you can't. It's fact of life with floating point numbers. See <Why-is-0-3-0-2-0-1-not-equal-to-zero?> for more explanation.
For comparison of floating point values with rounding error, see ismembertol
Arnaud WILHELM
2019년 8월 27일
Adam Danz
2019년 8월 27일
I know 0 about rpt files so I can't recommend alternative methods to read in the data. You're currently reading in the data usling dlmread (which has been replaced by readmatrix() starting in r2019a).
Some methods allow you to read in data as text in which case 10.8000 would be read as a string (or char array) '10.8000'. Then you could convert that to numeric by using str2double() or num2str() (the prior is better).
dpb
2019년 8월 27일
> x=10.8
x =
10.8000
>> fprintf('%.15f\n',x)
10.800000000000001
>>
so it doesn't matter how you enter it, 10.8 is not exactly representable.
"I don't remember seeing this when using fscanf and specifing a number format."
Probably because you just didn't notice until you did try to do an exact comparison on floating point values.
>> fprintf('%.15f\n',round(x,1))
10.800000000000001
>>
It's doing the best it can within the constraints of IEEE floating point representation.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Text Files에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!