• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

ceil(),round()

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
This is a question from javaprepare sample exam.
What gets written on the screen when the following program is compiled and run. Select the one right answer.
public class test {
public static void main(String args[]) {
int i;
float f = 2.3f;
double d = 2.7;
i = ((int)Math.ceil(f)) * ((int)Math.round(d));

System.out.println(i);
}
}

a. 4
b. 5
c. 6
d. 6.1
e. 9
The answer is e. I compiled the above code and got the answer 9. can anybody explain how it is to be calculated.
Thanks in advance.
Renuka
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Renuka Kilambi:
Hi everybody,
This is a question from javaprepare sample exam.
What gets written on the screen when the following program is compiled and run. Select the one right answer.
public class test {
public static void main(String args[]) {
int i;
float f = 2.3f;
double d = 2.7;
i = ((int)Math.ceil(f)) * ((int)Math.round(d));

System.out.println(i);
}
}

a. 4
b. 5
c. 6
d. 6.1
e. 9
The answer is e. I compiled the above code and got the answer 9. can anybody explain how it is to be calculated.
Thanks in advance.
Renuka


Hi,there:
Let me try to help.
-----------------------------------------------------
i = ((int)Math.ceil(f)) * ((int)Math.round(d));

step1:
Math.ceil(f)=Math.ceil(2.3) will return a double value which is 3.0, we need the expilicty casting to int, so we get 3;
step2:
Math.round(d)=Math.round(2.7) will return a long value which is 3L, we also need to cast it into int, so we get 3,
Step3:
do 3*3; you will get 9.
Hope this would help



[This message has been edited by Helen Yu (edited August 04, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Renuka,
ceil gives you the next highest number in the number-scale. In your case, the ceil of 2.3 is the next highest number, which is 3.
round gives you the next highest number if the decimal point is 5 or greater( a fractional part which is more than 1/2 ), and the next lowest number if the decimal point is less than 5. Hence round of 2.5 is the same as round of 2.7, which is 3. Round of 2.4 is the same as round of 2.2 which is 2.
In your example, ceil(f) returns 3 and round(d) returns 3. 3 * 3 = 9 and hence the answer.
Note that it is slightly tricky to figure out ceil, floor and round for negative numbers. The best thing to do is to imagine the number-scale and the position of the given number in the scale. For ceil you always move to the next number towards the RIGHT, and for floor move towards the next number towards the LEFT on the scale. So, ceil of -2.3 is -2 moving RIGHT on the scale. Similarly, floor of -2.3 is -3 moving LEFT on the scale.
Ajith
[This message has been edited by Ajith Kallambella (edited August 04, 2000).]
 
Renuka Kilambi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Helelen and Ajith,
Thanks a lot for ur replies
Renuka
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajith,
As you said
. While applying round for negative numbers, ignore the sign, apply the rule and apply the sign again!.
How come the round of 2.5 & -2.5 gives 3 & -2.
Please explain if I am missing something.
Thanks in advance.
Mita
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi mita,
I would like to change little bit to what ajith said:



round gives you the next highest number if the decimal point is 5 or greater( a fractional part which is more than 1/2 ), and the next lowest number if the decimal point is less than 5.
While applying round for negative numbers, ignore the sign, apply the rule and apply the sign again!.



I would like to change the first statement little bit for negative numbers only.

round gives you the next highest number if the decimal point is greater than 5( a fractional part which is more than 1/2 ), and the next lowest number if the decimal point is 5 or less .

try it , and feel free to correct me.
vivek
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For Math.round(d) use this to determine the outcome:
(return_type)(d + 0.5)
so when you have d = -2.5 the result will be:
(return_type)(-2.5 + 0.5) = 2
Note: This is an overloaded method, so if d is of type double return_type would be long, if d is of type float then return_type would be int.
Hope this helps.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS! That was a bummer, thanks for noticing. I have edited my post
Ajith
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
is this -2 or 2 ??

Originally posted by Ken Lai:
Hi,
For Math.round(d) use this to determine the outcome:
(return_type)(d + 0.5)
so when you have d = -2.5 the result will be:
(return_type)(-2.5 + 0.5) = 2
Note: This is an overloaded method, so if d is of type double return_type would be long, if d is of type float then return_type would be int.
Hope this helps.


 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be -2. Cause when you round a number if the decimal is greater then 5 then it number is converted to the one higher in the scale. In the scale the negative numbers decrease to zero and positive numbers increase from zero, which means that
for -2.5 the next higher will be -2. Hope this helps.
 
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic