Subtraction of dissimilar doubles resulting in 0.0
I'm trying to subtract two small doubles, both of which I got using the system time:
start = System.currentTimeMillis();
end = System.currentTimeMillis();
Then I subtracted two ( end - start
) and tried to store that value into an array double[] data
, which I did. For some reason, the result of this, instead of being double, continues to output 0.0
.
I have initialized the data array like this:
private double[] data = new double[maxTaps+1];
where MaxTaps
is some constant
I am using a textView to display the values ββthat I am walking through. For some reason, when I just print ( end - start
) it will give me a double that is nonzero. But if I put this double ( end - start
) in my data array and try to print a double from the data array, it results in 0.0.
For example: I am trying to get the time from the moment the button changes color to the moment it is used. After changing the color of the button, I set the start time. Then, when the user removes a specific button, the end time is recorded using the method described above and the two are subtracted and added to my dataset. The weird part looks like this: when I output data like below, the result is always 0.0
bottomText.setText("Data point " + Integer.toString(taps) + ": " + String.valueOf(data[taps]));
but if i change data[taps]
to ( end - start
) (shown below) then i get a reasonable value
bottomText.setText("Data point " + Integer.toString(taps) + ": " + String.valueOf((end - start));
I am really lost here because they must be the same.
Also, I just include what one of my click listeners looks like a button, which might also be helpful
buttonTopLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
if (lit == 1) {
end = System.currentTimeMillis();
data[taps] = (end - start);
taps++;
buttonTopLeft.setBackgroundColor(Color.WHITE);
mHandler.postDelayed(new Runnable() {
public void run(){
doUpdate();
}
}, 1000);
}
}
});
source to share
The problem is most likely because you are increasing the number of taps after setting the value, and the new data[taps]
one is actually zero. The data in data[taps-1]
is the value you are using. Let's say the taps are at 0, then you set the first item - data[0]= 15.0
. Then taps++
it branches off to 1 now and you actually print data[1]
which is not set, so it shows 0
source to share