# Class Math. Exponential functions

This topic is a continuation of the topic:

### Contents

Search other resources:

##### 1. List of exponential functions of class Math

The general list of exponential functions is as follows:

• cbrt() – returns the cube root of the specified argument;
• exp() – returns the exponent of the argument;
• expm1() – returns the exponent of the argument minus 1;
• log() – returns the natural logarithm;
• log10() – returns the algorithm with base 10;
• log1p() – returns the algorithm from the argument plus 1;
• pow() – raising to a power;
• scalb() – returns the result x*2^index;
• sqrt() returns the square root of the specified argument.

### ⇑

##### 2. Function cbrt(). Cubic root

The cbrt() function returns the cube root of its argument. According to the documentation, the general form of a function declaration is as follows

`double Math.cbrt(double a)`

here

• a – the number from which the cube root is obtained.

When using a function, the following possible special argument values are considered:

• a = NaN – the result of the function call is equal to NaN;
• a = Infinity – the result is equal to Infinity;
• a = 0.0 – the result is 0.0.

Example.

```public class MathFunctions {

public static void main(String[] args) {

// Function cbrt()
// 1. Positive Number
double x = 27.0;
double res = Math.cbrt(x); // res = 3.0
System.out.println("cbrt(27.0) = " + res);

// 2. Negative number
res = Math.cbrt(-729.0); // res = -9.0
System.out.println("cbrt(-249.0) = " + res);
}
}```

Program result

```cbrt(27.0) = 3.0
cbrt(-249.0) = -9.0```

### ⇑

##### 3. Function exp(). Exponent

The exp() function raises the number e (e = 2.71828) to the power given by the function argument. Function declaration is as follows

`double Math.exp(double a)`

here

• a is the number to which the exponent is raised.

The result can be processed according to the value of the argument a, which can be equal to:

• numerical value. In this case, the corresponding numerical result is obtained;
• NaNNaN value is obtained;
• positive infinity (for example, 1.0/0.0) – the result of positive infinity (Infinity) is returned;
• negative infinity (for example, -1.0/0.0) – 0.0 is returned.

Example.

```public class MathFunctions {

public static void main(String[] args) {
// 1. Function exp()
// 1.1. Argument is a positive number
double x = 1.0;
double res = Math.exp(x); // res = 2.718281828459045
System.out.println("exp(1.0) = " + res);

// 1.2. Argument is a negative number
res = Math.exp(-1.0); // res = 0.36787944117144233
System.out.println("exp(-1.0) = " + res);

// 1.3. Argument - positive infinity
res = Math.exp(1.0/0.0); // res = Infinity
System.out.println("exp(1.0/0.0) = " + res);

// 1.4. Argument - negative infinity
res = Math.exp(-1.0/0.0); // res = 0.0
System.out.println("exp(-1.0/0.0) = " + res);
}
}```

Result

```exp(1.0) = 2.718281828459045
exp(-1.0) = 0.36787944117144233
exp(1.0/0.0) = Infinity
exp(-1.0/0.0) = 0.0```

### ⇑

##### 4. Function expm1(). Calculate exp(x)-1

For the given x, the expm1() function evaluates the expression

According to the documentation for a given argument x of the function declaration the following

`double Math.expm1(double x)`

The function is useful for obtaining more accurate values than the ex function when the x argument is close to 0. In this case, the program implements the call

`expm1(x) + 1`

Depending on the value of the x argument, the following special situations are considered:

• x – any positive number. In this case, the function result will be greater than or equal to -1.0;
• x = NaN – the function returns NaN;
• x = Infinity (positive infinity) – the function returns Infinity;
• x = -Infinity (negative infinity) – return result –1.0;
• x = 0.0 – the result is 0.0.

Example.

```public class MathFunctions {

public static void main(String[] args) {
// 1. Function expm1()
// 1.1. Argument is a positive number
double x = 1.0;
double res = Math.expm1(x); // res = 1.718281828459045
System.out.println("expm1(1.0) = " + res);

// 1.2. Argument is a negative number
res = Math.expm1(-2.0); // res = -0.8646647167633873
System.out.println("expm1(-2.0) = " + res);

// 1.3. Argument - positive infinity
res = Math.expm1(1.0/0.0); // res = Infinity
System.out.println("expm1(1.0/0.0) = " + res);

// 1.4. Argument - negative infinity
res = Math.expm1(-1.0/0.0); // res = -1.0
System.out.println("expm1(-1.0/0.0) = " + res);
}
}```

Result

```expm1(1.0) = 1.718281828459045
expm1(-2.0) = -0.8646647167633873
expm1(1.0/0.0) = Infinity
expm1(-1.0/0.0) = -1.0```

### ⇑

##### 5. Function log(). Natural logarithm

To get the logarithm to base e (the natural logarithm), use the log() function. The function declaration is as follows

`double Math.log(double a)`

For the argument a, the following possible situations are considered:

• if a = NaN, then the function returns NaN;
• a = Infinity – the function returns Infinity;
• a = 0.0 – the function returns -Infinity.

Example.

```public class MathFunctions {

public static void main(String[] args) {
// 1. Function log() - natural logarithm
// 1.1. Argument - positive number
double x = Math.exp(1.0);
double res = Math.log(x); // res = 1.0
System.out.println("log(exp(1.0)) = " + res);

res = Math.log(1.0); // res = 0.0
System.out.println("log(1.0) = " + res);

res = Math.log(5.5); // res = 1.7047480922384253
System.out.println("log(5.5) = " + res);

// 1.2. Argument - null value
res = Math.log(0.0); // res = -Infinity
System.out.println("log(0.0) = " + res);

// 1.3. Argument - negative value
res = Math.log(-1.0); // res = NaN
System.out.println("log(-1.0) = " + res);

// 1.4. Argument - NaN
res = Math.log(Math.log(-1.0)); // res = NaN
System.out.println("log(NaN) = " + res);
}
}```

Result

```log(exp(1.0)) = 1.0
log(1.0) = 0.0
log(5.5) = 1.7047480922384253
log(0.0) = -Infinity
log(-1.0) = NaN
log(NaN) = NaN```

### ⇑

##### 6. Function log10(). Base 10 logarithm

The log10() function is used to get the value of the base 10 logarithm. The general form of a function declaration

`double java.lang.Math.log10(double a)`

here

• a is the value from which you want to get the logarithm.

The result obtained depends on the value of a, for which the following cases are considered:

• if a = NaN or a < 0, then the result is also NaN;
• if a > 0 , then the result is also a positive number;
• if a = 0, then the result is equal to Infinity (infinity);
• if a = 10n for an integer n, then the result is n.

Example.

```public class MathFunctions {

public static void main(String[] args) {
// Function log10() - base 10 logarithm
// 1. Argument - positive number
double x = 100;
double res = Math.log10(x); // res = 2.0
System.out.println("log10(100) = " + res);

res = Math.log10(1.0); // res = 0.0
System.out.println("log10(1.0) = " + res);

// 2. Argument - null value
res = Math.log10(0.0); // res = -Infinity
System.out.println("log10(0.0) = " + res);

// 3. Argument - negative value
res = Math.log10(-10.0); // res = NaN
System.out.println("log10(-10.0) = " + res);

// 4. Argument - NaN
res = Math.log10(Math.log10(-1.0)); // res = NaN
System.out.println("log10(NaN) = " + res);

// 5. Argument - Infinity
res = Math.log10(5.0/0.0); // res = Infinity
System.out.println("log10(Infinity) = " + res);
}
}```

Result

```log10(100) = 2.0
log10(1.0) = 0.0
log10(0.0) = -Infinity
log10(-10.0) = NaN
log10(NaN) = NaN
log10(Infinity) = Infinity```

### ⇑

##### 7. Function log1p(). Logarithm of the argument + 1

The log1p() function returns the natural logarithm of the sum of the argument value and 1. The log1p(x) function is used for more accurate results than the log(1.0+x) call.

The general form of a function declaration

`double log1p(double x)`

where x is the value for which the result is obtained.

Depending on the value of x, the following cases are considered:

• if x = NaN or x < -1, then the result is NaN;
• if x = -1.0, then the result is -Infinity;
• if x = Infinity, then the function returns Infinity;
• if x = 0.0, then the function returns 0.0.

Example.

```public class MathFunctions {

public static void main(String[] args) {
// Function log1p(x) => log(x + 1)
// 1. Argument x > -1.0
double x = 1.71;
double res = Math.log1p(x); // res = 0.9969486348916095
System.out.println("log1p(101) = " + res);

// 2. x = NaN
res = Math.log1p(Math.log(-10)); // res = NaN
System.out.println("log1p(NaN) = " + res);

// 3. x < -1.0
res = Math.log1p(-2.5);
System.out.println("log1p(x<-1) = " + res);

// 4. x = -1.0
res = Math.log1p(-1.0); // res = -Infinity
System.out.println("log1p(-1) = " + res);

// 5. x = Infinity
res = Math.log1p(5.0/0.0); // res = Infinity
System.out.println("log1p(Infinity) = " + res);

// 6. x = 0.0
res = Math.log1p(0.0); // res = 0.0
System.out.println("log1p(0) = " + res);
}
}```

Result

```log1p(101) = 0.9969486348916095
log1p(NaN) = NaN
log1p(x<-1) = NaN
log1p(-1) = -Infinity
log1p(Infinity) = Infinity
log1p(0) = 0.0```

### ⇑

##### 8. Function pow(). Exponentiation

According to the documentation, the general form of a function declaration is as follows

`double pow(double a, double b)`

here

• a – base, which is raised to the power b;
• b – exponent.

Accordingly, the function call looks something like this

`res = pow(a, b);`

Arguments a, b can take different values. Based on this context, the following cases are considered:

• if b = -0.0 or b = 0.0 then the result is 1.0 (res = 1.0);
• if b = 1.0 then res = a;
• if b = NaN then res = NaN;
• if a = NaN and b ≠ 0 then res = NaN;
• if |a| > 1 and b = Infinity then res = Infinity;
• if |a| < 1 and b = -Infinity then res = Infinity;
• if |a| > 1 and b = -Infinity then res = +0.0;
• if |a| < 1 and b = Infinity then res = +0.0;
• if |a| = 1 and b = Infinity then res = NaN;
• if a = +0.0 and b > 0 then res = +0.0;
• if a = Infinity and b < 0 then res = +0.0;
• if a = +0.0 and b < 0 then res = Infinity;
• if a = Infinity and b > 0.0 then res = Infinity;
• if a = -0.0 and b > 0.0 and b is not an odd integer, then res = +0.0;
• if a = -Infinity and b < 0.0 and b is not an odd integer, then res = +0.0;
• if a = -0.0 and b is an odd integer then res = -0.0;
• if a = -0.0 and b < 0.0 and b is not an odd integer then res = Infinity;
• if a = -Infinity and b > 0.0 and b is not an odd integer then res = Infinity;
• if a = -0.0 and b is a negative odd integer then res = -Infinity;
• if a = -Infinity and b is a positive odd integer then res = -Infinity;

Example.

