如何在Python中查詢大於x的最小數?


在本文中,我們將向您展示如何在Python中查詢大於x的最小數。

Python中ceil函式介紹

Ceil是Python的math模組中的一個函式,包含在Python標準庫中。在數學中,它與最小整數函式或上取整函式相同。

如果您給定一個實數x,ceil(x)在數學符號中表示為⌈x⌉,其中括號的上方向對應於上取整運算(因為天花板位於您的頭頂)。

相反,floor(x)(返回最大整數≤x)用⌊x⌋表示,其中向下符號表示下取整運算

使用分段定義,ceil(x) =

x, ifx∈Z
⌊x+1⌋, ifx∉Z

ceil() 函式

在Python中,ceil(x)方法返回大於或等於x的最小整數。它被稱為x的上取整值。

語法

import math
math.ceil(x)

引數

  • − 任何實數

返回值 − 返回不小於x的最小整數。

兩種呼叫ceil()函式的方法

根據您已匯入到Python應用程式中的內容,您可以透過兩種方式呼叫ceil()方法。

  • 如果您匯入了整個math模組而不是僅僅是函式,則必須使用點(.)運算子來訪問ceil()函式(因為它是在math模組中定義的)。如果y是我們的輸出變數,x是我們的數值輸入變數,則格式如下:

y= math.ceil(x)
  • 如果您使用math模組匯入了ceil()函式,則呼叫函式的語法將變得更容易,如下所示:

y= ceil(x)
  • 如果您還匯入了另一個模組,該模組有其自身定義的ceil()(可能出於相同目的,也可能出於不同目的),則必須使用第一種方法來避免歧義。

示例

演算法(步驟)

以下是執行所需任務的演算法/步驟

  • 使用import關鍵字匯入math模組。

  • 使用math.ceil()函式獲取數字的上取整值,即透過將數字作為引數傳遞給它來獲取大於或等於該數字的最小整數。

示例

以下程式使用math.ceil()函式返回上取整值,即大於或等於x的最小整數:

# importing math module import math # getting the ceiling value of numbers using math.ceil() print("The Smallest Integer Greater than -12.11 is:", math.ceil(-12.11)) print("The Smallest Integer Greater than 50.26 is:", math.ceil(50.26)) print("The Smallest Integer Greater than 30.5 is:", math.ceil(30.5)) print("The Smallest Integer Greater than 1.1 is:", math.ceil(1.1))

輸出

執行上述程式後,將生成以下輸出:

The Smallest Integer Greater than -12.11 is: -12
The Smallest Integer Greater than 50.26 is: 51
The Smallest Integer Greater than 30.5 is: 31
The Smallest Integer Greater than 1.1 is: 2

使用int()函式的最小整數函式

演算法(步驟)

以下是執行所需任務的演算法/步驟

  • 檢查傳遞的數字是否大於0,如果為真,則使用int()函式(從給定物件返回整數)+1返回傳遞數字的整數格式。這將返回大於給定引數的最小數。

  • 否則返回數字的整數格式。

  • 透過將數字作為引數傳遞來呼叫smallestInteger()函式,並列印其返回的數字的最小整數函式值。

  • 同樣,查詢其他數字並觀察變化。

示例

以下程式使用int()函式返回上取整值,即大於或等於x的最小整數:

# creating a function that returns by taking the given number # as an argument def smallestInteger(n): # Checking if the number is greater than 0 if(n>=0): # Return the integer format of the given number+1 return int(n)+1 # Else if it is a negative number else: # Return the integer format return int(n) # calling the smallestInteger() function by passing given number as an argument print('The Smallest Integer Greater than 5.2 is:',smallestInteger(5.2)) print('The Smallest Integer Greater than 0.2 is:',smallestInteger(0.2)) # Negative Number print('The Smallest Integer Greater than -5.2 is:',smallestInteger(-5.2))

輸出

執行上述程式後,將生成以下輸出:

The Smallest Integer Greater than 5.2 is: 6
The Smallest Integer Greater than 0.2 is: 1
The Smallest Integer Greater than -5.2 is: -5

傳遞無效引數時的異常情況

正如我們在上一節中看到的,Python中的ceil()接受單個數值引數。在Python中,數值型別包括int、float和complex,但是隻有實數型別int和float被允許作為ceil()的輸入引數。

因為布林值是int的子型別,所以True和False值也將被接受為輸入。事實上,Python ceil()將分別將其視為1和0。

傳遞字串作為輸入

以下程式返回TypeError,因為ceil()函式不接受字串作為輸入:

# importing math module import math # passing the input as string # returns a TypeError print(math.ceil("2.3"))

輸出

執行上述程式後,將生成以下輸出:

Traceback (most recent call last):
  File "main.py", line 6, in 
print(math.ceil("2.3"))
TypeError: must be real number, not str

傳遞列表作為輸入

以下程式返回TypeError,因為ceil()函式不接受列表作為輸入:

# importing math module import math # input list list = [1,2,3] # passing the input as list # returns a TypeError print(math.ceil(list))

輸出

執行上述程式後,將生成以下輸出:

Traceback (most recent call last):
  File "main.py", line 9, in 
    print(math.ceil(list))
TypeError: must be real number, not list

結論

在本文中,我們學習了兩種在Python中查詢大於x的最小數的方法。我們還透過示例學習了ceil()(最小整數函式)的異常情況。

更新於:2022年11月9日

1K+ 瀏覽量

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告