Recent Posts

Archives

Topics


« | Main | »

double math in Java

By Jeremy | March 14, 2010

I have been programming extensively using doubles and Doubles in Java.  Often, I have run into problems where simple mathematical operators will return 0.0. For example:

ArrayList<Double> a, b;
double temp = (a.get(0) + b.get(0)) * (a.get(0) + b.get(0));

After executing this code, temp would be equal to 0 no matter what values a.get(0) and b.get(0) had. This seemed very annoying and backwards and the only way I had to solve it was thusly:

double temp = (double)a.get(0) + b.get(0);
double result = temp * temp;

Then, result would actually have a result in it. Go figure.

Topics: bugs | No Comments »

Comments

You must be logged in to post a comment.