Why not getting same constant value after converting to single from double in MATLAB 2016b simulink

조회 수: 4 (최근 30일)
I have a implemented below logic in simulink
double a = 0.01;
b = single(a);
Expected value is 0.01 but getting 0.0099999123412 (in display block with format long).
What to do to get exact value?
Configurations settings:
fixed step, auto solver
step size : 0.01
Please let me know how get exact value.

답변 (2개)

John D'Errico
John D'Errico 2023년 7월 24일
Welcome to the wonderful wacky world of floating point arithmatic.
I think you don't understand floating point arithmetic. 0.1 is NOT representable in floating point arithmetic. You CANNOT get an exact value, as a single OR a double. It does not exist.
Perhaps you think it does. But can you represent 1/3 or 2/3 as exact numbers as a decimal in a FINITE number of decimal digits? If you cannot do that, you must accept that other numbers will also have problems. Remember that singles and doubles are stored in a BINARY form, NOT as decimals. This is a fact of using a computer, in literally any programming language. They use an IEEE standard for number storage.
In binary, 0.1 would be the infinite bit sequence
0.00011001100110011001100110011...
where the ones represent negative powers of 2.
So what you see is probably a 1 bit error down at the level of the least significant bit for a single.
  댓글 수: 1
siva prasad
siva prasad 2023년 7월 24일
Hello,
Thanks for the reply.
I have checked same logic in MATLAB 2022b also but there i am getting same value after conversion with single.
How i am getting in 2022b and not in 2016b?

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


Andy Bartlett
Andy Bartlett 2023년 7월 24일
편집: Andy Bartlett 2023년 7월 24일
The facts that John D'Errico mentioned are very good to keep in mind as you are analyzing/debugging your model.
The rounding of an ideal decimal value in text to a floating-point value can produce half an eps of error.
Likewise displaying a floating point value in decimal text using long format can introduce a few eps of inaccuracy.
However, for your model, the difference between what you observed and what you expected is 94 eps.
format long e
val1 = single(0.01)
val1 = single
9.9999998e-03
val2 = single(0.0099999123412)
val2 = single
9.9999122e-03
valDiff = val1 - val2
valDiff = single
8.7544322e-08
epsSingle0p1 = eps(val1)
epsSingle0p1 = single
9.3132257e-10
differenceInBits = valDiff / epsSingle0p1
differenceInBits = single
94
94 eps of error suggests that math operations in your model have accumulated round off errors, such as through addition and subtraction, or amplified those round off errors, such as through multiplication.
I think you'll have to analyze your model more closely to see where the original round off errors are introduced and how they are built-up through calculations. Simulink's stepper capability is very helpful for this kind of debugging.
Again keep in mind, the floating-point representation issues that John mentioned and the inaccuracies that even format long can have in displaying values. It may be helpful to read the following for more information related to that topic.

카테고리

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

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by