vb编写程序,利用下面的公式计算cosx的近似值

vb编写程序,利用下面的公式计算cosx的近似值
cosx=1-x^2/2!+x^4/4!.+x^(2n)/(2n)!我写的代码:
Private Sub Command1_Click()
Dim x As Single,t As Single,s As Single,n As Integer
x = Val(Text1.Text)
s = 1:t = 1:n = 1
Do
t = (-1) * t * (x ^ (2 * n)) / ((2 * n - 1) * (2 * n))
s = t + s
n = n + 1
Loop Until t = -10 ^ (-7)
Text2.Text = s
但是“溢出”,= (-1) * t * (x ^ (2 * n)) / ((2 * n - 1) * (2 * n)),但是我不明白为什么
wwas 1年前 已收到1个回答 举报

hxt331 花朵

共回答了14个问题采纳率:92.9% 举报

'cosx=1-x^2/2!+x^4/4!.+x^(2n)/(2n)!我写的代码:
Private Sub Command1_Click()
Dim x As Double, t As Double, s As Double, n As Integer, jc As Double
x = Val(Text1.Text)
s = 1: t = 1: n = 1
Do
jc = 1
For i = 1 To 2 * n
jc = jc * i
Next i
t = (-1) ^ n * (x ^ (2 * n)) / jc'这里是阶乘,你用的公式是连加的公式
s = t + s
n = n + 1
Loop Until t <= 10 ^ (-7) And t >= -10 ^ (-7)
Text2.Text = s
End Su

1年前 追问

4

wwas 举报

溢出的问题解决了,但是text2,显示不出0,比如0.5,显示.5,这咋办,我觉得跟s的数据类型或者text2有关

举报 hxt331

Private Sub Command1_Click()
Dim x As Double, t As Double, s As Double, n As Integer, jc As Double
x = Val(Text1.Text)
s = 1: t = 1: n = 1
Do
jc = 1
For i = 1 To 2 * n
jc = jc * i
Next i
t = (-1) ^ n * (x ^ (2 * n)) / jc '这里是阶乘,你用的公式是连加的公式
s = t + s
n = n + 1
Loop Until t <= 10 ^ (-7) And t >= -10 ^ (-7)
Text2.Text = Format(s, "0.######") '主要是这句
End Sub
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 17 q. 0.023 s. - webmaster@yulucn.com