Rounding off numbers using Round, Floor, Ceil and Fix functions in MATLAB

Leave a Comment
MATLAB: The Language of Technical Computing
MATLAB: The Language of Technical Computing

Welcome once again to a new session of MATLAB!

Today we are going to learn some number rounding functions. Since numbers are what we deal with at almost all times in MATLAB, it is important we learn some operations upon them. Today's session will enable you to round off numbers in MATLAB in different ways according to the different needs one may face while working with MATLAB.

The functions that we would be studying together would be:

  • Round Function
  • Floor Function
  • Ceil Function
  • Fix Function
Hence lets get started!


ROUND function

The ROUND function, as its name goes, rounds a number to the nearest integer value. If the number lies exactly halfway between two integers, it is rounded off towards the larger integer. We can see its implementation using the following code:

A=5.34;
B=round(A);
display(A);
display(B);
A=5.67;
B=round(A);
display(A);
display(B);
A=5.5;
B=round(A);
display(A);
display(B);

And the above commands produce the below output:

Implementing ROUND function
Implementing ROUND function

FLOOR function

The FLOOR function rounds your number to the closest integer towards negative infinity.
To simplify this statement, lets suppose you got a number like 1.2. To obtain the floor value of 1.2, move towards 0 (zero). What is the first integer you obtain on the way? Surely 1, right? Hence 1 is the floor value of 1.2. Now read the first statement I wrote again :)

The following code example helps us see the above concept:

A=5.34;
B=floor(A);
display(A);
display(B);
A=5.67;
B=floor(A);
display(A);
display(B);
A=5.50;
B=floor(A);
display(A);
display(B);

And we hence obtain the following output:

Implementing FLOOR function
Implementing FLOOR function

CEIL function

The CEIL function does exactly the opposite of the FLOOR function. The CEIL function rounds your number to the closest integer towards positive infinity.
So for the same example we took for FLOOR, if CEIL function is used for the number 1.2, it will be rounded off to 2 as 2 is the first integer we get when we go towards positive infinity from 1.2.

The following code will help us understand this better:

A=5.34;
B=ceil(A);
display(A);
display(B);
A=5.67;
B=ceil(A);
display(A);
display(B);
A=5.50;
B=ceil(A);
display(A);
display(B);

And hence the following output is produced:

Implementing CEIL function
Implementing CEIL function

FIX function

The FIX function doesnt take into account the magnitude of the number. It simply rounds the number to the nearest integer towards 0 (zero).
We can see its functioning in the example below:

A=5.34;
B=fix(A);
display(A);
display(B);
A=5.67;
B=fix(A);
display(A);
display(B);
A=5.50;
B=fix(A);
display(A);
display(B);

And hence the following output is produced:

Implementing FIX function
Implementing FIX function


And with that we finish off with today's lesson. This was a short one, as will be some of the next ones, because they will be dealing with specific applications of MATLAB. Hope you enjoyed learning it as much as I did. With that I sign off for now. Meet you in the next post!

Take care :)



0 comments:

Post a Comment

Powered by Blogger.