2012-03-15 114 views
5

我會說所有的編程語言都具有這些名稱的功能選擇更低或更高的兩個值:這對函數floor()/ ceil()和min()/ max()之間有什麼區別?

  • min() & max()
  • floor() & ceil()/ceiling()

某些語言有都。我相信JavaScript就是一個例子。

我一直對前一對和後一對之間的區別有點模糊。我有一個模糊的印象,min/max是更簡單和floor/ceiling更數學,但這並不多。

奇怪的是,我無法在StackOverflow或互聯網上的任何地方找到這個討論,一般通過搜索Google。 那麼是否有一些最佳實踐或經驗法則來決定在您的編程語言提供這兩種功能時要使用哪些功能?

回答

8

這是蘋果與橙子。在大多數語言/ API中,min/max需要兩個(或更多)輸入,並返回最小/最大值。 floor/ceil取一個參數,並將其四捨五入爲最接近的整數。

+5

Oof!看起來很明顯!但是我不會刪除它,如果有像我這樣的其他笨頭笨腦的人想到同樣的事情! '\ - :' – hippietrail 2012-03-15 17:29:22

10

據我所知,maxmin用於一個集合,比如數組。 Floorceiling用於單個數字。例如:

min(1, 2, 3, 4) => 1 
max(1, 2, 3, 4) => 4 
floor(3.5) => 3 
ceiling(3.5) => 4 
3

min()max()返回較小或者2個的值越大,有些人可能會做的比2點的值,如

min(3, 5);返回3

floor()ceiling()截斷雙成

floor(5.3);返回5.

3

不知道我理解你的問題,例如參見:

a = 1.7 
b = 2.8 

min(a,b) = 1.7 (returns the minimum of a and b) 
max(a,b) = 2.8 (returns the maximum of a and b) 

floor(a) = 1 (rounds towards 0) 
floor(b) = 2 (rounds towards 0) 

ceil(a) = 2 (rounds up) 
ceil(b) = 3 (rounds up) 
4
min(1, 2) == 1 
max(1, 2) == 2 

floor(3.9) == 3 
round(3.9) == 4 

ceil(3.1) == 4 
round(3.1) == 3 

trunc, as in (int)(3.xxx) = 3 (no matter what xxx is) 

的定義:

floorn少的最大整數。

ceil是大於n的最小整數。

相關問題