How to Round toward Negative Infinity in MATLAB?


Rounding toward negative infinity is a method of rounding a number "N" down to the nearest integer less than or equal to the number "N". This method is also called "flooring".

  • In simple words, rounding a number toward negative infinity is a method of finding the largest integer number which is less than or equal to the given number.

  • For example, consider a number 4.8, when this number is round to negative infinity. Then, it will become 4. This is because, 4 is the largest integer number less than or equal to 4.8.

  • Similarly, if we have a negative number like -4.7, then it would be -5. This is because -5 is the largest integer number less than or equal to -4.7.

In this tutorial, let us learn to round a number toward negative infinity in MATLAB.

Round Toward Negative Infinity in MATLAB

In MATLAB, there is a built-in function named, "floor" that is used to round toward negative infinity. This function can be used to round any number or an array of numbers.

Syntax

The "floor" function rounds the given number or each element of the array down to the nearest integer number toward negative infinity.

rounded_num = floor(num);

This function can have other syntax formats as well.

To round a vector of elements −

rounded_vector = floor(A);

To round a duration array −

rounded_duration = floor(t);

To round a duration array with specified unit of time −

rounded_duration = floor(t, unit);

Now, let us discuss these syntax formats with the help of examples.

Round a Number toward Negative Infinity

In MATLAB, we can round a given number toward negative infinity as per the following steps −

  • Step 1 − Input the number that has to be rounded.

  • Step 2 − Use the "floor" function to round the number.

  • Step 3 − Display the rounded number.

Example 1

The implementation of the above steps is demonstrated in the following example.

% MATLAB code to round a number toward negative infinity
% Input the number
num = input('Please Enter a Number:');

% Round the number towards negative infinity
rounded_num = floor(num);

% Display the input and rounded numbers
disp('Input Number:');
disp(num);

disp('Rounded Number towards Negative Infinity:');
disp(rounded_num);

Output

Here is the output −

Please Enter a Number: -4.7
Input Number: -4.7000
Rounded Number towards Negative Infinity: -5

Hence, this MATLAB has rounded the input number towards negative infinity.

Round a Vector of Elements Toward Negative Infinity

In MATLAB, the "floor" function can also be used to round a vector of elements. When a vector of elements is input to the "floor" function, it rounds each element of the input vector toward negative infinity.

The step-by-step procedure to round a vector of elements toward negative infinity using MATLAB is explained below −

  • Step 1 − Create the input vector.

  • Step 2 − Use the "floor" function to round each element of the input vector toward negative infinity.

  • Step 3 − Display the rounded vector.

Example 2

Let us see an example to understand this implementation.

% MATLAB code to round a vector toward negative infinity
% Create an input vector
A = [-1.3, -0.4, -7.4, 9.2, -14.7, -3.6];

% Round the vector toward negative infinity
rounded_vector = floor(A);

% Display the input and rounded vectors
disp('Input Vector:');
disp(A);

disp('Rounded Vector toward Negative Infinity:');
disp(rounded_vector);

Output

Here is the output −

Input Vector:
   -1.3000   -0.4000   -7.4000    9.2000  -14.7000   -3.6000
Rounded Vector toward Negative Infinity:
   -2    -1    -8     9   -15    -4

Round an Array of Duration toward Negative Infinity

In MATLAB, the "floor" function is also used to round an array of duration toward negative infinity. Here are the steps involved in rounding duration values toward negative infinity.

  • Step 1 − Create an array of duration values.

  • Step 2 − Format the array into time format as −

hh:mm:ss.SS

Where, "hh" represents hours, "mm" represents minutes, "ss" represents seconds, and "SS" denotes milliseconds.

  • Step 3 − Use the "floor" function to round the array of duration values.

  • Step 4 − Display the results.

Example 3

Let us take an example to understand the implementation of these steps in MATLAB programming.

% MATLAB code to round a duration array
% Create an example duration array
t = [hours(3.7), minutes(-25.5), seconds(120.8)];

% Format the array in time format
t.Format = 'hh:mm:ss.SS';

% Round the duration array towards negative infinity
rounded_t = floor(t);

% Display the input and rounded duration arrays
disp('Input duration array:');
disp(t);

disp('Rounded duration array towards negative infinity:');
disp(rounded_t);

Output

When you run this code, it will produce the following output −

Input duration array:
   03:42:00.00   -00:25:30.00    00:02:00.80

Rounded duration array towards negative infinity:
   03:42:00.00   -00:25:30.00    00:02:00.00

This example shows how we can round a duration array toward negative infinity. It rounds the duration array in seconds.

Round an Array of Duration toward Negative Infinity with Specified Unit of Time

In MATLAB, the "floor" function also provides a way of rounding an array of duration toward negative infinity with specified unit of time.

The steps involved in rounding a duration array with specified unit of time are given below −

  • Step 1 − Create a duration array.

  • Step 2 − Format the array in time format.

  • Step 3 − Specify the unit of time for rounding. It can be "hours", "minutes", or "seconds".

  • Step 4 − Round the duration array toward negative infinity with specified unit of time.

  • Step 5 − Display the results.

Example 4

The following example demonstrates the implementation of these steps in MATLAB programming.

% MATLAB code to round a duration array with specified unit of time
% Create an example duration array
t = [hours(3.7), minutes(-25.5), seconds(120.8)];

% Format the duration array in time format
t.Format = 'hh:mm:ss.SS';

% Round the duration array towards negative infinity
rounded_t_h = floor(t, 'hours');	% Round in hours
rounded_t_m = floor(t, 'minutes');	% Round in minutes

% Display the input and rounded duration arrays
disp('Input duration array:');
disp(t);

disp('Rounded duration array towards negative infinity in hours:');
disp(rounded_t_h);

disp('Rounded duration array towards negative infinity in minutes:');
disp(rounded_t_m);

Output

Here is the output of this MATLAB code −

Input duration array:
    03:42:00.00   -00:25:30.00    00:02:00.80

Rounded duration array towards negative infinity in hours:
    03:00:00.00   -01:00:00.00    00:00:00.00

Rounded duration array towards negative infinity in minutes:
    03:42:00.00   -00:26:00.00    00:02:00.00

This example shows how we can round a duration array toward negative infinity with a specified unit of time.

Conclusion

In conclusion, MATLAB has a built-in function "floor" that can be used to round toward negative infinity. This function can be used to round numbers, arrays, duration values toward negative infinity. In this tutorial, I explained in detail all the possible rounding operations with the "floor" function in MATLAB.

Updated on: 25-Oct-2023

23 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements