AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Java Math: How to use this class?

PARTAGEZ

With Java Mathyou can perform simple calculations. The methods are numerous and also cover logarithms and trigonometry. The syntax is relatively simple and therefore easy to learn.

What is Java Math?

If you want to perform operations with simple numerical calculations, the programming language knows the standard for this Math. This Java class does not need to be imported separately and has many methods that we will further list and explain in more detail during this article. The class Math is not not instantiated and all its methods are used statically. Even the two constants of the class are static: the Euler number (approximately e = 2.7182818284590) as the base for the natural logarithm and the natural exponential function, as well as the circular number Pi (approximately π = 3.1415926535). The class Math is contained in the java.lang package. The result type of the calculations is usually « double ».

Web hosting with personal advisor!

Powerful, flexible and high-performance web hosting with email box, personal advisor and domain included 1time year !

Domain

SSL Certificate

24/7 Support

Determine the absolute value

The operation and syntax of the Java class Math can be better illustrated by examples and in relation to the different methods. If you want to determine the absolute value of a parameter, you can use abs(). By absolute value we mean the distance between a number and 0 or an unsigned number. The result will therefore always be positive. The types allowed for this method are double, float, int And long. Therefore Math.abs works with a positive number. For output, we use here and in the following examples the Java command System.out.println.

public class Main {
public static void main(String args[]) {
int nombre = +7;
System.out.println("Le nombre d'origine est : " + nombre);
System.out.println("La valeur absolue est : Math.abs(" + nombre + ") = " + Math.abs(nombre));
}
}

Java

The corresponding output looks like this:

Le nombre d'origine est : 7
La valeur absolue est : Math.abs(7) = 7

Java

You can also use a negative initial value. The result will still be positive. If we take the same example as before:

public class Main {
public static void main(String args[]) {
int nombre = -7;
System.out.println("Le nombre d'origine est : " + nombre);
System.out.println("La valeur absolue est : Math.abs(" + nombre + ") = " + Math.abs(nombre));}
}

Java

The output is basically the same as in the previous example:

Le nombre d'origine est : -7
La valeur absolue est : Math.abs(-7) = 7

Java

So the method ignores the sign of the negative integer (-7) and therefore also returns « 7 » as the result.

Show highest value

The method max() allows you to determine the larger value of two inputs. It looks like this:

public class Main {
public static void main(String args[]) {
double nombre = Math.max(3, 9);
System.out.println("Le nombre le plus grand est : " + nombre);
}
}

Java

The corresponding output is this:

Le nombre le plus grand est : 9.0

Java

Determine the smallest value

The class Math in Java works very similarly if you want to determine the smallest value. The appropriate method is min() :

public class Main {
public static void main(String args[]) {
double nombre = Math.min(3, 9);
System.out.println("Le nombre le plus petit est : " + nombre);
}
}

Java

You then get the following output:

Le nombre le plus petit est : 3.0

Java

Calculate a power

While the previous examples were still quite simple, there are also more sophisticated calculations than the Java class Math can take care of it for you. The method for calculating power is called for example pow(). To do this, we first define a base and an exponent and then let the calculation run.

public class Main {
public static void main(String args[]) {
double base = 4;
double exposant = 2;
double puissance = Math.pow(base, exposant);
System.out.println("Le résultat est : " + puissance);
}
}

Java

This is what our output looks like:

Le résultat est : 16.0

Java

Take the square root

The class can also be used for calculating square roots with the method sqrt(). In the following example, we calculate the square root of 64:

public class Main {
public static void main(String args[]) {
double nombre = 64;
double racine = Math.sqrt(nombre);
System.out.println("Le résultat est : " + racine);
}
}

Java

We thus obtain this output:

Le résultat est : 8.0

Java

Determine random numbers

With the method random()you get a random number that is either between 0.0 and 1.0 or in a range that you define yourself. We show you both possibilities:

public class Main {
public static void main(String args[]) {
double nombreAleatoire;
nombreAleatoire = Math.random();
System.out.println(nombreAleatoire);
}
}

Java

A possible output would then be for example this:

You can also limit the possible results and only allow integers between 0 and 100. To do this, use the following code:

public class Main {
public static void main(String args[]) {
int nombreAleatoire = (int) (Math.random() * 101);
System.out.println(nombreAleatoire);
}
}

Java

This allows you to get random results like this:

List of the most important methods

There are many methods you can use with the Java class Math. Here are the most important ones:

Method Function
abs() Returns the absolute value of an argument.
max() Returns the larger of two values.
min() Returns the smaller of the two values.
pow() Returns the value of the power.
sqrt() Calculate the square root.
random() Makes a random double value.
cbrt() Calculates the cube root.
log() Returns the natural logarithm of a double value.
sin() Calculates the sine of a double value.
cos() Calculates the cosine of a double value.
tan() Calculates the tangent value of a double value.
round() Rounds a double value up or down to an integer.
negateExact() Indicates the opposite value of an argument.
floor() Rounds the largest double value that is less than or equal to the given argument.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Web Marketing

Arm servers: overview

Arm servers are servers based on ARM architecture and not on the X86 instructions. As the demand for more efficient servers of servers for the

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact