- #1
testme
- 68
- 0
Homework Statement
I'm given a maximum time, a time step (or interval) and I'm supposed to form an array from 0 to the maximum time going up by timestep each time.
double tMax; //maximum time
double tStep; //time interval to go up by
Homework Equations
Our teacher told us to use the following equation:
ti = ti-1 + tStep
The Attempt at a Solution
public static double[] timeArray(double tMax, double tStep)
{
double[] timeArr = new double[(int)(tMax/tStep) + 1];
timeArr[0] = [0];
for(int i = 1; timeArr[i - 1] <= tMax; i++)
{
timeArr = timeArr[i - 1] + tStep;
}
return timeArr;
}
public static void displayTime(double[] timeArr)
{
int n = timeArr.length;
System.out.println("Time");
for(int i = 0; i < n; i++)
{
System.out.println(timeArr);
}
}
Everything seems to be working however when I print out the time array (using 1 as tMax and 0.05 as tStep) it gives me the list of values such as:
0.0
0.05
0.1
0.15000000000000002
0.2
0.25
0.3
0.35
0.39999999999999997
0.44999999999999996
0.49999999999999994
0.5499999999999999
0.6
0.65
0.7000000000000001
0.7500000000000001
0.8000000000000002
0.8500000000000002
0.9000000000000002
0.9500000000000003
1.0000000000000002
Can anyone tell me why it's adding or taking off .000000000000000#? and how to fix it