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일

1 개 추천

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

Walter Roberson
Walter Roberson 2017년 1월 11일
편집: Walter Roberson 2017년 1월 11일
Also using linspace() can help a bit as it tries to take this into account.
More accurate would be (0:2:100)/10
Jan
Jan 2017년 1월 11일
Because this is not actually a "problem", there need not be a "work-around" to be exact. It is required to check floating point numbers with a certain tolerance and the useful limit depends on the nature of the computations.
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
My (our) pleasure.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

질문:

2017년 1월 11일

댓글:

2017년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by