• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

MATH CLASS

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank's naveen
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic