programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Math.PI

Ranch Hand
Posts: 39
• Number of slices to send:
Optional 'thank-you' note:
. Given
double pi = Math.PI;
Which two are valid ways to round pi to an int?(Choose two.)
a) int p = pi;
b) int p = Math.round(pi);
c) int p = (int)Math.round(pi);
d) int p = (int)Math.min(pi + 0.5d);
e) int p = (int)Math.floor(pi + 0.5d);

correct answers are c and e .
can anyone explain wht does Math.PI do??
can anyone give a simple xample also??

regards
Kamal

Ranch Hand
Posts: 33
• Number of slices to send:
Optional 'thank-you' note:
Math.PI=3.1415926.....
you should know how to pick info from JDK doc: http://java.sun.com/j2se/1.3/docs/api/java/lang/Math.html#PI

Ranch Hand
Posts: 103
• Number of slices to send:
Optional 'thank-you' note:
pi is a mathematical constant(22/7) returned by Math.PI.
a) int p = pi;
pi is a double value, so it needs explicit casting into int narrowing conversion)
b) int p = Math.round(pi);
Math.round() returns a long value, so it needs explicit casting into int(narrowing conversion)
c) int p = (int)Math.round(pi);
explicit conversion to int done, hence correct
d) int p = (int)Math.min(pi + 0.5d);
Math.min takes 2 arguments and returns a double value, hence wrong
e) int p = (int)Math.floor(pi + 0.5d);
double value returned by Math.floor() converted to int hence correct
[This message has been edited by Sweekriti Engineer (edited March 21, 2001).]

Ranch Hand
Posts: 66
• Number of slices to send:
Optional 'thank-you' note:
Thats a very good answer ... hope it has solved the doubt

"The Hood"
Posts: 8521
• Number of slices to send:
Optional 'thank-you' note:
If you read the round() method in Math you will see that it uses the syntax shown in e to accomplish the rounding.

kamal jaisingh
Ranch Hand
Posts: 39
• Number of slices to send:
Optional 'thank-you' note:
thx a lot all u guys u have replied,
now its clear to me abt math.pi.
thx again
regards
Kamal

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.