3

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:

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

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

The number line

Source: Quora

EDIT: What I am asking is: the description broke my mind. My logic says (about floor for example): First, Ok when I listen floor I think in the smallest not in the largest. Second, if I returns the largest, that is greater not less than to the argument. The same happens with ceil

  • 3
    do you have a question? – Scary Wombat Sep 20 '18 at 04:31
  • With your "new and improved" description of `floor()`, why would `floor(2.1)` return `2.0`? After all, `1.0`, `0.0`, `-1.0`, ... are all closer to negative infinity and all smaller than `2.0`. – Ted Hopp Sep 20 '18 at 04:39
  • Under your new definition, `floor` always returns the smallest possible `double`. – Dawood ibn Kareem Sep 20 '18 at 05:01
  • Re your edit: The current descriptions for `floor()` and `ceil()` make perfect sense if you think of them as describing [constrained optimization](https://en.wikipedia.org/wiki/Constrained_optimization) problems (specifically, [integer programming](https://en.wikipedia.org/wiki/Integer_programming) problems). For example, `floor(a)` solves the constrained optimization problem of maximizing f(x) = x subject to the constraints that x is an integer and x <= a. – Ted Hopp Sep 20 '18 at 14:08

1 Answers1

3

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.

Ditto for ceil: the description mentions the smallest value that is larger or equal to the input value...

So, the original descriptions are in fact correct.

Jason
  • 11,744
  • 3
  • 42
  • 46
  • I understand the key in that phrase. But if the description would say: "Returns the smallest (closest to negative infinity) double value", I would understand the entire description, because: `floor(2.8)` returns `2.0` – learnprogramming Sep 20 '18 at 13:55
  • @Jason Just quibbling here, but "largest" suggests greatest magnitude. That's OK for positive numbers, but for negative it would return negative infinity. I think it would be clearer to say "greatest" instead of "largest". YMMV. – Robert Dodier Sep 20 '18 at 17:59
  • I guess that's why they clarified with `largest (closest to positive infinity)`. – Jason Sep 21 '18 at 01:01
  • Ahhh, I think that I understood the description. I was trying to understand: on one side "largest" and on the other side "that is less than or equal to the argument" separately, so that broke my mind. I am going to explain it in ELI5, so in the example: `floor(2.8)` is like to say: "ehh the maximum is 2.8 so find the largest in the range 2.8 to below." – learnprogramming Sep 21 '18 at 20:38