1、取整數(shù)
向上取整數(shù)
ceil(_: Double) -> Double
向下取整數(shù)
floor(_: Double) -> Double
四舍五入取整數(shù)
round(_: Double) -> Double
直接去掉數(shù)字的小數(shù)部分
trunc(_: Double) -> Double
let floatNum = 3.1415926
print("ceil = \(ceil(floatNum)), floor = \(floor(floatNum)), round = \(round(floatNum))")
print("trunc\(trunc(-6.534))")
輸出:
ceil = 4.0邪驮, floor = 3.0, round = 3.0
trunc = 6.0
2、求絕對(duì)值
浮點(diǎn)數(shù)求絕對(duì)值
fabs(_: Double) -> Double
整數(shù)求絕對(duì)值
func abs<T>(_ x: T) -> T where T : Comparable, T : SignedNumeric
此函數(shù)可以根據(jù)T的類型傲茄,求對(duì)應(yīng)的絕對(duì)值
let oneNum = -3.14
let oneInt = -4
print("fabs = \(fabs(oneNum)) abs = \(abs(oneInt))")
輸出:fabs = 3.14 abs = 4
3毅访、指數(shù)與對(duì)數(shù)
求取自然數(shù)e的冪
exp(_ x: Double) -> Double
開平方
sqrt(_: Double) -> Double
求以e為底的對(duì)數(shù)
log(_ x: Double) -> Double
求以2為底的對(duì)數(shù)
log2(_ x: Double) -> Double
求以10為底的對(duì)數(shù)
log10(_ x: Double) -> Double
計(jì)算以x為底數(shù)的y次冪
pow(_: Double, _: Double) -> Double
let twoNum = 2.0
print("exp = \(exp(twoNum)), sqrt = \(sqrt(twoNum))\n log = \(log(twoNum)), log2 = \(log2(twoNum)), log10 = \(log10(twoNum)) \npow = \(pow(twoNum, 3))")
輸出:
exp = 7.38905609893065, sqrt = 1.4142135623730951
log = 0.6931471805599453 log2 = 1.0 log10 = 0.3010299956639812
pow = 8.0