如何打印 long long
我正在做一个近似 PI 的程序,我试图使用 long long,但它不起作用。 这是代码
#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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
%lld
是标准 C99 方式,但这不适用于我正在使用的编译器 (mingw32-gcc v4.6.0)。在此编译器上执行此操作的方法是:%I64d
所以尝试一下:
并且
我知道以完全可移植的方式执行此操作的唯一方法是使用
。在你的情况下,它看起来像这样:
它确实非常丑陋......但至少它是便携式的。
%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.
scanf()
语句也需要使用%lld
。表达式中括号太多且空格太少
main()
使用显式返回类型int
。main()
末尾的返回,并且我自己不使用它;为了明确起见,我写了return 0;
。我认为使用
long long
编写时整个算法是可疑的;数据类型可能应该更像long double
(使用%Lf
表示scanf()
格式,也许%19.16Lf
用于printf()
格式。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.首先, %d 是一个
int
所以
%1.16lld
没有意义,因为 %d 是一个整数你所做的 typedef ,也是不必要的,使用 type直接前进,使代码更具可读性。
您要使用的是类型
double
,用于计算 pi然后使用
%f
或%1.16f
。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 piand then using
%f
or%1.16f
.