I'm doing a program that aproximate PI and i'm trying to use long long, but it isn't working. Here is the code
#include<stdio.h>
#include<math.h>
typedef long long num;
main(){
num pi;
pi=0;
num e, n;
scanf("%d", &n);
for(e=0; 1;e++){
pi += ((pow((-1.0),e))/(2.0*e+1.0));
if(e%n==0)
printf("%15lld -> %1.16lld\n",e, 4*pi);
//printf("%lld\n",4*pi);
}
}
scanf()
statement needs to use%lld
too.There are far too many parentheses and far too few spaces in the expression
int
formain()
.int main(void)
when it ignores its arguments, though that is less of a categorical statement than the rest.main()
and don't use it myself; I writereturn 0;
to be explicit.I think the whole algorithm is dubious when written using
long long
; the data type probably should be more likelong double
(with%Lf
for thescanf()
format, and maybe%19.16Lf
for theprintf()
formats.First of all, %d is for a
int
So
%1.16lld
makes no sense, because %d is an integerThat typedef you do, is also unnecessary, use the type straight ahead, makes a much more readable code.
What you want to use is the type
double
, for calculating pi and then using%f
or%1.16f
.%lld
is the standard C99 way, but that doesn't work on the compiler that I'm using (mingw32-gcc v4.6.0). The way to do it on this compiler is:%I64d
So try this:
and
The only way I know of for doing this in a completely portable way is to use the defines in
<inttypes.h>
.In your case, it would look like this:
It really is very ugly... but at least it is portable.