diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index 3e8acf70744203a1c5c211c195ebde22bdcddc3a..e88e2f01f8bdd18e53284f74b9d62d5f4fa0d6c8 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -404,17 +404,21 @@ representation</screen></td> <figure xml:id="sd1_fig_varDeclare"> <title>Variable declaration</title> - <programlisting language="java">double pi;</programlisting> + <programlisting language="java">// Variable declaration: +// Variable's type is double +// Variable's name is «pi» (identifier) + +double pi;</programlisting> </figure> <para>We may assign values to variables or build expressions like <code language="java">pi * 2.0 * 2.0</code> :</para> <figure xml:id="sd1_fig_varDeclareAndUse"> - <title>Declare and use</title> + <title>Declare, assign and use</title> <programlisting language="java">double pi; // Variable declaration -... + pi = 3.1415926; // Assigning value to variable // Print a circle's area of radius 2.0 @@ -429,8 +433,8 @@ System.out.println(pi * 2.0 * 2.0);</programlisting> <glossterm>Separate declaration and initialization</glossterm> <glossdef> - <programlisting language="java">double pi; // Declaration of variable pi -... + <programlisting language="java">double pi; // Declaration of variable pi + pi = 3.1415926; // Value assignment</programlisting> </glossdef> </glossentry> @@ -491,8 +495,8 @@ int c;</programlisting> <listitem> <para><link linkend="sd1_sect_methods">Method</link> names, e.g. - <methodname>public static void - main(String[args])</methodname>.</para> + <methodname>public static void <emphasis + role="red">main</emphasis>(String [] args)</methodname>.</para> </listitem> </itemizedlist> </listitem> @@ -3690,7 +3694,7 @@ _____________________________________ <programlisting language="java">float float2Power31 = Integer.MAX_VALUE + 1f; -float floatDoubleMAX_VALUE = 2 * float2Power31* float2Power31 - 1f; // 2^63 - 1 +float floatDoubleMAX_VALUE = 2 * float2Power31 * float2Power31 - 1f; // 2^63 - 1 System.out.format( " Float value: %f\n", floatDoubleMAX_VALUE); System.out.println("Expected value: " + Long.MAX_VALUE);</programlisting> @@ -3711,6 +3715,206 @@ Expected value: 9223372036854775807</screen> </imageobject> </mediaobject> </figure> + + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_floatDoubleLiteral"> + <title><code>float</code> vs. <code>double</code></title> + + <qandadiv> + <qandaentry> + <question> + <para>We consider:</para> + + <informaltable border="1"> + <tr> + <th>Code</th> + + <th>Output</th> + </tr> + + <tr> + <td valign="top"><programlisting language="java">System.out.println( 3.14 ); +System.out.println( 3.14f ); +System.out.println( 3.14d );</programlisting></td> + + <td valign="top"><screen>3.14 +3.14 +3.14</screen></td> + </tr> + </informaltable> + + <para>There seems to be no difference between the three literals + <code language="java">3.14</code>, <code + language="java">3.14f</code> and <code + language="java">3.14d</code>. Is this actually true?</para> + + <tip> + <para>Read the section about floating point literals in <xref + linkend="glo_Java"/>. Write code exhibiting possible + differences.</para> + </tip> + </question> + + <answer> + <para>The System.out.println( 3.14f ) statement's output is + actually truncated with respect to output precision. Forcing 16 + fractional digits to become visible reads:<informaltable + border="1"> + <tr> + <th>Code</th> + + <th>Output</th> + </tr> + + <tr> + <td valign="top"><programlisting language="java">System.out.println(new DecimalFormat( + "#0.0000000000000000").format(3.14f));</programlisting></td> + + <td valign="top"><screen>3.1400001049041750</screen></td> + </tr> + </informaltable>The value 3.1400001049041750 is the closest + possible approximation to 3.14 when using a <link + xlink:href="https://en.wikipedia.org/wiki/IEEE_754-2008_revision">4-byte + IEEE float</link>. A <code language="java">3.14f</code> <code + language="java">double</code> literal won't be exact either but + doubles the number of representing bytes to 8 thereby enhancing + its representational precision substantially. This causes + unexpected results:</para> + + <informaltable border="1"> + <tr> + <th>Code</th> + + <th>Output</th> + </tr> + + <tr> + <td valign="top"><programlisting language="none">System.out.println(<emphasis + role="red">3.14f</emphasis> - <emphasis role="red">3.14d</emphasis>);</programlisting></td> + + <td valign="top"><screen>1.0490417468034252E-7</screen></td> + </tr> + </informaltable> + + <para>This difference is a result of <code + language="java">3.14d</code> providing four additional bytes of + precision therefore matching the exact value of <inlineequation> + <m:math display="inline"> + <m:mfrac> + <m:mi>314</m:mi> + + <m:mi>100</m:mi> + </m:mfrac> + </m:math> + </inlineequation> better than <code + language="java">3.14f</code>.</para> + + <para>Regarding types we have:</para> + + <informaltable border="1"> + <tr> + <th>Literal</th> + + <th>Comment</th> + </tr> + + <tr> + <td valign="top"><code language="java">3.14f</code></td> + + <td valign="top">4-byte <code language="java">float</code> + literal</td> + </tr> + + <tr> + <td valign="top"><code language="java">3.14d</code></td> + + <td valign="top">8-byte <code language="java">double</code> + literal</td> + </tr> + + <tr> + <td valign="top"><code language="java">3.14</code></td> + + <td valign="top">Equivalent to <code + language="java">3.14d</code></td> + </tr> + </informaltable> + + <para>Assignments to variables of type <code + language="java">double</code> are thus always guaranteed to + work:</para> + + <informaltable border="1"> + <tr> + <th>Code</th> + + <th>Comment</th> + </tr> + + <tr> + <td valign="top"><programlisting language="java">double d = 3.14f;</programlisting></td> + + <td valign="top">o.K., assigning <code + language="java">float</code> to <code + language="java">double</code> (widening)</td> + </tr> + + <tr> + <td valign="top"><programlisting language="java">double d = 3.14d;</programlisting></td> + + <td valign="top">o.K., assigning <code + language="java">double</code> to <code + language="java">double</code></td> + </tr> + + <tr> + <td valign="top"><programlisting language="java">double d = 3.14;</programlisting></td> + + <td valign="top">o.K., assigning <code + language="java">double</code> to <code + language="java">double</code> as well.</td> + </tr> + </informaltable> + + <para>Assignments to variables of type <code + language="java">float</code> may fail:</para> + + <informaltable border="1"> + <tr> + <th>Code</th> + + <th>Comment</th> + </tr> + + <tr> + <td valign="top"><programlisting language="java">float d = 3.14f;</programlisting></td> + + <td valign="top">o.K., assigning <code + language="java">float</code> to <code + language="java">float</code> (widening)</td> + </tr> + + <tr> + <td valign="top"><programlisting language="java"><emphasis + role="red">float d = 3.14d;</emphasis></programlisting></td> + + <td valign="top"><emphasis role="red">Error, assigning <code + language="java">double</code> to <code + language="java">float</code></emphasis></td> + </tr> + + <tr> + <td valign="top"><programlisting language="java"><emphasis + role="red">float d = 3.14;</emphasis></programlisting></td> + + <td valign="top"><emphasis role="red">Error, assigning <code + language="java">double</code> to <code + language="java">float</code> as well.</emphasis></td> + </tr> + </informaltable> + </answer> + </qandaentry> + </qandadiv> + </qandaset> </section> <section xml:id="sectConversions">