CLHEP 2.0.4.7 Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 #ifndef DOUBCONV_HH 00002 #define DOUBCONV_HH 00003 00004 #include <string> 00005 #include <vector> 00006 #include <exception> 00007 00008 namespace CLHEP { 00009 00010 class DoubConvException : public std::exception { 00011 public: 00012 DoubConvException(const std::string & w) throw() : msg(w) {} 00013 ~DoubConvException() throw() {} 00014 const char* what() const throw() { return msg.c_str(); } 00015 private: 00016 std::string msg; 00017 }; 00018 00019 class DoubConv { 00020 public: 00021 00022 // dto2longs(d) returns (in a vector) two unsigned longs string containing the 00023 // representation of its double input. This is byte-ordering 00024 // independant, and depends for complete portability ONLY on adherance 00025 // to the IEEE 754 standard for 64-bit floating point representation. 00026 // The first unsigned long contains the high-order bits in IEEE; thus 00027 // 1.0 will always be 0x3FF00000, 00000000 00028 static std::vector<unsigned long> dto2longs(double d); 00029 00030 // longs2double (v) returns a double containing the value represented by its 00031 // input, which must be a vector containing 2 unsigned longs. 00032 // The input is taken to be the representation according to 00033 // the IEEE 754 standard for a 64-bit floating point number, whose value 00034 // is returned as a double. The byte-ordering of the double result is, 00035 // of course, tailored to the proper byte-ordering for the system. 00036 static double longs2double (const std::vector<unsigned long> & v); 00037 00038 // dtox(d) returns a 16-character string containing the (zero-filled) hex 00039 // representation of its double input. This is byte-ordering 00040 // independant, and depends for complete portability ONLY on adherance 00041 // to the IEEE 754 standard for 64-bit floating point representation. 00042 static std::string d2x(double d); 00043 00044 private: 00045 union DB8 { 00046 unsigned char b[8]; 00047 double d; 00048 }; 00049 static void fill_byte_order (); 00050 static bool byte_order_known; 00051 static int byte_order[8]; 00052 // Meaning of byte_order: The first (high-order in IEEE 754) byte to 00053 // output (or the high-order byte of the first unsigned long) 00054 // is of db.b[byte_order[0]]. Thus the index INTO byte_order 00055 // is a position in the IEEE representation of the double, and the value 00056 // of byte_order[k] is an offset in the memory representation of the 00057 // double. 00058 }; 00059 00060 00061 } 00062 00063 #endif // DOUBCONV_HH