NaN情况下to_string()可以返回什么 [英] what can to_string() return in NaN cases

查看:244
本文介绍了NaN情况下to_string()可以返回什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2012,遇到了非常令人讨厌的行为(有时我的浮动对象是NaN):

Im using VS 2012 and I encountered really irritating behaviour(sometimes my float is NaN):

auto dbgHelp = std::to_string(myFloat);

dbgHelp 最终包含 5008 charachters(您无法发明这种东西)
其中大多数为0,
最终以:
0.#INF00

dbgHelp ends up containing 5008 charachters ( you cant invent this stuff up) most of them 0, it ends up with : 0.#INF00

那么这是否是一个bug或标准,允许像这样的惯用返回值?

So is this a bug or standard allows idiotic return values like this ?

修复很简单,对我来说,用三元运算符和 isnan()
,但我想知道标准是否为此特定...

fix is trivial, for me to manualy do the proper thig with ternary operator and isnan() but I wonder if standard is specific regarding this...

推荐答案

std :: to_string 重载在[string.conversions] / 7中指定:

The std::to_string overloads are specified in [string.conversions]/7:


string to_string(int val);
string to_string(unsigned val);
string to_string(long val);
string to_string(unsigned long val);
string to_string(long long val);
string to_string(unsigned long long val);
string to_string(float val);
string to_string(double val);
string to_string(long double val);

7 返回:每个函数返回一个 string 对象,包含通过调用 sprintf(buf,fmt,val)的格式说明符为%d %u %ld %lu %lld %llu %f %f %Lf ,其中 buf 表示足够大的内部字符缓冲区。

7 Returns: Each function returns a string object holding the character representation of the value of its argument that would be generated by calling sprintf(buf, fmt, val) with a format specifier of "%d", "%u", "%ld", "%lu", "%lld", "%llu", "%f", "%f", or "%Lf", respectively, where buf designates an internal character buffer of sufficient size.

sprintf 在C99中进行了详细说明(实际上是N1256)7.19.6.6/2:

sprintf is detailed in C99 (N1256 actually) 7.19.6.6/2:


sprintf 函数等效于 fprintf ,除了将输出写到
数组(由参数 s 指定)而不是流中。在写入的字符末尾将空字符写入
;它不算作返回值的一部分。如果在重叠的对象之间进行
复制,则行为是不确定的。

The sprintf function is equivalent to fprintf, except that the output is written into an array (specified by the argument s) rather than to a stream. A null character is written at the end of the characters written; it is not counted as part of the returned value. If copying takes place between objects that overlap, the behavior is undefined.

对于<$,跳转到7.19.6.1 c $ c> fprintf ,格式标志在第8段中进行了说明。具体地说,对于 f 格式:

Jumping over to 7.19.6.1 for fprintf, the format flags are described in paragraph 8. Specifically, for the f format:


一个 double 表示浮点数的参数将转换为
十进制表示形式 [−] ddd.ddd ,其中
后小数点字符的位数等于精度规格。如果缺少
精度,则该精度为6;如果精度为零,并且未指定#标志为
,则不显示小数点字符。如果出现小数点的
字符,则在其前面至少出现一位数字。该值将四舍五入到
的适当位数。

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 表示无穷大的参数是转换为以下一种样式:
[-] inf [-] infinity —哪种样式实现-定义。表示NaN的
双重参数转换为
[-] nan [-] nan( n-char-sequence —哪种样式以及
的含义 n -char-sequence 是实现定义的。 F 转换说明符
产生 INF INFINITY NAN 代替 inf infinity nan ,分别是

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.

将所有内容放在一起时 float foo 是正无穷大, std :: to_string(foo)应该返回 std :: string( inf)

Putting it all together, when float foo is positive infinity, std::to_string(foo) should return std::string("inf").

这篇关于NaN情况下to_string()可以返回什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