Package java.lang

Class Math

  • All Implemented Interfaces:
    Modified

    public final class Math
    extends Object
    implements Modified
    Class Math provides basic math constants and operations such as trigonometric functions, hyperbolic functions, exponential, logarithms, etc.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double E
      The double value closest to e, the base of the natural logarithm.
      static double PI
      The double value closest to pi, the ratio of a circle's circumference to its diameter.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double abs​(double d)
      Returns the absolute value of the argument.
      static float abs​(float f)
      Returns the absolute value of the argument.
      static int abs​(int i)
      Returns the absolute value of the argument.
      static long abs​(long l)
      Returns the absolute value of the argument.
      static double acos​(double x)
      Returns the arc cosinus of an angle in radians
      static double atan​(double arg)
      Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
      static double atan2​(double arg1, double arg2)
      Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
      static int ceil​(float x)
      Round toward positive infinity
      static double cos​(double arg)
      Returns the trigonometric cosine of an angle.
      static int fix​(float x)
      Round toward zero
      static int floor​(float x)
      Round toward negative infinity
      static double log​(double d)
      Returns the closest double approximation of the natural logarithm of the argument.
      static double log​(double d, double base)
      Returns the closest double approximation of the logarithm to a certain base.
      static double log10​(double d)
      Returns the closest double approximation of the base 10 logarithm of the argument.
      static double max​(double a, double b)
      Returns maximum value
      static int max​(int i1, int i2)
      Returns the most positive (closest to positive infinity) of the two arguments.
      static long max​(long l1, long l2)
      Returns the most positive (closest to positive infinity) of the two arguments.
      static double min​(double a, double b)
      Returns minimum value
      static int min​(int i1, int i2)
      Returns the most negative (closest to negative infinity) of the two arguments.
      static long min​(long l1, long l2)
      Returns the most negative (closest to negative infinity) of the two arguments.
      static double powIntExp​(double base, int exp)
      Calculation of power with exponent being an integer value
      static double random()
      Returns a pseudo-random double n, where n >= 0.0 && n < 1.0.
      static double sin​(double arg)
      Returns the trigonometric sine of an angle.
      static double sqrt​(double arg)
      Calculates square root.
      static double tan​(double arg)
      Returns the trigonometric tangent of an angle.
      static double toDegrees​(double angrad)
      Returns the measure in degrees of the supplied radian angle.
      static double toRadians​(double angdeg)
      Returns the measure in radians of the supplied degree angle.
    • Field Detail

      • E

        public static final double E
        The double value closest to e, the base of the natural logarithm.
        See Also:
        Constant Field Values
      • PI

        public static final double PI
        The double value closest to pi, the ratio of a circle's circumference to its diameter.
        See Also:
        Constant Field Values
    • Method Detail

      • powIntExp

        public static double powIntExp​(double base,
                                       int exp)
        Calculation of power with exponent being an integer value
        Parameters:
        base - base
        exp - exponent
        Returns:
        power baseexp.
      • log

        public static double log​(double d,
                                 double base)
        Returns the closest double approximation of the logarithm to a certain base. The returned result is within 1 ulp (unit in the last place) of the real result.
        Parameters:
        d - the value whose log has to be computed.
        base - the base to which the logarith is computed.
        Returns:
        the natural logarithm of the argument.
      • log

        public static double log​(double d)
        Returns the closest double approximation of the natural logarithm of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.
        Parameters:
        d - the value whose log has to be computed.
        Returns:
        the natural logarithm of the argument.
      • log10

        public static double log10​(double d)
        Returns the closest double approximation of the base 10 logarithm of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.
        Parameters:
        d - the value whose base 10 log has to be computed.
        Returns:
        the natural logarithm of the argument.
      • abs

        public static double abs​(double d)
        Returns the absolute value of the argument.

        Special cases:

        • abs(-0.0) = +0.0
        • abs(+infinity) = +infinity
        • abs(-infinity) = +infinity
        • abs(NaN) = NaN
        Parameters:
        d - Input value.
        Returns:
        Absolute value.
      • abs

        public static float abs​(float f)
        Returns the absolute value of the argument.

        Special cases:

        • abs(-0.0) = +0.0
        • abs(+infinity) = +infinity
        • abs(-infinity) = +infinity
        • abs(NaN) = NaN
        Parameters:
        f - Input value.
        Returns:
        Absolute value.
      • abs

        public static int abs​(int i)
        Returns the absolute value of the argument.

        If the argument is Integer.MIN_VALUE, Integer.MIN_VALUE is returned.

        Parameters:
        i - Input value.
        Returns:
        Absolute value.
      • abs

        public static long abs​(long l)
        Returns the absolute value of the argument. If the argument is Long.MIN_VALUE, Long.MIN_VALUE is returned.
        Parameters:
        l - Input value.
        Returns:
        Absolute value.
      • max

        public static int max​(int i1,
                              int i2)
        Returns the most positive (closest to positive infinity) of the two arguments.
        Parameters:
        i1 - First value.
        i2 - Second value.
        Returns:
        Maximum of the two input values.
      • max

        public static long max​(long l1,
                               long l2)
        Returns the most positive (closest to positive infinity) of the two arguments.
        Parameters:
        l1 - First value.
        l2 - Second value.
        Returns:
        Maximum of the two input values.
      • max

        public static double max​(double a,
                                 double b)
        Returns maximum value
        Parameters:
        a - First value.
        b - Second value.
        Returns:
        Maximum of the two input values.
      • min

        public static int min​(int i1,
                              int i2)
        Returns the most negative (closest to negative infinity) of the two arguments.
        Parameters:
        i1 - First value.
        i2 - Second value.
        Returns:
        Minimum of the two input values.
      • min

        public static long min​(long l1,
                               long l2)
        Returns the most negative (closest to negative infinity) of the two arguments.
        Parameters:
        l1 - First value.
        l2 - Second value.
        Returns:
        Minimum of the two input values.
      • min

        public static double min​(double a,
                                 double b)
        Returns minimum value
        Parameters:
        a - First value.
        b - Second value.
        Returns:
        Minimum of the two input values.
      • sqrt

        public static double sqrt​(double arg)
        Calculates square root.
        Parameters:
        arg - Input value
        Returns:
        square root
      • cos

        public static double cos​(double arg)
        Returns the trigonometric cosine of an angle. Special cases:
        • If the argument is NaN or an infinity, then the result is NaN.

        The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

        Parameters:
        arg - an angle, in radians.
        Returns:
        the cosine of the argument.
      • sin

        public static double sin​(double arg)
        Returns the trigonometric sine of an angle. Special cases:
        • If the argument is NaN or an infinity, then the result is NaN.
        • If the argument is zero, then the result is a zero with the same sign as the argument.

        The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

        Parameters:
        arg - an angle, in radians.
        Returns:
        the sine of the argument.
      • tan

        public static double tan​(double arg)
        Returns the trigonometric tangent of an angle. Special cases:
        • If the argument is NaN or an infinity, then the result is NaN.
        • If the argument is zero, then the result is a zero with the same sign as the argument.

        The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

        Parameters:
        arg - an angle, in radians.
        Returns:
        the tangent of the argument.
      • atan

        public static double atan​(double arg)
        Returns the arc tangent of an angle, in the range of -pi/2 through pi/2. Special cases:
        • If the argument is NaN, then the result is NaN.
        • If the argument is zero, then the result is a zero with the same sign as the argument.

        The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

        Parameters:
        arg - the value whose arc tangent is to be returned.
        Returns:
        the arc tangent of the argument.
      • atan2

        public static double atan2​(double arg1,
                                   double arg2)
        Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
        arctan(arg1/arg2) is calculated considering signs of the inputs
        Special cases:
        If both arguments are zero, NaN is returned
        Parameters:
        arg1 - Nominator
        arg2 - Denominator
        Returns:
        the arc tangent of the argument
      • acos

        public static double acos​(double x)
        Returns the arc cosinus of an angle in radians
        Parameters:
        x - the value whose arc cosinus is to be returned
        Returns:
        the arc cosinus of the argument
      • fix

        public static int fix​(float x)
        Round toward zero
        Parameters:
        x - value to round
        Returns:
        rounded value
      • ceil

        public static int ceil​(float x)
        Round toward positive infinity
        Parameters:
        x - value to round
        Returns:
        rounded value
      • floor

        public static int floor​(float x)
        Round toward negative infinity
        Parameters:
        x - value to round
        Returns:
        rounded value
      • random

        public static double random()
        Returns a pseudo-random double n, where n >= 0.0 && n < 1.0. This method reuses a single instance of Random. This method is thread-safe because access to the Random is synchronized, but this harms scalability. Applications may find a performance benefit from allocating a Random for each of their threads.
        Returns:
        a pseudo-random number.
      • toRadians

        public static double toRadians​(double angdeg)
        Returns the measure in radians of the supplied degree angle. The result is angdeg / 180 * pi.

        Special cases:

        • toRadians(+0.0) = +0.0
        • toRadians(-0.0) = -0.0
        • toRadians(+infinity) = +infinity
        • toRadians(-infinity) = -infinity
        • toRadians(NaN) = NaN
        Parameters:
        angdeg - an angle in degrees.
        Returns:
        the radian measure of the angle.
      • toDegrees

        public static double toDegrees​(double angrad)
        Returns the measure in degrees of the supplied radian angle. The result is angrad * 180 / pi.

        Special cases:

        • toDegrees(+0.0) = +0.0
        • toDegrees(-0.0) = -0.0
        • toDegrees(+infinity) = +infinity
        • toDegrees(-infinity) = -infinity
        • toDegrees(NaN) = NaN
        Parameters:
        angrad - an angle in radians.
        Returns:
        the degree measure of the angle.