How to differentiate between ceil, fix and floor
Show older comments
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.
Answers (1)
Image Analyst
on 1 Jan 2018
4 votes
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
- ceil() rounds to the next higher integer, in the +infinity direction.
- floor() rounds to the next lower integer, in the -infinity direction.
- 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.
1 Comment
Panagiotis Papias
on 5 Feb 2021
@Image Analyst, thank you for clarifying them :)
Categories
Find more on Linear Algebra 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!