c如何输出高精度浮点型数
环境:cygwin64的gcc
(mingw64的gcc不行)
测试只能支持小数点后21位
1 2 3 4 5 6 7 8 9 10 11 | #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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库