VS2013:strtof 没有设置 ERANGE?
VS2013: strtof doesn't set ERANGE?
在Visual Studio2013年,我有以下测试代码:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( int argc, const char* argv[])
{
static const char* floatStr = "3.40282346638528859811704183485E+39";
static const char* doubleStr = "1.79769313486231570814527423732E+309";
char* end;
float floatVal = 42.0f;
double doubleVal = 42.0;
errno = 0;
floatVal = strtof(floatStr, &end);
printf("Conversion of '%s':\nfloatVal=%f\nerrno='%s'\n",
floatStr,
floatVal,
strerror(errno));
errno = 0;
doubleVal = strtod(doubleStr, &end);
printf("Conversion of '%s':\ndoubleVal=%f\nerrno='%s'\n",
doubleStr,
doubleVal,
strerror(errno));
return 0;
}
目的是展示在非常大的(溢出)输入上使用 strtof() 和 strtod() 函数时观察到的行为。
我发现 strtof() 函数会将浮点值设置为 +INF 但不会设置 ERANGE errno。然而,strtod() 函数将 double 值设置为 +INF 并设置 ERANGE errno:
Conversion of '3.40282346638528859811704183485E+39':
floatVal=1.#INF00
errno='No error'
Conversion of '1.79769313486231570814527423732E+309':
doubleVal=1.#INF00
errno='Result too large'
这种行为是预期的,还是特定于实现的细微差别?
旁注:在我看来 strtof() 可能正在调用 strtod() 并且在从 double 转换为 float 产生 +INF 结果后没有适当地设置 errno?
正如 WeatherVane 指出的那样,这不会发生在 VS2015 中,似乎是 VS2013 实现中的错误。
在Visual Studio2013年,我有以下测试代码:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( int argc, const char* argv[])
{
static const char* floatStr = "3.40282346638528859811704183485E+39";
static const char* doubleStr = "1.79769313486231570814527423732E+309";
char* end;
float floatVal = 42.0f;
double doubleVal = 42.0;
errno = 0;
floatVal = strtof(floatStr, &end);
printf("Conversion of '%s':\nfloatVal=%f\nerrno='%s'\n",
floatStr,
floatVal,
strerror(errno));
errno = 0;
doubleVal = strtod(doubleStr, &end);
printf("Conversion of '%s':\ndoubleVal=%f\nerrno='%s'\n",
doubleStr,
doubleVal,
strerror(errno));
return 0;
}
目的是展示在非常大的(溢出)输入上使用 strtof() 和 strtod() 函数时观察到的行为。
我发现 strtof() 函数会将浮点值设置为 +INF 但不会设置 ERANGE errno。然而,strtod() 函数将 double 值设置为 +INF 并设置 ERANGE errno:
Conversion of '3.40282346638528859811704183485E+39':
floatVal=1.#INF00
errno='No error'
Conversion of '1.79769313486231570814527423732E+309':
doubleVal=1.#INF00
errno='Result too large'
这种行为是预期的,还是特定于实现的细微差别?
旁注:在我看来 strtof() 可能正在调用 strtod() 并且在从 double 转换为 float 产生 +INF 结果后没有适当地设置 errno?
正如 WeatherVane 指出的那样,这不会发生在 VS2015 中,似乎是 VS2013 实现中的错误。