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.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.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 used to raise the value specified in the a, the first argument, (the base), to the power of the value specified in b, the second argument, (the exponent). The exponent tells us how many times the base should be multiplied by itself. So, if I have the number of 5, and I want to raise it to the 3rd power, it means that I have to multiply 5 by itself for 3 times.

There are some special cases for the Math.pow() method, as you can read in the Javadocs, here four of them taken from the Javadocs:

  • If the second argument is positive or negative zero, then the result is 1.0.
  • If the second argument is 1.0, then the result is the same as the first argument.
  • If the second argument is NaN, then the result is NaN.
  • If the first argument is NaN and the second argument is nonzero, then the result is NaN.

You can read more detail about the Math.pow() method on the following Javadocs, Math.pow().

Let’s see a code snippet as an example:

package org.kodejava.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);

        // Raised to zero
        System.out.println("Math.pow(2, 0): " + Math.pow(2, 0));

        // Raised to one
        System.out.println("Math.pow(2, 1): " + Math.pow(2, 1));

        // Raised to NaN
        System.out.println("Math.pow(2, Double.NaN): " + Math.pow(2, Double.NaN));

        // NaN raised to the nonzero exponent
        System.out.println("Math.pow(Double.NaN, 2): " + Math.pow(Double.NaN, 2));
    }
}

Our program print the following output:

5.0 cubed is 125.0
Math.pow(2, 0): 1.0
Math.pow(2, 1): 2.0
Math.pow(2, Double.NaN): NaN
Math.pow(Double.NaN, 2): NaN

How do I calculate logarithm?

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

package org.kodejava.math;

public class LogarithmExample {

    public static void main(String[] args) {
        double number1 = 1000.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 used to round the value of a number. These methods are Math.ceil(), Math.floor() and Math.round().

package org.kodejava.math;

public class GetRoundedValueExample {

    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