mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
4511638: Double.toString(double) sometimes produces incorrect results
Reviewed-by: aturbanov, darcy, bpb
This commit is contained in:
parent
2f19144249
commit
72bcf2aa03
18 changed files with 4075 additions and 120 deletions
|
@ -32,6 +32,7 @@ import java.util.Optional;
|
|||
|
||||
import jdk.internal.math.FloatingDecimal;
|
||||
import jdk.internal.math.DoubleConsts;
|
||||
import jdk.internal.math.DoubleToDecimal;
|
||||
import jdk.internal.vm.annotation.IntrinsicCandidate;
|
||||
|
||||
/**
|
||||
|
@ -280,39 +281,109 @@ public final class Double extends Number
|
|||
* {@code "-0.0"} and positive zero produces the result
|
||||
* {@code "0.0"}.
|
||||
*
|
||||
* <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
|
||||
* than 10<sup>7</sup>, then it is represented as the integer part of
|
||||
* <i>m</i>, in decimal form with no leading zeroes, followed by
|
||||
* '{@code .}' ({@code '\u005Cu002E'}), followed by one or
|
||||
* more decimal digits representing the fractional part of <i>m</i>.
|
||||
* <li> Otherwise <i>m</i> is positive and finite.
|
||||
* It is converted to a string in two stages:
|
||||
* <ul>
|
||||
* <li> <em>Selection of a decimal</em>:
|
||||
* A well-defined decimal <i>d</i><sub><i>m</i></sub>
|
||||
* is selected to represent <i>m</i>.
|
||||
* This decimal is (almost always) the <em>shortest</em> one that
|
||||
* rounds to <i>m</i> according to the round to nearest
|
||||
* rounding policy of IEEE 754 floating-point arithmetic.
|
||||
* <li> <em>Formatting as a string</em>:
|
||||
* The decimal <i>d</i><sub><i>m</i></sub> is formatted as a string,
|
||||
* either in plain or in computerized scientific notation,
|
||||
* depending on its value.
|
||||
* </ul>
|
||||
* </ul>
|
||||
* </ul>
|
||||
*
|
||||
* <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
|
||||
* equal to 10<sup>7</sup>, then it is represented in so-called
|
||||
* "computerized scientific notation." Let <i>n</i> be the unique
|
||||
* integer such that 10<sup><i>n</i></sup> ≤ <i>m</i> {@literal <}
|
||||
* 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
|
||||
* mathematically exact quotient of <i>m</i> and
|
||||
* 10<sup><i>n</i></sup> so that 1 ≤ <i>a</i> {@literal <} 10. The
|
||||
* magnitude is then represented as the integer part of <i>a</i>,
|
||||
* as a single decimal digit, followed by '{@code .}'
|
||||
* ({@code '\u005Cu002E'}), followed by decimal digits
|
||||
* representing the fractional part of <i>a</i>, followed by the
|
||||
* letter '{@code E}' ({@code '\u005Cu0045'}), followed
|
||||
* by a representation of <i>n</i> as a decimal integer, as
|
||||
* produced by the method {@link Integer#toString(int)}.
|
||||
* <p>A <em>decimal</em> is a number of the form
|
||||
* <i>s</i>×10<sup><i>i</i></sup>
|
||||
* for some (unique) integers <i>s</i> > 0 and <i>i</i> such that
|
||||
* <i>s</i> is not a multiple of 10.
|
||||
* These integers are the <em>significand</em> and
|
||||
* the <em>exponent</em>, respectively, of the decimal.
|
||||
* The <em>length</em> of the decimal is the (unique)
|
||||
* positive integer <i>n</i> meeting
|
||||
* 10<sup><i>n</i>-1</sup> ≤ <i>s</i> < 10<sup><i>n</i></sup>.
|
||||
*
|
||||
* <p>The decimal <i>d</i><sub><i>m</i></sub> for a finite positive <i>m</i>
|
||||
* is defined as follows:
|
||||
* <ul>
|
||||
* <li>Let <i>R</i> be the set of all decimals that round to <i>m</i>
|
||||
* according to the usual <em>round to nearest</em> rounding policy of
|
||||
* IEEE 754 floating-point arithmetic.
|
||||
* <li>Let <i>p</i> be the minimal length over all decimals in <i>R</i>.
|
||||
* <li>When <i>p</i> ≥ 2, let <i>T</i> be the set of all decimals
|
||||
* in <i>R</i> with length <i>p</i>.
|
||||
* Otherwise, let <i>T</i> be the set of all decimals
|
||||
* in <i>R</i> with length 1 or 2.
|
||||
* <li>Define <i>d</i><sub><i>m</i></sub> as the decimal in <i>T</i>
|
||||
* that is closest to <i>m</i>.
|
||||
* Or if there are two such decimals in <i>T</i>,
|
||||
* select the one with the even significand.
|
||||
* </ul>
|
||||
*
|
||||
* <p>The (uniquely) selected decimal <i>d</i><sub><i>m</i></sub>
|
||||
* is then formatted.
|
||||
* Let <i>s</i>, <i>i</i> and <i>n</i> be the significand, exponent and
|
||||
* length of <i>d</i><sub><i>m</i></sub>, respectively.
|
||||
* Further, let <i>e</i> = <i>n</i> + <i>i</i> - 1 and let
|
||||
* <i>s</i><sub>1</sub>…<i>s</i><sub><i>n</i></sub>
|
||||
* be the usual decimal expansion of <i>s</i>.
|
||||
* Note that <i>s</i><sub>1</sub> ≠ 0
|
||||
* and <i>s</i><sub><i>n</i></sub> ≠ 0.
|
||||
* Below, the decimal point {@code '.'} is {@code '\u005Cu002E'}
|
||||
* and the exponent indicator {@code 'E'} is {@code '\u005Cu0045'}.
|
||||
* <ul>
|
||||
* <li>Case -3 ≤ <i>e</i> < 0:
|
||||
* <i>d</i><sub><i>m</i></sub> is formatted as
|
||||
* <code>0.0</code>…<code>0</code><!--
|
||||
* --><i>s</i><sub>1</sub>…<i>s</i><sub><i>n</i></sub>,
|
||||
* where there are exactly -(<i>n</i> + <i>i</i>) zeroes between
|
||||
* the decimal point and <i>s</i><sub>1</sub>.
|
||||
* For example, 123 × 10<sup>-4</sup> is formatted as
|
||||
* {@code 0.0123}.
|
||||
* <li>Case 0 ≤ <i>e</i> < 7:
|
||||
* <ul>
|
||||
* <li>Subcase <i>i</i> ≥ 0:
|
||||
* <i>d</i><sub><i>m</i></sub> is formatted as
|
||||
* <i>s</i><sub>1</sub>…<i>s</i><sub><i>n</i></sub><!--
|
||||
* --><code>0</code>…<code>0.0</code>,
|
||||
* where there are exactly <i>i</i> zeroes
|
||||
* between <i>s</i><sub><i>n</i></sub> and the decimal point.
|
||||
* For example, 123 × 10<sup>2</sup> is formatted as
|
||||
* {@code 12300.0}.
|
||||
* <li>Subcase <i>i</i> < 0:
|
||||
* <i>d</i><sub><i>m</i></sub> is formatted as
|
||||
* <i>s</i><sub>1</sub>…<!--
|
||||
* --><i>s</i><sub><i>n</i>+<i>i</i></sub><code>.</code><!--
|
||||
* --><i>s</i><sub><i>n</i>+<i>i</i>+1</sub>…<!--
|
||||
* --><i>s</i><sub><i>n</i></sub>,
|
||||
* where there are exactly -<i>i</i> digits to the right of
|
||||
* the decimal point.
|
||||
* For example, 123 × 10<sup>-1</sup> is formatted as
|
||||
* {@code 12.3}.
|
||||
* </ul>
|
||||
* <li>Case <i>e</i> < -3 or <i>e</i> ≥ 7:
|
||||
* computerized scientific notation is used to format
|
||||
* <i>d</i><sub><i>m</i></sub>.
|
||||
* Here <i>e</i> is formatted as by {@link Integer#toString(int)}.
|
||||
* <ul>
|
||||
* <li>Subcase <i>n</i> = 1:
|
||||
* <i>d</i><sub><i>m</i></sub> is formatted as
|
||||
* <i>s</i><sub>1</sub><code>.0E</code><i>e</i>.
|
||||
* For example, 1 × 10<sup>23</sup> is formatted as
|
||||
* {@code 1.0E23}.
|
||||
* <li>Subcase <i>n</i> > 1:
|
||||
* <i>d</i><sub><i>m</i></sub> is formatted as
|
||||
* <i>s</i><sub>1</sub><code>.</code><i>s</i><sub>2</sub><!--
|
||||
* -->…<i>s</i><sub><i>n</i></sub><code>E</code><i>e</i>.
|
||||
* For example, 123 × 10<sup>-21</sup> is formatted as
|
||||
* {@code 1.23E-19}.
|
||||
* </ul>
|
||||
* </ul>
|
||||
* How many digits must be printed for the fractional part of
|
||||
* <i>m</i> or <i>a</i>? There must be at least one digit to represent
|
||||
* the fractional part, and beyond that as many, but only as many, more
|
||||
* digits as are needed to uniquely distinguish the argument value from
|
||||
* adjacent values of type {@code double}. That is, suppose that
|
||||
* <i>x</i> is the exact mathematical value represented by the decimal
|
||||
* representation produced by this method for a finite nonzero argument
|
||||
* <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
|
||||
* to <i>x</i>; or if two {@code double} values are equally close
|
||||
* to <i>x</i>, then <i>d</i> must be one of them and the least
|
||||
* significant bit of the significand of <i>d</i> must be {@code 0}.
|
||||
*
|
||||
* <p>To create localized string representations of a floating-point
|
||||
* value, use subclasses of {@link java.text.NumberFormat}.
|
||||
|
@ -321,7 +392,7 @@ public final class Double extends Number
|
|||
* @return a string representation of the argument.
|
||||
*/
|
||||
public static String toString(double d) {
|
||||
return FloatingDecimal.toJavaFormatString(d);
|
||||
return DoubleToDecimal.toString(d);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue