Page 163 - computer science (868) class 11
P. 163

Example 2: int i = Math.min(2,5);               Output: 2
                    Example 3: double d= Math.min(2.7,2);           Output: 2.0
                    [converts to higher datatype]

                 2.  Math.max()
                    This function finds the maximum number between two number arguments passed to it as parameters.
                    Example 1: double m=Math.max(23.9,24.32);       Output: 24.32
                    Example 2: int i = Math.max(2,5);               Output: 5

                    Example 3: double d= Math.max(2.7,2);           Output: 2.7
                 3.  Math.sqrt()
                    This function finds the square root of a positive value passed to it as a parameter.
                    Example 1: double d=Math.sqrt(25)               Output: 5.0

                    Example 2: double d=Math.sqrt(8.1);             Output: 0.9
                    If a negative value is used, the output will be NaN.
                    Example 3: double d = Math.sqrt(-64);           Output: NaN

                 4.   Math.cbrt()
                    This function finds the cube root of a number passed to it as a parameter. It works on both positive and negative
                    numbers.

                    Example 1: double d = Math.cbrt(8.0);           Output: 2.0
                    Example 2: double d = Math.cbrt(-64);           Output: -4.0
                 5.   Math.pow()
                                          th
                                                                                 n
                    This function finds the “n ” power of a number “m” in the format m .
                    Example 1: double d = Math.pow(2,2);            Output: 4.0
                    Example 2: double d = Math.pow(4,-3);           Output: 0.015625
                 6.   Math.log()

                    This function finds the logarithm of a value provided in the argument.
                    Example 1: double d = Math.log(4);              Output: 1.3862943611198906
                    Example 2: double d = Math.log(-2);             Output: NaN
                 7.   Math.abs()
                    This function finds the absolute value of the argument, i.e., it always returns the positive value.

                    Example 1: double d = Math.abs(2.3);            Output: 2.3
                    Example 2: int i = Math.abs(2);                 Output: 2
                    For negative value:
                    Example 3: double d = Math.abs(-8.6);           Output: 8.6
                    Example 4: int i = Math.abs(-7);                Output: 7

                 8.   Math.round()
                    This function rounds a number passed as an argument to its nearest integer using normal math round rules.
                    If it is an integer value, then it will return the same value.
                    Example : int i = Math.round(5);                Output: 5
                    If it is a real positive value, then the value after the decimal is considered according to the following.



                                                                                                                       161
                                                                                                 Statements and Scope  161
   158   159   160   161   162   163   164   165   166   167   168