-2

I dont have experience with Java math expression. I'm trying to calulate distance between two locations using the below java expression but my application is give the below error message

sqrt((var1___4 - var2___4)^2 + (var1___5 - var2___5)^2)

Error Msg : The variable [var___5] inside the jave mathematical expression is not found.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
HarryD
  • 63
  • 1
  • 6
  • 1
    can you include initialization of the variables? – Smern Aug 12 '13 at 15:53
  • 1
    I dont see variable var___5. – Ankur Shanbhag Aug 12 '13 at 15:53
  • 2
    Can you show us the context of your code? You don't even use var___5. – Mirco Widmer Aug 12 '13 at 15:53
  • 5
    In addition the `...^2` is not doing what you think it does. – Henry Aug 12 '13 at 15:54
  • Dude, use some decent variable names. No wonder it gives symbol not found... And ^ is XOR, not power. – m0skit0 Aug 12 '13 at 15:54
  • 5
    Since I doubt any Java exceptions misspell "Java", can you copy/paste the exact error message you're getting? – Michelle Aug 12 '13 at 15:55
  • Side note: Java comes with a [Math.hypot(x, y)](http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#hypot%28double,%20double%29) function, so you can do `Math.hypot(x1 - x2, y1 - y2)`. This has the benefit of not overflowing the final result in case `x*x` or `y*y` would overflow. Sadly, not enough developers are aware of this gem. – Mattias Buelens Aug 12 '13 at 15:57

4 Answers4

6

Here's a function that will calculate distance for you:

public double getDistance(double x1, double y1, double x2, double y2){
    double dx = x1-x2;
    double dy = y1-y2;
    return Math.sqrt(dx * dx + dy * dy);
}

The ^ character is not a valid way to raise a number to a power in java. Here's a link to valid operators in Java.

Also you must reference the static square root function, Math.sqrt, to calculate the root of a number.

I recommend you use a utility function for calculations like this as they're more difficult to read than an well named function (like the one given here.)

Edit:

In comments, Ted suggested using Math.hypot instead of Math.sqrt if you are using java 1.5 or better. The function then, would look like this:

public double getDistance(double x1, double y1, double x2, double y2){
    double dx = x1-x2;
    double dy = y1-y2;
    return Math.hypot(dx, dy);
}

If you are using Java 1.5 or better, this is a better solution because hypot prevents overflow or underflow in computing the square of dx and dy.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • Or if using Java 1.5 or later, simply `return Math.hypot(x1-x2, y1-y2);`. – Ted Hopp Aug 12 '13 at 16:01
  • The var___5 was a typo. I'm using an application which has java mathematical expression to calculate. Consider the var1 as a database table with 5 columns, Col1 is the username and col4 is longitude and col5 is latitude. Then there is another table with the same information (Col1=username, col4=longitude, col5=latitude). I want to compare the two to calculate the distance. I found this jave math expression in the application KB. – HarryD Aug 12 '13 at 16:07
  • Oh wow, I didn't know that existed. Thanks for the knowledge Ted, I'll update my answer and credit you. – William Morrison Aug 12 '13 at 16:08
  • Ok, does this work for you @HarryD? – William Morrison Aug 12 '13 at 16:09
  • @WilliamMorrison - How do I tell it to compare col4 of table1 with col4 of table2? – HarryD Aug 12 '13 at 16:12
  • For each row in the table, retrieve the cell for each of your latitutde,longitude columns, and perform the distance function I've given above. However, if this is latitude and longitude, you probably shouldn't use straightline distance, but something like the [Haversine](http://stackoverflow.com/questions/120283/working-with-latitude-longitude-values-in-java) formula for measuring distance on a spherical object like the earth. – William Morrison Aug 12 '13 at 16:15
1

Here's how:

Math.sqrt(Math.pow(var1___4 - var2___4, 2) + Math.pow(var1___5 - var2___5, 2));

Take note:

  • In Java, all mathematical functions reside in the Math class, as static methods
  • To take the square root of a number n, use Math.sqrt(n)
  • To square a number n, use Math.pow(n, 2) or simply n * n
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

The ^ isn't the Mathematical Power Operator in Java, it is the Bitwise exclusive OR.

So you need to write Math.pow(x, y) instead.

Thereby to fix you code you need to write the following instead.

Math.sqrt(Math.pow(var1___4 - var2___4, 2) + Math.pow(var1___5 - var2___5, 2));
vallentin
  • 23,478
  • 6
  • 59
  • 81
0

The ^ operator isn't used for exponentiation in Java. If you want to square a value, you can use the pow method of the math class:

sqrt(Math.pow((var1___4 - var2___4),2) + Math.pow((var1___5 - var2___5),2));

You're also missing a semi-colon at the end of your statement.

The variable var___5 isn't used in your code sample, but if it's not found then you haven't declared it before using it.

Undefined
  • 655
  • 1
  • 4
  • 13