用Machin公式计算圆周率的源程序

用Machin公式计算圆周率的源程序

 
/*  Program to compute PI, by Jason Chen, May 1999
**
**  Open VC++ IDE, new a win32 console application project,
**  add this file to the project and build it.
**
**  Homepage : http://www.jason314.com
**  Email    : jason@szonline.net
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <io.h>
int     *arctg5, *arctg239, *tmp;
int     DecLen, BinLen;
int     x, n, sign, NonZeroPtr;
int     Step;
char    fn_status[]   = "~pi_sts.___";
char    fn_arctg5[]   = "~atg5.___";
char    fn_arctg239[] = "~atg239.___";
char    fn_tmp[]      = "~tmp.___";
time_t  TotalTime, time1, time2;


void __cdecl FirstDiv(int *arctg_array)
{
    __asm {
        mov esi, arctg_array
        mov dword ptr [esi], 1
        xor edx, edx
        mov ebx, x
        mov ecx, BinLen
fd1:    mov eax, [esi]
        div ebx
        mov [esi], eax
        add esi, 4
        loop fd1
        mov esi, arctg_array
        mov edi, tmp
        mov ecx, BinLen
        pushf
        cld
        rep movsd
        popf
    }
}


void __cdecl arctgx(int *arctg_array)
{
    int         NonZeroBytePtr;
    int         key;
    FILE        *fp;
    for (;NonZeroPtr<BinLen;) {
        NonZeroBytePtr = NonZeroPtr * 4;
        if (_kbhit()) {
            key=_getch();
            if (key==0 || key==0xe0) _getch();
            if (key=='p') {
                printf("Swap data to disk .../n");
                if (x==25)
                    Step = 1;
                else
                    Step = 2;
                time(&time2);
                TotalTime += time2 - time1;
                if ((fp=fopen(fn_status,"wt"))==NULL) {
                    printf("Create file %s error!!/n", fn_status);
                    exit(0);
                }
                fprintf(fp, "%d %d %d %d %d %d %d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你想让我帮你解决关于函数模块和循环的问题。关于使用 Machin 公式计算圆周率,我可以给你一些指导。 首先,让我们了解一下 Machin 公式Machin 公式是一种用于计算圆周率公式,它的公式如下: π / 4 = 4 × arctan(1/5) - arctan(1/239) 其中,arctan 表示反正切函数。 实际上,这个公式是由丹尼尔·贝尔努利(Daniel Bernoulli)在 1655 年发现的,但由于其后被约翰·马钦(John Machin)在 1706 年重新发现,所以这个公式就被称为 Machin 公式。 接下来,我们可以将这个公式转化为计算机程序的形式。在程序中,我们需要使用反正切函数和循环来计算圆周率。 下面是一个使用 Python 语言实现 Machin 公式计算圆周率的示例代码: ```python import math def machin_formula(n): pi_sum = 0 for i in range(n): pi_sum += ((-1) ** i) / (2 * i + 1) * (4 / 5) ** (2 * i + 1) pi_sum -= ((-1) ** i) / (2 * i + 1) * (1 / 239) ** (2 * i + 1) pi = 4 * pi_sum return pi n = 1000 pi = machin_formula(n) print("圆周率的为:", pi) print("与 math 模块中的相差:", abs(math.pi - pi)) ``` 在这个程序中,我们定义了一个名为 `machin_formula` 的函数,它接受一个参数 `n`,表示计算的精度。函数使用了一个循环,从 0 到 `n-1`,Machin 公式计算圆周率。 你可以尝试修改 `n` 的,来得到不同精度的圆周率。当然,越大,计算时间就越长。 最后,我们使用 `print` 函数来输计算的圆周率,并与 Python 内置的 `math` 模块中的进行比较,以验证我们的程序的正确性。 希望这个示例能够对你有所帮助,如果你有任何问题,随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值