如何打印 long long

发布于 2024-11-16 14:40:11 字数 387 浏览 2 评论 0原文

我正在做一个近似 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云归处 2024-11-23 14:40:11

%lld 是标准 C99 方式,但这不适用于我正在使用的编译器 (mingw32-gcc v4.6.0)。在此编译器上执行此操作的方法是: %I64d

所以尝试一下:

if(e%n==0)printf("%15I64d -> %1.16I64d\n",e, 4*pi);

并且

scanf("%I64d", &n);

我知道以完全可移植的方式执行此操作的唯一方法是使用

在你的情况下,它看起来像这样:

scanf("%"SCNd64"", &n);
//...    
if(e%n==0)printf("%15"PRId64" -> %1.16"PRId64"\n",e, 4*pi);

它确实非常丑陋......但至少它是便携式的。

%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:

if(e%n==0)printf("%15I64d -> %1.16I64d\n",e, 4*pi);

and

scanf("%I64d", &n);

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:

scanf("%"SCNd64"", &n);
//...    
if(e%n==0)printf("%15"PRId64" -> %1.16"PRId64"\n",e, 4*pi);

It really is very ugly... but at least it is portable.

神也荒唐 2024-11-23 14:40:11
  • 您的 scanf() 语句也需要使用 %lld
  • 您的循环没有终止条件。
  • 表达式中括号太多且空格太少

    pi += pow(-1.0, e) / (2.0*e + 1.0);
    
  • 在循环的第一次迭代中添加 1,然后将 0 添加到“pi”的值;这不会改变该值太多。
  • 您应该对 main() 使用显式返回类型 int
  • 总的来说,当它忽略其参数时,最好指定 int main(void),尽管这不像其他语句那么明确。
  • 我不喜欢 C99 中授予的显式许可,即省略 main() 末尾的返回,并且我自己不使用它;为了明确起见,我写了 return 0;

我认为使用long long编写时整个算法是可疑的;数据类型可能应该更像 long double (使用 %Lf 表示 scanf() 格式,也许 %19.16Lf 用于 printf() 格式。

  • Your scanf() statement needs to use %lld too.
  • Your loop does not have a terminating condition.
  • There are far too many parentheses and far too few spaces in the expression

    pi += pow(-1.0, e) / (2.0*e + 1.0);
    
  • You add one on the first iteration of the loop, and thereafter zero to the value of 'pi'; this does not change the value much.
  • You should use an explicit return type of int for main().
  • On the whole, it is best to specify int main(void) when it ignores its arguments, though that is less of a categorical statement than the rest.
  • I dislike the explicit licence granted in C99 to omit the return from the end of main() and don't use it myself; I write return 0; to be explicit.

I think the whole algorithm is dubious when written using long long; the data type probably should be more like long double (with %Lf for the scanf() format, and maybe %19.16Lf for the printf() formats.

故事未完 2024-11-23 14:40:11

首先, %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 integer

That 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.

苍白女子 2024-11-23 14:40:11
    // acos(0.0) will return value of pi/2, inverse of cos(0) is pi/2 
    double pi = 2 * acos(0.0);
    int n; // upto 6 digit
    scanf("%d",&n); //precision with which you want the value of pi
    printf("%.*lf\n",n,pi); // * will get replaced by n which is the required precision
    // acos(0.0) will return value of pi/2, inverse of cos(0) is pi/2 
    double pi = 2 * acos(0.0);
    int n; // upto 6 digit
    scanf("%d",&n); //precision with which you want the value of pi
    printf("%.*lf\n",n,pi); // * will get replaced by n which is the required precision
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文