dubmojo
dubmojo

Reputation: 6838

Java double with no decimal place doesn't multiply correctly?

I'm really sorry to ask this but I'm completely losing my mind over Java math at this point and its multitude of quirks. I have a function that converts from geographic longitude/latitude into a relative X/Y graph. (not important) But during testing I ran into a problem where a double multiplication, which should result in a double with no decimal place (e.g. 100.0), is returning an incorrect value.

Here's my latest attempt:

double DEGREE_DISTANCE_AT_EQUATOR = 111329;
double E6 = Double.valueOf(1E6);

double lng1 = 44.34648;
double lat1 = -76.888694;
double lng2 = 44.346356;
double lat2 = -76.888215;

xyFromLongLat(lng1, lat1, lng2, lat2);


public static Pair<Double, Double> xyFromLongLat(double long0, double lat0, double long1, double lat1) {
    double x = Math.round(((long1 - long0) * longitudeDistanceAtLatitude(lat0)) * E6);
    double y = Math.round(((lat1 - lat0) * DEGREE_DISTANCE_AT_EQUATOR) * E6);
    return new Pair<Double, Double>(x, y);
}

Double y results in 5.3326591E7? e.g. (-76.888694 - -76.888215) = -0.000479 * 111329.0 = -53.326591 * 1000000.0 SHOULD EQUAL -53326591.

Instead it equals: 5.332659099984012E7

(lat1 - lat0) * DEGREE_DISTANCE_AT_EQUATOR
(-76.888694 - -76.888215) * 111329.0 = -53.326591 <- This is correct

((lat1 - lat0) * DEGREE_DISTANCE_AT_EQUATOR) * E6
((-76.888694 - -76.888215) * 111329.0) * 1000000.0 = 5.332659099984012E7 <- What is this?!

I have no clue why this is an issue. At some point there's probably some automatic conversion to a long or float possibly because the result has no decimal places yet is supposed to be a double?

I'm really sorry if this is an obvious problem. Any suggestions are greatly appreciated as are any edits that clarify any terminology I've misused. Thanks!

Note that I realize Math.round returns a long. This was the latest attempt, without the rounding Y produces the same incorrect value.

Upvotes: 0

Views: 235

Answers (1)

Nadim Baraky
Nadim Baraky

Reputation: 457

It's a scientific notation and means 5.3326 multiplied by 107.

So E here stands for 10, followed by the exponent of 10.

Upvotes: 1

Related Questions