manually-written floor function code for rounding non-integers
Show older comments
Hi,
I need your help. See what I get when I used edit to view the steps involved into the floor function. It is a built-in function but I still want to see the mathematics on which this floor function is based on via matlab codes. Can you help? E.g., If floor (2.3) = 2. I want to see a mathematical expression that grabs 2 and/or discards 0.3 to output 2.
%FLOOR Round towards minus infinity.
% FLOOR(X) rounds the elements of X to the nearest integers
% towards minus infinity.
%
% See also ROUND, CEIL, FIX.
% Copyright 1984-2005 The MathWorks, Inc.
% Built-in function.
Accepted Answer
More Answers (2)
If floor (2.3) = 2. I want to see a mathematical expression that grabs 2 and/or discards 0.3 to output 2.
There is no fundamental formula for the floor function. In C\C++ it is done simply by casting the input to an integer type. One way to implement it manually though would be,
myfloor(2.3)
function y=myfloor(x)
y=str2double( extractBefore(string(x),'.') );
end
Kapil Gupta
on 8 Jul 2021
0 votes
I assume you want to know some details regarding the floor function. The following MATLAB documentation contains some details, you can check this out:
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!