Given an int
x, is (int)Math.sqrt(x)
guaranteed to return the mathematically correct result, truncated to an int (i.e., the exact mathematical value of floor(sqrt(x))
) for all non-negative x
?
Asked
Active
Viewed 370 times
1

BeeOnRope
- 60,350
- 16
- 207
- 386
-
Can you specify with mathematically correct result? – FailingCoder Apr 25 '19 at 23:45
-
@FailingCoder - I don't understand your question. – BeeOnRope Apr 25 '19 at 23:47
-
1@BeeOnRope do you mind provide an example of an incorrect result and a correct one? – nortontgueno Apr 25 '19 at 23:48
-
1@ngueno sure, `(int)Math.sqrt(10)` gives 3 which is the correct result, any other result would be an incorrect result. I mean "the exact square root with any fractional part chopped off", i.e,. `floor()` of the exact result. – BeeOnRope Apr 25 '19 at 23:51
-
The most popular questions seem to be really short. Anyway, my answer would be the same as a currently deleted answer. – FailingCoder Apr 26 '19 at 00:04
-
1@jasli answered this correctly, then deleted their answer. The short answer is _yes_. Please, jasli, will you undelete your answer? The comment about it not applying for `long` is irrelevant, because all `int` values can be represented exactly by a `double`, whereas the same is untrue for `long`. – Dawood ibn Kareem Apr 26 '19 at 00:13
-
1@DawoodibnKareem - the answer was correct, but I think the reasoning wasn't, or at least it was incomplete? In particular, they didn't mention the "all int values can be represented exactly by a double" part at all. I agree the delete was hasty though! The observation about `long` applies because you could substitute `long` in that answer without contradicting anything, so if the answer fails in that case, the reasoning cannot be complete. – BeeOnRope Apr 26 '19 at 00:18
-
It would have been a really good answer to keep, as it would keep an example of things an answer would need in this question. – FailingCoder Apr 26 '19 at 00:19
-
@FailingCoder agreed, voted to undelete. – BeeOnRope Apr 26 '19 at 00:22
-
OK, the bit about casting a `double` to an `int` being equivalent to flooring is untrue, but the difference is provably irrelevant. The key point is that you can't lose information casting `int` to `double`; whereas you _can_ lose information casting `long` to `double` - and jasli didn't mention this. Perhaps I should write an answer myself. – Dawood ibn Kareem Apr 26 '19 at 00:24
-
@DawoodibnKareem - can you give an example over the non-negative `int`s where casting to `int` is not equivalent to `floor`? – BeeOnRope Apr 26 '19 at 00:34
-
https://stackoverflow.com/questions/3300290/cast-to-int-vs-floor might help. – FailingCoder Apr 26 '19 at 00:36
-
@FailingCoder - well the accepted answer contradicts Dawood: it says the results are only different for negative values, and the deleted answer and my question explicitly refer only to _non-negative values_. – BeeOnRope Apr 26 '19 at 00:39
-
1Exactly. That's the point. – FailingCoder Apr 26 '19 at 00:40
-
@FailingCoder - makes sense :). – BeeOnRope Apr 26 '19 at 00:40
-
Use comments to ask for more information or suggest improvements. *Avoid answering questions in comments.* Oops. – FailingCoder Apr 26 '19 at 00:42
-
1No, there is no such example over `int`s. You get into trouble with numbers that are too big for `int`. – Dawood ibn Kareem Apr 26 '19 at 02:51
1 Answers
0
According to the Java documentation, Math.sqrt()
returns "the double value closest to the true mathematical square root of the argument value", and casting a double
to an int
is equivalent to flooring for non-negative numbers (for negative numbers, floor()
truncates towards negative infinity while casting truncates towards 0). The only case I can think of where (int)Math.sqrt(x)
wouldn't do the job is if the result of the square root is too large to fit into an int
.
Additional Links
https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sqrt(double)

jasli
- 3
- 3
-
I don't think this answer is complete, because you could write the same answer with `long` substituted for `int`, but I know for that for `long` it fails, e.g., for `4503599761588224`. The answer can always fit in an `int` because the result of `sqrt` is never larger than the argument (except for fractional values from 0 to 1 which don't apply for int arguments). – BeeOnRope Apr 25 '19 at 23:55
-