如何在swift中实现arcus cotangent(arccot)? 导入需要哪个库,我正在使用达尔文,但在试图打电话时我看不到它?
答案 0 :(得分:6)
如果您
,可以在Swift中使用<math.h>
中的所有数学函数
import Darwin
(如果您导入Foundation或UIKit,则会自动导入)。
现在反余切函数不在其中,但你可以使用关系从atan()
轻松计算它(参见例如Inverse trigonometric functions):
arccot(x) = arctan(1/x) (for x > 0)
arccot(x) = π + arctan(1/x) (for x < 0)
arccot(x) = π/2 - atan(x)
前两个公式在数值上更适合 large
(绝对)值
x
的最后一个,对于小值更好,
所以你可以将acot()
函数定义为
func acot(x : Double) -> Double {
if x > 1.0 {
return atan(1.0/x)
} else if x < -1.0 {
return M_PI + atan(1.0/x)
} else {
return M_PI_2 - atan(x)
}
}
对于 Swift 3 ,请M_PI
替换Double.pi
。
答案 1 :(得分:1)
在Swift 4(iOS 11,Xcode 9)中
放置import UIKit
后,您可以编写如下内容:
let fi = atan(y/x)
CGFloat类型的x和y(在我的情况下)。
或者,如果需要,您可以使用其他人:
let abc = acos(y)
数学函数适用于参数类型:Double,Float,CGFloat等。