计算PI [英] Calcutate PI

查看:101
本文介绍了计算PI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我对C语言编程很陌生.现在,我必须计算pi的值.接近pi应该是这样的:
pi = 4 *(1-1/3 + 1/5-1/7 + 1/9-1/11 + 1/13 ...)

有人可以帮我怎么做吗?

Thx

Dear all,

I''m new with programming C... Now I''ve to calculate the value of pi. It should be like this to approach pi:
pi = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ... )

Can anyone help me how to do it?

Thx

推荐答案

您可以使用标志来确定是要添加还是减去下一个值.

我将把实施权交给您;)

Dave
You can use a flag to determine if you are adding or subtracting the next value.

I will leave the implementation up to you ;)

Dave


关于像这样计算pi的有趣之处在于如何终止循环.理想情况下,您要继续累积项,直到(在所需的有效数字的数量之内)在该数目的数字内第n个和第(n + 2)个术语相同为止.

无论如何,我都会回到您的教科书或讲义上,来看看如何控制循环,如何声明和修改变量,然后尝试将某些东西结合在一起.在执行操作时打印每个术语,然后将其张贴到此处,如果它不起作用,则有人会尝试引导您完成您不了解的部分.

干杯,



PS:由于这是一项家庭作业,请记住CS讲师有一个递归恋物癖,因此,如果您已经涵盖了递归,请确保使用它,但是在特定问题的情况下这是不合适的!
The interesting point about calculating pi like this is how to terminate the loop. Ideally you want to keep accumulating terms until (within the number of significant figures you want) the nth and (n+2)th terms are the same within that number of figures.

Anyway I''d go back to your textbook or lecture notes and have a look at how you control loops, how you declare and modify variables and then try and bolt something together. Print each term as you do it and then post what you''ve got here if it''s not working and someone will try and guide you through the bits you''re not getting.

Cheers,

Ash

PS: As this is a homework assignment, remember CS lecturers have a recursion fetish so if you''ve covered recursion make sure you use it, however inappropriate it is in the context of a particular problem!


在c#中,Ystem.Math类具有PI属性.您可以按原样使用它
In c# Ystem.Math class has a PI property. You can use that as is


这篇关于计算PI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