Skip to content
Snippets Groups Projects
workingWithNumbers.xml 41.7 KiB
Newer Older
BigDecimal result = zero_dot_99.
  subtract(zero_dot_1).
  subtract(zero_dot_1).
  subtract(zero_dot_1);

System.out.println(result);</programlisting>

      <screen>0.69</screen>
    </figure>

    <qandaset defaultlabel="qanda"
              xml:id="sd1_workingWithNumbers_BigDecimalChaining">
      <title>Chaining subtract method calls</title>

      <qandadiv>
        <qandaentry>
          <question>
            <para>Explain the chaining mechanism implementing three successive
            subtractions in <xref
            linkend="sd1_numbers_fig_usingBigDecimalChaining"/>.</para>
          </question>

          <answer>
            <para>We may re-write the statement in question:</para>

            <programlisting language="none">result = zero_dot_99.subtract(zero_dot_1).subtract(zero_dot_1).subtract(zero_dot_1);
                    \               /                     /             /
                     \             /                     /             /
                      \           /                     /             /
                        0.99 - 0.1                     /             /
                                  \                   /             /
                                   \                 /             /
                                    \               /             /
                                        0.98 - 0.1               /
                                              \                 /
                                               \               /
                                                  0.97 - 0.1
                                                    == 0.96</programlisting>

            <para>Each <methodname>subtract(...)</methodname> call returns a
            new result instance of class <classname
            xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigDecimal.html">BigDecimal</classname>.
            We can thus chain a subsequent
            <methodname>subtract(...)</methodname> call using this returned
            instance.</para>
          </answer>
        </qandaentry>
      </qandadiv>
    </qandaset>

    <figure xml:id="sd1_numbers_fig_usingBigDecimalFeatures">
      <title><classname
Goik Martin's avatar
Goik Martin committed
      xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigDecimal.html">BigDecimal</classname>
      features</title>

      <itemizedlist>
        <listitem>
          <para>Higher memory allocation hosting higher precision.</para>
        </listitem>

        <listitem>
          <para>Immutable instances</para>
        </listitem>

        <listitem>
          <para>Calculation performance penalty.</para>
        </listitem>

        <listitem>
          <para>Clumsy interface.</para>
        </listitem>
      </itemizedlist>
    </figure>
  </section>

  <section xml:id="sd1_numbers_sect_GeneratingRandomNumbers">
    <title>Generating Random Numbers</title>
    <figure xml:id="sd1_numbers_fig_random">
      <title>Using <methodname
Goik Martin's avatar
Goik Martin committed
      xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Math.html#random()">static
      double random​()</methodname></title>

      <programlisting language="java">for (int i = 0; i &lt; 10; i++) {
  System.out.println(Math.random());
}</programlisting>

      <screen>0.4754286988826202
0.0060114391743414375
...
0.9047785351372987
0.2516070321935864</screen>
    </figure>

    <figure xml:id="sd1_numbers_fig_randomSeed">
      <title>Seeding a pseudo random generator</title>

      <programlisting language="java">try(final Scanner scanner = new Scanner(System.in)) {
  System.out.print("Enter an integer seed:");
  final long seed = scanner.nextLong();

  Random generator = new Random(seed);
  for (int i = 0; i &lt; 10; i++) {
    System.out.print(generator.nextBoolean() + " ");
  }
}</programlisting>

      <screen>Enter an integer seed:4237549835735
Goik Martin's avatar
Goik Martin committed
false true true true false false false true false true</screen>
    </figure>
Goik Martin's avatar
Goik Martin committed
  </section>
</chapter>