Ads

Mathematical Library Methods

The library methods (functions) are the in-built methods designed by the developers. These predefined functions help the user to perform certain tasks easily and quickly which are frequently used in Java programming. All the Mathematical functions are included in a  class called Math under java.lang package.

Math.min()

It returns the minimum of two numbers. It returns int/long/double type value depending upon the type of arguments passed to it.
Syntax: <Return data type> <variable> = <Function name(argument 1, argument 2)>;
e.g., double m = Math.min(-4.5, -5.4);
Output: -5.4

Math.max()

It returns the greatest value of two numbers. It returns int/long/double type value depending upon the type of arguments passed to it.
Syntax: <Return data type> <variable> = <Function name(argument 1, argument 2)>;
e.g., double m = Math.max(9.2, 9.45);
Output: 9.45

Math.pow()

This method is used to find the power raised to a given base value. It always returns a double type value.
Syntax: <Return data type> <variable> = <Function name(argument 1, argument 2)>;
e.g., double m = Math.pow(2,4);
Output: 16.0

Math.sqrt()

It is used to find the square root of a positive number. It returns a double type value.
Syntax: <Return data type> <variable> = <Function name(Positive number)>;
e.g., double m = Math.sqrt(7.29);
Output: 2.7
Square root of a negative number is an imaginary number.


Math.cbrt()

It is used to find the cube root of a positive number or negative number. It returns a double type value.
Syntax: <Return data type> <variable> = <Function name(number)>;
e.g., double m = Math.cbrt(27.791);
Output: 3.1

Math.log()

This function is used to find the natural logarithmic value of a given argument. It always return a double type value.
Syntax: <Return data type> <variable> = <Function name(number)>;
e.g., double m = Math.log(6.25);
Output: 1.8325

Math.abs()

It always returns the absolute value of an argument (magnitude of the number without its sign, i.e., a positive value). The return value may be int/long/double data type depending upon the input arguments.
Syntax: <Return data type> <variable> = <Function name(number)>;
e.g., double m = Math.abs(-6.25);
Output: 6.25

Math.round()

This function return the value of a number rounded to its nearest integer. If the fractional part is less than 0.5 then it returns the same integer value, otherwise it returns the next higher integer. It returns the value in long or int type.
Syntax: <Return data type> <variable> = <Function name(argument)>;
e.g., double m = Math.round(8.5);
Output: 9
e.g., double m = Math.round(-8.5);
Output: -8

Math.rint()

This function returns the nearest integer of a given fractional number. The return data type will always be double.
Syntax: <Return data type> <variable> = <Function name(argument)>;
e.g., double m = Math.round(8.5);
Output: 8.0
e.g., double m = Math.round(-9.5);
Output: -10.0

Math.ceil()

It returns the next higher integer number that is greater than or equal to the argument. It always return the value as a double data type.
Syntax: <Return data type> <variable> = <Function name(argument)>;
e.g., double m = Math.ceil(8.5);
Output: 9.0
e.g., double m = Math.round(-8.5);
Output: -8.0

Math.floor()

It returns the lower integer number that is less than or equal to the argument. It always returns the value as a double data type.
Syntax: <Return data type> <variable> = <Function name(argument)>;
e.g., double m = Math.floor(8.912);
Output: 8.0
e.g., double m = Math.round(-8.912);
Output: -9.0

Math.floor()

It results in the exponential value of an argument x (i.e.e^x). It returns a double type value.
Syntax: <Return data type> <variable> = <Function name(number)>;
e.g., double m = Math.exp(6.25);
Output: 518.0128
SHARE

Author

Hi, Its me Hafeez. A webdesigner, blogspot developer and UI/UX Designer. I am a certified Themeforest top Author and Front-End Developer. I'am business speaker, marketer, Blogger and Javascript Programmer.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 Comments:

Post a Comment