Numerical Issues in Simulink when converting from double to uint16

조회 수: 22 (최근 30일)
Christopher
Christopher 2025년 8월 21일 8:59
댓글: Harald 2025년 8월 21일 13:39
In Simulink, I am converting the result of a simple calculation from double to uint16 (see attached picture and model).
Depending on how I do the calculation, the uint16 result differs.
Is this normal behaviour or am I missing something here?

채택된 답변

Harald
Harald 2025년 8월 21일 9:41
The challenge is that the result of the computation is a bit smaller than 2047, but only such a little bit that even the truncated value in the long format looks as if it was 2047. This boils down to:
format long
x1 = 204.7 / 0.1
x1 =
2.047000000000000e+03
x2 = 204.7 * 10
x2 =
2047
fprintf("x1 = %.16f\n", x1)
x1 = 2046.9999999999997726
fprintf("x2 = %.16f\n", x2)
x2 = 2047.0000000000000000
In other words, the calculation is correct up to a minor numerical error. The mean thing is that the discrepancy is so small that even with format long the only indication that x1 is not exactly 2047 is the large number of zeros.
I would not blame MATLAB or Simulink for this, but rather that computers use the binary system, and 0.1 does not have a finite representation in the binary system. It's a bit like 1/3 in the decimal system: the more digits you add, the more accurate it gets, but it never becomes exact.
Hope this helps. :)
  댓글 수: 2
Christopher
Christopher 2025년 8월 21일 10:42
Thank you for the detailed explanation.
This was the key point I did not know:
"0.1 does not have a finite representation in the binary system"
This calculation was part of a compiled software for an embedded hardware and it also happened on the target hardware and that explains why.
Harald
Harald 2025년 8월 21일 13:39
In that case, thank you very much for your efforts of coming up with the simplified reproduction example! I imagine it was not too easy...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by