# Thread: ceil(x) and floor(x)

1. Member 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!  Reply With Quote

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?  Reply With Quote

3. Member 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.  Reply With Quote

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?  Reply With Quote

5. Member 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?  Reply With Quote

6. ## Re: ceil(x) and floor(x)

It would have to be typo because that isn't what it returns.  Reply With Quote

7. Member 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 :)  Reply With Quote

8. ## Re: ceil(x) and floor(x)

If that is indeed what the book says, then yeah, that's a typo.  Reply With Quote

9. Member 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!  Reply With Quote

10. ## Re: ceil(x) and floor(x)

The reason why you dug up a year old thread and posted a reply, is beyond me ...

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•