Page 210 - ComputerScience_Class_11
P. 210

8.5 MATHEMATICAL FUNCTIONS
              There are many built-in methods in Java that help us to perform mathematical tasks quite easily. These built-in methods
              are pre-defined functions that are already declared in some packages. To use the built-in functions, we must include
              those packages in our program by using the following syntax:

                  import java.packagename.classname;
              For example:

              To include the util package which contains the Scanner class, we must use the following at the beginning of the program.
                  import java.util.*;
              These built-in methods are also called library or system methods. There are many types of library or system methods,
              e.g., String methods, Mathematical methods, Graphics methods, etc. In this chapter, we will deal with the different
              types of Mathematical methods that are frequently used in coding.

              To use Mathematical methods in programs, we need to use “Math” class which resides in “java.lang” package. Since
              this lang package is included in the program automatically, we do not have to include it separately by writing the
              import syntax.

              To use it in the program, use the following syntax:
              Math. function name(arguments)

              8.5.1 Different Mathematical Methods
              There are various mathematical built-in methods in Java as listed below:

              1.   Math.min()
                 This function finds the minimum number between two number arguments passed to it as parameters.
                 Example 1: double m=Math.min(23.9,24.32);              Output: 23.9

                 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



                  208  Touchpad Computer Science (Ver. 3.0)-XI
   205   206   207   208   209   210   211   212   213   214   215