VB中定义Arccos函数
我是这样的:Private Function Arccos(x As Double, n As Long)
Dim i As Long, y As Double
For i = 1 To n
y = y + (-1 / Sqr(1 + (x / n * i) ^ 2)) * x / n
Next
Arccos = y
End Function
用的是Arccos的微分来算近似值,怎么不对?
公式对就没有问题拉 Const pi As Double = 3.14159265359
Private Function Arccos(x As Double, n As Long) ' n 为函数精度,建议为100
Select Case x
Case 1
Arccos = 0
Case -1
Arccos = pi
Case -1 To 1
Dim dx As Double, y As Double, i As Long
dx = x / n
For i = 1 To n
y = y + (-1 / Sqr(1 - (dx * i + dx / 2) ^ 2)) * dx
Next
Arccos = y + pi / 2
Case Else
'异常
End Select
End Function
搞定了
页:
[1]