epubjs
Version:
Render ePub documents in the browser, across many devices
182 lines (179 loc) • 34.4 kB
HTML
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Math Utilities</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Math Utilities"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-11-SECT-1"/>Math Utilities</h1></div></div></div><p>Java supports integer and floating-point arithmetic directly in the
language. Higher-level math operations are supported through the
<a id="I_indexterm11_id735971" class="indexterm"/><code class="literal">java.lang.Math</code> class. As
you may have seen by now, wrapper classes for primitive data types allow
you to treat them as objects. Wrapper classes also hold some methods for
basic conversions.</p><p>First, a few words about built-in arithmetic in Java. Java handles
errors in integer arithmetic by throwing an <a id="I_indexterm11_id735987" class="indexterm"/><code class="literal">ArithmeticException</code>:</p><a id="I_11_tt681"/><pre class="programlisting"> <code class="kt">int</code> <code class="n">zero</code> <code class="o">=</code> <code class="mi">0</code><code class="o">;</code>
<code class="k">try</code> <code class="o">{</code>
<code class="kt">int</code> <code class="n">i</code> <code class="o">=</code> <code class="mi">72</code> <code class="o">/</code> <code class="n">zero</code><code class="o">;</code>
<code class="o">}</code> <code class="k">catch</code> <code class="o">(</code> <code class="n">ArithmeticException</code> <code class="n">e</code> <code class="o">)</code> <code class="o">{</code>
<code class="c1">// division by zero</code>
<code class="o">}</code></pre><p>To generate the error in this example, we created the intermediate
variable <code class="literal">zero</code>. The compiler is somewhat
crafty and would have caught us if we had blatantly tried to perform
division by a literal zero.</p><p>Floating-point arithmetic expressions, on the other hand, don’t
throw exceptions. Instead, they take on the special out-of-range values
shown in <a class="xref" href="ch11s01.html#learnjava3-CHP-11-TABLE-1" title="Table 11-1. Special floating-point values">Table 11-1</a>.</p><div class="table"><a id="learnjava3-CHP-11-TABLE-1"/><p class="title">Table 11-1. Special floating-point values</p><div class="table-contents"><table summary="Special floating-point values" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; "><colgroup><col/><col/></colgroup><thead><tr><th style="text-align: left"><p>Value</p></th><th style="text-align: left"><p>Mathematical
representation</p></th></tr></thead><tbody><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736071" class="indexterm"/> <code class="literal">POSITIVE_INFINITY</code> </p></td><td style="text-align: left"><p>1.0/0.0</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736095" class="indexterm"/> <code class="literal">NEGATIVE_INFINITY</code> </p></td><td style="text-align: left"><p>-1.0/0.0</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736118" class="indexterm"/> <a id="I_indexterm11_id736125" class="indexterm"/> <code class="literal">NaN</code>
</p></td><td style="text-align: left"><p>0.0/0.0</p></td></tr></tbody></table></div></div><p>The following example generates an infinite result:</p><a id="I_11_tt682"/><pre class="programlisting"> <code class="kt">double</code> <code class="n">zero</code> <code class="o">=</code> <code class="mf">0.0</code><code class="o">;</code>
<code class="kt">double</code> <code class="n">d</code> <code class="o">=</code> <code class="mf">1.0</code><code class="o">/</code><code class="n">zero</code><code class="o">;</code>
<code class="k">if</code> <code class="o">(</code> <code class="n">d</code> <code class="o">==</code> <code class="n">Double</code><code class="o">.</code><code class="na">POSITIVE_INFINITY</code> <code class="o">)</code>
<code class="n">System</code><code class="o">.</code><code class="na">out</code><code class="o">.</code><code class="na">println</code><code class="o">(</code> <code class="s">"Division by zero"</code> <code class="o">);</code></pre><p>The special value <code class="literal">NaN</code> (not a
number) indicates the result of dividing zero by zero. This value has the
special mathematical distinction of not being equal to itself (<code class="literal">NaN != NaN</code> evaluates to <code class="literal">true</code>). Use <code class="literal">Float.isNaN()</code> or <code class="literal">Double.isNaN()</code> to test for <code class="literal">NaN</code>.</p><div class="sect2" title="The java.lang.Math Class"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-11-SECT-1.1"/>The java.lang.Math Class</h2></div></div></div><p><a id="idx10630" class="indexterm"/> <a id="idx10661" class="indexterm"/>The <code class="literal">java.lang.Math</code>
class is Java’s math library. It holds a suite of static methods
covering all of the usual mathematical operations like <a id="I_indexterm11_id736231" class="indexterm"/><code class="literal">sin()</code>, <a id="I_indexterm11_id736241" class="indexterm"/><code class="literal">cos()</code>, and <a id="I_indexterm11_id736252" class="indexterm"/><code class="literal">sqrt()</code>. The <code class="literal">Math</code> class isn’t very object-oriented (you
can’t create an instance of <code class="literal">Math</code>).
Instead, it’s really just a convenient holder for static methods that
are more like global functions. As we saw in <a class="xref" href="ch06.html" title="Chapter 6. Relationships Among Classes">Chapter 6</a>, it’s possible to use the static import
functionality to import the names of static methods and constants like
this directly into the scope of our class and use them by their simple,
unqualified names.</p><p><a class="xref" href="ch11s01.html#learnjava3-CHP-11-TABLE-2" title="Table 11-2. Methods in java.lang.Math">Table 11-2</a> summarizes the
methods in <code class="literal">java.lang.Math</code>.</p><div class="table"><a id="learnjava3-CHP-11-TABLE-2"/><p class="title">Table 11-2. Methods in java.lang.Math</p><div class="table-contents"><table summary="Methods in java.lang.Math" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; "><colgroup><col/><col/><col/></colgroup><thead><tr><th style="text-align: left"><p>Method</p></th><th style="text-align: left"><p>Argument type(s)</p></th><th style="text-align: left"><p>Functionality</p></th></tr></thead><tbody><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736352" class="indexterm"/> <code class="literal">Math.abs(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">int</code>, <code class="literal">long</code>, <code class="literal">float</code>, <code class="literal">double</code></p></td><td style="text-align: left"><p>Absolute value</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736399" class="indexterm"/> <code class="literal">Math.acos(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Arc cosine</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736433" class="indexterm"/> <code class="literal">Math.asin(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Arc sine</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736466" class="indexterm"/> <code class="literal">Math.atan(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Arc tangent</p></td></tr><tr><td style="text-align: left"><p> <code class="literal">Math.atan2(a,b)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Angle part of rectangular-to-polar
coordinate transform</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736527" class="indexterm"/> <code class="literal">Math.ceil(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Smallest whole number greater than or
equal to <code class="literal">a</code></p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736565" class="indexterm"/> <code class="literal">Math.cbrt(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Cube root of <code class="literal">a</code></p></td></tr><tr><td style="text-align: left"><p> <code class="literal">Math.cos(a)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Cosine</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736630" class="indexterm"/> <code class="literal">Math.cosh(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Hyperbolic cosine</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736663" class="indexterm"/> <code class="literal">Math.exp(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p> <code class="literal">Math.E</code> to the power <code class="literal">a</code></p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736706" class="indexterm"/> <code class="literal">Math.floor(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Largest whole number less than or
equal to <code class="literal">a</code></p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736744" class="indexterm"/> <code class="literal">Math.hypot(a,b)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Precision calculation of the <code class="literal">sqrt()</code> of <code class="literal">a</code>2 + <code class="literal">b</code>2</p></td></tr><tr><td style="text-align: left"><p><code class="literal">Math.log(a)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Natural logarithm of <code class="literal">a</code></p></td></tr><tr><td style="text-align: left"><p> <code class="literal">Math.log10(a)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Log base 10 of <code class="literal">a</code></p></td></tr><tr><td style="text-align: left"><p><code class="literal">Math.max(a,
b)</code> </p></td><td style="text-align: left"><p> <code class="literal">int</code>, <code class="literal">long</code>, <code class="literal">float</code>, <code class="literal">double</code></p></td><td style="text-align: left"><p>The value <code class="literal">a</code> or <code class="literal">b</code> closer to <code class="literal">Long.MAX_VALUE</code></p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736911" class="indexterm"/> <code class="literal">Math.min(a, b)</code>
</p></td><td style="text-align: left"><p> <code class="literal">int</code>, <code class="literal">long</code>, <code class="literal">float</code>, <code class="literal">double</code></p></td><td style="text-align: left"><p>The value <code class="literal">a</code> or <code class="literal">b</code> closer to <code class="literal">Long.MIN_VALUE</code></p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id736973" class="indexterm"/> <code class="literal">Math.pow(a, b)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p> <code class="literal">a</code>
to the power <code class="literal">b</code></p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737016" class="indexterm"/> <code class="literal">Math.random()</code>
</p></td><td style="text-align: left"><p> <code class="literal">None</code> </p></td><td style="text-align: left"><p>Random-number generator</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737050" class="indexterm"/> <code class="literal">Math.rint(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Converts double value to integral
value in double format</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737084" class="indexterm"/> <code class="literal">Math.round(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">float</code>, <code class="literal">double</code></p></td><td style="text-align: left"><p>Rounds to whole number</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737121" class="indexterm"/> <code class="literal">Math.signum(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code>, <code class="literal">float</code></p></td><td style="text-align: left"><p>Get the sign of the number at 1.0,
–1.0, or 0</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737160" class="indexterm"/> <code class="literal">Math.sin(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Sine</p></td></tr><tr><td style="text-align: left"><p> <code class="literal">Math.sinh(a)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Hyperbolic sine</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737220" class="indexterm"/> <code class="literal">Math.sqrt(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Square root</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737254" class="indexterm"/> <code class="literal">Math.tan(a)</code>
</p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Tangent</p></td></tr><tr><td style="text-align: left"><p> <code class="literal">Math.tanh(a)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Hyperbolic tangent</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737314" class="indexterm"/> <code class="literal">Math.toDegrees(a)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Convert radians to
degrees</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id737349" class="indexterm"/> <code class="literal">Math.toRadians(a)</code> </p></td><td style="text-align: left"><p> <code class="literal">double</code> </p></td><td style="text-align: left"><p>Convert degrees to
radians</p></td></tr></tbody></table></div></div><p><a id="I_indexterm11_id737378" class="indexterm"/> <code class="literal">log()</code>, <a id="I_indexterm11_id737390" class="indexterm"/><code class="literal">pow()</code>, and <code class="literal">sqrt()</code> can throw a runtime <a id="I_indexterm11_id737405" class="indexterm"/><code class="literal">ArithmeticException</code>.
<a id="I_indexterm11_id737416" class="indexterm"/><code class="literal">abs()</code>, <a id="I_indexterm11_id737427" class="indexterm"/><code class="literal">max()</code>, and <code class="literal">min()</code> are overloaded for all the scalar
values, <code class="literal">int</code>, <code class="literal">long</code>, <code class="literal">float</code>, or <code class="literal">double</code>, and return the corresponding type.
Versions of <a id="I_indexterm11_id737464" class="indexterm"/><code class="literal">Math.round()</code> accept
either <code class="literal">float</code> or <code class="literal">double</code> and return <code class="literal">int</code> or <code class="literal">long</code>, respectively. The rest of the methods
operate on and return <code class="literal">double</code>
values:</p><a id="I_11_tt683"/><pre class="programlisting"> <code class="kt">double</code> <code class="n">irrational</code> <code class="o">=</code> <code class="n">Math</code><code class="o">.</code><code class="na">sqrt</code><code class="o">(</code> <code class="mf">2.0</code> <code class="o">);</code> <code class="c1">// 1.414...</code>
<code class="kt">int</code> <code class="n">bigger</code> <code class="o">=</code> <code class="n">Math</code><code class="o">.</code><code class="na">max</code><code class="o">(</code> <code class="mi">3</code><code class="o">,</code> <code class="mi">4</code> <code class="o">);</code> <code class="c1">// 4</code>
<code class="kt">long</code> <code class="n">one</code> <code class="o">=</code> <code class="n">Math</code><code class="o">.</code><code class="na">round</code><code class="o">(</code> <code class="mf">1.125798</code> <code class="o">);</code> <code class="c1">// 1</code></pre><p>For convenience, <code class="literal">Math</code> also
contains the static final double values <a id="I_indexterm11_id737519" class="indexterm"/><code class="literal">E</code> and <code class="literal">PI</code>:<a id="I_indexterm11_id737535" class="indexterm"/><a id="I_indexterm11_id737542" class="indexterm"/></p><a id="I_11_tt684"/><pre class="programlisting"> <code class="kt">double</code> <code class="n">circumference</code> <code class="o">=</code> <code class="n">diameter</code> <code class="o">*</code> <code class="n">Math</code><code class="o">.</code><code class="na">PI</code><code class="o">;</code></pre></div><div class="sect2" title="Big/Precise Numbers"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-11-SECT-1.2"/>Big/Precise Numbers</h2></div></div></div><p><a id="idx10628" class="indexterm"/> <a id="idx10659" class="indexterm"/>If the <code class="literal">long</code> and
<code class="literal">double</code> types are not large or precise
enough for you, the <code class="literal">java.math</code> package
provides two classes, <a id="I_indexterm11_id737609" class="indexterm"/><code class="literal">BigInteger</code> and
<a id="I_indexterm11_id737619" class="indexterm"/><code class="literal">BigDecimal</code>, that
support arbitrary-precision numbers. These full-featured classes have a
bevy of methods for performing arbitrary-precision math and precisely
controlling rounding of remainders. In the following example, we use
<code class="literal">BigDecimal</code> to add two very large
numbers and then create a fraction with a 100-digit result:</p><a id="I_11_tt685"/><pre class="programlisting"> <code class="kt">long</code> <code class="n">l1</code> <code class="o">=</code> <code class="mi">9223372036854775807L</code><code class="o">;</code> <code class="c1">// Long.MAX_VALUE</code>
<code class="kt">long</code> <code class="n">l2</code> <code class="o">=</code> <code class="mi">9223372036854775807L</code><code class="o">;</code>
<code class="n">System</code><code class="o">.</code><code class="na">out</code><code class="o">.</code><code class="na">println</code><code class="o">(</code> <code class="n">l1</code> <code class="o">+</code> <code class="n">l2</code> <code class="o">);</code> <code class="c1">// -2 ! Not good.</code>
<code class="err"> </code>
<code class="k">try</code> <code class="o">{</code>
<code class="n">BigDecimal</code> <code class="n">bd1</code> <code class="o">=</code> <code class="k">new</code> <code class="n">BigDecimal</code><code class="o">(</code> <code class="s">"9223372036854775807"</code> <code class="o">);</code>
<code class="n">BigDecimal</code> <code class="n">bd2</code> <code class="o">=</code> <code class="k">new</code> <code class="n">BigDecimal</code><code class="o">(</code> <code class="mi">9223372036854775807L</code> <code class="o">);</code>
<code class="n">System</code><code class="o">.</code><code class="na">out</code><code class="o">.</code><code class="na">println</code><code class="o">(</code> <code class="n">bd1</code><code class="o">.</code><code class="na">add</code><code class="o">(</code> <code class="n">bd2</code> <code class="o">)</code> <code class="o">);</code> <code class="c1">// 18446744073709551614</code>
<code class="err"> </code>
<code class="n">BigDecimal</code> <code class="n">numerator</code> <code class="o">=</code> <code class="k">new</code> <code class="n">BigDecimal</code><code class="o">(</code><code class="mi">1</code><code class="o">);</code>
<code class="n">BigDecimal</code> <code class="n">denominator</code> <code class="o">=</code> <code class="k">new</code> <code class="n">BigDecimal</code><code class="o">(</code><code class="mi">3</code><code class="o">);</code>
<code class="n">BigDecimal</code> <code class="n">fraction</code> <code class="o">=</code>
<code class="n">numerator</code><code class="o">.</code><code class="na">divide</code><code class="o">(</code> <code class="n">denominator</code><code class="o">,</code> <code class="mi">100</code><code class="o">,</code> <code class="n">BigDecimal</code><code class="o">.</code><code class="na">ROUND_UP</code> <code class="o">);</code>
<code class="c1">// 100 digit fraction = 0.333333 ... 3334</code>
<code class="o">}</code>
<code class="k">catch</code> <code class="o">(</code><code class="n">NumberFormatException</code> <code class="n">nfe</code><code class="o">)</code> <code class="o">{</code> <code class="o">}</code>
<code class="k">catch</code> <code class="o">(</code><code class="n">ArithmeticException</code> <code class="n">ae</code><code class="o">)</code> <code class="o">{</code> <code class="o">}</code></pre><p>If you implement cryptographic or scientific algorithms for fun,
<code class="literal">BigInteger</code> is crucial. Other than
that, you’re not likely to need these classes.<a id="I_indexterm11_id737664" class="indexterm"/><a id="I_indexterm11_id737671" class="indexterm"/></p></div><div class="sect2" title="Floating-Point Components"><div class="titlepage"><div><div><h2 class="title"><a id="id1644633"/>Floating-Point Components</h2></div></div></div><p><a id="idx10629" class="indexterm"/> <a id="idx10660" class="indexterm"/>As we mentioned in <a class="xref" href="ch04.html" title="Chapter 4. The Java Language">Chapter 4</a>, Java uses the IEEE 754 standard to
represent floating-point numbers (float and double types) internally.
Those of you familiar with how floating-point math works will already
know that “decimal” numbers are represented in binary in this standard
by separating the number into three components: a sign (positive or
negative), an exponent representing the <a id="I_indexterm11_id737724" class="indexterm"/><span class="emphasis"><em>magnitude</em></span> in powers of 2 of the
number, and a <a id="I_indexterm11_id737733" class="indexterm"/><span class="emphasis"><em>mantissa</em></span> using up most of the bits to
represent the precise value irrespective of its magnitude. While for
most applications the precision of float and double-type floating-point
numbers is sufficient enough that we don’t need to worry about running
into limitations, there are times when specialized apps may wish to work
with the floating-point values more directly.</p><p>By definition, floating-point numbers trade off precision and
scale. Even the smallest Java floating-point type, <a id="I_indexterm11_id737751" class="indexterm"/><code class="literal">float</code>, can represent
(literally) astronomical numbers ranging from negative
10<sup>–45</sup> to positive
10<sup>38</sup>. This is accomplished, put in decimal
terms, by having the mantissa part of the floating-point value represent
a fixed number of “digits” and the exponent tell us where to put the
decimal point. As the numbers get larger in magnitude, the “precision”
therefore gets shifted to the “left” as more digits appear to the left
of the decimal point. What this means is that floating-point numbers can
very precisly (with a large number of digits) represent small values
like pi, but for bigger numbers (in the billions and trillions)
those digits will be taken up with the more signifcant digits.
Therefore, the gap between any two consecutive numbers that can be
represented by a floating-point value grows larger as the numbers get
bigger.</p><p>For some applications, knowing the limitations may be important.
The <code class="literal">java.lang.Math</code> class therefore
provides a few methods for interrogating floats and doubles about their
precision. The <a id="I_indexterm11_id737796" class="indexterm"/><code class="literal">Math.ulp()</code> method
retrieves the “unit of least precision” for a given floating-point
number, which is the smallest value that bits in the mantissa represent
at their current exponent. Another way to say this is that the <code class="literal">ulp()</code> is the approximate distance from the
floating-point number to the next closest higher or lower floating-point
number that can be represented. Adding positive values smaller than half
the ULP to a float will not yield a new number. Adding values between
half and the full ULP will result in the value plus the ULP. The
<a id="I_indexterm11_id737819" class="indexterm"/><code class="literal">Math.nextUp()</code> method is
a convenience that will take a float and tell you the next number that
can be represented by adding the ULP.</p><a id="I_programlisting11_id737831"/><pre class="programlisting"> <code class="kt">float</code> <code class="n">trillionish</code> <code class="o">=</code> <code class="o">(</code><code class="kt">float</code><code class="o">)</code><code class="mi">1</code><code class="n">e12</code><code class="o">;</code> <code class="c1">// trillionish ~= 999,999,995,904</code>
<code class="kt">float</code> <code class="n">ulp</code> <code class="o">=</code> <code class="n">Math</code><code class="o">.</code><code class="na">ulp</code><code class="o">(</code> <code class="n">f</code> <code class="o">);</code> <code class="c1">// ulp = 65536</code>
<code class="kt">float</code> <code class="n">next</code> <code class="o">=</code> <code class="n">Math</code><code class="o">.</code><code class="na">nextUp</code><code class="o">(</code> <code class="n">f</code> <code class="o">);</code> <code class="c1">// next ~= 1000000061440</code>
<code class="n">trillionish</code> <code class="o">+=</code> <code class="mi">32767</code><code class="o">;</code> <code class="c1">// trillionish still ~= 999,999,995,904. No change!</code></pre><p>Additionally, the <code class="literal">java.lang.Math</code> class contains the method
<a id="I_indexterm11_id737847" class="indexterm"/><code class="literal">getExponent()</code>, which
retrieves the exponent part of a floating-point number (and from there
one could determine the mantissa by division). It is also possible to
get the raw bits of a float or double using their corresponding wrapper
class methods <a id="I_indexterm11_id737860" class="indexterm"/><code class="literal">floatToIntBits()</code> and
<a id="I_indexterm11_id737871" class="indexterm"/><code class="literal">doubleTo</code><code class="literal">RawLongBits()</code> and pick out the (IEEE
standard) bits yourself.<a id="I_indexterm11_id737890" class="indexterm"/><a id="I_indexterm11_id737898" class="indexterm"/></p></div><div class="sect2" title="Random Numbers"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-11-SECT-1.3"/>Random Numbers</h2></div></div></div><p><a id="idx10631" class="indexterm"/> <a id="idx10662" class="indexterm"/> <a id="I_indexterm11_id737938" class="indexterm"/>You can use the <a id="I_indexterm11_id737945" class="indexterm"/><code class="literal">java.util.Random</code> class
to generate random values. It’s a pseudorandom-number generator that is
initialized with a 48-bit seed.<sup>[<a id="learnjava3-CHP-11-FN-1" href="#ftn.learnjava3-CHP-11-FN-1" class="footnote">31</a>]</sup> Because it’s a pseudorandom algorithm, you’ll get the same
series of values every time you use the same seed value. The default
constructor uses the current time to produce a seed, but you can specify
your own value in the constructor:</p><a id="I_11_tt686"/><pre class="programlisting"> <code class="kt">long</code> <code class="n">seed</code> <code class="o">=</code> <code class="n">mySeed</code><code class="o">;</code>
<code class="n">Random</code> <code class="n">rnums</code> <code class="o">=</code> <code class="k">new</code> <code class="n">Random</code><code class="o">(</code> <code class="n">seed</code> <code class="o">);</code></pre><p>After you have a generator, you can ask for one or more random
values of various types using the methods listed in <a class="xref" href="ch11s01.html#learnjava3-CHP-11-TABLE-3" title="Table 11-3. Random-number methods">Table 11-3</a>.</p><div class="table"><a id="learnjava3-CHP-11-TABLE-3"/><p class="title">Table 11-3. Random-number methods</p><div class="table-contents"><table summary="Random-number methods" style="border-collapse: collapse;border-top: 0.5pt solid ; border-bottom: 0.5pt solid ; "><colgroup><col/><col/></colgroup><thead><tr><th style="text-align: left"><p>Method</p></th><th style="text-align: left"><p>Range</p></th></tr></thead><tbody><tr><td style="text-align: left"><p> <a id="I_indexterm11_id738049" class="indexterm"/> <code class="literal">nextBoolean()</code>
</p></td><td style="text-align: left"><p> <code class="literal">true</code> or <code class="literal">false</code></p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id738082" class="indexterm"/> <code class="literal">nextInt()</code>
</p></td><td style="text-align: left"><p>–2147483648 to
2147483647</p></td></tr><tr><td style="text-align: left"><p> <code class="literal">nextInt(int</code>
<em class="replaceable"><code>n</code></em> <code class="literal">)</code> </p></td><td style="text-align: left"><p>0 to (n – 1) inclusive</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id738131" class="indexterm"/> <code class="literal">nextLong()</code>
</p></td><td style="text-align: left"><p>–9223372036854775808 to
9223372036854775807</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id738155" class="indexterm"/> <code class="literal">nextFloat()</code>
</p></td><td style="text-align: left"><p>0.0 inclusive to 1.0
exclusive</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id738179" class="indexterm"/> <code class="literal">nextDouble()</code>
</p></td><td style="text-align: left"><p>0.0 inclusive to 1.0
exclusive</p></td></tr><tr><td style="text-align: left"><p> <a id="I_indexterm11_id738203" class="indexterm"/> <code class="literal">nextGaussian()</code>
</p></td><td style="text-align: left"><p>Gaussian distributed double with mean
0.0 and standard deviation of 1.0</p></td></tr></tbody></table></div></div><p>By default, the values are uniformly distributed. You can use the
<code class="literal">nextGaussian()</code> method to create a
Gaussian (bell curve) distribution of <code class="literal">double</code> values, with a mean of 0.0 and a
standard deviation of 1.0. (Lots of natural phenomena follow a Gaussian
distribution rather than a strictly uniform random one.)</p><p>The <code class="literal">static</code> method <code class="literal">Math.random()</code> retrieves a random <code class="literal">double</code> value. This method initializes a
<code class="literal">private</code> random-number generator in
the <code class="literal">Math</code> class the first time it’s
used, using the default <code class="literal">Random</code>
constructor. Thus, every call to <code class="literal">Math.random()</code> corresponds to a call to
<code class="literal">nextDouble()</code> on that random-number
generator.<a id="I_indexterm11_id738283" class="indexterm"/><a id="I_indexterm11_id738290" class="indexterm"/></p></div><div class="footnotes"><br/><hr/><div class="footnote"><p><sup>[<a id="ftn.learnjava3-CHP-11-FN-1" href="#learnjava3-CHP-11-FN-1" class="para">31</a>] </sup><a id="I_indexterm11_id737962" class="indexterm"/><a id="I_indexterm11_id737967" class="indexterm"/>The generator uses a linear congruential formula. See
<span class="emphasis"><em>The Art of Computer Programming</em></span>,
<span class="emphasis"><em>Volume 2: Semi-numerical Algorithms</em></span> by Donald
Knuth (Addison-Wesley).</p></div></div></div></body></html>