我正在从字符串中读取 float 。它们可以写成各种形式,所以
float f1 = strtof("999999999999.16");
float f2 = stof("000999999999999.1600000");
assert(f1 == f2);
无论前导零和尾随零,我能否确定断言始终为真?分隔符始终是一个点,stof
不处理逗号。
最佳答案
The C11 standard, in 7.22.1.3p9 , 关于 C 的 strtof
有这个说法吗?/strtod
/strtold
(这应该是 C++ 版本在下面使用的,至少从 cppreference 判断):
If the subject sequence has the decimal form and at most
DECIMAL_DIG
(defined in<float.h>
) significant digits, the result should be correctly rounded.
鉴于您的两个代码行具有相同数量的有效数字,它们应该表现相同。但这只是基于标准在这里根本没有提到“有效数字”这一事实的推测;它没有在其他任何地方提及,并且该标准没有更明确地说明前导(小数点前)或尾随(小数点后)零。
关于c++ - stof、strtof是确定性的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39900952/