```public class MathFunctions {

public static void main(String[] args) {
// The pow() function - exponentiation
// 1. Regular numbers
double a = 2.5;
double b = 3.8;
double res = Math.pow(a, b); // res = 32.52160966413567
System.out.println("2.5 ^ 3.8 = " + res);

res = Math.pow(8, 0); // res = 1.0
System.out.println("8 ^ 0 = " + res);

res = Math.pow(0.0, 2.5);
System.out.println("0 ^ 2.5 = " + res);

// 2. Необычные ситуации
res = Math.pow(5.0/0.0, -2.0); // res = 0.0
System.out.println("Infinity ^ -2.0 => " + res);

res = Math.pow(3.0, 1.0/0.0); // res = Infinity
System.out.println("3.0 ^ Infinity = > " + res);

res = Math.pow(3.0, -1.0/0.0); // res = 0.0
System.out.println("3.0 ^ -Infinity => " + res);
}
}```

Result

```2.5 ^ 3.8 = 32.52160966413567
8 ^ 0 = 1.0
0 ^ 2.5 = 0.0
Infinity ^ -2.0 => 0.0
3.0 ^ Infinity = > Infinity
3.0 ^ -Infinity => 0.0```

### ⇑

##### 9. Function scalb(). Exponentiation to the power of a multiple of 2

The scalb() function returns the product of the given number times 2 to the given power. The general form of a function declaration

`double scalb(double d, int scaleFactor)`

here

• d is a number that is multiplied by 2 to the power of scaleFactor;
• scaleFactor – exponent of a power of two.

When the function is called, the result is returned

`d × 2scaleFactor`

The exact result is returned if the exponent of the result is between Double.MIN_EXPONENT and Double.MAX_EXPONENT. Otherwise, the result is infinity.

When using the function, the following possible cases are considered:

• if d = NaN, then the result is NaN;
• if d = Infinity, then the function returns Infinity;
• if d = -Infinity, then the function returns –Infinity;
• if d = 0, then the function returns 0.

Example.

```public class MathFunctions {

public static void main(String[] args) {
// scalb() function - exponentiation multiple of 2
// 1. Regular numbers
double a = 2.5;
int b = 3;
double res = Math.scalb(a, b); // res = 2.5*2^3 = 20.0
System.out.println("scalb(2.5, 3) = " + res);

// 2. Unusual situations
res = Math.scalb(Math.log(-2), 2); // res = NaN
System.out.println("scalb(NaN, 2) = " + res);

res = Math.scalb(5.0/0.0, 4); // res = Infinity
System.out.println("scalb(Infinity, 4) = " + res);

res = Math.scalb(0.0, 2); // res = 0.0
System.out.println("scalb(0.0, 2) = " + res);
}
}```

Result

```scalb(2.5, 3) = 20.0
scalb(NaN, 2) = NaN
scalb(Infinity, 4) = Infinity
scalb(0.0, 2) = 0.0```

### ⇑

##### 10. Function sqrt(). Square root

The sqrt() function is used to extract the square root of a number. The general form of a function declaration

`double java.lang.Math.sqrt(double a)`

here a is the argument from which you want to get a number.

The following special cases are considered when using the function:

• if a = NaN then the result is also NaN;
• if a < 0, then the result is NaN;
• if a = Infinity, then the result is equal to Infinity;
• if a = 0, then the result is a.

Example.

```public class MathFunctions {

public static void main(String[] args) {
// Function sqrt() - square root
// 1. The root of a positive number
double x = 121.0;
double res = Math.sqrt(x);
System.out.println("sqrt(121) = " + res);

// 2. The root of NaN
res = Math.sqrt(Double.NaN); // res = NaN
System.out.println("sqrt(NaN) = " + res);

// 3. The root of a negative number
res = Math.sqrt(-1.0); // res = NaN
System.out.println("sqrt(-1) = " + res);

// 4. Root from Infinity
res = Math.sqrt(Double.POSITIVE_INFINITY); // res = Infinity
System.out.println("sqrt(Infinity) = " + res);

// 5. The root of 0
res = Math.sqrt(0.0); // sqrt(0) = 0.0
System.out.println("sqrt(0) = " + res);
}
}```

Result

```sqrt(121) = 11.0
sqrt(NaN) = NaN
sqrt(-1) = NaN
sqrt(Infinity) = Infinity
sqrt(0) = 0.0```