Page 212 - ComputerScience_Class_11
P. 212

Example 1: int i = Math.rint(7.8);                     Output: 8.0
                 Example 2: int i = Math.rint(7.2);                     Output: 7.0
                 Example 3: int i = Math.rint(7.5);                     Output: 8.0
                 Example 4: int i = Math.rint(4.5);                     Output: 4.0
                 Example 5: int i = Math.rint(9.5);                     Output: 10.0
                 [Explanation: In case of 0.5, if it is an even integer value it will return the previous integer value and if it is an odd
                 integer value it will return the next integer value. See examples 3, 4 and 5.]
                 For negative fractional numbers:
                 Example 1: int i = Math.rint(-7.8);                    Output: -8.0
                 Example 2: int i = Math.rint(-7.2);                    Output: -7.0
                 Example 3: int i = Math.rint(-7.5);                    Output: -8.0

              12.  Math.exp()

                  This function returns the exponential value of the passed argument. It uses a double data type.
                 Example 1: double d = Math.exp(2.5);                   Output: 12.182493960703473
              13.  Math.random()
                  This function returns a random real number between 0 and 1.
                  Example 1: Math.random();                             Output: 0.001110128

                  Some special calculations using Math.random()
                  1. Using this function, we can generate a random number between 1 and x
                    Say, x=3;

                   int a = (int)(Math.random() * x) + 1;
                    [It will return any integer number from 1 to 3]

                  2. Using this function, we can generate a random number between m and (n -1).
                    Say, m=2, n=9;
                   int i = (int)(Math.random() * (n – m));
                    [It will return any integer number from 2 to 8]
              14.  Trigonometric Functions

                  In Java, there are three Trigonometric functions:
                  •  Math.sin()
                  •  Math.cos()
                  •  Math.tan()

                  In the above methods, the arguments are passed in Radians which are generally derived from degrees. We can
                  convert degree into radian by using the following formula:

                                                   Radian = (π * degree given)/180.0
                  For example:
                  If double degree = 60, radian;
                  radian = (22.0/7.0*60)/180.0 = 1.0476
                  Example 1: double r = Math.rint(Math.sin(Radian));                  Output : 1.0
                  Example 2: double r = Math.rint(Math.cos(Radian));                  Output : 0.0
                  Example 3: double r = Math.rint(Math.tan(Radian));                  Output : 2.0




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