필터 지우기
필터 지우기

num2str, accuracy, precision, rounding error

조회 수: 28 (최근 30일)
Lu Maz
Lu Maz 2017년 1월 12일
답변: Jyotish Robin 2017년 1월 17일
Please have a look at this code ...
b = 1.00000001;
a = num2str(b,'%.100f')
a = 1.0000000099999999000000000000000000000000000000000000000000000000000000000000000000000000000000000000
a = num2str(b,'%.10f')
a = 1.0000000100
Why is the result different?
How to obtain the right result in the first case?

답변 (1개)

Jyotish Robin
Jyotish Robin 2017년 1월 17일
Hi Lu,
By default, MATLAB stores all numeric values as double-precision floating point. The fact is, almost all decimal numbers are NOT representable exactly in floating point arithmetic as a double. This is why this issue is completely about precision and the binary form that a double comprises.
If you are interested in converting a double or single input to the exact decimal string, you could refer the following file exchange submission: https://www.mathworks.com/matlabcentral/fileexchange/22239-num2strexact--exact-version-of-num2str-
Hope the above suggestions help!

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by