Page 242 - Computer science 868 Class 12
P. 242

8.   Math.round(): Rounding of a number to its nearest integer is done by this method. If the decimal is less than 0.5,
                  then it returns the integer value of the given real value, else it returns the next higher integer value.
                     int r1 = Math.round(7.4);                    // Output: 7
                     int r2 = Math.round(7.55);                   // Output: 8
                9.  Math.ceil(): This method returns the smallest integer value greater than the number in double data type.
                     double d = Math.ceil(3.4);                   // Output: 4.0
                     double d = Math.ceil(-0.8);                  // Output: -0.0

                10.  Math.ceil(): This method returns the largest integer value smaller than the number in double data type.
                     double d = Math.floor(4.4);                  // Output: 4.0
                     double d = Math.floor(-4.4)                  // Output: -5.0
                11.  Math.rint(): This method returns the nearest value of the fractional number in double data type.

                     int i = Math.rint(7.2);                      // Output: 7.0
                     int i = Math.rint(9.5);                      // Output: 10.0
                     int i = Math.rint(-7.2);                     // Output: -7.0
                12.  Math.exp(): This function returns the exponential value of the passed argument in double data type.
                   double d = Math.exp(4.8);                      // Output: 121.51041751873485

                13.  Math.random(): This method generates the random real numbers between 0 and 1. It returns the output in
                  double data type.
                     Math.random();                               // Output: 0.1284638928
                  Some special calculations using Math.random()

                  •  Using this function, we can generate a random number between 1 and m.
                       Say, m=4;
                      int a = (int)(Math.random() * m) + 1;
                     [It will return any integer number from 1 to 4]
                  •  Using this function, we can generate a random number between a and (b-1).

                        Say, a=2, b=5;
                      int i = (int)(Math.random() * (b – a)) + a;
                     [It will return any integer number from 2 to 4]
                14.    Trigonometrical functions: They are used to calculate and solve the sine, cosine and tangent of a number passed
                   as an argument which is passed in Radians (derived from degrees). The three methods are
                   •  Math.sin()
                   •  Math.cos()
                   •  Math.tan()

                      Note: To convert from degree to radian, we use the following formula:
                      Radian = (π * degree given)/180.0]
                      If double degree = 30, radian;
                      Radian = (22.0/7.0*30)/180.0 = 0.5238

                   Example 1: double r = Math.sin(Radian);        // Output: 0.5
                   Example 2: double r = Math.cos(Radian);        // Output: 0.865
                   Example 3: double r = Math.tan(Radian);        // Output: 0.5776





                240240  Touchpad Computer Science-XII
   237   238   239   240   241   242   243   244   245   246   247