manually-written floor function code for rounding non-integers
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (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
1 commentaire
Gobert
le 8 Juil 2021
Kapil Gupta
le 8 Juil 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:
1 commentaire
Gobert
le 8 Juil 2021
Catégories
En savoir plus sur Elementary Math dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
