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

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.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 cubeRoot = Math.cbrt(cube);
        System.out.println("Cube root of " + cube + " is " + cubeRoot);

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

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 reread the content of a buffer?

The example shown below tells you how to reread the contents of a buffer. To reread the data from a buffer we can use the buffer’s rewind() method. This method set the position back to 0 while the limit is unchanged, it still holds the value of how many data can be read from the buffer.

package org.kodejava.io;

import java.nio.CharBuffer;

public class BufferRewind {
    public static void main(String[] args) {
        CharBuffer buffer = CharBuffer.allocate(1024);
        buffer.put("The quick brown fox jumps over the lazy dog.");
        buffer.flip();

        // Read buffer's data using the get() method call.
        while (buffer.hasRemaining()) {
            System.out.print(buffer.get());
        }
        System.out.println();

        // Rewind the buffer will set the position back to 0.
        // We rewind the buffer so that we can reread the buffer
        // data for another purposes.
        buffer.rewind();

        // Reread the buffer and append its data to a StringBuilder
        StringBuilder bufferText = new StringBuilder();
        while (buffer.hasRemaining()) {
            bufferText.append(buffer.get());
        }
        System.out.println(bufferText);
    }
}

The output of the code snippet:

The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

How do I use trigonometric calculation methods?

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

package org.kodejava.math;

public class TrigonometricExample {
    public static void main(String[] args) {
        double radians = 1.0d;

        double sine = Math.sin(radians);
        double cosine = Math.cos(radians);
        double tan = Math.tan(radians);

        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