游侠无极限 发表于 2003-4-18 01:11:00

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的微分来算近似值,怎么不对?

煎饼 发表于 2003-4-19 22:04:00

公式对就没有问题拉

游侠无极限 发表于 2003-4-24 22:58:00

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]
查看完整版本: VB中定义Arccos函数