Sử dụng bốn phương thức bất kỳ trong mô-đun toán học trong python

Một mô-đun toán học trong python nói một cách đơn giản là một nhóm python[. py] có thể được nhập vào một số chương trình python khác. Nói một cách đơn giản, hãy coi một mô-đun là một thư viện có một số mã viết sẵn có thể được sử dụng lại trong mã của bạn

Mô-đun toán học trong python là một mô-đun tiêu chuẩn trong python có chức năng thực hiện một số phép tính khá đơn giản như cộng [+], trừ [-] cho các hàm cấp cao khác nhau như hàm logarit, hàm mũ, hàm lượng giác. Nó cũng định nghĩa một số hằng số toán học như π [chiếc bánh],e [số Euler]

Các chức năng cơ bản của mô-đun toán học được viết bằng CPython và hiệu quả đối với thư viện toán học tiêu chuẩn C

Để sử dụng các chức năng có sẵn trong mô-đun này, hãy nhập thư viện này như sau

import math # this line imports all the functions available in math module

Hãy để chúng tôi thảo luận về các hàm số và hằng số khác nhau có sẵn trong thư viện toán học bằng ngôn ngữ python

Các hằng số của mô-đun toán học Python

Thỉnh thoảng có một số hằng số toán học được yêu cầu để thực hiện các phép tính toán học nhất định. Ví dụ: bạn hẳn đã sử dụng một hằng số gọi là π [chiếc bánh] để tính diện tích hình tròn có bán kính 'r'. Một loạt các hằng số có sẵn trong mô-đun toán học của python như sau

1. môn Toán. số Pi

Nó đại diện cho hằng số toán học π = 3. 14159…. Thí dụ. Nó có thể được sử dụng để tính diện tích hình tròn có bán kính 'bán kính' như sau

import math
radius = 4.5
area = math.pi * radius * radius
print[area]         

đầu ra

2. môn Toán. e

Nó đại diện cho hằng số toán học e = 2. 718281… còn được gọi là số Euler. It can be used in various mathematical applications where the final result has ‘e’. Example. for calculating the value of e3 + 5

import math
# ** operator means raise to power i.e. ab for a ** b
res = math.e ** 3 + 5
print[res]

Output

3. math. tau

It represents the mathematical constant τ = 6. 28318…. It can be used in various mathematical applications where the final result has ‘τ’. Example. for calculating the value of τ3 + 5

import math
# ** operator means raise to power i.e. ab for a ** b
res = math.tau ** 3 + 5
print[res]

đầu ra

4. math. inf

It represents the value of floating-point infinity i. e. a very large number

See its value

import math
print[math.inf]

đầu ra

5. math. nan

It represents floating-point NaN value which means Not a Number. See its value

import math
print[math.nan]

đầu ra

Python Math module’s Numeric Functions

The numeric functions in the math module take real numbers/integers as arguments and after doing the specified mathematical calculations, it returns a number or a boolean value

There are a variety of functions in this catalog, like the function math. pow[x,y] that calculates the value of xy and returns it. There are a lot of operations too like finding square root, finding factorial of a number, and much more that could be done by the math module’s mathematical function. Let’s explore all of them one by one

1. môn Toán. ceil[x]

  • Input. Real Number
  • Return Type . Integer

This function takes one number as an argument and returns the smallest integer that is greater than or equal to x. This function can be understood by the graph given below

For all the numbers input in the range [1,2], the output is 2. Giống như, toán học. ceil[1. 6]=2, 2 is the smallest integer such that 1. 6

Chủ Đề