试图计算圆周率,我在做什么错? math.Cos可能有问题吗?

我在直径1的圆内绘制一个正方形,正方形的对角线是该圆的直径 。 然后,我使用余弦定律将此正方形分成4个直角三角形,并知道三角形上a和b的长度为0.5,我创建了4个三角形,其斜边加在一起形成正方形的周长。 给我们方程式周长=边数*(a ^ 2 + b ^ 2 -2abcos(360 /边数))通过增加此形状上的边数,周长越来越接近圆的周长( 3.14)。</ p>

我以前在python中完成过此操作,并且可以正常工作,但是在度数上使用余弦定律而不是在python中使用rad会造成麻烦。</ </> p>

 包main 

import“ fmt”
import“ math”

func main(){

for n:= float64(4); n == n; n * = 2 {
fmt.Println(n)
c:= math.Pow(0.5-(0.5 * math.Cos(360 / n)),0.5)
fmt.Println(c * n)
}

}
</ code> </ pre>

答案应该从大约3开始,然后上升到接近3.14,但答案改为上升到180。 我已经一遍又一遍地检查了数学,但是我认为这不是我在做什么,这是语言的问题。</ p>
</ div>

展开原文

原文

I'm drawing a square inside a circle of diameter 1, the diagonal of the square is the diameter of the circle. I then split this square into 4 right angled triangles, using cosine law and knowing that the lengths of a and b on the triangle are 0.5, I create 4 triangles whose hypotenuses add together to form the perimeter of the square. Giving us the equation perimeter = number of sides * (a^2 + b^2 -2abcos(360 / number of sides)) By increasing the number of sides on this shape the perimeter gets closer and closer to the perimeter of the circle (3.14).

I've done this in python before, and it worked, but there was a problem with using cosine law on degrees instead of rad in python that messed it up.

package main

import "fmt"
import "math"

func main() {

    for n := float64(4) ; n == n; n *= 2 {
        fmt.Println(n)
        c := math.Pow(0.5 - (0.5 * math.Cos(360 / n)), 0.5)
        fmt.Println(c * n)
    }

}

The answer should start at about 3, and go up approaching 3.14, but instead the answer goes up to 180 instead. I've checked my math over and over again, but I think it's a problem with the language not what I am doing.

dongyirong3564
dongyirong3564 如@SteffenUllrich所写,您必须编写math.Cos(2*math.Pi/n)才能将度数转换为弧度。这将导致迭代收敛到Pi一段时间,之后可能由于@kostix倾斜的原因而转移。
11 个月之前 回复
doushuangdui5419
doushuangdui5419 360是一个以度为单位的完整圆,这意味着您可以以度为单位计算角度,然后将其放入math.Cos中。但是math.Cos期望弧度,即您需要使用2*pi而不是360。本质上,然后输入pi来计算pi。“...但是在度数上使用余弦定律而不是在python中使用rad会造成麻烦。”-是的,这里的问题完全一样。
11 个月之前 回复
doumingchen3628
doumingchen3628 我认为这是浮点数gui.de
11 个月之前 回复

1个回答

Don't use the standard function Cos - as many people have said, to use that you need to already know the value of pi because it takes its argument in radians.

Most computer languages' trigonometric functions use radians as their arguments. This is because there are nice, simple formulas for approximating trig. functions if you use radians.

In any case though, here you don't need it! You're doubling the value of n each time, so you can use the well-known half-angle formula for cosine:

package main

import "fmt"
import "math"

func main() {
    cosVal := float64(-1) // Start at cosine of 180 degrees
    for n := 4 ; n < 5000; n *= 2 {
        fmt.Println(n)
        cosVal = math.Sqrt(0.5 * (cosVal + 1.0))
        c := math.Pow(0.5 - 0.5*cosVal, 0.5)
        fmt.Println(c * float64(n))
    }

}

This converges as desired.


Incidentally, note that initially you wanted something that converged to half a circle in radians (that is, to pi), but instead got something that converged to half a circle in degrees (that is, to 180). This is precisely because you handed degrees to Cos when you should have handed it radians.

doujiaoben28596
doujiaoben28596 是的,你可以。 我故意使此代码尽可能与原始代码相似。
10 个月之前 回复
dongyouzhi7218
dongyouzhi7218 您不能使用math.Sqrt()代替math.Pow(...,0.5)吗?
10 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问