Relationship Block Not Working As Expected when using single data type

1 回表示 (過去 30 日間)
LIAO Jimmy
LIAO Jimmy 2022 年 9 月 9 日
回答済み: Mark McBroom 2022 年 9 月 11 日
I found out that single(1)-single(0.9) is larger than single(0.1) in simulink. Using the display block, I can see that this is due to how the numbers are represented. However, I expected that, intuitively, 1-0.9 should be equal to 0.1, I am wondering how can I fix this issue. Meanwhile, if I use the generated code, will the code perform differently(especially on diferent hardwares). Please see the attached picture for details.

回答 (1 件)

Mark McBroom
Mark McBroom 2022 年 9 月 11 日
There is nothing to "fix". You should design your algorithm so that you are not comparing floating point variables to floating point constants. If this is not possible, you could try using fixed point or integer math. This answer gives more detail to floating point numbers.

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by