SlideShare a Scribd company logo
1 of 28
Download to read offline
math
Benson
math
★屬 Python 在數學計算的的套件
★math 是內含在 Python 的標準套件
★提供對 C 標準定義的數學函數的訪問
math
(一)匯入(import) math
(二)常數函數
(三)數論和表示函數
(四)功率和對數函數
(五)三角函數
(六)雙曲函數
(七)角度轉換函數
(八)特殊功能函數
math
(一)匯入(import) math
In [1]: import math
math
(二)常數函數
============= ===============================
character description
============= ===============================
math.pi 數字常數π= 3.141592653589793
math.e 數字常數 e= 2.718281828459045
math.tau 數字常數τ= 6.283185307179586
math.inf 浮點正無窮大
-math.inf 浮點負無窮大
math.nan 浮點“非數字”(NaN)值
============= ===============================
math
★常數函數-範例
In [1]: math.pi
Out[1]: 3.141592653589793
In [2]: math.e
Out[2]: 2.718281828459045
In [3]: math.tau
Out[3]: 6.283185307179586
In [4]: math.inf
Out[4]: inf
In [5]: -math.inf
Out[5]: -inf
In [6]: math.nan
Out[6]: nan
math
(三)數論和表示函數
============= ===============================
character description
============= ===============================
math.ceil(x) 返回 x 的最大上限整數值
math.copysign(x,y) 返回 x 絕對值浮點數,符號 y 為 x 返回正數或負數
math.fabs(x) 返回 x 絕對值
math.factorial(x) 返回 x 階乘, x 需為正整數
math.floor(x) 返回 x 的最小下限整數值
math.fmod(x,y) 返回 y 除於 x 的餘數,亦即 x%y
math.frexp(x) 返回一個浮點數 m,和一個整數 e,亦即 x=m*2**e
math.fsum([x]) 返回一個疊代中精確浮點值
math.gcd(x,y) 返回 x,y 的最大公約數
math
math.isclose(x,y) 返回 True(x=y),或 False(x≠y)
math.isinf(x) 返回 True(x=正負無窮大),否則為 False
math.isnan(x) 返回 True(x=非數字),否則為 False
math.ldexp(x,i) 返回一個數值等於 x*(2**i)
math.modf(x) 返回一個浮點數 m,和一個整數 n,亦即 x=m+n
math.remainder(x,y)返回對於 y 的 IEEE 754 樣式的 x 餘數
math.trunc(x) 返回 x 的整數值
============= ===============================
math
★數論和表示函數-範例
In [1]: math.ceil(5.3)
Out[1]: 6
In [2]: math.ceil(-5.3)
Out[2]: -5
In [3]: math.copysign(5.3 , 0.0)
Out[3]: 5.3
In [4]: math.copysign(5.3 , -0.0)
Out[4]: -5.3
In [5]: math.fabs(-5.3)
Out[5]: 5.3
math
In [6]: math.factorial(5)
Out[6]: 120
In [7]: math.floor(5.3)
Out[7]: 5
In [8]: math.floor(-5.3)
Out[8]: -6
In [9]: math.fmod(5 , 2.0)
Out[9]: 1.0
In [10]: math.frexp(5)
Out[10]: (0.625, 3)
In [11]: math.fsum([0.3,0.3,0.3,0.3,0.3,0.3])
Out[11]: 1.7999999999999998
math
In [12]: math.gcd(6 , 2)
Out[12]: 2
In [13]: math.isclose(6 , 6)
Out[13]: True
In [14]: math.isclose(6 , 6.1)
Out[14]: False
In [15]: math.isinf(math.inf)
Out[15]: True
In [16]: math.isinf(6)
Out[16]: False
In [17]: math.isnan(math.nan)
Out[17]: True
math
In [18]: math.isnan(6)
Out[18]: False
In [19]: math.ldexp(5 , 2)
Out[19]: 20.0
In [20]: math.modf(5.3)
Out[20]: (0.2999999999999998, 5.0)
In [21]: math.remainder(5 , 2)
Out[21]: 1.0
In [22]: math.trunc(5.3)
Out[22]: 5
math
(四)功率和對數函數
============= ===============================
character description
============= ===============================
math.exp(x) 返回自然指數 e**x,x 為自然對數的基數
math.expm1(x) 返回自然指數 e**x-1
math.log(x) 返回 x 的自然指數(基數為 e=2.71828…),亦即返
回 m 值.其中,e 的平方 m 等於 x
math.log1p(x) 返回 1+x 的自然指數(基數為 e=2.71828…),亦即
返回 m 值.其中,e 的平方 m 等於 1+x
math.log2(x) 返回 x 的自然指數(基數為 2)
math.log10(x) 返回 x 的自然指數(基數為 10)
math.pow(x,y) 返回 x 的 y 次方,亦即 x**y
math.sqrt(x) 返回 x 的平方根
============= ===============================
math
★功率和對數函數-範例
In [1]: math.exp(2)
Out[1]: 7.38905609893065
In [2]: math.expm1(2)
Out[2]: 6.38905609893065
In [3]: math.log(7.38905609893065)
Out[3]: 2.0
In [4]: math.log1p(7.38905609893065)
Out[4]: 2.1269280110429727
math
In [5]: math.log2(8)
Out[5]: 3.0
In [6]: math.log10(100)
Out[6]: 2.0
In [7]: math.pow(2,5)
Out[7]: 32.0
In [8]: math.sqrt(9)
Out[8]: 3.0
math
(五)三角函數
============= ===============================
character description
============= ===============================
math.sin(x) 返回 x 的正弦值
math.cos(x) 返回 x 的餘弦值
math.tan(x) 返回 x 的正切值
math.asin(x) 返回 x 的反正弦值
math.acos(x) 返回 x 的反餘弦值
math.atan(x) 返回 x 的反正切值
math.atan2(y,x) 返回 y/x 的反正切值
math.hypot(x,y) 返回歐幾里德範數,亦即 sqrt(x*x + y*y)
============= ===============================
math
★三角函數-範例
In [1]: math.sin(30*math.pi/180)
Out[1]: 0.49999999999999994
In [2]: math.cos(60*math.pi/180)
Out[2]: 0.5000000000000001
In [3]: math.tan(45*math.pi/180)
Out[3]: 0.9999999999999999
In [4]: math.asin(-1)
Out[4]: -1.5707963267948966
In [5]: math.asin(0)
Out[5]: 0.0
In [6]: math.asin(1)
Out[6]: 1.5707963267948966
math
In [7]: math.acos(-1)
Out[7]: 3.141592653589793
In [8]: math.acos(0)
Out[8]: 1.5707963267948966
In [9]: math.acos(1)
Out[9]: 0.0
In [10]: math.atan(-math.inf)
Out[10]: -1.5707963267948966
In [11]: math.atan(0)
Out[11]: 0.0
In [12]: math.atan(math.inf)
Out[12]: 1.5707963267948966
math
In [13]: math.atan2(-1,0)
Out[13]: -1.5707963267948966
In [14]: math.atan2(0,0)
Out[14]: 0.0
In [15]: math.atan2(1,0)
Out[15]: 1.5707963267948966
In [17]: math.hypot(1,1)
Out[17]: 1.4142135623730951
In [18]: math.hypot(3,4)
Out[18]: 5.0
math
(六)雙曲函數
============= ===============================
character description
============= ===============================
math.sinh(x) 返回 x 的雙曲正弦值
math.cosh(x) 返回 x 的雙曲餘弦值
math.tanh(x) 返回 x 的雙曲正切值
math.asinh(x) 返回 x 的反雙曲正弦值
math.acosh(x) 返回 x 的反雙曲餘弦值
math.atanh(x) 返回 x 的反雙曲正切值
============= ===============================
math
★雙曲函數-範例
In [1]: math.sinh(30*math.pi/180)
Out[1]: 0.5478534738880397
In [2]: math.cosh(60*math.pi/180)
Out[2]: 1.600286857702386
In [3]: math.tanh(40*math.pi/180)
Out[3]: 0.603180554488537
In [4]: math.asinh(-2.3)
Out[4]: -1.570278543484978
In [5]: math.asinh(0)
Out[5]: 0.0
In [6]: math.asinh(2.3)
Out[6]: 1.570278543484978
math
In [7]: math.acosh(1)
Out[7]: 0.0
In [8]: math.acosh(1.5)
Out[8]: 0.9624236501192069
In [9]: math.acosh(3)
Out[9]: 1.762747174039086
In [10]: math.atanh(-0.9)
Out[10]: -1.4722194895832204
In [11]: math.atanh(0)
Out[11]: 0.0
In [12]: math.atanh(0.9)
Out[12]: 1.4722194895832204
math
(七)角度轉換函數
============= ===============================
character description
============= ===============================
math.degrees(x) 將角度 x 從弧度轉換為度數
math.radians(x) 將角度 x 從度數轉換為弧度
============= ===============================
math
★角度轉換函數-範例
In [1]: math.degrees(30*math.pi/180)
Out[1]: 29.999999999999996
In [2]: math.radians(30)
Out[2]: 0.5235987755982988
math
(八)特殊功能函數
============= ===============================
character description
============= ===============================
math.erf(x) 返回 x 的誤差函數,又稱高斯誤差函數,
範圍[- 1,1]
math.erfc(x) 返回 x 的互補誤差函數
math.gamma(x) 返回 x 的 gamma 函數
math.lgamma(x) 返回 Gamma 函數的絕對值的自然對數 x
============= ===============================
math
★特殊功能函數-範例
In [1]: math.erf(-math.inf)
Out[1]: -1.0
In [2]: math.erf(0)
Out[2]: 0.0
In [3]: math.erf(math.inf)
Out[3]: 1.0
In [4]: math.erfc(-math.inf)
Out[4]: 2.0
In [5]: math.erfc(0)
Out[5]: 1.0
In [6]: math.erfc(math.inf)
Out[6]: 0.0
math
In [7]: math.gamma(-3.5)
Out[7]: 0.27008820585226917
In [8]: math.gamma(1)
Out[8]: 1.0
In [9]: math.gamma(3.5)
Out[9]: 3.323350970447842
In [10]: math.lgamma(-3.5)
Out[10]: -1.3090066849930417
In [11]: math.lgamma(1)
Out[11]: 0.0
In [12]: math.lgamma(3.5)
Out[12]: 1.2009736023470738
math
Reference:
https://docs.python.org/3/library/math.html

More Related Content

What's hot (20)

Ch12 範例
Ch12 範例Ch12 範例
Ch12 範例
 
Ch10 範例
Ch10 範例Ch10 範例
Ch10 範例
 
Ch5
Ch5Ch5
Ch5
 
Ch10 教學
Ch10 教學Ch10 教學
Ch10 教學
 
Ch2 教學
Ch2 教學Ch2 教學
Ch2 教學
 
Ch6 教學
Ch6 教學Ch6 教學
Ch6 教學
 
Ch12 教學
Ch12 教學Ch12 教學
Ch12 教學
 
Ch9 範例
Ch9 範例Ch9 範例
Ch9 範例
 
Appendix B 範例
Appendix B 範例Appendix B 範例
Appendix B 範例
 
Appendix A 教學
Appendix A 教學Appendix A 教學
Appendix A 教學
 
Ch5 範例
Ch5 範例Ch5 範例
Ch5 範例
 
Appendix B 教學
Appendix B 教學Appendix B 教學
Appendix B 教學
 
Ppt 127-135
Ppt 127-135Ppt 127-135
Ppt 127-135
 
Ppt 1-50
Ppt 1-50Ppt 1-50
Ppt 1-50
 
Ch7 範例
Ch7 範例Ch7 範例
Ch7 範例
 
