Why does num2str() of 138.97 return 138.96999999999999886?

2 ビュー (過去 30 日間)
Mitsu
Mitsu 2022 年 8 月 10 日
編集済み: dpb 2022 年 8 月 10 日
I use num2str to set parameters into a Simulink model via set_param.
The parameters are sometimes irrational fractions (hence num2str(parameter,20)) makes sense, but some other times they are simpler values like 138.9. Therefore, to "work with all cases", I thought of using 20 digits all the time.
Then I noticed that num2str(parameter,20) does not add just zeros, but extra decimals. For example:
K>> A = 138.97;
K>> num2str(A,20)
ans =
'138.96999999999999886'
K>> num2str(A,'%20.20f')
ans =
'138.96999999999999886313'
Why does this happen?
I would expect that either setting the number of digits or the format spec would just fill the string with extra zeros.

採用された回答

dpb
dpb 2022 年 8 月 10 日
編集済み: dpb 2022 年 8 月 10 日
Wrong expectation for floating point numbers -- short story is if a fraction is not exactly representable by 1/2^n, it'll not be able to be stored exactly and so what you get is the closest representation that can be stored.
If you want only n digits of precision past the decimal point, set that in the format string.
I don't know anything about Simulink; can't you pass the actual variable into it, not a string? Then you get the closest binary representation without any rounding, whatever the value. All it's going to do with the string is convert it back to internal representation, anyway.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by