Page 146 - CA_Blue( J )_Class9
P. 146

7.2.11 Math.rint()
                  It returns the nearest value of the fractional number in double. It returns different values for positive and
                  negative value.

                                             Examples of Math.rint()                Output
                                     System.out.println(Math.rint(6.8));    7.0
                                     int d = (int)Math.rint(25.8);          26
                                     double d = Math.rint(-51.0/50.0);      -1.0
                                     double i = Math.rint(7.2);             7.0
                                     double i = Math.rint(8.5);             8.0
                                     double i = Math.rint(7.5);             8.0
                                     double i = Math.rint(4.5);             4.0
                                     double i = Math.rint(9.5);             10.0
                  Explanation: For 8.5 this function will return 8.0 and for 9.5 it will return 10.0 showing the result for this function
                  is biased towards even numbers. Same goes for negative values.

                                             Examples of Math.rint()                Output
                                     double i = Math.rint(-7.8);            -8.0
                                     double i = Math.rint(-7.2);            -7.0
                                     System.out.println(Math.rint(-7.5));   -8.0

                  7.2.12 Math.exp()
                  It returns the exponential value of the passed argument. It returns the result in double.

                                             Examples of Math.exp()                 Output
                                     double d = Math.exp(2.5);              12.182493960703473

                  7.2.13 Math.random()
                  It generates a random real number between 0 and 1. Thus returning a different number for each execution. It
                  returns the result in double.
                                           Examples of Math.random()                Output
                                     double d = Math.random();              0.001110128



                   Program 1      Write a program to demonstrate the use of mathematical functions.

                     1     class mathematicalfunction
                     2     {

                     3         public static void main()

                     4         {
                     5            System.out.println(" ----Mathematical Functions ----");
                     6            System.out.println("Math.min(3.4,5.8)    : " + Math.min(3.4,5.8));

                     7            System.out.println("Math.max(3.4,5.8)    : " + Math.max(3.4,5.8));
                     8            System.out.println("Math.sqrt(4.0)       : " + Math.sqrt(4.0));




                   144    Touchpad Computer Applications-IX
   141   142   143   144   145   146   147   148   149   150   151