#include <math.h> #include <stdio.h> float fun ( float num ) { int s ; float n, t, pi ; t = 1 ; pi = 0 ; n = 1 ; s = 1 ; /**************found**************/ while(fabs(t) >= num) { pi = pi + t ; n = n + 2 ; s = -s ; /**************found**************/ t = s/ n ; } pi = pi * 4 ; return pi ; } main( ) { float n1, n2 ; printf("Enter a float number: ") ; scanf("%f", &n1) ; n2 = fun(n1) ; printf("%6.4f\n", n2) ; }
下列给定程序中函数fun的功能是:用下面的公式求π的近似值,直到最后一项的绝对值小于指定的数为止,π/4=1-1/3+1/5-1/7+...,例如,程序运行后,输入0.0001,程序输出3.1414
原文:http://www.cnblogs.com/jun699701/p/7554112.html