c如何输出高精度浮点型数
环境:cygwin64的gcc
(mingw64的gcc不行)
测试只能支持小数点后21位
#include<math.h> int main(int argc, char const *argv[]) { printf("%.19lf\n",cos(3.1415926/3)); printf("%.49f\n",1.3456789123456789123456789123456789123456789123456789); printf("%.49lf\n",1.3456789123456789123456789123456789123456789123456789); long double ld=1.3456789123456789123456789123456789123456789123456789L; printf("%.49Lf\n",ld); printf("%.49llf\n",1.3456789123456789123456789123456789123456789123456789); return 0; }
输出
0.5000000154700405819
1.3456789123456789347699213976738974452018740000000
1.3456789123456789347699213976738974452018740000000
1.3456789123456789123269364272239556612476010000000
1.3456789123456789347699213976738974452018740000000
其他测试
int main(int argc, char const *argv[]) { long double ld=1.23L; printf("1%Lf\n",ld); double d=1.3456789123; printf("2%f\n",d); printf("3%lf\n",d); printf("4%llf\n",d); long double ld2=(long double)1.3456789123L; printf("5%f\n",ld2); printf("6%lf\n",ld2); printf("7%Lf\n",ld2); printf("8%llf\n",ld2); printf("9%f\n",(1.3456789123L)); printf("0%lf\n",1.3456789123L); printf("1%Lf\n",1.3456789123L); printf("1%.15Lf\n",1.3456789123L); printf("2%llf\n",1.3456789123L); printf("3%.10f\n",1.3456789123L); printf("4%lf\n",1.3456789123L);//Lf printf("5%.15Lf\n",1.3456789123L); printf("6%llf\n",1.3456789123L);// llf不行 long double x = 1.3456789123; printf("7%.15Lf\n", x); printf("%.15Lf %d %d %d %d\n",ld,sizeof(ld),sizeof(1e-7L),sizeof((long double)1e-7),sizeof(1e-7)); return 0; }
输出:
04.c: 在函数‘main’中:
04.c:4:5: 警告:隐式声明函数‘printf’ [-Wimplicit-function-declaration]
4 | printf("1%Lf\n",ld);
| ^~~~~~
04.c:4:5: 警告:隐式声明与内建函数‘printf’不兼容
04.c:1:1: 附注:include ‘<stdio.h>’ or provide a declaration of ‘printf’
+++ |+#include <stdio.h>
1 | int main(int argc, char const *argv[])
11.230000
21.345679
31.345679
41.345679
50.000000
60.000000
71.345679
80.000000
90.000000
00.000000
11.345679
11.345678912300000
20.000000
30.0000000000
40.000000
51.345678912300000
60.000000
71.345678912300000
1.230000000000000 16 16 16 8