简体   繁体   English

BigDecimal.toDouble 如何工作?

[英]How does BigDecimal.toDouble Work?

I am using BigDecimal and doubles in my math program.我在我的数学程序中使用 BigDecimal 和 doubles。 What decimal places does this method take?这个方法取几位小数? Is it fast?速度快吗? Is this recommended over just using BigDecimal?是否建议仅使用 BigDecimal?

Q: How does BigDecimal.toDouble work?问:BigDecimal.toDouble 如何工作?

Multiplications and additions.乘法和加法。 Take a look at the source code .看看源代码 (You can find it by googling for "java.math.BigDecimal source".) (您可以通过谷歌搜索“java.math.BigDecimal 源代码”找到它。)

Q: What decimal places does this method take?问:此方法取小数点后几位?

Here is what the javadoc states:这是 javadoc 中的说明:

"Converts this BigDecimal to a double. This conversion is similar to the narrowing primitive conversion from double to float as defined in section 5.1.3 of The Java™ Language Specification: if this BigDecimal has too great a magnitude represent as a double, it will be converted to Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY as appropriate." "将此 BigDecimal 转换为双精度数。此转换类似于 Java™ 语言规范的第 5.1.3 节中定义的从双精度数到浮点数的缩小原始转换:如果此 BigDecimal 具有太大的量级表示为双精度数,它将根据需要转换为 Double.NEGATIVE_INFINITY 或 Double.POSITIVE_INFINITY。”

5.1.3 of the JLS states: JLS 的 5.1.3 规定:

"A narrowing primitive conversion from double to float is governed by the IEEE 754 rounding rules (§4.2.4). This conversion can lose precision, but also lose range, resulting in a float zero from a nonzero double and a float infinity from a finite double. A double NaN is converted to a float NaN and a double infinity is converted to the same-signed float infinity." “从 double 到 float 的缩小原始转换受 IEEE 754 舍入规则(第 4.2.4 节)的约束。这种转换可能会失去精度,但也会失去范围,导致非零双精度浮点数为零和浮点数无穷大有限双。双 NaN 转换为浮点 NaN,双无穷大转换为同号浮点无穷大。”

And 4.2.4 states: 4.2.4 规定:

"The Java programming language requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision. Inexact results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, the one with its least significant bit zero is chosen. This is the IEEE 754 standard's default rounding mode known as round to nearest." “Java 编程语言要求浮点算术的行为就像每个浮点运算符都将其浮点结果四舍五入到结果精度。不精确的结果必须四舍五入到最接近无限精确结果的可表示值;如果两个最接近的可表示的值同样接近,则选择最低有效位为零的值。这是 IEEE 754 标准的默认舍入模式,称为舍入到最近。”


So what does this mean?那么这是什么意思?

  1. If the number is representable, the BigDecimal.doubleValue() will give the double value that is closest to the Real number represented by the object.如果数字是可表示的, BigDecimal.doubleValue()将给出最接近对象所表示的数的double BigDecimal.doubleValue()值。

  2. If the number is not representable (ie it is too large or too small to be represented as a double ) then the method will give +INF or -INF.如果数字不可表示(即它太大或太小而无法表示为double ),则该方法将给出 +INF 或 -INF。


Q: Is it fast?问:速度快吗?

It is typically roughly as fast as converting a string representation of the number to a double .它通常与将数字的字符串表示形式转换为double大致一样快。 (Indeed, in some cases the conversion is actually done by converting the value to a string and then parsing it!) (确实,在某些情况下,转换实际上是通过将值转换为字符串然后解析它来完成的!)

Fast is a relative term.快速是一个相对的术语。

Q: Is this recommended over just using BigDecimal?问:是否建议只使用 BigDecimal?

Recommended for what purpose?推荐用于什么目的?

If you convert BigDecimal values to double values, then operations on the double values will be orders of magnitude faster than the analogous BigDecimal operations.如果将BigDecimal值转换为double值,则对double值的操作将比类似的BigDecimal操作快BigDecimal数量级。 But the results won't be as accurate.但结果不会那么准确。 (Assuming that you specify the BigDecimal precision appropriately.) (假设您适当地指定了BigDecimal精度。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM