Page 143 - CA_Blue( J )_Class9
P. 143

We can use the methods of ‘Math’ class in our program by using the following syntax:
                 Math. function_name(arguments);
                 Some commonly used mathematical methods are given in table:
                  Function Name             Syntax                                  Description
                 Math.min         Math.min (value1, value)    Returns the smaller value
                 Math.max         Math.max (value1, value2)   Returns the larger value
                 Math.sqrt        Math.sqrt(value)            Returns the square root of the value
                 Math.cbrt        Math.cbrt(value)            Returns the cube root of the value
                 Math.pow         Math.pow (value1, value2)   Returns the result of (value1) value2
                 Math.abs         Math.abs(value)             Returns the positive value
                 Math.round       Math.round (value)          Returns the nearest integer value
                 Math.ceil        Math.ceil (value)           Returns the smallest integer value greater  than  the given
                                                              argument
                 Math.floor       Math.floor (value)          Returns the largest integer value smaller than  the given
                                                              argument
                 Math.rint        Math.rint (value)           Returns the trimmed value of the argument
                 Math.random      Math.random()               Returns any value between 0 and 1
                 Math.log         Math.log(value)             Returns the natural logarithm of the value
                 Math.exp         Math.exp(value)             Returns the exponent: e value

                 Let us learn about these mathematical methods in details.

                 7.2.1 Math.min()
                 It is used to find the minimum number between the two arguments. The return type of the method depends on
                 the types of arguments passed. For example, if both are integer, then it returns an integer, and if both are double,
                 then it returns a double value. On the other hand, if both the arguments are of different data types, then the
                 return value is converted into a higher data type.
                                            Examples of Math.min()                  Output
                                   System.out.println(Math.min(3,1));      1
                                   int minval = Math.min(39,45);           39

                                   double d= Math.min(3.2,-4.2);           -4.2
                                   System.out.println(Math.min(3,5.8));    3.0
                                   [converts to higher datatype]

                 7.2.2 Math.max()
                 It is used to find the maximum number between the two arguments. The return type depends on the types of
                 arguments passed. For example, if both are integers, then it returns an integer.
                                           Examples of Math. max ()                Output
                                    System.out.println(Math.max(3,1));     3
                                    int maxval = Math.max(39,45);          45
                                    double d= Math.max(3.2,-4.2);          3.2
                                    System.out.println(Math.max(3.2,5));   5.0

                                    [converts to higher datatype]




                                                                                    Mathematical Library Methods   141
   138   139   140   141   142   143   144   145   146   147   148