diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml index 8fa818bce5825321daf49c51468cc1e02bcc5bd3..5e410707fcf7244a1368359009a946c7918c22e2 100644 --- a/Doc/Sd1/languageFundamentals.xml +++ b/Doc/Sd1/languageFundamentals.xml @@ -1194,7 +1194,7 @@ Seconds:31</programlisting> <qandaentry> <question> <para>A programmer named Joe intends to map <code>long</code> - values to <code>int</code> values. This requires mapping the + values to <code>int</code> values. This requires scaling the <code>long</code> data type's range <inlineequation> <m:math display="inline"> <m:mrow> @@ -1229,7 +1229,7 @@ Seconds:31</programlisting> <m:mo>]</m:mo> </m:mrow> </m:math> - </inlineequation> to an <code>int</code>'s range of + </inlineequation> down to an <code>int</code>'s range of <inlineequation> <m:math display="inline"> <m:mrow> @@ -1264,142 +1264,75 @@ Seconds:31</programlisting> <m:mo>]</m:mo> </m:mrow> </m:math> - </inlineequation>. Joe's first attempt reads:</para> + </inlineequation>. </para> - <programlisting language="java">final long longValue = ...; - ... -final int reducedValue = (int) (longValue / (2 * (Integer.MAX_VALUE + 1)));</programlisting> - - <para>Unfortunately the results are not promising. May you help - Joe correcting his error? Provide an example showing the - problem.</para> - </question> - - <answer> - <para>We consider:</para> - - <programlisting language="java">final long longValue = Long.MAX_VALUE; - -final int reducedValue = (int) (longValue / (2 * (Integer.MAX_VALUE + 1))); -System.out.println(reducedValue);</programlisting> - - <para>The intended result should assign Integer.MAX_VALUE to the - variable <code>reducedValue</code>. Actually Joe created a - runtime error:</para> - - <programlisting language="none">Exception in thread "main" java.lang.ArithmeticException: / by zero</programlisting> - - <para>Joe's idea is dividing a long value reducing it to an - <code>int</code> accordingly. Since - <code>Integer.MAX_VALUE</code> is equal to <inlineequation> + <para>Joe thinks of dividing long values by <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:msup> + <m:mi>2</m:mi> - <m:mi>1</m:mi> - </m:mrow> + <m:mi>32</m:mi> + </m:msup> </m:math> - </inlineequation> Joe assumes <code>(2 * (Integer.MAX_VALUE + - 1)</code> to be equal to <inlineequation> + </inlineequation>. As a result a long value of <inlineequation> <m:math display="inline"> <m:mrow> - <m:mrow> - <m:mi>2</m:mi> - - <m:mo>×</m:mo> - - <m:msup> - <m:mi>2</m:mi> - - <m:mi>31</m:mi> - </m:msup> - </m:mrow> - - <m:mo>=</m:mo> + <m:mo>-</m:mo> <m:msup> <m:mi>2</m:mi> - <m:mi>32</m:mi> + <m:mi>63</m:mi> </m:msup> </m:mrow> </m:math> - </inlineequation>. Dividing a <code>long</code> indeed maps + </inlineequation> will be reduced to the intended value of <inlineequation> <m:math display="inline"> <m:mrow> - <m:mo>[</m:mo> - - <m:mrow> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> - - <m:mi>63</m:mi> - </m:msup> - </m:mrow> - - <m:mo>,</m:mo> - - <m:mrow> - <m:msup> - <m:mi>2</m:mi> - - <m:mi>63</m:mi> - </m:msup> - - <m:mo>-</m:mo> + <m:mo>-</m:mo> - <m:mi>1</m:mi> - </m:mrow> - </m:mrow> + <m:msup> + <m:mi>2</m:mi> - <m:mo>]</m:mo> + <m:mi>31</m:mi> + </m:msup> </m:mrow> </m:math> - </inlineequation> to <inlineequation> + </inlineequation>. Since <inlineequation> <m:math display="inline"> - <m:mrow> - <m:mo>[</m:mo> - - <m:mrow> - <m:mrow> - <m:mo>-</m:mo> - - <m:msup> - <m:mi>2</m:mi> + <m:msup> + <m:mi>2</m:mi> - <m:mi>31</m:mi> - </m:msup> - </m:mrow> + <m:mi>32</m:mi> + </m:msup> + </m:math> + </inlineequation> seems to be equal to <code>2 * + (Integer.MAX_VALUE + 1))</code> (why?) Joe's first attempt + reads:</para> - <m:mo>,</m:mo> + <programlisting language="java">final long longValue = ...; + ... +final int reducedValue = (int) (longValue / (2 * (Integer.MAX_VALUE + 1)));</programlisting> - <m:mrow> - <m:msup> - <m:mi>2</m:mi> + <para>Unfortunately the results are not promising. May you help + Joe correcting his error? Provide an example showing the + problem.</para> + </question> - <m:mi>31</m:mi> - </m:msup> + <answer> + <para>We consider:</para> - <m:mo>-</m:mo> + <programlisting language="java">final long longValue = Long.MAX_VALUE; + +final int reducedValue = (int) (longValue / (2 * (Integer.MAX_VALUE + 1))); +System.out.println(reducedValue);</programlisting> - <m:mi>1</m:mi> - </m:mrow> - </m:mrow> + <para>The intended result should assign Integer.MAX_VALUE to the + variable <code>reducedValue</code>. Actually Joe's attempt + creates a runtime error:</para> - <m:mo>]</m:mo> - </m:mrow> - </m:math> - </inlineequation>.</para> + <programlisting language="none">Exception in thread "main" java.lang.ArithmeticException: / by zero</programlisting> <para>Unfortunately Joe's assumption is seriously flawed:</para>