Why define G4double?

I was going through the G4Types.hh code where the data types are defined and I was wondering the need for defining GEANT4 specific data types (G4double, G4int, etc.). From what I could see in the code, they’re basically the same as the primitive data types in C++. What was the need for defining G4 specific data types?

In the comments above the data type definition, it says -
// Typedefs to decouple from library classes
// Typedefs for numeric types

Not quite sure what those comments mean either.

This:

// Typedefs for numeric types
//
typedef double G4double;

describes a typedef (G4double) for a numeric type (double).

This:

// Typedefs to decouple from library classes
//
typedef std::complex<G4double> G4complex;

describes a typedef (G4complex) for a library class (std::complex<G4double>).

Now, it’s probably obvious why the latter case is nice: I prefer to type G4complex rather than std::complex<G4double> (or even std::complex<double>). The former case is probably not so obvious: it’s a question of portability and future proofing. For example, int and long might have implementations that differ from computer to computer: the C++ standard doesn’t specify the implementation. Perhaps not now, but perhaps some time in the future, it will be necessary to re-write code to ensure that G4double etc. have a particular desirable implementation - or at least avoid a particular undesirable one. That might seem unlikely, but if it ever were to be the case, it’d turn out to have been much better to have used G4double now, and only have to change one header, than worry about all the individual uses of double spread throughout the toolkit - and perhaps your application, too.

1 Like

Hey. Thanks for the response. Makes much more sense now.

P.S. - I wasn’t quite sure what “implementation” actually means so I did a quick google search. This link seemed to help. Putting it here for future reference - https://stackoverflow.com/questions/21357014/what-is-data-type-and-how-is-it-implemented