You are on page 1of 6

Let us now understand these functions' working with the help of some examples.

cow method

The Math.pow method returns first argument (called the base NOTE
number) raised to power of second-argument (called the Please note that you need
exponent). That is, return value of Math.powx, Y)
isX. not write statement:

Consider this code fragment: importjava.lang.Math


to use methods of Math
double source = 10.01; class. This is because
java.lang package is by
double exp 3.12; default included in every
double result =Math.pow(source, exp) program. BUT evenif
"

System.out.println("Pow(" + source+"," + exp +") = + result); you write above import


statement, it would not
will be like :
ne
output produced by above code fragment give you an error.

Pow(10.01, 3.12) = 1322.3740609470738

sanl) melhod
root of a number given as its argument.
Vath.sqrt method returns the square
Consider this code fragment:
num 36.0;
Sq_root =Math.sqrt(num) "

of" +num+ is " + sq_root)


system.out.p
stem.out.println("The square root

The utput above code fragment will


be like:
produced by
The square root of 36.0 is 6.0
158 COMPUTER APPLICATIONS V
3. cbrt() method
The Math.cbrt method returns the cube root of a number given as its argument. Conhat s
code fragment: Consider this
num 64.0;

cb_root Math.cbrt(num)
S y s t e m . o u t . p r i n t i n ( " T h e c u b e r o o t o f" + n u m + "is " + cb_root);

The output produced by above code fragment will be like:

The cube root of 64.0 is 4.0

4. ceill) method
The Math.ceil method returns the ceiling of a number. In arithmetic, the
ceiling of a
is the closest integer that is greater or higher than the number considered.
numhar
er

For example, consider a


floating-point number say 12.345. This number is between integer1
and integer 13 and its ceiling is 13 because 13 is the closest
integer greater than or equal to the
number 12.345.

Similarly, consider a number such as -24.56. As this number is negative, it is between -24
and
-25, with-24 being greater. The ceiling of -24.56 is -24 as -24> -24.56.
Ceiling numbers of some other example numbers are shown below:
ceil(1.2) =2
2 NOTE
The Math class' methods
ceil(7.8) =8 ceil(4) = 4
are defined as
static.
ceil (-7.8) = -7 simply means that they
ceil()-4 can be used directly by
ceil(0.5) = 1
the programmer without
Now consider this code the trouble of creating an
fragment object.
double valuel 125.45;
double value2 = -14.56;

System.out.println("The celiling of" + valuel +"is " +


Math.ceil(value1))i
System.out.println("The celing of " + value2 +" is " +
Math.ceil(value2) )
The output produced by above code
fragment will be like
The ceiling of 125.45 is 126.0
The ceiling of -14.56 is
-14.0
5. tloor() method
The Math.floor method returns
the
the closest integer that is lowest floor of a number. In arithmetic, the floor or
mberis

example, consider a
but closest
integer value of a number adFof
consider

tloating-
point number say 12.345. This
integer 13 and its floor is 12 because number is r12
ween integer
betwe
and
12 is the
lowest and closest 12.345.
to the integer nu
NCTIONS
AL LIBRARY F U N .

T H é A T I C AL / B A
159
number such as -24.56.
sider a n u m b e r : As this number is
consbeing greater. The
larly, c o n s i c

floor of -24.56 is -25 as negative, it is between -24 and


25, w i t h - 2 4 .
-25-24.56.
Floor
numbers
of some other example numbers are
shown below
fioor(1.2) =1 floor

floor(7.8) = 7 floor(4) =4
floor(-7.8) = -8 floor() =3
floor(0.5) =0

Now consider this code fragment:

double valuel 125.45, = value2 =


- 14.56:
System.out.printin("The floor of " + v a l u e i + " is " + Math.floor(value1 ) ) ;

SYstem.out.printin("The floor of " + value2 + " is " + Math.floor(value2)) ;

The output produced by above code fragment will be like:

The floor of 125.45 is 125.0


The floor of -14.56 is -15.0

round) method

The Math.round method returns the value of its argument rounded to the nearest whole
number. Consider the following code fragment:
double x = 4.2, y = 4.8;

long a, b;
a =
Math.round (x) ;
b Math.round(y)
System.out.println("Rounded value of " + x + " is " + a);

system.out.printin("Rounded value of " + y + " is " + b);

executes, variable a will contain 7 and variable b will contain -6 and the output
will
will COde
be like
ROunded value of 7.2 is 7
ROunded value of -5.8 is -6
1.obsdl meihod
The
Math.abs method returns the absolute value of its argument, which is
a number.
Consider the
following
8 code fragment: code
double x= -7.2, y
à = Math.abs(x)
=
7.2, a, bi
b Math.abs(y)

System..println("Absolute
out.p
oUr.println("Absolute value of "
value of" +x+" is " +
+ y+ " is " +
a);
b);
160 COMPUTER APP
After this code executes, both the variables a and b will contain the value
and the cuty
will be
Absolute value of -7.2 is 7.2
Absolute value of 7.2 is 7.2

