Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

# MATH CLASS

Ranch Hand
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.

Greenhorn
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.

Greenhorn
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.

nitin sharma
Ranch Hand
Posts: 290
• • • • thank's naveen

Ranch Hand
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
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. New rule: no elephants at the chess tournament. Tiny ads are still okay. Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton