In Java, various mathematical functions such as root, absolute value, sine and other trigonometric functions are often used.

To use them, we need to get to know the Math library from the java.lang package. A few of the most important functions are below.

  • sqrt (double number) – returns the root of the number double. As a parameter we can specify any numeric type, then it will be automatically converted it to double.
  • pow (double a, double b) – returns the number a raised to the power of b
  • abs (number) – the parameter can be any number, the method returns the absolute value from the argument
  • you can learn more about these methods here in Math Class

All methods are static, to invoke them use the structure:

Let’s see it on a simple example of a program that calculates the root of a number and then raises it to the 2rd power.

First, we declare variables: number – our main number, b – power, and then using mathematical functions we calculate the root and power. At the end, we display our results on the console.

An interesting thing worth mentioning here is the static import. Thanks to its application, we will be able to omit the prefixes of Math in front of function names. Below is the example.

As you can see before the start of the class body, we had to add a header in the form:

This improves the readability of our code.

In the Math class, there are also two constant PIs and Es, thanks to them we do not have to declare our own pi and e numbers. We call them like functions.

Big Numbers

It may happen that even the range of long or double types is not enough for our calculations. What to do then? In Java, there are two classes to store really huge numbers that offer additional mathematical functions.

  • BigInteger – a class for large integers
  • BigDecimal – a class for large floating point numbers

Their use in traditional programs is not very convenient, because you can not directly assign the BigInteger value to the int value, even though BigInteger would store a number in the int range. This is due to the fact that the BigInteger class is an object type. Both classes also have constants, which are often used, the most important are:

  • BigInteger.ONE / BigDecimal.ONE
  • BigInteger.ZERO / BigDecimal.ZERO

To create a new BigInteger or BigDecimal type variable, we must first create an object. Don’t forget to import your class.

First, we create a large number of type, notice that we give it in the form of a String. However, you can also use a different form.

In this case, however, remember that the valueOf () method argument should not exceed the long or double range for the BigDecimal class. With the String method they can be practically unlimited. To add two huge numbers, we can not use standard operators, use: add (), subtract (), multiply (), divide ().

Here we created two big numbers a and b. Then we calculated their sum thanks to the construction of a.add (b), which returns the result of adding two BigInteger numbers. We display the result thanks to the fact that the BigInteger class has a toString () method that converts the number to a string.

LEAVE A REPLY

Please enter your comment!
Please enter your name here