## How do I calculate the length of hypotenuse?

Hypot is a mathematical function defined to calculate the length of the hypotenuse of a right-angle triangle. It was designed to avoid errors arising due to limited precision calculations performed on computers.

The Math.hypot(double x, double y) return the sqrt(x2 + y2) without intermediate overflow or underflow. The result will be same with this calculation
Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)).

package org.kodejava.example.math;

public class HypotExample {
public static void main(String[] args) {
double number1 = 3.0d;
double number2 = 5.0d;

//
// calculate square root of total value of
// number1 ^ 2 + number2 ^ 2
//
double sqr = Math.hypot(number1, number2);

System.out.println("Total value= " + (Math.pow(number1, 2) +
Math.pow(number2, 2)));
System.out.println("Square root= " + sqr);

}
}

## How do I calculate exponential function?

In mathematics, the exponential function is the function ex, where e is the number (approximately 2.718281828) such that the function ex equals its own derivative. The function f(x) = ex at the point x = 0 is equal to 1.

package org.kodejava.example.math;

public class ExponentExample {

public static void main(String[] args) {
double x = 0.0d;

//
// calculates e raised to the power of x (e^x)
//
double fx = Math.exp(x);

//
// calculates e raised to the power of x minus 1 (e^x - 1)
//
double fxm1 = Math.expm1(x);

System.out.println("fx  = " + fx);
System.out.println("fxm1= " + fxm1);
}
}

## How do I get the exponent of exponential function?

The exponential function is f(x) = ex. The Math.getExponent() method is used to get the x value of the given parameter, in which the parameter is a result of exponential function calculation.

package org.kodejava.example.math;

public class GetExponent {
public static void main(String[] args) {
double fx = 1.0d;
float fx1 = 1.0f;

int x = Math.getExponent(fx);
System.out.println("Exponent  = " + x);

//
// argument in float
//
int xf = Math.getExponent(fx1);
System.out.println("Exponent1 = " + xf);
}
}

## How do I get signum function of a number?

The code below show you how to get the signum function of a number using the Math.signum() static method call. This method extracts the sign of a real number. If you have a number of x the signum function of x is -1 if x < 0; 0 if x = 0 and 1 if x > 0.

package org.kodejava.example.math;

public class SignumExample {

public static void main(String[] args) {
Double zero = 0.0D;
Double negative = -25.0D;
Double positive = 15.0D;

//
// Get the signum function of value of a number.
// It returns:
// * 0 if the value is zero.
// * 1.0 if value is greater than zero.
// * -1.0 if value is less than zero.
//
double sign1 = Math.signum(zero);
double sign2 = Math.signum(negative);
double sign3 = Math.signum(positive);

//
// For floating-point value
//
float sign4 = Math.signum(zero.floatValue());
float sign5 = Math.signum(negative.floatValue());
float sign6 = Math.signum(positive.floatValue());

System.out.println("In double:");
System.out.println("Signum of " + zero + " is " + sign1);
System.out.println("Signum of " + negative + " is " + sign2);
System.out.println("Signum of " + positive + " is " + sign3);

System.out.println("In float:");
System.out.println("Signum of " + zero + " is " + sign4);
System.out.println("Signum of " + negative + " is " + sign5);
System.out.println("Signum of " + positive + " is " + sign6);
}
}

Here is the output of the program:

In double:
Signum of 0.0 is 0.0
Signum of -25.0 is -1.0
Signum of 15.0 is 1.0
In float:
Signum of 0.0 is 0.0
Signum of -25.0 is -1.0
Signum of 15.0 is 1.0

## How do I raised a number to the power of n?

The static method Math.pow(double a, double b) can be use to raised the value specified in the a to the power of the value specified in b.

package org.kodejava.example.math;

public class PowerExample {

public static void main(String[] args) {
double cubeRoot = 5d;

//
// Get the cubed number of cube root
// x cubed = x^3 (multiplication three times)
//
double cubed = Math.pow(cubeRoot, 3);
System.out.println(cubeRoot + " cubed is " + cubed);
}
}

Our program print the following output:

5.0 cubed is 125.0

## How do I calculate logarithm?

