我是靠谱客的博主 阔达胡萝卜,最近开发中收集的这篇文章主要介绍谁是代码界3%的王者?- 第四题BigDecimal问题简单解读,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、背景

阿里技术的公众发了一篇文章《谁是代码界3%的王者?》,

提到“在Java代码界,有些陷阱外表看起来是个青铜实际上是王者,据说97%工程师会被“秒杀””

给出了五道题,非常考验基础。

本文简单解读第4题,并分享通用的学习和研究方法。

 

二、题目

题目配套代码

public class BigDecimalTest {
    public static void main(String[] args) {
        BigDecimal a = new BigDecimal(0.1);
        System.out.println(a);
        BigDecimal b = new BigDecimal("0.1");
        System.out.println(b);
    }
}

题目内容

下列哪种说法是正确的:

A: 两种赋值的方式是一样的
B: 推荐a的赋值方式
C: 推荐b的赋值方式

先公布答案:C

三、分析

3.1 直接运行看效果

上面源代码输出的效果如下

0.1000000000000000055511151231257827021181583404541015625
0.1

显然b是我们想要的效果

 

3.2 源代码大法

java.math.BigDecimal#BigDecimal(java.lang.String)

  /**
     * Translates the string representation of a {@code BigDecimal}
     * into a {@code BigDecimal}.  The string representation consists
     * of an optional sign, {@code '+'} (<tt> '&#92;u002B'</tt>) or
     * {@code '-'} (<tt>'&#92;u002D'</tt>), followed by a sequence of
     * zero or more decimal digits ("the integer"), optionally
     * followed by a fraction, optionally followed by an exponent.
     *
     * <p>The fraction consists of a decimal point followed by zero
     * or more decimal digits.  The string must contain at least one
     * digit in either the integer or the fraction.  The number formed
     * by the sign, the integer and the fraction is referred to as the
     * <i>significand</i>.
     *
     * <p>The exponent consists of the character {@code 'e'}
     * (<tt>'&#92;u0065'</tt>) or {@code 'E'} (<tt>'&#92;u0045'</tt>)
     * followed by one or more decimal digits.  The value of the
     * exponent must lie between -{@link Integer#MAX_VALUE} ({@link
     * Integer#MIN_VALUE}+1) and {@link Integer#MAX_VALUE}, inclusive.
     *
     * <p>More formally, the strings this constructor accepts are
     * described by the following grammar:
     * <blockquote>
     * <dl>
     * <dt><i>BigDecimalString:</i>
     * <dd><i>Sign<sub>opt</sub> Significand Exponent<sub>opt</sub></i>
     * <dt><i>Sign:</i>
     * <dd>{@code +}
     * <dd>{@code -}
     * <dt><i>Significand:</i>
     * <dd><i>IntegerPart</i> {@code .} <i>FractionPart<sub>opt</sub></i>
     * <dd>{@code .} <i>FractionPart</i>
     * <dd><i>IntegerPart</i>
     * <dt><i>IntegerPart:</i>
     * <dd><i>Digits</i>
     * <dt><i>FractionPart:</i>
     * <dd><i>Digits</i>
     * <dt><i>Exponent:</i>
     * <dd><i>ExponentIndicator SignedInteger</i>
     * <dt><i>ExponentIndicator:</i>
     * <dd>{@code e}
     * <dd>{@code E}
     * <dt><i>SignedInteger:</i>
     * <dd><i>Sign<sub>opt</sub> Digits</i>
     * <dt><i>Digits:</i>
     * <dd><i>Digit</i>
     * <dd><i>Digits Digit</i>
     * <dt><i>Digit:</i>
     * <dd>any character for which {@link Character#isDigit}
     * returns {@code true}, including 0, 1, 2 ...
     * </dl>
     * </blockquote>
     *
     * <p>The scale of the returned {@code BigDecimal} will be the
     * number of digits in the fraction, or zero if the string
     * contains no decimal point, subject to adjustment for any
     * exponent; if the string contains an exponent, the exponent is
     * subtracted from the scale.  The value of the resulting scale
     * must lie between {@code Integer.MIN_VALUE} and
     * {@code Integer.MAX_VALUE}, inclusive.
     *
     * <p>The character-to-digit mapping is provided by {@link
     * java.lang.Character#digit} set to convert to radix 10.  The
     * String may not contain any extraneous characters (whitespace,
     * for example).
     *
     * <p><b>Examples:</b><br>
     * The value of the returned {@code BigDecimal} is equal to
     * <i>significand</i> &times; 10<sup>&nbsp;<i>exponent</i></sup>.
     * For each string on the left, the resulting representation
     * [{@code BigInteger}, {@code scale}] is shown on the right.
     * <pre>
     * "0"            [0,0]
     * "0.00"         [0,2]
     * "123"          [123,0]
     * "-123"         [-123,0]
     * "1.23E3"       [123,-1]
     * "1.23E+3"      [123,-1]
     * "12.3E+7"      [123,-6]
     * "12.0"         [120,1]
     * "12.3"         [123,1]
     * "0.00123"      [123,5]
     * "-1.23E-12"    [-123,14]
     * "1234.5E-4"    [12345,5]
     * "0E+7"         [0,-7]
     * "-0"           [0,0]
     * </pre>
     *
     * <p>Note: For values other than {@code float} and
     * {@code double} NaN and &plusmn;Infinity, this constructor is
     * compatible with the values returned by {@link Float#toString}
     * and {@link Double#toString}.  This is generally the preferred
     * way to convert a {@code float} or {@code double} into a
     * BigDecimal, as it doesn't suffer from the unpredictability of
     * the {@link #BigDecimal(double)} constructor.
     *
     * @param val String representation of {@code BigDecimal}.
     *
     * @throws NumberFormatException if {@code val} is not a valid
     *         representation of a {@code BigDecimal}.
     */
    public BigDecimal(String val) {
        this(val.toCharArray(), 0, val.length());
    }

