Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2013: strtof doesn't set ERANGE?

In Visual Studio 2013, I have the following test code:

#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;
}

The intent is to show a behavior observed when using the strtof() and strtod() functions on very large (overflow) inputs.

What I find is that the strtof() function will set the float value to +INF but not set the ERANGE errno. The strtod() function, however, sets the double value to +INF and sets the 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'

Is this behavior expected, or is it an implementation specific nuance?

Side note: It seems to me that strtof() may be calling strtod() and not setting errno appropriately after the cast from double to float produces a +INF result?

like image 814
Squirrel Avatar asked Oct 27 '25 00:10

Squirrel


1 Answers

As WeatherVane has pointed out, this does not occur in VS2015 and appears to be a bug in the VS2013 implementation.

like image 145
Squirrel Avatar answered Oct 29 '25 17:10

Squirrel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!