You are on page 1of 12
Mawr enees murary | Introduction (© package, Methods diets); Behaviour of positive g sPhg). Mathufloor(x), Math.ro fain, b), Math.random ) (it hod is a pros Instances already defined in the are created by system devel methods. i In this chapter, we are going to introduce that are provided by developers and are ‘of Java.lang package (a default package). Hence, is added as shown below: 3 Math.pow( ) This function is used to find the F value of expressed exponential form. It always returns its value cl a a .. For example: a” is read as ‘a to the power n’ mee ae sailed the index. n’; where ‘a’ is Called the base and yntax: = Function name (number 1. aA : , number 2); />\double d= Math.pow(2.0, 3.0); ~~ , the function returns a double type value for das 2.099 = 8.0 9) double d= Math pow(S.0, 20); cs? « se = Oe ~The value for d will return as 0.04 ina double data type- + Math.sqrt() This function umber. It returns @ double type value Syntax: = Function name (Positive number); _ eg. double n=Math.sqrt(4-0); yAlt returns a double type ¥ > a 5 Math.cbrt() pe This function is used to find the cube root > ora It always returns a value in a double data type, yntax: = Function name (Po e.g. M double n=Math. cbrt(27.0); wilt returns a double type double n=Math.cbrt(15. 625); vt returns a double type Vi 1 ~ double n=Math.cbrt(-3.375); : — Tt will return a value for n | . Math.log( ) ‘ This function returns the natural_logarithmic value of a given al returns a double type valu a yntax: = Function name (Positive nj eg. 0 double x = Math.log(6.25); It returns a double type value for x as 1.8325. + ith.abs( ) This function is used to return the absolute value of an argument (i.e. of the number without its sign, i.e., a positive value). It returns int/lo data type value depending upon the arguments sled) yyntax: = Function name (number); ~ eg. + ): int n = Math.abs(3); It returns an integer value for n as 3. 4: int n= Math. abs(-8); It returns an integer value for the vari $ ‘double d=Math.abs(3.66); It returns a double type value for _ » double d=Math.abs(-9.99); It returns a double type value fo i it returns the next hig) - igher integer value, Nhis funct —_ unction always returns otherwise, | the integeT value in long ae ee g or int data type. However, it predict outpu jor positive and negative number: io ae Usgaiax: =Function name (argument); For positive Numbers: If fractional part lies between 0 (inch clusive) and 0.5 (exclusive) then the func Math.round() returns the integer part of the ee mane eg Gystem.out.printin(Math.round(S.0) output as 8 printin(Math.round(8.49)); ore then the function M output as 8 nd() returns the “o\system.out ath.roul If fractional part 1s 0.5 or m next hight System. out.pr out printin(M er integer. output as 9 jntIn(Math.round(8 5)) (8.99)); System jath.round! output as 9 For negative numbers: en 0 (inclusive) and 0.5 (inclusive) then the function If fractional part lies betwe' Math.round() returns the integer part of the number. eg. Si sem out printin(Math-round’-® 0% output as 8 ath.round(-8. 5); : output as 3 urns the next lower integet of qs tem.out.printin(M If fractional part is more than 0.5, then it ret the number. eg. . 2 out print Math round Sh soup i‘ oat peinincMath sound 89°)” ,ourpul aaa trated below: é\ “a” Sal ted value (integral part) of a number. It always returns an | ale in double data type. However I predicts the different outputs for e positive and negative number Scvariable>=Function name (argum d It returns the truncal _Ayntax: =Function name (argument); For positive Numbers: System.out.printIn(Math ceil(8)); : output as 8.0 \ i ystem.out.printIn(Math.ceil(8 5)); : output as 9.0 | System.out.printIn(Math.ceil(8.912)); : output as 9.0 3] | For negative numbers: System.out.printIn(Math.ceil(-0.45)); : output as -0.0 System.out.printin(Math.ceil(-8)); . System.out.printin(Math.ceil(-8.5)); Lout.printIn(Math.ceil(-8.912)); : output as -8.0 : output as -8.0 : output as -8.0 hn A sample program is illustrated below: W floor( ) This function returns the largest integer that is less returns the value as a double data type than or equal to the argument. hether the argument is integer ft always (int), float or a double data type. aie =Function name (argument); For positive Numbers: gystem.out printin(Math floor(®)) - output as 8.0 QO system.out.printin(Math floor(8 912) : output as 8.0 For negative numbers: tem out printin(Math.floor(-048)) - output as -1.0 g : output as 8.0 Math.floor(-8)); out.println( ; (-8.912)); : output as 9.0 tions with random function © get an integer random number between 1 and n: int r = (int) (Math.random( )*n)+1; Here, you will get any random integer number between 1 and n. (ii) To get any integer random number between m and n where m and n are the lower and upper limits of the integer numbers respectively: int x =(int) ((Math-random( ) “ioom)jem), )

You might also like