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