Strange approximation during vector creation!

1 次查看(过去 30 天)
Hello,
I am using Matlab for a few years now and I surprisely discovered a strange Matlab Behavior:
0:0.2:10 provides an array of numbers sometime approximated which is rather bad for testing...
Here is the command result:
>> 0:0.2:10
ans = Columns 1 through 4
0 0.200000000000000 0.400000000000000 0.600000000000000
Columns 5 through 8
0.800000000000000 1.000000000000000 1.200000000000000 1.400000000000000
Columns 9 through 12
1.600000000000000 1.800000000000000 2.000000000000000 2.200000000000000
Columns 13 through 16
2.400000000000000 2.600000000000000 2.800000000000000 3.000000000000000
Columns 17 through 20
3.200000000000000 3.400000000000000 3.600000000000000 3.800000000000000
Columns 21 through 24
4.000000000000000 4.200000000000000 4.400000000000000 4.600000000000001
Columns 25 through 28
4.800000000000001 5.000000000000000 5.199999999999999 5.399999999999999
Columns 29 through 32
5.600000000000000 5.800000000000000 6.000000000000000 6.199999999999999
Columns 33 through 36
6.400000000000000 6.600000000000000 6.800000000000000 7.000000000000000
Columns 37 through 40
7.199999999999999 7.400000000000000 7.600000000000000 7.800000000000000
Columns 41 through 44
8.000000000000000 8.199999999999999 8.400000000000000 8.600000000000000
Columns 45 through 48
8.800000000000001 9.000000000000000 9.199999999999999 9.400000000000000
Columns 49 through 51
9.600000000000000 9.800000000000001 10.000000000000000
As you can see 4.6, 5.2, 5.4, 6.2, 7.2, 8.2, 8.8 and 9.1 are approximated!
Is it normal? Does a workaround exist???
Thanks for your time,
regards,
Robin
  2 个评论
Jan
Jan 2017-1-11
@Robin: Welcome to the world of arithmetics with limited precision. This is the second most asked question (behind EVAL). The effects are defined in the IEEE754 norm for storing decimal floating point data in binary format. This concerns Matlab and all other programming languages, which work with the floating point units of the processors.
Stephen23
Stephen23 2017-1-11
编辑:Stephen23 2017-1-11
"a strange Matlab Behavior"
Not at all. That is quite a normal floating point number behavior. This behavior is defined by IEEE Standard 754, which is a very commonly used standard in hardware and software, and so is not "a strange Matlab Behavior" at all.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2017-1-11
That’s normal. It has to do with floating-point calculations and approximations. See: Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link) for details.
The only work-around I can imagine is to round them:
V1 = 0:0.2:10;
V2 = round(V1,1);
Test = V1-V2;
The more recent versions of round allow rounding to a specific number of decimal places. If you don’t have it, this anonymous funciton will do the same thing (with the same accuracy):
roundn = @(x,n) round(x .* 10.^n)./10.^n; % Round ‘x’ To ‘n’ Digits, Emulates Latest ‘round’ Function
  4 个评论
Robin tournemenne
Robin tournemenne 2017-1-11
Thanks a lot for you quick answers! I was surprised, because I forgot that the floating part of a number has to be a multiple of EPS to be coded without approximation.
Thanks again,
regards

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by