Skip to content
Snippets Groups Projects
Commit 5cf12a9f authored by Goik Martin's avatar Goik Martin
Browse files

Cosmetics

parent 7f9611b7
No related branches found
No related tags found
No related merge requests found
......@@ -190,8 +190,9 @@ Provided: double</screen>
</listitem>
<listitem>
<para>The cleanest way is replacing the <code>double</code>
literal by a a <code>float</code> literal:</para>
<para>Replacing the <code>double</code> literal by a a
<code>float</code> literal leaves us with an overall
<code>float</code> expression resolving the issue:</para>
<screen>radius * radius * 3.1415926<emphasis role="red">F</emphasis>
......@@ -200,8 +201,6 @@ Provided: double</screen>
int * float
⭨ ⭩
float</screen>
<para>This resolves the return type clash.</para>
</listitem>
</orderedlist>
</answer>
......@@ -229,14 +228,14 @@ Provided: double</screen>
adds a logging statement:</para>
<programlisting language="java">static public int getSquare (int value) {
int result = value * value;
final int result = value * value;
log.info(result);
return result;
}</programlisting>
<para>The team is baffled: Albeit squares of integer values are
expected to be always positive occasionally negative results show
up:</para>
expected to be strictly positive occasionally negative results
show up:</para>
<screen>INFO [main] sd1.SupportMethods (SupportMethods.java:15) - -1757895751</screen>
......@@ -289,9 +288,23 @@ Provided: double</screen>
</informaltable>
<para>The product <code>46341 * 46341</code> of type
<code>int</code> however exceeds this value for the very first
time. Due to an <code>int</code>'s four-byte two complement
representation we have:</para>
<code>int</code> for example exceeds <inlineequation>
<m:math display="inline">
<m:mrow>
<m:msup>
<m:mi>2</m:mi>
<m:mi>31</m:mi>
</m:msup>
<m:mo>-</m:mo>
<m:mi>1</m:mi>
</m:mrow>
</m:math>
</inlineequation> for the very first time. Due to an
<code>int</code>'s four-byte two complement representation we
have:</para>
<informaltable border="1">
<tr>
......@@ -306,12 +319,16 @@ Provided: double</screen>
<td valign="top"><screen>-2147479015</screen></td>
</tr>
</informaltable>
<para>Starting from <code>46341</code> all computed squares
are simply wrong due to arithmetic overflow regardless of
their result's sign. </para>
</listitem>
<listitem>
<para>Solving the issue requires a data type being able to
accommodate squares of arbitrary <code>int</code> values. The
smallest int value is <inlineequation>
<para>Solving the issue requires a data type to accommodate
squares of arbitrary <code>int</code> values. The smallest int
value (having maximum amount) is <inlineequation>
<m:math display="inline">
<m:mrow>
<m:mo>-</m:mo>
......@@ -323,7 +340,7 @@ Provided: double</screen>
</m:msup>
</m:mrow>
</m:math>
</inlineequation> and its square thus <inlineequation>
</inlineequation>. Its square is thus <inlineequation>
<m:math display="inline">
<m:msup>
<m:mi>2</m:mi>
......@@ -331,12 +348,31 @@ Provided: double</screen>
<m:mi>62</m:mi>
</m:msup>
</m:math>
</inlineequation>.We choose <code>long</code> as result
type. Turning an <code>int</code> into a long is easy:</para>
</listitem>
</inlineequation>. Choosing return type <code>long</code>
allows for a maximum value of <inlineequation>
<m:math display="inline">
<m:mrow>
<m:msup>
<m:mi>2</m:mi>
<listitem>
<para/>
<m:mi>63</m:mi>
</m:msup>
<m:mo>-</m:mo>
<m:mi>1</m:mi>
</m:mrow>
</m:math>
</inlineequation>which is ways larger than required. Turning
the <code>int</code> into a <code language="java">long</code>
expression is easy:</para>
<programlisting language="java">static public long getSquare (int value) {
long result = value;
result *= value;
log.info(result);
return result;
}</programlisting>
</listitem>
</orderedlist>
</answer>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment