c语言的long double怎么输出啊

zhangqiang423 2009-08-09 08:59:52
找了很多地方都说用printf("%lf",a)输出 试了试错误的 float用%f double用%ld 那long double用该用什么啊
...全文
8497 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
btw616 2010-06-11
  • 打赏
  • 举报
回复
顶二楼的~~~
yangs2000 2009-08-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sunjiankirk 的回复:]
double 是用%lf吧,
long double 还是第一次见
[/Quote]

%f就是double吧? 没有float的printf,float好像都是转换成double作为形参的。
yangs2000 2009-08-09
  • 打赏
  • 举报
回复
在C标准中规定(ISO/IEC 9899:1999 (E))是 La,Le,Lf,Lg 4个表示法
L Specifies that a following a, A, e, E, f, F, g, or G conversion specifier
applies to a long double argument.

必须是大写。小写l是下面的意思
l (ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument; that a following n conversion specifier applies to a pointer to a long int argument; that afollowing c conversion specifier applies to a wint_t argument; that a following s conversion specifier applies to a pointer to a wchar_t argument; or has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.

afeg的意思如下
f,F A double argument representing a floating-point number is converted to
decimal notation in the style [−]ddd.ddd, where the number of digits after
the decimal-point character is equal to the precision specification. If the
precision is missing, it is taken as 6; if the precision is zero and the # flag is
not specified, no decimal-point character appears. If a decimal-point
character appears, at least one digit appears before it. The value is rounded to
the appropriate number of digits.
A double argument representing an infinity is converted in one of the styles
[-]inf or [-]infinity — which style is implementation-defined. A
double argument representing a NaN is converted in one of the styles
[-]nan or [-]nan(n-char-sequence) — which style, and the meaning of
any n-char-sequence, is implementation-defined. The F conversion specifier
produces INF, INFINITY, or NAN instead of inf, infinity, or nan,
respectively.234)

e,E A double argument representing a floating-point number is converted in the
style [−]d.ddd e±dd, where there is one digit (which is nonzero if the
argument is nonzero) before the decimal-point character and the number of
digits after it is equal to the precision; if the precision is missing, it is taken as
6; if the precision is zero and the # flag is not specified, no decimal-point
character appears. The value is rounded to the appropriate number of digits.
The E conversion specifier produces a number with E instead of e
introducing the exponent. The exponent always contains at least two digits,
and only as many more digits as necessary to represent the exponent. If the
value is zero, the exponent is zero.
A double argument representing an infinity or NaN is converted in the style
of an f or F conversion specifier.

g,G A double argument representing a floating-point number is converted in
style f or e (or in style F or E in the case of a G conversion specifier), with
the precision specifying the number of significant digits. If the precision is
zero, it is taken as 1. The style used depends on the value converted; style e
(or E) is used only if the exponent resulting from such a conversion is less
than −4 or greater than or equal to the precision. Trailing zeros are removed
from the fractional portion of the result unless the # flag is specified; a
decimal-point character appears only if it is followed by a digit.
A double argument representing an infinity or NaN is converted in the style
of an f or F conversion specifier.

a,A A double argument representing a floating-point number is converted in the
style [−]0xh.hhhh p±d, where there is one hexadecimal digit (which is
nonzero if the argument is a normalized floating-point number and is
otherwise unspecified) before the decimal-point character235) and the number
of hexadecimal digits after it is equal to the precision; if the precision is
missing and FLT_RADIX is a power of 2, then the precision is sufficient for
an exact representation of the value; if the precision is missing and
FLT_RADIX is not a power of 2, then the precision is sufficient to
distinguish236) values of type double, except that trailing zeros may be
omitted; if the precision is zero and the # flag is not specified, no decimalpoint
character appears. The letters abcdef are used for a conversion and
the letters ABCDEF for A conversion. The A conversion specifier produces a
number with X and P instead of x and p. The exponent always contains at
least one digit, and only as many more digits as necessary to represent the
decimal exponent of 2. If the value is zero, the exponent is zero.
A double argument representing an infinity or NaN is converted in the style
of an f or F conversion specifier.
zhangqiang423 2009-08-09
  • 打赏
  • 举报
