How to use Floor command

조회 수: 8 (최근 30일)
trung trinh
trung trinh 2012년 10월 4일
I have some wrong thing with floor command. This problem is that
Step 1: Initial step
t=[0.90 1.00 1.30 1.80 1.90];
step = (t(5)-t(1))/100;
time = t(1):step:10;
Step 2: Check floor command
time(181)
ans =
2.7000
floor((time(181)-0.9)/(2*0.9))
ans =
0
*But i realize that*
floor((2.7-0.9)/(2*0.9))
ans =
1
What problem was happen in this case?
Question 2: Logic condition
t_ = 4.5 - 4*0.9
t_ =
0.9000
t_ >= 0.9
ans =
0
But
0.9>=0.9
ans =
1

채택된 답변

Matt Fig
Matt Fig 2012년 10월 4일
편집: Matt Fig 2012년 10월 4일
You have fallen into thinking that you are using infinite precision arithmetic when you are only using double precision. This is such a common mistake that it has its own FAQ.
Here is a little more about is going on:
t = [0.90 1.00 1.30 1.80 1.90];
step = (t(5)-t(1))/100;
time = t(1):step:10;
A = time(181)-0.9;
B = 2*0.9;
C = A/B;
fprintf('%16.16f\n',time(181),A,B,C)
2.6999999999999997
1.7999999999999998
1.8000000000000000
0.9999999999999999
As for the second question: same problem:
fprintf('%16.16f\n',4.5 - 4*0.9)
0.8999999999999999

추가 답변 (3개)

Matt J
Matt J 2012년 10월 4일
편집: Matt J 2012년 10월 4일
You're not accounting for floating point errors.
4.5 - 4*0.9
will not give you precisely 9/10, but rather something accurate to many decimal places.

Honglei Chen
Honglei Chen 2012년 10월 4일
This has nothing to do with floor, it's part of the floating point computation. See the link below

trung trinh
trung trinh 2012년 10월 4일
Thank a lot :)

카테고리

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