logoalt Hacker News

CamperBob2 • today at 4:48 AM • 2 replies • view on HN

     if (sscanf(string,"%lf",&foo) != 1) 
        { 
        bad_float(); 
        }
This has the benefit of handling scientific notation, too, which some of the other CRTL functions inexplicably do not.

For extra reassurance, initialize foo to NaN or DBL_MAX or something, and verify that it is no longer equal to that value.


Replies

lelanthran • today at 5:36 AM

That doesn't detect overflows and underflows. Also changes based on locale, which is not thread safe (so you cannot set locale, convert, then unset locale).

shakna • today at 5:28 AM

sscanf has locale things that make it include implementation-defined behaviour, doesn't it?

➕ show 1 reply