Ch10 習題
Ch10 習題Ch10 習題
Ch10 習題
 
Ch8
Ch8Ch8
Ch8
 
Ch7 教學
Ch7 教學Ch7 教學
Ch7 教學
 
Sym py edu
Sym py eduSym py edu
Sym py edu
 
Ppt 51-77
Ppt 51-77Ppt 51-77
Ppt 51-77
 

Similar to Math basic - v01

实验一 Mathematica软件简介
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介guestfe33f0e
 
实验一 Mathematica软件简介
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介Xin Zheng
 
241525第三章初等代数运算命令与例题
241525第三章初等代数运算命令与例题241525第三章初等代数运算命令与例题
241525第三章初等代数运算命令与例题gwadhysys
 
C语言学习100例实例程序
C语言学习100例实例程序C语言学习100例实例程序
C语言学习100例实例程序yiditushe
 
第1章 Matlab操作基础
第1章  Matlab操作基础第1章  Matlab操作基础
第1章 Matlab操作基础eterou
 
姜昱安108學年度下學期數學科課程學習成果
姜昱安108學年度下學期數學科課程學習成果姜昱安108學年度下學期數學科課程學習成果
姜昱安108學年度下學期數學科課程學習成果ssuserf9a326
 
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版逸 張
 
手把手打開Python資料分析大門
手把手打開Python資料分析大門手把手打開Python資料分析大門
手把手打開Python資料分析大門Yen-lung Tsai
 