回复
#include<stdio.h>
int main()
{
int i,j;
long double sum=0,n=1;
printf("n e\n");
printf("- -----------\n");
for (j=0;j<=9;j++)
{
i=j;
if (i==0) n=1;
else
{
while (i!=0)
{
n=n*i;
i--;
}
}
sum=1.0/n+sum;
n=1;
if(j==0) printf("%d %.0Lf\n",j,sum);
else if(j==1) printf("%d %.0Lf\n",j,sum);
else if(j==2) printf("%d %.1Lf\n",j,sum);
else
printf("%d %.9Lf\n",j,sum);

}
return 0;
}
谢谢大家 那是北大ACM上的一道题 ko了!!!
sonicrang 2009-08-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lzy0001sl 的回复:]
long double a;
printf("%lf",a);
long double 类型的变量也是8字节
[/Quote]

不一定哦,有的是8字节,有的是10字节,有的是12字节或更多。
baihacker 2009-08-09
  • 打赏
  • 举报
回复
#include <stdio.h>

int main(void)
{
double e=2.5;
int i=3,n=9,s=2;
printf("n e\n- -----------\n0 1\n");
printf("1 2\n2 2.5\n");
while(i <= n)
{
s=s*i;
e +=1.0/s;
printf("%d %.9f\n",i++ ,e);
}
return 0;
}
baihacker 2009-08-09
  • 打赏
  • 举报
回复

//要这样
#include <iostream>
#include <cstdio>
using namespace std;

int main(void)
{
double e=2.5;
int i=3,n=9,s=2;
printf("n e\n- -----------\n0 1\n");
printf("1 2\n2 2.5\n");
while(i <= n)
{
s=s*i;
e +=1.0/s;
printf("%d %.9f\n",i++ ,e);
}
return 0;
}
zhangqiang423 2009-08-09
  • 打赏
  • 举报
回复 1
题目是这样的 如果用double只能确定小数点后6位而题目要求确定小数点后9位必须用long double

Description

A simple mathematical formula for e is

e=Σ0<=i<=n1/i!

where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
Input

No input
Output

Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
Sample Input

no input
Sample Output

n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
...

补充一句 我用的软件是codeblock 专门用来编c和c++的 用turbo c 试了用printf(“%Ld”,a)也不对啊
  • 打赏
  • 举报
回复
%lf
副组长 2009-08-09
  • 打赏
  • 举报
回复
没见过 long double 呢。 double和float都是%f,想输出多少位小数啊?
pmerOFc 2009-08-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lzy0001sl 的回复:]
long double a;
printf("%lf",a);
long double 类型的变量也是8字节
[/Quote]
不一定
cheng_fengming 2009-08-09
  • 打赏
  • 举报
回复
我不确定上面那位老兄说的是不是long double 的输出格式,不过我想说的是在32位机里面long double 并没有什么实际意义啊,它和double 表示的范围数是一样的。都占8个字节。
lzy0001sl 2009-08-09
  • 打赏
  • 举报
回复
long double a;
printf("%lf",a);
long double 类型的变量也是8字节
sunjiankirk 2009-08-09
  • 打赏
  • 举报
回复
double 是用%lf吧,
long double 还是第一次见
pmerOFc 2009-08-09
  • 打赏
  • 举报
回复
dev c++里%Lf就不行
baihacker 2009-08-09
  • 打赏
  • 举报
回复

#include <stdio.h>
int main()
{
long double x = 1;
printf("%Lf", x);
return 0;
}


d:\Projects>gcc c.c -ansi

d:\Projects>a
1.000000
baihacker 2009-08-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhangqiang423 的回复:]
找了很多地方都说用printf("%lf",a)输出 试了试错误的 float用%f double用%ld 那long double用该用什么啊
[/Quote]
在vc9下是正常输出的

如果是用gcc的话
#include <stdio.h>
int main()
{
long double x = 1;
printf("%Lf", x);
return 0;
}
d:\Projects>g++ c.c -ansi

d:\Projects>a
1.000000
baihacker 2009-08-09
  • 打赏
  • 举报
回复
"%Lf"

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