# java.lang.Math.hypot J# equivalent?

### Question

• I'm a total newbie to J#, I'm a C# and VB.NET programmer. I know a bit about Java, but not at a professional level, and I need to convert a Java program to J#. The part that I'm stuck on is I need to find a J# equivalent method of:

java.lang.Math.hypot

Any ideas? Also, I need to have the same exact results, not a result that is "close enough and will suffice."

Thanks:)
Wednesday, May 31, 2006 3:38 PM

• java.lang.Math.hypot(x, y) is identical with System.Math.Sqrt(x * x + y * y).

CLR handles Infinity and NaN correctly.

Thursday, June 01, 2006 1:30 PM
• Hi,

We are not supporting this hypot function in our libraries.I have a snippet of code to solve your problem

public static double hypot(double a, double b)

{

a = Math.abs(a);

b = Math.abs(b);

if (a < b)

{

double temp = a;

a = b;

b = temp;

}

if (a == 0.0)

return 0.0;

else

{

double ba = b / a;

return a * Math.sqrt(1.0 + ba * ba);

}

}

I think this will solve your problem.

regards,

Thilak

Thursday, June 01, 2006 1:55 PM

### All replies

• java.lang.Math.hypot(x, y) is identical with System.Math.Sqrt(x * x + y * y).

CLR handles Infinity and NaN correctly.

Thursday, June 01, 2006 1:30 PM
• Hi,

We are not supporting this hypot function in our libraries.I have a snippet of code to solve your problem

public static double hypot(double a, double b)

{

a = Math.abs(a);

b = Math.abs(b);

if (a < b)

{

double temp = a;

a = b;

b = temp;

}

if (a == 0.0)

return 0.0;

else

{

double ba = b / a;

return a * Math.sqrt(1.0 + ba * ba);

}

}

I think this will solve your problem.

regards,

Thilak

Thursday, June 01, 2006 1:55 PM
• thanks, that worked like a charm:)
Thursday, June 01, 2006 6:08 PM