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

Adding sine degree radian exercise

parent 08bf3e50
No related branches found
No related tags found
No related merge requests found
......@@ -15,8 +15,8 @@
<abstract>
<para>Working with class <classname>String</classname>.</para>
<para>Pitfalls when using operator <code language="java">==</code>
</para>
<para>Pitfalls when using operator <code
language="java">==</code></para>
<para>Using <methodname>equals(...)</methodname>.</para>
</abstract>
......@@ -652,7 +652,228 @@ hashcode of BB: 2112</screen></td>
<para><classname
xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html">Math</classname>
is yet another class belonging to the core set of the <xref
linkend="glo_Java"/> programing language.</para>
linkend="glo_Java"/> programing language. We take a tour on selected
methods:</para>
<figure xml:id="sd1_coreclasses_fig_mathSine">
<title><classname>Math</classname>.<methodname>sin(double
x)</methodname></title>
<informaltable border="1">
<colgroup width="50%"/>
<colgroup width="38%"/>
<colgroup width="12%"/>
<tr>
<th>Code</th>
<th>Result</th>
<th>Math notation</th>
</tr>
<tr>
<td valign="top"><programlisting language="java">final double x = 90;
final double y = Math.sin(x);
System.out.println(y + " == sin(" + x + ")");</programlisting></td>
<td valign="top"><screen>0.8939966636005579 == sin(90.0) </screen></td>
<td valign="top"><informalequation>
<m:math display="block">
<m:mrow>
<m:mi>y</m:mi>
<m:mo>=</m:mo>
<m:mrow>
<m:mi>sin</m:mi>
<m:mo></m:mo>
<m:mi>x</m:mi>
</m:mrow>
</m:mrow>
</m:math>
</informalequation></td>
</tr>
</informaltable>
</figure>
<qandaset defaultlabel="qanda" xml:id="sw1QandaMathSine">
<title>Common pitfall using trigonometric functions</title>
<qandadiv>
<qandaentry>
<question>
<para>We reconsider <xref
linkend="sd1_coreclasses_fig_mathSine"/>. Did you expect a value
of <code>0.8939966636005579</code> corresponding to an angle of
90° here? Discuss the underlying problem.</para>
</question>
<answer>
<para>The mathematically inclined reader may have expected a
result of <code>1.000...</code> corresponding to a right angle of
90° rather than <code>0.893...</code>.</para>
<para>This is a common misconception: At school you were probably
using so called <quote>degrees</quote> ranging from 0° to 360° for
describing angle values. In Mathematics however trigonometric
functions are being defined as power series e.g.:</para>
<informalequation>
<m:math display="block">
<m:mrow>
<m:mrow>
<m:mi>sin</m:mi>
<m:mo></m:mo>
<m:mi>x</m:mi>
</m:mrow>
<m:mo>=</m:mo>
<m:mi>x</m:mi>
<m:mo>-</m:mo>
<m:mfrac>
<m:msup>
<m:mi>x</m:mi>
<m:mi>3</m:mi>
</m:msup>
<m:mi>3!</m:mi>
</m:mfrac>
<m:mo>+</m:mo>
<m:mfrac>
<m:msup>
<m:mi>x</m:mi>
<m:mi>5</m:mi>
</m:msup>
<m:mi>5!</m:mi>
</m:mfrac>
<m:mo>+</m:mo>
<m:mi>...</m:mi>
<m:mo>=</m:mo>
<m:mrow>
<m:munderover>
<m:mo></m:mo>
<m:mi>n = 0</m:mi>
<m:mi mathvariant="normal"></m:mi>
</m:munderover>
<m:mfrac>
<m:mrow>
<m:msup>
<m:mrow>
<m:mo>(</m:mo>
<m:mi>-1</m:mi>
<m:mo>)</m:mo>
</m:mrow>
<m:mi>n</m:mi>
</m:msup>
<m:mo></m:mo>
<m:msup>
<m:mi>x</m:mi>
<m:mrow>
<m:mi>2n</m:mi>
<m:mo>+</m:mo>
<m:mi>1</m:mi>
</m:mrow>
</m:msup>
</m:mrow>
<m:mrow>
<m:mo>(</m:mo>
<m:mrow>
<m:mi>2n</m:mi>
<m:mo>+</m:mo>
<m:mi>1</m:mi>
</m:mrow>
<m:mo>)</m:mo>
<m:mi>!</m:mi>
</m:mrow>
</m:mfrac>
</m:mrow>
</m:mrow>
</m:math>
</informalequation>
<para>As an immediate consequence describing a full circle of
angle values the variable x here is ranging from 0 to
<inlineequation>
<m:math display="inline">
<m:mrow>
<m:mi>2</m:mi>
<m:mo></m:mo>
<m:mi>π</m:mi>
</m:mrow>
</m:math>
</inlineequation> rather than from 0° to 360°. This angle unit
is called radians. If you still want to use degrees you will have
to convert these to radians beforehand by multiplying with
<inlineequation>
<m:math display="inline">
<m:mfrac bevelled="true">
<m:mrow>
<m:mi>2</m:mi>
<m:mo></m:mo>
<m:mi>π</m:mi>
</m:mrow>
<m:mi>360°</m:mi>
</m:mfrac>
</m:math>
</inlineequation> or simply <inlineequation>
<m:math display="inline">
<m:mfrac bevelled="true">
<m:mi>π</m:mi>
<m:mi>180</m:mi>
</m:mfrac>
</m:math>
</inlineequation>:</para>
<programlisting language="java">final double x = 90;
final double y = Math.sin(x * Math.PI / 180); //converting degrees to radians
System.out.println(y + " == sin(" + x + ")");</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
<qandaset defaultlabel="qanda" xml:id="sw1QandaCircleAreaMathPackage">
<title>Using constants from <classname
......@@ -677,12 +898,12 @@ hashcode of BB: 2112</screen></td>
}</programlisting>
<para>You may have wondered why you had to punch in the value of
<inlineequation>
such an important constant as <inlineequation>
<m:math display="inline">
<m:mi>π</m:mi>
</m:math>
</inlineequation> yourself. Actually <xref linkend="glo_Java"/>
predefines constants in <classname
</inlineequation> by yourself. Actually <xref
linkend="glo_Java"/> predefines constants in <classname
xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html">java.lang.Math</classname>
class. Read its documentation to rewrite your code thereby
replacing your own variable <code language="java">pi</code>'s
......
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