8. max) method
The Math.max method returns the maximum of two passed arguments. The argur
nts can
of int, long, float or double types.
Consider the following code fragment:
intnum1 = 80;

intnum2 = 23
S y s t e m . o u t . p r i n t i n ( " M a x i m u m of " + num1+" and " + num2 + " is " +leth.rax(nu

The output of above code will be like:


Maximum of 80 and 23 is 80

9. min )method
The Math.min method returns the minimum of two passed arguments. The arguments can
of int, long,float
or double
types
Consider the following code fragment:
intnum1 = 80
intnum2 = 23;
System.out.println("Minimum of " + num1 + "and " + num2 + " is " +

The output of above code will be like

Minimum of 80 and 23 is 23

10. random) netlod

The Math.random method returns ual to

0.0 and less than 1.0.


a
pseudorandom double number greater than
or
Consider following code fragment:
double rand =

Math.f endom()
System.out.printin("Generated Random Number:" + rand);
The output produced by above code is like

Generated Random Number:0.22715986724367854


The above output will vary each time
doutle

you run above code as a new randomn


type between 0.0 and 1.0 will be
generated.
RY FUNCTIONS
C A LL I B R A R

A,
her
MeatingN u m b e r
between a
given Range
ult, Math.random method
od 161
b y d

ant to
enerate number in anygenerates
a nu
other
a
double number
you

for min, max values of the range? In that case, in the range 0.0 to
Ask
you should do 1.0. What if,
shoulo know the
minimum andrange. To get
maximum random number in
the
following
nefine range. Next, you need to
numbers of the a
specific range, you
(i) define the range.
range =maximum minimum+1 -
range as
follows
nuumber
Generate numb
randomly. Next, to get a
maximum limit
maxin is excluded, use formula like random number in given
: range where
Math.random() * range This is the
range that you
calculated in step 2.
hae code will generate a random number
in the range. ie.
minimum <= Math.random() < maximum

Now, consider the following code that generates a


IMPORTANT Please note that in double
random number in the range 1.0 to 10.0. type range, adding min might give you a
number which is outside the range because
// define the range the random() will generute a number max
and adding min might take it beyond the
double max = 10.0, min = 1.0;
range. However. if you typecast it to int. i.et,
double range = m a x min + 1; for integer type ranges, it will work fine.

// generate random number from 1.0 to 10.0

double rand Math.random()* range +min ; - Do not add min for generatung

mumberin double sype


runge

System.out.println("Generated
Random Number:"+ rand);

like :
The above code will generate output somewhat
5.22715986724367854
Generated Random Number:
If vou
EeTate random number of int type in a given range random
number of double type.
the
must typecast
method generates then you
Pleas note
lease that the Math.random other
n u m e r i c datatype,

any of
want to generate random1 example
number
code:

shown in following
generatednumber. as

/ define the range


int max =
20, min
= 10;
number

-
min + 1;
to 20
generated
int range = max the
fro
omm .10 ypecust

(inttype)
to desined datarype

number
andom
/generate min);
+ rand);
range "+
t h . r a n d o m ( ) *
N u m b e r :

rand (int) R a n d o m

o u t . p r i n t i n ( " G e n e r a t e o like :
s o m e w h a t

will be
code
The output pro uced
by
above

Generated Random Number


162 COMPUTER PLICATIONS-
11. The Pl Constant
You have used II in so many calculations e.g., area calculation, perimeter, volume cal

etc. You also refer to it as Pi. calculation


You can use this Pi constant in your calculations using Math.PI as the Math class is en..
with a constant named PI that contains the value as 3.1415926535897932. equipped
which is
approximate value of Pi.
For example, to calculate area as IIr, you may write :

Math.PI *r*r;

8.3 Using Math Class Methods in Expressions


To allow for mathematical operations beyond basic arithmetic, Java provides additional
functionality using a large group of methods contained in the Math class. We can use the Math
lass methods dependingg upon expressions, eg, for

we shall write Math.powx, 3)


we shall write Math.cbrt(x)
Consider some more
examples:
Mathematical operation Java Syntax Description
() Math.sqrt(x) square root of x

Math.pow(x, y) x raised to the exponent y

I() Math.abs(x) absolute value of x

Similarly, you can Math class' methods to write some complex mathematical expressions a
shown in table below

Mathematical expression Java expression

(i) a b-4ac a
=(Math.sqrt(b*b -4*a*c))/(2*a) ;
2a

(i) ax +b
C +d*
(a Math.pow(x, 5) + b)/(c*x + Math.pow(d, 4) )
(ii)
Math.PI*Math.pow(t, 2) + Math. PI*Math.pow(r,
Similarly you can use Math class
methods as ion.
per a
specific mathematical exp
Let us now apply this
knowledge practically in the form of some
programs.

You might also like