-1

I am doing a program that calculates radio activity at a previous point in time based on measured radioactivity.

However if the half-life is very large compared with the elapsed time I get a 0.00 as the answer which then leaves me with A= Ao at least until (times[i]-times[0]) gets large.

For example if (times[i]-times[0]) = 5 and Thalflife = 6540 I want to get 0.00076452599 however I get 0.0 is double the wrong type to use or is it the / command that is giving me the problem?

private double[]theoreticalVals(double activity) {
   /* below is an implementation of the activity decay 
    * calculation formula A= Ao/2^(t/Thalflife)
    * An array of values will be returned. 
    * The times array defines the length of the array and gives the point in time values
    */
 double Ao= activity; 
 double [] vals = new double[times.length];
 double a; 
 vals[0]=Ao;                            //initial activity
 for(int i = 1; i<times.length;i++){                
   a =(times[i]-times[0])/Hf;       //  
   double lowerhalfterm = Math.pow(.5,a);                   //  2-(^(t/Thalflife))
   vals[i]= Ao/lowerhalfterm;

}                                          // A=Ao/2^(t/Thalflife)

return vals; 
}
RNJ
  • 15,272
  • 18
  • 86
  • 131
DavyGravy
  • 311
  • 1
  • 4
  • 14
  • 1
    You neither specify a language nor provide the type of `times`, but see http://stackoverflow.com/questions/8906722/c-programming-division – Pascal Cuoq Feb 03 '13 at 19:41

2 Answers2

3

times is probably an array of integers, and Hf is probably also an integer. You either need to change the declaration of one of those variables to a floating variable, like double, or you will need to cast one of the variables in the calculation.

Change the declaration from:

int Hf;

to:

double Hf;

Or cast in the calculation:

(times[i]-times[0])/((double)Hf)
Daniel Jonsson
  • 3,261
  • 5
  • 45
  • 66
1

You're dividing an int by an int, so you're using integer division. To have double division, one of the operands at least must be a double:

double result = ((double) i1) / i2
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255