[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Strange atof and strtof behaviors



On Mon, 2008-07-14 at 18:48 +0200, Émeric Maschino wrote:
> Hi,
> 
> I don't know exactly when the change occurs (was it with gcc 4.1, 4.2,
> 4.3?), but I'm experiencing problems with the atof and strtof
> functions since then.
> 
> In one of my C++ projects, I'm using atof to recover data from an
> ASCII file to convert them into a float value. But,
> 
> string s( "0.549" );
> cerr << atof( s ) << endl;    // Returns 0
> cerr << atof( "0.513" ) << endl;     // Returns 0
> cerr << atof( "1.117" ) << endl;    // Returns 1
> 
> I've tried to replace the calls to atof by calls to strtof. Same
> problem.
> By contrast, a single test project with only the above lines of code
> gives the expected results (i.e., 0.549, 0.513 and 1.117). So this may
> be due to incorrect cascaded headers. I have #include <cstdlib>
> however.

I don't believe this is a IA64 problem.  Have you checked on i386 or
x86_64?

My first guess is that your program is calling atof without a function
prototype, somehow.  Be sure to compile with "-Wall -Wextra".  Functions
without a prototype default to an "int" return value.

If you include <cstdlib> in your C++ source, make sure that you call it
as std::atof or have an active "using namespace std;" somewhere in
scope.

Another thing...I am pretty sure that std::string does not automatically
convert to char*, so your atof( s ) example should be horribly wrong.
atof( s.c_str() ) is the right way to do it.  Unless someone added
std::string to the prototypes in cstdlib when I wasn't looking.
-- 
Zan Lynx <zlynx@acm.org>

Attachment: signature.asc
Description: This is a digitally signed message part


Reply to: