File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

MATH CLASS

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
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

Joined: Mar 15, 2001
Posts: 9
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

Joined: Mar 10, 2001
Posts: 23
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.

ANaveenS
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
thank's naveen
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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).]
Ranch Hand

Joined: Jan 10, 2001
Posts: 42
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.

I agree. Here's the link: http://aspose.com/file-tools

subject: MATH CLASS