-4

I'm not sure if this question has been answered(couldn't find it when I did a google search). I saw http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html that the math class square root method returns a double. I experimented with it in eclipse with some ints that have whole square roots - 4, 9 and found that the square root with those always returned a floating point value with one decimal - 4.0, 9.0. I was curious as to why it even returned that extra decimal? I thought that ints could be considered as double values too. To me returning just 1 makes more sense cause you conserve more memory(i guess more memory is needed to store that extra decimal point) I even tested it out

public static double control(){
    return 1;
}

and saw it was valid to just return 1.

committedandroider
  • 8,711
  • 14
  • 71
  • 126
  • no, ints can't be considered the same as doubles. (int)4 and (double)4 have completely different internal values. and most ints can't be represented exactly as a float either. (float)4 is actually 3.9999999999xxxxxx – Marc B Sep 02 '14 at 21:31
  • not saying int, but isn't this valid double x = 6? – committedandroider Sep 02 '14 at 21:32
  • there's some values where float<->int is exact, but it's easier to just assume that ALL ints can't ever be represented exactly as floats. But yes, you're saying it's a double, so the 6 will get "floated". but that means it'll be 5.9999xxx internally. – Marc B Sep 02 '14 at 21:32
  • I agree with that, but for the values for which float<-> int is exact, couldn't you return that value, say 6, not 6.0? Conserve memory? – committedandroider Sep 02 '14 at 21:33
  • 3
    there's essentially a near infinite number of floats, and a very tiny subset have exact translations. there is zero point in storing that list of exceptions and having to check it each time to see if your value IS an exception to the "never exact" rule – Marc B Sep 02 '14 at 21:34
  • 3
    The `sqrt` function returns a `double` value (for natural reasons which I'm sure that you understand). This value is typically represented in 8 bytes, in floating-point format (as specified by the standard). It doesn't have decimal digits in the way that you see them. When you print it to the screen (using `printf` for example), you can specify how many digits you want to print past the decimal point. You can also cast it to an integer if that's what you want (and as you've mentioned `sqrt(4)` or `sqrt(9)` - that's when you might want to perform this cast). – barak manos Sep 02 '14 at 21:35
  • "I thought that ints could be considered as double values too." - nope. ints can be *converted* to double values. You are not saving memory when you return an int from `control` - it's just being silently converted to a double. If a method signature says `double`, it *always* returns double. – Blorgbeard Sep 02 '14 at 21:37
  • 2
    Continuing my previous comment: But the `double` value itself is simply comprised of 64 bits of data, on which the FPU (floating-point unit) of your processor can apply any of the arithmetic operations supported (division, multiplication, etc). It doesn't have "decimal digits" as you refer to them. See http://en.wikipedia.org/wiki/Double-precision_floating-point_format for the "structure" of `double` according to the standard. – barak manos Sep 02 '14 at 21:38
  • Well think about it, how would a machine know if result of a square root can be represented as an integer ? It can not compare it with every integer value. Calculation must be done and result must be a double. You know that can be represented as an Integer, but machine does not. It is not addition or subtraction or multiplication of two integers. Square root of an integer is not always an integer. Besides as all the other people said, internally it actually is something like .999999999. Having a sqrt method with an integer return value would definitely be erroneous – Bren Sep 02 '14 at 21:38
  • 1
    Judging by your question and comments. I think you should read some more on how numbers are stored internally and about binary representation of integers and decimal numbers. – Bren Sep 02 '14 at 21:42
  • @gomyes on my bucket list. you know any good resources? – committedandroider Sep 02 '14 at 21:45
  • Any computer science introduction books' relevant chapters would do. I can recommend "Computer Science Illuminated by Nell Dale, John Lewis" or "Computer Science An Overview by J. Glenn Brookshear" – Bren Sep 02 '14 at 21:49
  • @MarcB What do you mean by, "essentially a near infinite number of floats?" Do you consider 2^32 to be near infinite? Because that's approximately how many different float values there are. On the other hand, maybe you mean "double" when you say "float". There are approximately 2^64 different double values which, for some purposes, might as well be infinite. – Solomon Slow Sep 02 '14 at 21:57
  • @MarcB Every positive integer < 2^23 has an _exact_ float representation, and every positive integer < 2^52 has an exact double representation. (float)4 is _not_ 3.999999... (for any finite number of 9s) It's 4.0. Try it and see for yourself. – Solomon Slow Sep 02 '14 at 22:02

2 Answers2

1

[I] found that [Math.sqrt(x) where x is a perfect square] always returned a floating point value with one decimal.

You are mistaking a particular printed representation of a double value with the value itself. A double does not have a decimal point. A double is a bit pattern that represents a particular real number (4 for example).

Decimal points only appear in a particuular decimal representation of real numbers. If I write "0.25", that obviously has a decimal point. If I write "1/4", there is no decimal point. But those are just two different representations of the same real number. So is the particular bit pattern that represents the double value returned by the Java expression, 1.0/4.0.

I don't know why Double.toString(4) returns the string, "4.0" instead of returning "4", but I'm guessing that somebody wanted to make it consistent with numeric literals in the Java language. When a "4" appears in your program, that's an int literal, and when "4.0" appears in your program, that's a double literal.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
-1

That method returns a double. It's going to display as a double because of the return type the method is set to. There is a solution to this here on stackoverflow to return the in if it's say 4.0 and show the double if it isn't. Solution on stackoverflow

Community
  • 1
  • 1
user3587554
  • 353
  • 1
  • 7
  • That is not really what he is asking. I think there is a lack of understanding about data types and their storages. – Bren Sep 02 '14 at 21:54