Strange approximation during vector creation!
1 view (last 30 days)
Show older comments
Robin tournemenne
on 11 Jan 2017
Commented: Star Strider
on 11 Jan 2017
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 Comments
Jan
on 11 Jan 2017
@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
on 11 Jan 2017
Edited: Stephen23
on 11 Jan 2017
"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.
Accepted Answer
Star Strider
on 11 Jan 2017
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 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!