manually-written floor function code for rounding non-integers

13 ビュー (過去 30 日間)
Gobert
Gobert 2021 年 7 月 8 日
コメント済み: Gobert 2021 年 7 月 8 日
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.

採用された回答

John D'Errico
John D'Errico 2021 年 7 月 8 日
編集済み: John D'Errico 2021 年 7 月 8 日
If you want a mathematical expression, WRITTEN in MATLAB (where it will not have been written in the actual code) this could work:
myfloor = @(x) double(int64(x));
myfloor(2.3)
ans = 2
After converting the floating point number to an integer, then it needs to be returned as a double. But that code would be better written to return a number of the same floating point class, since that would fail for single precision input. As well, for input that was already an integer class, it should not convert the result to a double. Finally, that code will fail for negative numbers, since floor(-2.3) is -3.
floor(-2.3)
ans = -3
myfloor(-2.3)
ans = -2
Negative numbers round down for floor. A simple function that will effectively work for numbers of any sign is:
mybetterfloor = @(x) x - mod(x,1);
floor([-2.3 2.3])
ans = 1×2
-3 2
mybetterfloor([-2.3, 2.3])
ans = 1×2
-3 2
As you can see, this code rounds down for negative numbers, as does floor itself.
x = randn(1,10000);
all(floor(x) == mybetterfloor(x))
ans = logical
1
No explicit cast to another class was done in that code, so it should work for any floating point or integer class. Is that how they implemented it in compiled C? Surely not.
  2 件のコメント
Stephen23
Stephen23 2021 年 7 月 8 日
編集済み: Stephen23 2021 年 7 月 8 日
When converting to integer MATLAB rounds to the nearest whole number, rather than chopping off the fractional part (as apparently C users assume all other languages must also do):
x = 2.6;
double(int64(x))
ans = 3
The MOD solution works well though:
x-mod(x,1)
ans = 2
x = -2.6; % negative too
x-mod(x,1)
ans = -3
Gobert
Gobert 2021 年 7 月 8 日
Thanks, @John D'Errico & @Stephen Cobeldick. With my question, I now understand that the key was the MOD operation as shown below I believe that every function implemented in any languages follow mathematical expressions?

サインインしてコメントする。

その他の回答 (2 件)

Matt J
Matt J 2021 年 7 月 8 日
編集済み: Matt J 2021 年 7 月 8 日
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)
ans = 2
function y=myfloor(x)
y=str2double( extractBefore(string(x),'.') );
end

Kapil Gupta
Kapil Gupta 2021 年 7 月 8 日
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 件のコメント
Gobert
Gobert 2021 年 7 月 8 日
@Kapil Gupta, Thanks but Unfortunately that is not what I wanted. Here's an example of what I wanted: E.g., If floor (2.3) = 2. I want to see a mathematical expression that grabs 2 and discards 0.3 to output 2.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by