人家都怕你不仔细看给了你那么多示例,还专门给了一个note

This is generally the preferred way to convert a {@code float} or {@code double} into a BigDecimal, as it doesn't suffer from the unpredictability of the {@link #BigDecimal(double)} constructor.

此构造函数是float或double转到BigDecimal的推荐方式,因为该构造方法不会像BigDecimal(double)一样会有一些不可预测的情况。

它最终调用了java.math.BigDecimal#BigDecimal(char[], int, int) 感兴趣大家可以自己去看。

 

我们再看另外一个构造函数

java.math.BigDecimal#BigDecimal(double)

 /**
     * Translates a {@code double} into a {@code BigDecimal} which
     * is the exact decimal representation of the {@code double}'s
     * binary floating-point value.  The scale of the returned
     * {@code BigDecimal} is the smallest value such that
     * <tt>(10<sup>scale</sup> &times; val)</tt> is an integer.
     * <p>
     * <b>Notes:</b>
     * <ol>
     * <li>
     * The results of this constructor can be somewhat unpredictable.
     * One might assume that writing {@code new BigDecimal(0.1)} in
     * Java creates a {@code BigDecimal} which is exactly equal to
     * 0.1 (an unscaled value of 1, with a scale of 1), but it is
     * actually equal to
     * 0.1000000000000000055511151231257827021181583404541015625.
     * This is because 0.1 cannot be represented exactly as a
     * {@code double} (or, for that matter, as a binary fraction of
     * any finite length).  Thus, the value that is being passed
     * <i>in</i> to the constructor is not exactly equal to 0.1,
     * appearances notwithstanding.
     *
     * <li>
     * The {@code String} constructor, on the other hand, is
     * perfectly predictable: writing {@code new BigDecimal("0.1")}
     * creates a {@code BigDecimal} which is <i>exactly</i> equal to
     * 0.1, as one would expect.  Therefore, it is generally
     * recommended that the {@linkplain #BigDecimal(String)
     * <tt>String</tt> constructor} be used in preference to this one.
     *
     * <li>
     * When a {@code double} must be used as a source for a
     * {@code BigDecimal}, note that this constructor provides an
     * exact conversion; it does not give the same result as
     * converting the {@code double} to a {@code String} using the
     * {@link Double#toString(double)} method and then using the
     * {@link #BigDecimal(String)} constructor.  To get that result,
     * use the {@code static} {@link #valueOf(double)} method.
     * </ol>
     *
     * @param val {@code double} value to be converted to
     *        {@code BigDecimal}.
     * @throws NumberFormatException if {@code val} is infinite or NaN.
     */
    public BigDecimal(double val) {
        this(val,MathContext.UNLIMITED);
    }

