fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. void print_bytes(char const *name, double d)
  5. {
  6. unsigned char *pd = reinterpret_cast<unsigned char*>(&d);
  7. std::cout << name << " = " << std::setw(2) << d << " => ";
  8. for(int i = 0 ; i < sizeof(d) ; ++i)
  9. std::cout << std::setw(-3) << (unsigned)pd[i] << " ";
  10. std::cout << std::endl;
  11. }
  12.  
  13. #define print_bytes_of(a) print_bytes(#a, a)
  14.  
  15. int main()
  16. {
  17. double a = 0.0;
  18. double b = -0.0;
  19.  
  20. std::cout << "Value comparison" << std::endl;
  21. std::cout << "(a==b) => " << (a==b) <<std::endl;
  22. std::cout << "(a!=b) => " << (a!=b) <<std::endl;
  23.  
  24.  
  25. std::cout << "\nValue representation" << std::endl;
  26. print_bytes_of(a);
  27. print_bytes_of(b);
  28. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Value comparison
(a==b) => 1
(a!=b) => 0

Value representation
a =  0 => 0 0 0 0 0 0 0 0 
b = -0 => 0 0 0 0 0 0 0 128