Skip to content

InterestingCodeSegments

Gergely Patay edited this page Apr 7, 2015 · 1 revision

Collection of the most WTF code segments from the GATE source code

Eating Pi(e)

#define PI ( 4 * ::atan( 1 ) )
G4double Pi = 3.14159265;
double PI = 4*(atan (1.0));
theta=3.1416/2.;
double GateSimplifiedDecayTransition::Pi            = 3.141592653589793 ;
double GateSimplifiedDecayTransition::E             = 2.718281828459045 ;

and a handful of correct solutions:

inline Double_t TMath::Pi()       { return 3.14159265358979323846; } // ROOT TMath.h, unnecessary dependency
#define M_PI		3.14159265358979323846  // stdlib math.h , maybe not present in Visual Studio??
static const double CLHEP::pi = 3.14159265358979323846; // CLHEP SystemOfUnits.h

Random thoughts

G4double aleac; return (aleac = (((G4double) rand ()) / 2147483647.0));
G4double rmd=(((G4double) rand ()) / 2147483647.0);
rdm =  ((double)std::rand()/((double)RAND_MAX+1)*mWeight);
double rnd = rand()/(double)(RAND_MAX);

correct solution:

double rnd = CLHEP::RandFlat::shoot();

Tangential remarks

A nice trick to introduce a (removable) discontinuity at X1==X2 and a NaN at Y1==Y2

if ((globalPosX1-globalPosX2) != 0.) {
    theta = atan( (globalPosX1 - globalPosX2) / (globalPosY1 - globalPosY2) );
    if (theta < 0.) theta = theta+3.1416;
  } else {
    theta=3.1416/2.;
}

better:

theta = atan2(globalPosX1-globalPosX2, globalPosY1-globalPosY2);
if (theta < 0.0) {
    theta = theta + CLHEP::pi;
}

Clone this wiki locally