'fix' function misbehaving
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi everyone,
I have a table of data.
The first column of the table reads how many heartbeats there have been. It's a number that increases with each row by 1. So I have 9 beats, hence 9 rows, reading 1 through 9.
Outside the table, I have a variable 'Z' calculated from two other variables 'X' and 'Y' which I previously specify.
X and Y are variables from some simulations I have been doing. X is dependant on the value of Y, but Y is held as a constant so that shouldn't matter.
X = 38.2
Y= 25
Z=X*Y, so Z=955.000. Simple enough.
Now, sometimes Z is not a whole number.
For example, one of my simulations had X=19.1000 and Y=25, giving a value of Z = 477.5000
For the next column of my table I want to multiply the heart-beat number by the integer part of Z.
Using the 'fix' function, Z=fix(Z), works perfecly when Z=477.5000. Z returns 477.
However when Z=955.0000, fix(Z) returns 954.
When I type out "Z=955.0000" in place of getting Z from the previous calculation then fix(Z) returns 955.
The same issue is ocuring with the floor funcition, however the round function returns 955.
Any idea what's going wrong? Thanks
댓글 수: 0
채택된 답변
Star Strider
2021년 1월 21일
dZ = Z - 955
and check the result. This is most likely the result of rounding in the display, and floating-point approximation error. See Floating-Point Numbers for an extended discussion.
댓글 수: 2
Star Strider
2021년 1월 21일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!