Search code examples
c++precisionglog

How to print a full double precision by google glog


I am try to print a double variable a like this.

double a;
//some statements for a
LOG(INFO) << a;

How can I print a using full precision?


Solution

  • You should try

    #include <iomanip>      // std::setprecision
    
    double a = 3.141592653589793238;
    LOG(INFO) << std::fixed << std::setprecision( 15 ) << a;
    

    If that doesn't work you can convert it to an std::string and use std::stringstream

    #include <sstream>      // std::stringstream 
    #include <iomanip>      // std::setprecision 
    
    double a = 3.141592653589793238;
    std::stringstream ss;
    ss << std::fixed << std::setprecision( 15 ) << a;
    LOG(INFO) << ss.str();
    

    Alternatively, if you want full precision you can use one of the above with this answer.

    The first method is more than likely to be the most efficient way to do it.