Java - Misunderstand ceil and floor methods

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Java - Misunderstand ceil and floor methods



floor:



Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. ...



ceil:



Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. ...



Source: Docs Oracle



About floor: If I type System.out.print(Math.floor(2.1)); returns 2.0 . Other example: System.out.print(Math.floor(2.8)); returns 2.0 . I am going to argue this description with the example: if floor(2.1) was the largest (closest to positive infinity) as a result would be 3.0 not 2.0, because 2.0 is closest to negative infinity I think. So if I change the description about floor:


System.out.print(Math.floor(2.1));


2.0


System.out.print(Math.floor(2.8));


2.0


floor(2.1)


3.0


2.0


2.0



Returns the smallest (closest to negative infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. ...



It makes sense for me, I would understand that floor(2.1) returns 2.0


floor(2.1)


2.0



When I read "closest to positive infinity" and "closest to negative infinity" I think in the number line:



The number line



Source: Quora





do you have a question?
– Scary Wombat
3 mins ago




1 Answer
1



Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer



The key is in the phrase that is less than or equal to the argument.



So 2.0 is the largest double value that is less than or equal to 2.1 that is also equal to an integer value.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard