Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DoubConv.h
Go to the documentation of this file.
1 // $Id:$
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // Hep Random
6 // --- DoubConv ---
7 // class header file
8 // -----------------------------------------------------------------------
9 //
10 #ifndef DOUBCONV_HH
11 #define DOUBCONV_HH
12 
13 #include <string>
14 #include <vector>
15 #include <exception>
17 
18 namespace CLHEP {
19 
20 class DoubConvException : public std::exception {
21 public:
22  DoubConvException(const std::string & w) throw() : msg(w) {}
23  ~DoubConvException() throw() {}
24  const char* what() const throw() { return msg.c_str(); }
25 private:
26  std::string msg;
27 };
28 
29 class DoubConv {
30 public:
31 
32  // dto2longs(d) returns (in a vector) two unsigned longs string containing the
33  // representation of its double input. This is byte-ordering
34  // independant, and depends for complete portability ONLY on adherance
35  // to the IEEE 754 standard for 64-bit floating point representation.
36  // The first unsigned long contains the high-order bits in IEEE; thus
37  // 1.0 will always be 0x3FF00000, 00000000
38  static std::vector<unsigned long> dto2longs(double d);
39 
40  // longs2double (v) returns a double containing the value represented by its
41  // input, which must be a vector containing 2 unsigned longs.
42  // The input is taken to be the representation according to
43  // the IEEE 754 standard for a 64-bit floating point number, whose value
44  // is returned as a double. The byte-ordering of the double result is,
45  // of course, tailored to the proper byte-ordering for the system.
46  static double longs2double (const std::vector<unsigned long> & v);
47 
48  // dtox(d) returns a 16-character string containing the (zero-filled) hex
49  // representation of its double input. This is byte-ordering
50  // independant, and depends for complete portability ONLY on adherance
51  // to the IEEE 754 standard for 64-bit floating point representation.
52  static std::string d2x(double d);
53 
54 private:
55  union DB8 {
56  unsigned char b[8];
57  double d;
58  };
59  static void fill_byte_order ();
60  static CLHEP_THREAD_LOCAL bool byte_order_known;
61  static CLHEP_THREAD_LOCAL int byte_order[8];
62  // Meaning of byte_order: The first (high-order in IEEE 754) byte to
63  // output (or the high-order byte of the first unsigned long)
64  // is of db.b[byte_order[0]]. Thus the index INTO byte_order
65  // is a position in the IEEE representation of the double, and the value
66  // of byte_order[k] is an offset in the memory representation of the
67  // double.
68 };
69 
70 
71 }
72 
73 #endif // DOUBCONV_HH
const char * what() const
Definition: DoubConv.h:24
DoubConvException(const std::string &w)
Definition: DoubConv.h:22
static std::string d2x(double d)
Definition: DoubConv.cc:86
#define CLHEP_THREAD_LOCAL
Definition: thread_local.h:25
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:114