|
6鱼币
- #include <stdio.h>
- #include <math.h>
- int main(void)
- {
- int i;
- int j = 1;
- double result = 1;
- long double answer = 0;
-
- for (j = 1;result >= 1e-8; j++)
- {
- result = 1.0/(2 * (double)j - 1);
- //printf("%.8f\n",result);
- if (j % 2)
- i = 1;
- else
- i = -1;
- answer += i * result;
- }
-
- printf("PI的近似值是%9.7f",answer * 4);
-
- return 0;
- }
复制代码
- #include <stdio.h>
- int main()
- {
- int i;
- double result = 0;
- double last =1;
- int n;
-
- for(n = 1;last >= 1e-8; n++ )
- {
- if(n % 2)
- {
- i = 1;
- }
- else
- {
- i = -1;
- }
- result = result + i * (1 / (2 * (double)n - 1));
- last = (1 / (2 * (double)n - 1));
- }
- printf("%9.7f",result * 4);
- return 0;
- }
复制代码
怎么结果完全不一样?
- #include <stdio.h>
- #include <math.h>
- int main(void)
- {
- int i;
- int j = 1;
- double result = 1;
- long double answer = 0;
-
- for (j = 1;result >= 1e-8; j++)
- {
- result = 1.0/(2 * (double)j - 1);
- //printf("%.8f\n",result);
- if (j % 2)
- i = 1;
- else
- i = -1;
- answer += i * result;
- }
-
- //printf("PI的近似值是%9.7f",answer * 4);
- printf("PI的近似值是%9.7Lf\n",answer * 4);
-
- return 0;
- }
复制代码
|
|