专门提到

new BigDecimal(0.1)的结果是0.1000000000000000055511151231257827021181583404541015625.
This is because 0.1 cannot be represented exactly as a {@code double} (or, for that matter, as a binary fraction of any finite length). 
Thus, the value that is being passed <i>in</i> to the constructor is not exactly equal to 0.1, appearances notwithstanding.

这是因为double类型无法精确表示0.1。因此传入0.1参数到该构造方法其实并不精确等于0.1。

The {@code String} constructor, on the other hand, is perfectly predictable: writing {@code new BigDecimal("0.1")} creates a {@code BigDecimal} which is <i>exactly</i> equal to  0.1, as one would expect.  Therefore, it is generally recommended that the {@linkplain #BigDecimal(String <tt>String</tt> constructor} be used in preference to this one.

更推荐使用参数为String的构造方法,换句话说用BigDecimal("0.1")来构造完全等于0.1的BigDecimal。

因此,推荐带String参数的构造方法。

When a {@code double} must be used as a source for a {@code BigDecimal}, note that this constructor provides an exact conversion; it does not give the same result asconverting the {@code double} to a {@code String} using the {@link Double#toString(double)} method and then using the {@link #BigDecimal(String)} constructor.

如果必须把double作为构造方法的参数时,注意和new BigDecimal(Double.toString(0.1d))的结果是完全不同的。

因此答案就不言而喻了。

四、其他

4.1 双精度问题

计算机通过二进制来存储数据,双精度8字节(64位)的表示

其中第63索引位,共1位,表示符号位(sign bit),用s表示;0表示正数,1表示负数

第52到62索引位,共11位,表示指数(signed exponent),用e表示;2的多少次方

第51到0索引位(significant/mantissa value),共52位,表示小数部分,用m表示;有效位

浮点型:https://docs.oracle.com/cd/E19957-01/806-3568/ncg_math.html

 

十进制无法表示三分之一,二进制无法表示十分之一。

像三分之一一样,三分之一无法用有限个十进制数表示。10的-1次幂(0.1)不是有限个2的幂的和,所以不能用有限个2进制位表示,而double是8字节的,只有64位,是有限个二进制数,因此无法精确表示0.1。

 

五、启发

正如前面的几个问题解答中我提到的几个常见方法一样,这类问题我们最好的办法是看源码!看源码的注释!!

看官方文档!!看权威规范!!(如本文提到的《IEEE Arithmetic》的网页)。

另外一个启发是计算机专业基础要扎实!!!二进制要理解的透彻一些。

开发的时候尽量多去源码里看注释!!!

 

 

附录

《谁是代码界3%的王者?- 第三题switch问题简单解读》

《谁是代码界3%的王者?- 第五题Lock的简单解读》

 

 

最后

以上就是阔达胡萝卜为你收集整理的谁是代码界3%的王者?- 第四题BigDecimal问题简单解读的全部内容,希望文章能够帮你解决谁是代码界3%的王者?- 第四题BigDecimal问题简单解读所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部