$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-users] [conversion] Using Lexical_cast to convert string to float
From: Ryan McConnehey (mccorywork_at_[hidden])
Date: 2009-06-09 22:36:30
In writing some unit tests to convert strings to floats I've come across 
a puzzling situation.  I was trying to test that a std::bad_cast was 
thrown if a number was too large to fit into a float.  The error wasn't 
thrown but the float was given 1.#INF000.  The work around I used was to 
cast to a double and then do a numeric_cast on the result.  This 
provided an exception for values larger than could fit in a float.  
Lexical_cast on a double though works as I would have expected.  If a 
value is too large to fit into a double then a std::bad_cast is called.
float     test     = boost::lexical_cast<float>("3.4e050");             
                    //test will have the value of 1.#INF000
float     test     = boost::numeric_cast<float>( 
boost::lexical_cast<double>("3.4e50");     //std::bad_cast is thrown
double    test     = boost::lexical_cast<double>("3.4e350");          
                      //std::bad_cast is thrown
Is the lexical_cast for float behaving correctly?
Ryan
P.S.  This code is using boost 1.38.0