VS2013:strtof没有设置ERANGE?

时间:2017-01-09 00:18:34

标签: c string floating-point errno

在Visual Studio 2013中,我有以下测试代码:

#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()函数会将float值设置为+ 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?

1 个答案:

答案 0 :(得分:3)

正如WeatherVane指出的那样,这在VS2015中不会发生,并且似乎是VS2013实施中的一个错误。

相关问题