Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MATH CLASS

 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class lapore
{
public void lapore(double i)
{
double p=Math.ceil(i);
System.out.println("the result is "+p);
}
public static void main(String[]args)
{
double i=-12.4;
lapore l=new lapore();
l.lapore(i);
//System.out.println("the result is"+i);
}
}
In the above given code i have made use of ceil method of math class in which i have passed a negative value, result is -12.0.Please tell me how the output is coming less than the argument specified because the jdk say's
the method ceil return's the smallest double value that is not less than the argument specified,and is equal to mathematical integer.
 
Ravi Sankararaman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitin the ceil function gives the smallest number
bigger than the number entered.
that is if u send an argument of -12.3
then the smallest number bigger than -12.3
turns out to be -12.0.
If you can visualize the NUMBER LINE then
u will be able to understand better.
ceil() gives the number to the immediate right
and floor() to the immediate left.
 
Naveen Arumugam
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Ur doubt is correct, but 've to understand .
Actually, when u pass a +ve number eg: 3.4 , u get 4.0
When u pass a -ve number eg: -12.4 , u get -12.0 { because -12.0 is greater than -13.0 }
ie., -ve..... -13,-12,-11,...,-1,0,1,2,3,4,5,6....,12,13 ....+ve
Also have a look at javadoc expalnation
ceil:
-----
public static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.
Parameters:
a - a double value.
Returns:
the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank's naveen
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitin,
Here is a little drawing to figure out the returning result.
-1 0 1
--> ceiling
-1 0 1
<-- floor<br /> Using the above figure it is easy to see the following.<br /> double p = Math.ceil(-12.3) --> p = -12.0
and
double p = Math.floor(-12.3) --> p = -13.0
Regards,
Manfred.
[This message has been edited by Manfred Leonhardt (edited March 16, 2001).]
 
Vegad Arvind
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Tips for Ceil and floor :
Ceil(x) = round(x + 0.5)
Floor(x) = round(x - 0.5)

try with diff. values and see the result.

Avi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic