用VB子过程,算sin(x),为啥总是不对啊啊T TPrivate Sub Command1_Click()Picture1.ClsDim n As Integer, x As Singlen = Val(Text1.Text)x = Val(Text2.Text)Picture1.Print mysin(n, x)End SubPrivate Function mysin(ByVal n%, ByVal x!) As DoubleDim d
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/30 07:41:44
用VB子过程,算sin(x),为啥总是不对啊啊T TPrivate Sub Command1_Click()Picture1.ClsDim n As Integer, x As Singlen = Val(Text1.Text)x = Val(Text2.Text)Picture1.Print mysin(n, x)End SubPrivate Function mysin(ByVal n%, ByVal x!) As DoubleDim d
用VB子过程,算sin(x),为啥总是不对啊啊T T
Private Sub Command1_Click()
Picture1.Cls
Dim n As Integer, x As Single
n = Val(Text1.Text)
x = Val(Text2.Text)
Picture1.Print mysin(n, x)
End Sub
Private Function mysin(ByVal n%, ByVal x!) As Double
Dim d As Double, s As Double
d = 1: s = 0: a = 1
For i = 1 To n
For j = 1 To (2 * i - 1)
d = d * j
Next j
a = ((-1) ^ (i - 1)) * (x ^ (2 * i - 1)) / d
If i = n And a < 0.00001 Then Exit For
s = s + a
Next i
mysin = s
End Function
比如我用n=15 x=3.14 可算出来sinx竟然是-1.569……
改了好几遍,应该没错了呀.
只有20分,全给了.谢谢啊 .
用VB子过程,算sin(x),为啥总是不对啊啊T TPrivate Sub Command1_Click()Picture1.ClsDim n As Integer, x As Singlen = Val(Text1.Text)x = Val(Text2.Text)Picture1.Print mysin(n, x)End SubPrivate Function mysin(ByVal n%, ByVal x!) As DoubleDim d
楼主程序写得不错,但是有一些小问题
d = 1:s = 0:a = 1
For i = 1 To n
For j = 1 To (2 * i - 1)
d = d * j
Next j
这里d的初值放的位置有问题,应该像下面这样放
还有
If i = n And a < 0.00001 Then Exit For
应该改成
If i = n And abs(a) < 0.00001 Then Exit For
其中的差别你用sin(2,3.14)对比一下就知道了.
经过测试sin(n,3.14)当n>6后,其值趋于正值且精确度高.
如sin(7,3.14)=1.61353113944622E-03=1.61353*10^(-3)
嗯 就这样吧!满意请给分,哈哈!