ios - Arcus 余切在 Swift 中的实现

标签 ios swift math trigonometry

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/

相关文章:

iphone - 为什么 OpenEars 在我的 XCode 应用程序中出现 50 多个错误?

ios - 取代 iPhone UDID 的供应商 ID 和广告 ID 有多久?

ios - UICollectionViewCell 中的变量更改为不同的单元格

java - 两个不同数组的最大算术序列

powershell - PowerShell无法在某些系统上划分值

ios - 尝试将我的 UIImage 裁剪为 1 :1 aspect ratio (square) but it keeps enlarging the image causing it to be blurry. 为什么?

ios 13 推送通知在设备锁定后立即被清除

ios - Swift 一次删除多个对象解析服务器

ios - 在 SwiftUI 中使用 List 时如何使用 NavigationLink isActive 绑定(bind)?

python - 如何让用户使用 "sqrt()"+ 输入求输入的平方根?