How to differentiate between ceil, fix and floor

94 次查看(过去 30 天)
Hi, I want to separate whole number part from decimal number to use as an index for an array. I used following but failed:
1. index_of_array = fix((0.5005-0.5)/0.0005) giving me 0 but want 1(as solved in calculator)
2. Tried this: index_of_array = ceil((0.5005-0.5)/0.0005) giving me 1 ... ok good to go.but.... for this value:
index_of_array = ceil((0.8-0.5)/0.0005) giving me 601 but desired is 600 (as solved in calculator (0.8-0.5)/0.0005 = 600).. But in this case floor and fix giving correct answer...
So, please guide me when to use these functions and how to solve this problem.

回答(1 个)

Image Analyst
Image Analyst 2018-1-1
First see the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F because sometimes what you think should be an integer is really not - it's something like 3.0000000000000000001 due to standard numerical computing math like I'm sure they must have covered in your college classes (at least they did in mine).
Then, in general
  1. ceil() rounds to the next higher integer, in the +infinity direction.
  2. floor() rounds to the next lower integer, in the -infinity direction.
  3. fix() rounds to the integer next closest to zero, in other words, chops off the fractional part. In other words, rounds up for negative numbers and rounds down for positive numbers.

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by