nitin sharma

Ranch Hand

Posts: 290

posted 16 years ago

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.

{

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

posted 16 years ago

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.

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

posted 16 years ago

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.

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

Manfred Leonhardt

Ranch Hand

Posts: 1492

posted 16 years ago

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).]

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).]

It is sorta covered in the JavaRanch Style Guide. |