the initial value for m is 4 and n is 5 int m = 4, n = 5;
We then add m and n so 4+5. double d = Math.sqrt((m + n) / 2);
Then we divide by 2 and take the square root.
The answer is then rounded to the nearest whole number because of double. Double is a type of data. A double Data type can hold a floating-point real number. It can only hold several decimal points. Because the int is before the m and the n, when you take 4+5 to get 9 and divide by 2, you end up with 4.5 but only take the integer which is 4. The square root of 4 is 2 and because the code is calling for a double data type, it adds a decimal value which in this case is 0. For example:
If you take the square of 4.5, it will kick out 2.0. If you take the square root of 9.123412345, it will kick out 3. If you plug in 9.999999999999, it will kick out 3.0.
However, as soon as you take a square root of an integer that is not a perfect square, it will give you the decimal value for example, sqrt of 10 is 3.162277... and when it is output from the compiler, it is also 3.162277...
= 4+5=9. 9/2= 4.5 but you only take the int. SO...: sqrt of 4= 2. and because it is double. you get 2.0!!!
THIS BETTER BE CREDITED!!!!!
Ryan, nearly there.
ReplyDeleteLet's focus on this statement: "The answer is then rounded to complete a two integer answer."
Let's ask the question differently:
I pull out my calculator, and the square root of 4.5 is:
2.1213203436
But here Java is telling me it's 2.0
I need to know why exactly is it rounding like this? Extend your response to include that answer.
Thank You.
(FIXED)
ReplyDeletePlease elaborate.
ReplyDeleteAhhh, you were fixing it in the original post, I totally missed that. I was misinterpreting your "(FIXED)" response. Your frustration is warranted. As well as the 15 XPs for completing this... Twice.
ReplyDelete