Results 1 to 10 of 10
Like Tree2Likes
  • 2 Post By JosAH

Thread: ceil(x) and floor(x)

  1. #1
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default 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!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default 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!

  3. #3
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

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

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default 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!

  5. #5
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default 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?

  6. #6
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: ceil(x) and floor(x)

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

  7. #7
    Thammer is offline Member
    Join Date
    Sep 2014
    Posts
    47
    Rep Power
    0

    Default 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 :)

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    14

    Default 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!

  9. #9
    cathiep is offline Member
    Join Date
    Sep 2015
    Posts
    1
    Rep Power
    0

    Default 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!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default 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
    otacon and Mike U like this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Using the Math.ceil method
    By Allspark in forum New To Java
    Replies: 8
    Last Post: 09-10-2010, 05:52 AM
  2. Roundind up numbers using Math.ceil
    By Allspark in forum New To Java
    Replies: 1
    Last Post: 09-08-2010, 05:15 AM
  3. Math Class (ceil, floor, abs)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-13-2007, 09:21 AM

Posting Permissions

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