如何在 Python 中找到大於 x 的最小數?


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

Python 中向上取整函式簡介

向上取整 (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-09

1K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告