简体   繁体   English

如何在Windows上强制printf输出[-] inf或[-] infinity?

[英]How to force printf to output [-]inf or [-]infinity on Windows?

C99 standard says: C99标准说:

A double argument representing an infinity is converted in one of the styles [-]inf or [-]infinity -- which style is implemented is implementation-defined. 表示无穷大的double参数将转换为[-] inf或[-] infinity样式之一-实现的样式是实现定义的。 (p.278 section 7.19.6.1) (第278页,第7.19.6.1节)

Unfortunately on Windows: 不幸的是在Windows上:

printf("%f\n", 1.0f/0.0f)

produces: 1.#INF00 产生:1.#INF00

This is a problem because some applications expect C standard compliant strings as input (also C#'s Double.Parse works for "Infinity" but not for "1.#INF00", curiously "infinity" is not ok either at least when I tried it with Mono). 这是一个问题,因为某些应用程序希望使用符合C标准的字符串作为输入(C#的Double.Parse也适用于“ Infinity”,但不适用于“ 1.#INF00”,奇怪的是,至少当我尝试使用“ Infinity”时,它也不行)单声道)。

My question is how do I force printf under Windows to output "inf" or "infinity" instead of 1.#INF00 ? 我的问题是如何强制Windows下的printf输出“ inf”或“ infinity”而不是1.#INF00?

(I am compiling with MinGW gcc 4.8.2) (我正在使用MinGW gcc 4.8.2进行编译)

You can choose between the MSVC (default) and mingw version of the printf-functions. 您可以在MSVC(默认)和mingw版本的printf函数之间进行选择。 Just define __USE_MINGW_ANSI_STDIO like this, and the output should be C99 compliant: 像这样定义__USE_MINGW_ANSI_STDIO,并且输出应符合C99:

#define __USE_MINGW_ANSI_STDIO 1

Some documentation here and here . 一些文档在这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM