How to save and restore exact floating point values to a human readable file in C++ -
i need able save , restore exact floating point values human readable (text) file c++ program. can use %a format sprinf save exact value, i'm not able find way restore it. using %a format sscanf not working me.
my sample program below. prints 0x1.921fb4d12d84ap+1 value of num1, when type value in, sets num2 0. i'm running in cygwin environment (dll version 1.7.22) using gcc 4.7.3, g++ compile options include: -std=gnu++11 -wall -wextra
#include <cstdio> #include <string> #include <iostream> int main () { double num1 = 3.1415926; double num2; std::string input; int result; printf("%la\n", num1); std::cout << std::endl; std::cout << "input:" << std::endl; std::cin >> input; std::cout << "got: " << input << std::endl; num2 = 0.0; result = sscanf(input.c_str(), "%la", &num2); std::cout << "num2 " << num2 << " , result " << result << std::endl; return 0; }
i recommend instead use atof() function, or use boost::lexical_cast. both accomplish want if have string number.
Comments
Post a Comment