Learning to Rank: An Introduction to LambdaMART
Learning to Rank: An Introduction to LambdaMARTLearning to Rank: An Introduction to LambdaMART
Learning to Rank: An Introduction to LambdaMARTJulian Qian
 
ncuma_串列.pptx
ncuma_串列.pptxncuma_串列.pptx
ncuma_串列.pptxNCU MCL
 
第3章矩阵及其运算
第3章矩阵及其运算第3章矩阵及其运算
第3章矩阵及其运算eterou
 
[系列活動] 手把手打開Python資料分析大門
[系列活動] 手把手打開Python資料分析大門[系列活動] 手把手打開Python資料分析大門
[系列活動] 手把手打開Python資料分析大門台灣資料科學年會
 

Similar to Math basic - v01 (20)

实验一 Mathematica软件简介
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介
 
实验一 Mathematica软件简介
实验一   Mathematica软件简介实验一   Mathematica软件简介
实验一 Mathematica软件简介
 
Scilab Chap1
Scilab Chap1Scilab Chap1
Scilab Chap1
 
241525第三章初等代数运算命令与例题
241525第三章初等代数运算命令与例题241525第三章初等代数运算命令与例题
241525第三章初等代数运算命令与例题
 
C语言学习100例实例程序
C语言学习100例实例程序C语言学习100例实例程序
C语言学习100例实例程序
 
Ch2
Ch2Ch2
Ch2
 
第1章 Matlab操作基础
第1章  Matlab操作基础第1章  Matlab操作基础
第1章 Matlab操作基础
 
Ppt 51-77
Ppt 51-77Ppt 51-77
Ppt 51-77
 
Python story
Python storyPython story
Python story
 
姜昱安108學年度下學期數學科課程學習成果
姜昱安108學年度下學期數學科課程學習成果姜昱安108學年度下學期數學科課程學習成果
姜昱安108學年度下學期數學科課程學習成果
 
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
張逸 - 研究所 / 轉學考計算機概論 、公職計算機概要 - 程式語言 - 試閱版
 
第5章数组
第5章数组第5章数组
第5章数组
 
Ppt 1-25
Ppt 1-25Ppt 1-25
Ppt 1-25
 
手把手打開Python資料分析大門
手把手打開Python資料分析大門手把手打開Python資料分析大門
手把手打開Python資料分析大門
 
Learning to Rank: An Introduction to LambdaMART
Learning to Rank: An Introduction to LambdaMARTLearning to Rank: An Introduction to LambdaMART
Learning to Rank: An Introduction to LambdaMART
 
ncuma_串列.pptx
ncuma_串列.pptxncuma_串列.pptx
ncuma_串列.pptx
 
Ppt 26-50
Ppt 26-50Ppt 26-50
Ppt 26-50
 
第3章矩阵及其运算
第3章矩阵及其运算第3章矩阵及其运算
第3章矩阵及其运算
 
[系列活動] 手把手打開Python資料分析大門
[系列活動] 手把手打開Python資料分析大門[系列活動] 手把手打開Python資料分析大門
[系列活動] 手把手打開Python資料分析大門
 
Ch1 教學
Ch1 教學Ch1 教學
Ch1 教學
 

Math basic - v01