arcus cotangent(arccot)如何在swift中实现? 需要导入哪个库,我正在使用 Darwin 但在尝试调用时看不到它?
最佳答案
<math.h>
中的所有数学函数如果你可以在 Swift 中使用
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)
前两个公式在数值上更适合大
(绝对)值
的 x
,最后一个更适合 small 值,
所以你可以定义你的 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
.
关于ios - Arcus 余切在 Swift 中的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394092/