Message: Re: strange behaviour for "deg" unit Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

None Re: strange behaviour for "deg" unit 

Forum: Analysis
Re: None strange behaviour for "deg" unit
Date: 11 Aug, 2010
From: Michael H. Kelsey <Michael H. Kelsey>

On Wed, 11 Aug 2010 19:27:01 GMT, maurizio wrote:
> consider these 3 lines:
> 
>         G4double arg_theta = 30.0*deg;
>         G4int theta_slider_pos = 20.0*(arg_theta/deg);
>         cout <<  20*arg_theta/deg  << " " << theta_slider_pos << endl;
> 
> Normally we'd expect this to output "600 600". However it outputs "600
> 599".

From CLHEP/Units/SystemOfUnits.h (the G4 names are just the CLHEP names with "using CLHEP" directives), we discover the definitions

  //
  // Angle
  //
  static const double radian      = 1.;                  
  static const double milliradian = 1.e-3*radian;
  static const double degree = (3.14159265358979323846/180.0)*radian;
[...]
  static const double deg  = degree;

So you see that degree is defined as pi/180 (on my MacBook, that string of digits is the same as M_PI).

Your code above is doing double->int conversion. That means a floating point value is _truncated_, not rounded. I am able to reproduce your results, and by using "fixed" and "setprecision(30)," you can see what's going on:

  20.0*(arg_theta/deg) = 600
  20.0*(arg_theta/deg) = 599.999999999999886313162278383970
  theta_slider_pos = 599
  int(20*(arg_theta/deg)) = 599
  int((20*arg_theta)/deg) = 599

 Add Message Add Message
to: "Re: strange behaviour for "deg" unit"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]