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;
}
View Code
复制代码

输出:

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

posted @   败人两字非傲即惰  阅读(82)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示