为什么printf没有使用科学记数法?

时间:2021-01-03 22:34:03

I understand that this is a common problem. However I can't find a solid straight answer.

我知道这是一个常见的问题。但是我找不到一个可靠的直接答案。

16 ^ 54 = 1.0531229167e+65 (this is the result I want)

When I use pow(16,54), I get:

当我使用pow(16,54)时,我得到:

105312291668557186697918027683670432318895095400549111254310977536.0

Code is as follows:

代码如下:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main(){

   double public;
   double a = 16;
   double b = 54;
   public = (pow(a,b));

   printf("%.21f\n", public);
}

Code executed with:

执行代码:

gcc main.c -lm

gcc main.c -lm

What I'm doing wrong?

我做错了什么?

3 个解决方案

#1


18  

What am I doing wrong?

我究竟做错了什么?

Several things:

  • Use %.10e format for scientific notation with printf for a printout with ten digits after the dot,
  • 使用%.10e格式的科学记数法,printf用于点后10位数的打印输出,

  • Return an int from your main,
  • 从你的主要回来一个int,

  • Consider not using public to name a variable, on the chance that your program would need to be ported to C++, where public is a keyword.
  • 考虑不使用public命名变量,因为您的程序需要移植到C ++,其中public是关键字。

Here is how you can fix your program:

以下是修复程序的方法:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(){

   double p;
   double a = 16;
   double b = 54;
   p = (pow(a,b));

   printf("%.10e\n", p);
   return 0;
}

Demo on ideone.

在ideone上演示。

#2


7  

Have you tried:

你有没有尝试过:

printf("%e\n", public);

The %e specifier is for scientific notation, as described in the documentation

%e说明符用于科学记数法,如文档中所述

#3


3  

If you need scientific notation you need to use the %e format specifier:

如果您需要科学记数法,则需要使用%e格式说明符:

printf("%e\n", public);
        ^^   

Also, public is a keyword in C++ and so it would be a good idea to avoid that and any other keywords in case this code needs to be portable.

此外,public是C ++中的关键字,因此,如果此代码需要可移植,最好避免使用该关键字和任何其他关键字。

#1


18  

What am I doing wrong?

我究竟做错了什么?

Several things:

  • Use %.10e format for scientific notation with printf for a printout with ten digits after the dot,
  • 使用%.10e格式的科学记数法,printf用于点后10位数的打印输出,

  • Return an int from your main,
  • 从你的主要回来一个int,

  • Consider not using public to name a variable, on the chance that your program would need to be ported to C++, where public is a keyword.
  • 考虑不使用public命名变量,因为您的程序需要移植到C ++,其中public是关键字。

Here is how you can fix your program:

以下是修复程序的方法:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(){

   double p;
   double a = 16;
   double b = 54;
   p = (pow(a,b));

   printf("%.10e\n", p);
   return 0;
}

Demo on ideone.

在ideone上演示。

#2


7  

Have you tried:

你有没有尝试过:

printf("%e\n", public);

The %e specifier is for scientific notation, as described in the documentation

%e说明符用于科学记数法,如文档中所述

#3


3  

If you need scientific notation you need to use the %e format specifier:

如果您需要科学记数法,则需要使用%e格式说明符:

printf("%e\n", public);
        ^^   

Also, public is a keyword in C++ and so it would be a good idea to avoid that and any other keywords in case this code needs to be portable.

此外,public是C ++中的关键字,因此,如果此代码需要可移植,最好避免使用该关键字和任何其他关键字。