Results 1 to 10 of 10
Thread: ceil(x) and floor(x)
 10152014, 08:11 PM #1Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
ceil(x) and floor(x)
Hi,
I am currently reading about ceil(x) and floo(x), and there is something I do not understand.
"ceil(x) x is rounded up to its nearest integer. This integer is returned as a double value."
"floor(x) x is rounded down to its nearest integer. This integer is returned as a double value."
Math.ceil(2.1) returns 4.0
Math.ceil(2.0) returns 2.0
My understanding is this.
2.1 > Rounded to 3, this is the next int, if we round up.
2.0 > Rounded to 2, this is the closes int.
these ones are then to be returned as dobule value it will be 6 and 4?
What am I missunderstanding here?
I appreciate the help!
 10152014, 08:15 PM #2
Re: ceil(x) and floor(x)
"Returned as a double value" does not mean that it doubles the value. It means that the value returns is of type double, as opposed to type int, or long, etc.
What happened when you wrote a little example program to test your assumptions?How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 10152014, 08:29 PM #3Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
Re: ceil(x) and floor(x)
Oh okey ofcourse. Thanks.
But still...why does 2.1 go all the way up to 4?
I have not made any test programs, because I understood I was wrong, but couldnt quite figure out what I was missunderstanding, but for the future I understand that is what I should do.
 10152014, 08:36 PM #4
Re: ceil(x) and floor(x)
What makes you believe 2.1 rounds to 4? Can you post the code that shows this behavior?
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 10152014, 08:44 PM #5Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
Re: ceil(x) and floor(x)
public class c4test {
public static void main (String[]args){
double a = Math.ceil(2.1);
System.out.print(a);
}
}
It prints out 3.
I am reading the "Intro to Java Programming by Y. Daniel Liang.
This is a quote from the text:
ceil(x) x is rounded up to its nearest integer. This integer is returned as a double value.
floor(x) x is rounded down to its nearest integer. This integer is returned as a double value.
Math.ceil(2.1) returns 4.0
Math.ceil(2.0) returns 2.0
Math.ceil(2.0) returns 2.0
Math.ceil(2.1) returns 2.0
Math.floor(2.1) returns 2.0
Math.floor(2.0) returns 2.0
Math.floor(2.0) returns –2.0
Math.floor(2.1) returns 4.0
I persume this is a typo then?
 10152014, 09:01 PM #6
Re: ceil(x) and floor(x)
It would have to be typo because that isn't what it returns.
 10152014, 09:06 PM #7Member
 Join Date
 Sep 2014
 Posts
 47
 Rep Power
 0
Re: ceil(x) and floor(x)
Thanks for the replies, a bit frustrating as I have been reading trough sections in the book and then putting what I have read into programes.
I was to confused this time, but for the future I will make sure to test it in programes as typos could happen.
Thanks for the help. problem solved :)
 10162014, 03:17 PM #8
Re: ceil(x) and floor(x)
If that is indeed what the book says, then yeah, that's a typo.
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 09302015, 06:06 AM #9Member
 Join Date
 Sep 2015
 Posts
 1
 Rep Power
 0
Re: ceil(x) and floor(x)
If you go to the publisher's website for the book: Liang, Introduction to Java Programming, 10e
Click the "Errata" link. You will see the ALL the book errors posted by Professor Liang by chapter.
In the errata for chapter 4, he corrects several published errors. Specifically, he corrects to "Math.ceil(2.1) returns 3.0" and several lines down "Math.floor(2.1) returns 3.0"
Hope that helps!
 09302015, 07:38 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Similar Threads

Using the Math.ceil method
By Allspark in forum New To JavaReplies: 8Last Post: 09102010, 05:52 AM 
Roundind up numbers using Math.ceil
By Allspark in forum New To JavaReplies: 1Last Post: 09082010, 05:15 AM 
Math Class (ceil, floor, abs)
By Java Tip in forum Java TipReplies: 0Last Post: 12132007, 09:21 AM
Bookmarks