Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Java Math in Java

Java Math in Java

Math in Java

Java provides a comprehensive Math class that includes a variety of static methods for performing mathematical operations such as basic arithmetic, trigonometric functions, logarithmic functions, and more. The methods in the Math class are part of the java.lang package, so you don’t need to import anything to use them.

Here’s an overview of the most commonly used methods in the Math class:


1. Arithmetic Functions

  • abs(double a): Returns the absolute value of a number.

    System.out.println(Math.abs(-10.5));  // 10.5
  • max(double a, double b): Returns the larger of two numbers.

    System.out.println(Math.max(5, 10));  // 10
  • min(double a, double b): Returns the smaller of two numbers.

    System.out.println(Math.min(5, 10));  // 5
  • pow(double a, double b): Returns a raised to the power of b.

    System.out.println(Math.pow(2, 3));  // 8.0 (2^3)
  • sqrt(double a): Returns the square root of a.

    System.out.println(Math.sqrt(25));  // 5.0
  • cbrt(double a): Returns the cube root of a.

    System.out.println(Math.cbrt(27));  // 3.0
  • round(double a): Rounds the floating-point number to the nearest integer.

    System.out.println(Math.round(5.6));  // 6System.out.println(Math.round(5.3));  // 5
  • random(): Returns a random number between 0.0 (inclusive) and 1.0 (exclusive).

    System.out.println(Math.random());  // A random number between 0.0 and 1.0

2. Trigonometric Functions

  • sin(double a): Returns the sine of the angle a (in radians).

    System.out.println(Math.sin(Math.PI / 2));  // 1.0
  • cos(double a): Returns the cosine of the angle a (in radians).

    System.out.println(Math.cos(Math.PI));  // -1.0
  • tan(double a): Returns the tangent of the angle a (in radians).

    System.out.println(Math.tan(Math.PI / 4));  // 1.0
  • asin(double a): Returns the arcsine of a, in radians.

    System.out.println(Math.asin(1));  // 1.5708 (?/2)
  • acos(double a): Returns the arccosine of a, in radians.

    System.out.println(Math.acos(1));  // 0.0
  • atan(double a): Returns the arctangent of a, in radians.

    System.out.println(Math.atan(1));  // 0.7854 (?/4)
  • toDegrees(double a): Converts an angle from radians to degrees.

    System.out.println(Math.toDegrees(Math.PI));  // 180.0
  • toRadians(double a): Converts an angle from degrees to radians.

    System.out.println(Math.toRadians(180));  // 3.1416 (?)

3. Logarithmic and Exponential Functions

  • log(double a): Returns the natural logarithm (base e) of a.

    System.out.println(Math.log(10));  // 2.302585
  • log10(double a): Returns the base 10 logarithm of a.

    System.out.println(Math.log10(100));  // 2.0
  • exp(double a): Returns e raised to the power of a.

    System.out.println(Math.exp(2));  // 7.389056
  • expm1(double a): Returns e^a - 1.

    System.out.println(Math.expm1(1));  // 1.7182818

4. Hyperbolic Functions

  • sinh(double a): Returns the hyperbolic sine of a.

    System.out.println(Math.sinh(1));  // 1.175201193643801
  • cosh(double a): Returns the hyperbolic cosine of a.

    System.out.println(Math.cosh(1));  // 1.5430806348152437
  • tanh(double a): Returns the hyperbolic tangent of a.

    System.out.println(Math.tanh(1));  // 0.7615941559557649

5. Miscellaneous Functions

  • PI: A constant for the value of Pi.

    System.out.println(Math.PI);  // 3.141592653589793
  • E: A constant for the value of e (Euler's number).

    System.out.println(Math.E);  // 2.718281828459045
  • ulp(double a): Returns the size of the smallest adjacent floating-point number to a.

    System.out.println(Math.ulp(2.0));  // 4.44089209850063E-16

Example Usage

Here’s an example demonstrating various Math methods in Java:

public class MathExample {    public static void main(String[] args) {        // Arithmetic functions        System.out.println("Absolute value of -5: " + Math.abs(-5));  // 5        System.out.println("Max of 5 and 10: " + Math.max(5, 10));  // 10        System.out.println("2 raised to the power 3: " + Math.pow(2, 3));  // 8.0        // Trigonometric functions        System.out.println("Sine of 90 degrees: " + Math.sin(Math.toRadians(90)));  // 1.0        System.out.println("Cosine of 0 degrees: " + Math.cos(Math.toRadians(0)));  // 1.0        // Logarithmic functions        System.out.println("Natural log of 10: " + Math.log(10));  // 2.302585        System.out.println("Base 10 log of 100: " + Math.log10(100));  // 2.0        // Random number        System.out.println("Random number: " + Math.random());        // Hyperbolic functions        System.out.println("Hyperbolic sine of 1: " + Math.sinh(1));  // 1.175201    }}

Output:

Absolute value of -5: 5Max of 5 and 10: 102 raised to the power 3: 8.0Sine of 90 degrees: 1.0Cosine of 0 degrees: 1.0Natural log of 10: 2.302585Base 10 log of 100: 2.0Random number: 0.32843753456226585Hyperbolic sine of 1: 1.175201

Conclusion

The Math class in Java provides an extensive set of static methods for performing mathematical calculations. From basic arithmetic to advanced functions like logarithms, trigonometry, and hyperbolic functions, Java's Math class can handle a wide range of mathematical tasks. Let me know if you'd like to dive deeper into any specific function or topic! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql