Advertisement
Guest User

double to string to double

a guest
Oct 12th, 2011
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include "assert.h"
  2.  
  3. #include <cmath>
  4. #include <limits>
  5. #include <sstream>
  6. #include <iomanip>
  7. #include <iostream>
  8.  
  9.  
  10.  
  11.  
  12. int main(int argc, char* argv[])
  13. {
  14.     //testReadWrite();
  15.     int     const Prec = 35;
  16.     double d0 = 0.0070000000000000001;
  17.     double d1 = 0.007000000000000001;
  18.     double d2 = 0.00700000000000001;
  19.     //double d3 = 0.0070000000000000097;
  20.     char    buffer[100];
  21.     std::ostringstream out;
  22.  
  23.     memset(buffer,0,sizeof(buffer));
  24.     out.precision(Prec);
  25.     std::string  test1;
  26.  
  27.     out << d0;
  28.     test1 = out.str();
  29.  
  30.     out << "    ";
  31.     out << d1;
  32.     out << "    ";
  33.     out << d2;
  34.  
  35.     double d0_;
  36.     double d1_;
  37.     double d2_;
  38.  
  39.     std::istringstream in (out.str());
  40.     in.precision(Prec);
  41.     double d0X_ = strtod(test1.c_str(),NULL);
  42.    
  43.     in >> d0_;
  44.     in >> d1_;
  45.     in >> d2_;
  46.        
  47.     assert(d0 == d0X_); // this is ok
  48.     assert(d0 == d0_);   //this failes?!?!?!
  49.  
  50.     long long l0= *reinterpret_cast<long long *> (&d0);
  51.     long long l1= *reinterpret_cast<long long *> (&d1);
  52.     long long l2= *reinterpret_cast<long long *> (&d2);
  53.  
  54.     long long l0_= *reinterpret_cast<long long *> (&d0_);
  55.     long long l1_= *reinterpret_cast<long long *> (&d1_);
  56.     long long l2_= *reinterpret_cast<long long *> (&d2_);
  57.     return 0;
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement