This content has been marked as final.
Show 8 replies

1. Re: The value of a double
EJP Mar 5, 2013 9:34 PM (in response to 994967)The doc for Math.floor states that the returned value is equal to a "mathematical integer".
So it's an integer.What is the value of d: 4.0 or 4?
What's the difference?Why?
Why what?If they are different
Did somebody say they were different?why (4.0 != 4) does not evaluate to true?
Because they aren't different.
Do you see the circular thought process you started here? You asked whether something was X or Y, which assumes they are different; then you started asking why they are different; then you asked why they aren't. 
2. Re: The value of a double
rp0428 Mar 5, 2013 5:09 PM (in response to 994967)Welcome to the forum!
>
The doc for Math.floor states that the returned value is equal to a "mathematical integer".
double d = Math.floor (4.2);
What is the value of d: 4.0 or 4? Why?
>
Those are one and the same: the value of 'd' is the integer four. The integer four can be written as '4' without any decimals and can also be written as '4.0', '4.000', '004.00' and an infinite number of other ways.
You always need to be careful to differentiate between the way something is written and its actual value.
You also have to be careful to interpret the doc carefully. In Java if you evaluate 4.0 the value is 'equal to a "mathematical integer"' and that integer is four. But the datatype will be a double because you specified a decimal component (even though that decimal component is zero. Java will evaluate 4 as an int datatype because you did NOT specify a decimal component.
>
If they are different, why (4.0 != 4) does not
evaluate to true?
>
They aren't different.
>
Can you provide me with some pointers to documentation about this?
>
Documentation about what?
About how Java interprets numeric literals? See the Java Language Specification section 3.10 on Literals
>
3.10.2. FloatingPoint Literals
A floatingpoint literal has the following parts: a wholenumber part, a decimal or hexadecimal point (represented by an ASCII period character), a fraction part, an exponent, and a type suffix.
. . .
A floatingpoint literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d (§4.2.3).
>
Notice that the spec includes a 'decimal or hexadecimal point' and a fraction part. It doesn't explicitly say it but the fraction part can consist of zeros.
And the spec says without a suffix of F or f its type is double.
If you read the 3.10.1. Integer Literals section you will that they DO NOT include a decimal point.
>
An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int 
3. Re: The value of a double
J2EE_Life Mar 6, 2013 8:06 AM (in response to 994967)The signature for floor method is as follows:public static double floor(double a)
Which means it does return double(java sense) but since valuewise the decimal part is always 0 for returned value, it is mathematical integer(mathematical sense).
So this method would return a double which is an integer in mathematical sense as decimal part is zero.
I think with above explanation and this one you should get your answer if not, now where are you still stuck? 
4. Re: The value of a double
994967 Mar 6, 2013 1:17 PM (in response to J2EE_Life)Hi,
Thank you so much for your answers. To clarify a couple of points:
 I tried to ask only, not to state anything, I did not want to influence
you (not that I could ;), just a matter of principle ).
 My question "Why?" meant why is true or false (I was interested in
your arguments, not just the answer Yes/No).
 Your question: "who said is different?". My teacher. He slammed me in
a quiz when I said the value is 4 (because System.out.print prints it 4.0 ...
well ...).
Since I want to build a case for note revision as strong as possible, I would
like to ask you about your background: are you working for Oracle? Those
medals on the left of the screen (guru, etc ...): are they awarded be Oracle?
Thank you again for your time. 
5. Re: The value of a double
gimbal2 Mar 6, 2013 3:56 PM (in response to 994967)991964 wrote:
No, most people here don't work for Oracle (when they do, it is specifically indicated using an icon). The medals are awarded for providing correct answers, or at least answers that people perceive as helpful and/or correct. Gotta collect them all!
Since I want to build a case for note revision as strong as possible, I would
like to ask you about your background: are you working for Oracle? Those
medals on the left of the screen (guru, etc ...): are they awarded be Oracle?
PS: you might want to investigate type casting, which in the case of primitives Java sometimes does automatically. Its automagic. 
6. Re: The value of a double
rp0428 Mar 6, 2013 5:49 PM (in response to 994967)>
 Your question: "who said is different?". My teacher. He slammed me in
a quiz when I said the value is 4 (because System.out.print prints it 4.0 ...
well ...).
>
Because your question is in the 'New to Java' forum I'll go through the drill and elaborate.
As I said above you have to distinguish between the value and the way that value is represented.
This is what you ask:
>
The doc for Math.floor states that the returned value is equal to a "mathematical integer".
double d = Math.floor (4.2);
What is the value of d: 4.0 or 4?
>
The value of both 4.0 and 4 is the same: the integer four.
The representation is different: the first displays a single decimal fraction of k0 and the second doesn't.
If you have four dollars what is the value of those dollars when represented using digits? Is it $4 or $4.00? Is the amount of money you have different because you write $4.00?
An integer is a number that can be written without a fractional or decimal component. It doesn't stop being an integer value simply because it is written WITH a decimal component of all zeroes.
If you review the Java docs for the methods involved you will see exactly WHY 'double d' is printed as '4.0'.
The System class  http://docs.oracle.com/javase/6/docs/api/java/lang/System.htmlSystem.out.print prints it 4.0
>
static PrintStream out
The "standard" output stream.
>
So System.out references a PrintStream. The PrintStream class  http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html
PrintStream defines the println methods  the one for double is
>
println
public void println(double x)Prints a double and then terminate the line. This method behaves as though it invokes print(double) and then println().
Parameters:
x  The double to be printed.
>
Ah  it says to see 'print (double)' which is
>
print
public void print(double d)Prints a doubleprecision floatingpoint number. The string produced by String.valueOf(double) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
d  The double to be printed
See Also:
Double.toString(double)
>
The String class has the 'valueOf(double)' method'  http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#valueOf(double)
>
valueOf
public static String valueOf(double d)Returns the string representation of the double argument.
The representation is exactly the one returned by the Double.toString method of one argument.
Parameters:
d  a double.
Returns:
a string representation of the double argument.
See Also:
Double.toString(double)
>
This method (like the one before) also references Double.toString(double)
http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#toString(double)
>
toString
public static String toString(double d)Returns a string representation of the double argument. All characters mentioned below are ASCII characters.
If the argument is NaN, the result is the string "NaN".
Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument.
. . .
How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double.
>
See that? The API REQUIRES that 'There must be at least one digit to represent the fractional part'.
This is the 'toString' method to represent double values as a STRING. The way that a value is represented DOES NOT determine what the value is or alter the value.
An integer value is an integer value whether it is represented as a string, stored in an int, long, double, float or stored in any other way. The value is immutable as regards the manner in which it is stored or represented.
You didn't actually tell us the actual, exact question that your teacher ask.
If your teacher ask what will be printed or displayed then the answer is 4.0 and not 4. But the 'value' of each of those is the same.
Java Puzzlers, certification tests and other questions may very well ask 'what will be printed . . .' questions. And you need to make sure you answer that question with 4.0 and not 4. 
7. Re: The value of a double
994967 Mar 7, 2013 5:29 PM (in response to rp0428)Thank you for your detailed answer.
About the quiz question: I had to evaluate the result
of a bunch of operations, like (anInt + aDouble)%(...).
I had to say: what is the value and what is the type of the result?
I said the value is 4 and the type double (it evaluated to a straight
integer of 4, no rouding necessary). There was no question
about what some method would display or what the literal would be,
just the value and the type of that expression.
Now ... I do fully understand that, mathematically, the value of 4.0 is 4.
Actually, my knowledge in mathematics goes a little bit above this level.
The question is: in the computer programming world, is it the same?
The problem is that I cannot convince the guy, unless I can provide something written on
an official paper. I have tried all arguments you provided and a little bit more to no avail.
I am making the devil's advocate: what if he is right? I have to prove him wrong.
Why, in all official Java docs I found on oracle.com, when they mention a floatingpoint
value they say 2.0 and pretty much never 2 ? The obvious answer (to me) is that they want to emphasise this
is a real number (which happens to have an integer value). But what if there is a more
subtle answer to this? Each professional field has it's own peculiarities (at least in the eyes of the common of mortals).
Let me give you an example: in the every day language, when you say a car accelerates = the speed
increases. In physics, it could also go down or stay the same (in value). For us, speed is a scalar,
while for physicists is a vector.
I have a practical knowledge of programming, but I do lack the academical background. What if "value" in computer
language has an extended meaning (the kind of "vector" in the example above, which has a value  the scalar  but
also direction and origin)? What if the computer language "value" includes the mathematical value plus some other
(less obvious) dimensions? I do not expect this to make a practical difference, but it would be enough to prove the teacher right.
Here:
http://docs.oracle.com/javase/specs/jls/se7/html/jls4.html#jls4.2.3
they talk about "32bit and doubleprecision 64bit format IEEE 754 values". This is rather computer
language. In mathematics you talk about real numbers and integers etc.
Or:
"The elements of the double value set are exactly the values that can be represented using the double floatingpoint format defined in the IEEE 754 standard."
The example with Math.floor is an example where the docs explicitly associate a "double
value" (computer language) with a "mathematical value" (an integer in this case). I wish I could find
this kind of explicit connexion in a document about values.
Thank you very much for your time. 
8. Re: The value of a double
EJP Mar 7, 2013 6:28 PM (in response to 994967)The actual binary value held in the double consists of a sign, a mantissa of 1, and an exponent of 2, meaning 1 << 2, or 4. Or 4.0. Or 4.00. Or 4.000000000000000. It's all the same thing inside the double. What's different is what happens when you print, it, and that depends entirely on how you print it. If you use printf() or DecimalFormat with a mask, you will get whatever the mask dictates. If you use use print() or println(), you will get some default. None of that changes what's in the double.