[prev in list] [next in list] [prev in thread] [next in thread] 

List:       boost-users
Subject:    [Boost-users] Bug in lexical_cast<> with doubles
From:       "Leon Raj" <Leon.Raj () invetech ! com ! au>
Date:       2004-07-28 4:21:23
Message-ID: 5E06BFED29594F4C9C5EBE230DE320C623E2C3 () ewok ! vsl ! com ! au
[Download RAW message or body]

I think I've found a bug in the boost::lexical_cast<>
library (v1.31.0) on WinXP SP1, MSVC 6.0 (sp5) and 
MSVC.NET 2003.

--- Start code snippet ---

#include <boost/lexical_cast.hpp>
#include <iostream>

int main( int argc, char* argv[] )
{
    double dMax = = std::numeric_limits< double >::max();
    // On a Win32 x86 system, dMax is now 1.7976931348623157e+308

    // This is successful and strMax is "1.797693134862316e+308"
    std::string strMax = boost::lexical_cast< std::string >( dMax );

    try
    {
        double newDMax = boost::lexical_cast< double >( strMax );
        std::cout << "Successfuly" << std::endl;
    }
    catch ( boost::bad_lexical_cast& )
    {
        std::cout << "Failed to cast" << std::endl;
    }

    // On a Win32 x86 system, this always fails,
    // because (I think) strMax overflows a double,
    // because 1.7976931348623157e+308 is *rounded UP* 
    // to      1.797693134862316e+308, which cannot be
    // represented by a double
}

--- End code snippet ---

----------------------------------------------------- 
Leon Raj 
Senior Engineer 
Invetech Pty. Ltd. 
Phone: +61 3 9211 7000 (Switchboard) 
       +61 3 9211 7938 (DDI) 
leon.raj@invetech.com.au 

IMPORTANT - This e-mail and any attachments may be 
confidential. Any retransmissions, dissemination or 
other use of these materials by persons or entities 
other than the intended recipient is prohibited. 
If received in error, please contact us and delete 
all copies. Before opening or using attachments, 
check them for viruses and defects. Our liability 
is limited to resupplying any affected attachments.

[Any representations or opinions expressed in this 
e-mail are those of the individual sender, and not 
necessarily those of Vision Systems Limited]. 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic