printf和长双人

发布于 2025-02-10 06:48:46 字数 809 浏览 2 评论 0原文

我正在使用Windows上的Netbeans使用最新的海湾合作委员会。为什么长double不起作用? printf specifier %lf错误吗?

代码:

#include <stdio.h>

int main(void)
{
    float aboat = 32000.0;
    double abet = 5.32e-5;
    long double dip = 5.32e-5;

    printf("%f can be written %e\n", aboat, aboat);
    printf("%f can be written %e\n", abet, abet);
    printf("%lf can be written %le\n", dip, dip);

    return 0;
}

输出:

32000.000000 can be written 3.200000e+004
0.000053 can be written 5.320000e-005
-1950228512509697500000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000.000000
can be written 2.725000e+002
Press [Enter] to close the terminal ...

I am using the latest gcc with Netbeans on Windows. Why doesn't long double work? Is the printf specifier %lf wrong?

Code:

#include <stdio.h>

int main(void)
{
    float aboat = 32000.0;
    double abet = 5.32e-5;
    long double dip = 5.32e-5;

    printf("%f can be written %e\n", aboat, aboat);
    printf("%f can be written %e\n", abet, abet);
    printf("%lf can be written %le\n", dip, dip);

    return 0;
}

Output:

32000.000000 can be written 3.200000e+004
0.000053 can be written 5.320000e-005
-1950228512509697500000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000.000000
can be written 2.725000e+002
Press [Enter] to close the terminal ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

东走西顾 2025-02-17 06:48:46

是的 - 对于,您需要使用%lf(即,上case'l')。

Yes -- for long double, you need to use %Lf (i.e., upper-case 'L').

乖乖哒 2025-02-17 06:48:46

从printf manpage:

l(ell)以下整数
转换对应于一​​个长int
或未签名的长时间参数,或
以下n转换对应于
指向长期参数的指针,或
以下c
转换对应于wint_t参数或以下s
转换对应于指向
WCHAR_T参数。

la跟随a,a,e,e,f,f,
g或g转换对应于
长时间的论点。 (C99允许
%lf,但SUSV2没有。)

因此,您想要%le,而不是%le

编辑:一些进一步的调查似乎表明mingw使用msvc/win32运行时(对于printf之类的东西) - 将长时间映射到两倍。因此,将编译器(如GCC)混合在一起,该编译器提供了一个本机长双重的运行时,似乎不是一团糟。

From the printf manpage:

l (ell) A following integer
conversion corresponds to a long int
or unsigned long int argument, or a
following n conversion corresponds to
a pointer to a long int argument, or a
following c
conversion corresponds to a wint_t argument, or a following s
conversion corresponds to a pointer to
wchar_t argument.

and

L A following a, A, e, E, f, F,
g, or G conversion corresponds to a
long double argument. (C99 allows
%LF, but SUSv2 does not.)

So, you want %Le , not %le

Edit: Some further investigation seems to indicate that Mingw uses the MSVC/win32 runtime(for stuff like printf) - which maps long double to double. So mixing a compiler (like gcc) that provides a native long double with a runtime that does not seems to .. be a mess.

妖妓 2025-02-17 06:48:46

如果您使用的是mingw,则问题是默认情况下,mingw使用I/O resp。 Microsoft C运行时的格式化函数,该功能不支持80位浮点数(Long double == Double Microsoft Land中)。

但是,MINGW还提供了一套 DO 适当支持长双打的替代实现。要使用它们,请将函数名称与__ mingw _(例如__ mingw_printf)前缀。根据项目的性质,您可能还需要全局#define printf __mingw_printf或使用-D__USE_MINGW_ENSI_ANSI_STDIO /代码> - 家庭功能)。

If you are using MinGW, the problem is that by default, MinGW uses the I/O resp. formatting functions from the Microsoft C runtime, which doesn't support 80 bit floating point numbers (long double == double in Microsoft land).

However, MinGW also comes with a set of alternative implementations that do properly support long doubles. To use them, prefix the function names with __mingw_ (e.g. __mingw_printf). Depending on the nature of your project, you might also want to globally #define printf __mingw_printf or use -D__USE_MINGW_ANSI_STDIO (which enables the MinGW versions of all the printf-family functions).