We can calculate logarithm using the Math.log10() and the Math.log() static method call.

package org.kodejava.example.math;

public class LogarithmExample {

public static void main(String[] args) {
double number1 = 1000.0d;
double number2 = 999.0d;

//
// Get common logarithm
//
double log10 = Math.log10(number1);
System.out.println("Common logarithm of " + number1 + " = " + log10);

//
// Get natural logarithm
//
double loge = Math.log(number1);
System.out.println("Natural logarithm of " + number1 + " = " + loge);
}
}

The output of the program is:

Common logarithm of 1000.0 = 3.0
Natural logarithm of 1000.0 = 6.907755278982137

## How do I round a number?

The example below show you some methods of the Math class that can be use to round the value of a number. These methods are Math.ceil(), Math.floor() and Math.round().

package org.kodejava.example.math;

public class GetRoundingValueExample {

public static void main(String[] args) {
Double number = 1.5D;

//
// Get the smallest value that is greater than or equal to the
// argument and is equal to a mathematical integer
//
double roundUp = Math.ceil(number);
System.out.println("Result of rounding up of " + number + " = " + roundUp);

//
// Get the largest value that is less than or equal to the
// argument and is equal to a mathematical integer
//
double roundDown = Math.floor(number);
System.out.println("Result of rounding down of " + number + " = " + roundDown);

//
// Get the closest long value to the argument
//
long round1 = Math.round(number);
System.out.println("Rounding result of " + number + " (in long) =  " + round1);

//
// Get the closest int value to the argument
//
int round2 = Math.round(number.floatValue());
System.out.println("Rounding result of " + number + " (in int) = " + round2);
}
}

Here are the result of the program:

Result of rounding up of 1.5 = 2.0
Result of rounding down of 1.5 = 1.0
Rounding result of 1.5 (in long) =  2
Rounding result of 1.5 (in int) = 2

## How do I calculate cube root and square root of a number?

To calculate the cube root and the square root of a double value we can use the Math.cbrt(double a) and Math.sqrt(double a) static method call.

package org.kodejava.example.math;

public class CubeSquareRootExample {

public static void main(String[] args) {
double cube = 125.0d;
double square = 100.0d;

//
// Get the cube root of double value
//
double cbrt = Math.cbrt(cube);
System.out.println("Cube root of " + cube + " is " + cbrt);

//
// Get the square root of double value
//
double sqrt = Math.sqrt(square);
System.out.println("Square root of " + square + " is " + sqrt);
}

}

This snippet will print the following output:

Cube root of 125.0 is 5.0
Square root of 100.0 is 10.0

## How do I use trigonometric calculation methods?

This example demonstrates how to use the trigonometric methods of the Math class. You can see the use of method such as Math.sin(), Math.cos(), Math.tan(), etc.

package org.kodejava.example.math;

public class TrigonometricExample {
public static void main(String[] args) {

double asine = Math.asin(sine);
double acosine = Math.acos(cosine);
double atan = Math.atan(tan);

System.out.println("Sine of " + radians + " = " + sine);
System.out.println("Cosine of " + radians + " = " + cosine);
System.out.println("Tangent of " + radians + " = " + tan);
System.out.println("Arcsine of " + sine + " = " + asine);
System.out.println("Arccosine of " + cosine + " = " + acosine);
System.out.println("Arctangent of " + tan + " = " + atan);
}
}

The output of the program are:

Sine of 1.0 = 0.8414709848078965
Cosine of 1.0 = 0.5403023058681398
Tangent of 1.0 = 1.5574077246549023
Arcsine of 0.8414709848078965 = 1.0
Arccosine of 0.5403023058681398 = 1.0
Arctangent of 1.5574077246549023 = 1.0

## How do I convert angle from radians to degrees?

The example below show you how to convert an angle measured in radians into degrees and vice versa. We can use the Math.toDegrees() and Math.toRadians() method call to do the conversion.

package org.kodejava.example.math;

public static void main(String[] args) {
double degrees = 45d;

//
// Converts an angle measured in radians to an
// approximately equivalent angle measured in
// degrees.
//

//
// Converts an angle measured in degrees to an
// approximately equivalent angle measured in