フィルターのクリア

Incosistency Precision Value (decimal double) When using strcmpi/contains

1 回表示 (過去 30 日間)
Arif
Arif 2024 年 6 月 11 日
コメント済み: Arif 2024 年 6 月 12 日
Hi guys, im facing trouble when I want to compare string using 'strcmpi' or 'contains'. The problem is when I convert string to double, Its precision value sometimes are not concictency like picture as I shown below :
how do I fix this ?
  2 件のコメント
Stephen23
Stephen23 2024 年 6 月 11 日
"how do I fix this ?"
Do not store or process numeric data as text.
Arif
Arif 2024 年 6 月 12 日
but it is needed because of some my API's requirement

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

回答 (2 件)

Steven Lord
Steven Lord 2024 年 6 月 11 日
If you're trying to determine if one number is contained inside another array of numbers, converting them to strings then using contains is an indirect way to do so. I'd avoid comparing the strings and compare the numeric arrays instead using ismembertol. If you have integer values or know because of some property of the numbers that the exact value, down to the last bit, is contained in the array, you could use ismember.
This may help you avoid the repeated conversions back and forth between double and string.
  2 件のコメント
Walter Roberson
Walter Roberson 2024 年 6 月 11 日
For floating point numbers, it is best to use ismembertol
Steven Lord
Steven Lord 2024 年 6 月 11 日
I agree. That's why I suggested it in the second sentence. I probably should have linked to its documentation page to make it more visible.

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


Walter Roberson
Walter Roberson 2024 年 6 月 11 日
By default, string() of a double precision number gives a result to 5 digits of precision.
a = 0.367265
a = 0.3673
string(a)
ans = "0.36727"
To get more digits, you need to use compose()
compose("%.6g", a)
ans = "0.367265"
This relies upon you knowing the exact number of digits ahead of time.
You also have the problem that your input is comma as the decimal, but string() and compose() are going to use period as the decimal. You are already doing proper conversion -- you should just store all of the converted values instead of just max() of the converted values, and then work with the converted values.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by