# 數學庫
lua數學庫由一組標準的數學函數構成。數學庫的引入豐富了lua編程語言的功能,同時也方便了程序的編寫。常用數學函數見下表:
| 函數名 | 函數功能 |
|-----|-----|
| math.rad(x) | 角度x轉換成弧度 |
| math.deg(x) | 弧度x轉換成角度 |
| math.max(x, ...) | 返回參數中值最大的那個數,參數必須是number型 |
| math.min(x, ...) | 返回參數中值最小的那個數,參數必須是number型 |
| math.random ([m [, n]]) | 不傳入參數時,返回 一個在區間[0,1)內均勻分布的偽隨機實數;只使用一個整數參數m時,返回一個在區間[1, m]內均勻分布的偽隨機整數;使用兩個整數參數時,返回一個在區間[m, n]內均勻分布的偽隨機整數 |
| math.randomseed (x) | 為偽隨機數生成器設置一個種子x,相同的種子將會生成相同的數字序列 |
| math.abs(x) | 返回x的絕對值 |
| math.fmod(x, y) | 返回 x對y取余數 |
| math.pow(x, y) | 返回x的y次方 |
| math.sqrt(x) | 返回x的算術平方根 |
| math.exp(x) | 返回自然數e的x次方 |
| math.log(x) | 返回x的自然對數 |
| math.log10(x) | 返回以10為底,x的對數 |
| math.floor(x) | 返回最大且不大于x的整數 |
| math.ceil(x) | 返回最小且不小于x的整數 |
| math.pi | 圓周率 |
| math.sin(x) | 求弧度x的正弦值 |
| math.cos(x) | 求弧度x的余弦值 |
| math.tan(x) | 求弧度x的正切值 |
| math.asin(x) | 求x的反正弦值 |
| math.acos(x) | 求x的反余弦值 |
| math.atan(x) | 求x的反正切值 |
> 示例代碼:
~~~
print(math.pi) -->output 3.1415926535898
print(math.rad(180)) -->output 3.1415926535898
print(math.deg(math.pi)) -->output 180
print(math.sin(1)) -->output 0.8414709848079
print(math.cos(math.pi)) -->output -1
print(math.tan(math.pi / 4)) -->output 1
print(math.atan(1)) -->output 0.78539816339745
print(math.asin(0)) -->output 0
print(math.max(-1, 2, 0, 3.6, 9.1)) -->output 9.1
print(math.min(-1, 2, 0, 3.6, 9.1)) -->output -1
print(math.fmod(10.1, 3)) -->output 1.1
print(math.sqrt(360)) -->output 18.97366596101
print(math.exp(1)) -->output 2.718281828459
print(math.log(10)) -->output 2.302585092994
print(math.log10(10)) -->output 1
print(math.floor(3.1415)) -->output 3
print(math.ceil(7.998)) -->output 8
~~~
另外使用nath.random()函數獲得偽隨機數時,如果不使用math.randomseed ()設置偽隨機數生成種子或者設置相同的偽隨機數生成種子,那么得得到的偽隨機數序列是一樣的。
> 示例代碼:
~~~
math.randomseed (100) --把種子設置為100
print(math.random()) -->output 0.0012512588885159
print(math.random(100)) -->output 57
print(math.random(100, 360)) -->output 150
~~~
稍等片刻,再次運行上面的代碼。
~~~
math.randomseed (100) --把種子設置為100
print(math.random()) -->output 0.0012512588885159
print(math.random(100)) -->output 57
print(math.random(100, 360)) -->output 150
~~~
兩次運行的結果一樣。為了避免每次程序啟動時得到的都是相同的偽隨機數序列,通常是使用當前時間作為種子。
> 修改上例中的代碼:
~~~
math.randomseed (os.time()) --把100換成os.time()
print(math.random()) -->output 0.88369396038697
print(math.random(100)) -->output 66
print(math.random(100, 360)) -->output 228
~~~
稍等片刻,再次運行上面的代碼。
~~~
math.randomseed (os.time()) --把100換成os.time()
print(math.random()) -->output 0.88946195867794
print(math.random(100)) -->output 68
print(math.random(100, 360)) -->output 129
~~~
- 序
- Lua簡介
- Lua環境搭建
- 基礎數據類型
- 表達式
- 控制結構
- if/else
- while
- repeat
- 控制結構for的使用
- break,return
- Lua函數
- 函數的定義
- 函數的參數
- 函數的返回值
- 函數回調
- 模塊
- String庫
- Table庫
- 日期時間函數
- 數學庫函數
- 文件操作
- 元表
- 面向對象編程
- FFI
- LuaRestyRedisLibrary
- select+set_keepalive組合操作引起的數據讀寫錯誤
- redis接口的二次封裝(簡化建連、拆連等細節)
- redis接口的二次封裝(發布訂閱)
- pipeline壓縮請求數量
- script壓縮復雜請求
- LuaCjsonLibrary
- json解析的異常捕獲
- 稀疏數組
- 空table編碼為array還是object
- 跨平臺的庫選擇
- PostgresNginxModule
- 調用方式簡介
- 不支持事務
- 超時
- 健康監測
- SQL注入
- LuaNginxModule
- 執行階段概念
- 正確的記錄日志
- 熱裝載代碼
- 阻塞操作
- 緩存
- sleep
- 定時任務
- 禁止某些終端訪問
- 請求返回后繼續執行
- 調試
- 調用其他C函數動態庫
- 我的lua代碼需要調優么
- 變量的共享范圍
- 動態限速
- shared.dict 非隊列性質
- 如何添加自己的lua api
- 正確使用長鏈接
- 如何引用第三方resty庫
- 使用動態DNS來完成HTTP請求
- 緩存失效風暴
- Lua
- 下標從1開始
- 局部變量
- 判斷數組大小
- 非空判斷
- 正則表達式
- 不用標準庫
- 虛變量
- 函數在調用代碼前定義
- 抵制使用module()函數來定義Lua模塊
- 點號與冒號操作符的區別
- 測試
- 單元測試
- API測試
- 性能測試
- 持續集成
- 灰度發布
- web服務
- API的設計
- 數據合法性檢測
- 協議無痛升級
- 代碼規范
- 連接池
- c10k編程
- TIME_WAIT問題
- 與Docker使用的網絡瓶頸
- 火焰圖
- 什么時候使用
- 顯示的是什么
- 如何安裝火焰圖生成工具
- 如何定位問題