浪推晚风 2025-02-17 06:48:46

除了错误的修饰符(Windows的哪个GCC港口)之外? MINGW使用Microsoft C库,我似乎还记得该库不支持80bits long double(Microsoft C编译器使用64位长double,原因是各种原因)。

In addition to the wrong modifier, which port of gcc to Windows? mingw uses the Microsoft C library and I seem to remember that this library has no support for 80bits long double (microsoft C compiler use 64 bits long double for various reasons).

甜警司 2025-02-17 06:48:46

正在遇到这个问题,测试长双打,las,我遇到了一个修复程序!您必须使用-d__use_mingw_ansi_stdio编译项目:

Jason Huntley@centurian/home/developer/dependerencies/python-2.7.3/test
$ gcc main.c

Jason Huntley@centurian/home/developer/dependerencies/python-2.7.3/test
$ A.EXE C = 0.000000

Jason Huntley@centurian/home/developer/dependerencies/python-2.7.3/test
$ gcc main.c -d__use_mingw_ansi_stdio

Jason Huntley@centurian/home/developer/dependerencies/python-2.7.3/test
$ A.EXE C = 42.000000

代码:

Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test
$ cat main.c
#include <stdio.h>

int main(int argc, char **argv)
{
   long double c=42;

   c/3;

   printf("c=%Lf\n",c);

   return 0;
}

Was having this issue testing long doubles, and alas, I came across a fix! You have to compile your project with -D__USE_MINGW_ANSI_STDIO:

Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test
$ gcc main.c

Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test
$ a.exe c=0.000000

Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test
$ gcc main.c -D__USE_MINGW_ANSI_STDIO

Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test
$ a.exe c=42.000000

Code:

Jason Huntley@centurian /home/developer/dependencies/Python-2.7.3/test
$ cat main.c
#include <stdio.h>

int main(int argc, char **argv)
{
   long double c=42;

   c/3;

   printf("c=%Lf\n",c);

   return 0;
}
活泼老夫 2025-02-17 06:48:46

在C99中,的长度修饰符长似乎是l而不是lMAN FPRINTF(或Windows的等效内容)应告诉您特定平台。

In C99 the length modifier for long double seems to be L and not l. man fprintf (or equivalent for windows) should tell you for your particular platform.

初见终念 2025-02-17 06:48:46

如其他答案中所述,正确的转换说明符是“%lf”

您可能需要通过使用-wformat(或-wall)打开格式警告,其中包括-wformat

$ gcc source.c
$ gcc -Wall source.c
source.c: In function `main`:
source.c:5: warning: format "%lf" expects type `double`, but argument 2 has type `long double`
source.c:5: warning: format "%le" expects type `double`, but argument 3 has type `long double`
$

As has been said in other answers, the correct conversion specifier is "%Lf".

You might want to turn on the format warning by using -Wformat (or -Wall, which includes -Wformat) in the gcc invocation

$ gcc source.c
$ gcc -Wall source.c
source.c: In function `main`:
source.c:5: warning: format "%lf" expects type `double`, but argument 2 has type `long double`
source.c:5: warning: format "%le" expects type `double`, but argument 3 has type `long double`
$
夏末 2025-02-17 06:48:46

C/C ++中的printf和Scanf函数使用Microsoft C库,此库不支持10个字节长double。因此,当您在C/C ++代码中使用printf和scanf函数以打印长时间的双重输出,并将一些输入作为长双重双重,它将始终给您错误的结果。

如果要使用长double,则必须使用“ __GNGW_PRINTF”和“ __MingW_Scanf”功能而不是printf和scanf。它支持10个字节长双重。

或者,您可以定义两个宏:“ #define printf __mingw_printf”和“ #define scanf __mingw_scanf”

使用标准格式,用于长double:%lf

printf and scanf function in C/C++ uses Microsoft C library and this library has no support for 10 byte long double. So when you are using printf and scanf function in your C/C++ code to print a long double as output and to take some input as a long double, it will always give you wrong result.

If you want to use long double then you have to use " __mingw_printf " and " __mingw_scanf " function instead of printf and scanf. It has support for 10 byte long double.

Or you can define two macro like this : " #define printf __mingw_printf " and " #define scanf __mingw_scanf "

Use standard format for long double : %Lf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文