Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the problem of Java double Type addition

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "example Analysis of the addition of Java double types". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Java double type addition problem

When the numbers of multiple double types are added directly, there may be accuracy errors. (due to the computer algorithm and hardware environment, only 0 1 can be recognized. The default calculation results of the computer are all within a specified precision range. If you want to understand deeply, you can learn numerical analysis, etc.)

It is absolutely not allowed in finance, but fortunately, java developers have this foresight.

BigDecimal class is provided in java.math.* (provides a method of high-precision calculation)

First, at this time, the BigDecimal function will be used for operation.

The first step is to create data of type String

The second step is to create BigDecimal object BigDecimal (Double.toString (double))

The following two are not recommended:

BigDecimal (double) or BigDecimal (Double.valueOf (double)

Suggestion: when it comes to precision, the whole calculation process is to use String type or BigDecimal class objects. In the end, the result is turned around according to the demand.

In addition, this article provides a strange thing that occurs when calculating the addition of auxiliary classes Java Double.

If you need it, learn and create the tool class directly, and you can complete the project. Here are several ways to implement the addition algorithm.

New BigDecimal (Double.toString (double)) .add (new BigDecimal (Double.toString (double)); / * * @ param b1 * BigDecimal * @ param v2 * double * @ return BigDecimal * * / public BigDecimal add (BigDecimal b1, double v2) {/ / BigDecimal b1=new BigDecimal (Double.toString (v1)); BigDecimal b2 = new BigDecimal (Double.toString (v2)) Return b1.add (b2);} / * * @ param b1 * double * @ param v2 * double * @ return BigDecimal * * / public BigDecimal add (double v1, double v2) {BigDecimal b1=new BigDecimal (Double.toString (v1)); BigDecimal b2 = new BigDecimal (Double.toString (v2)); return b1.add (b2) } / * * @ param b1 * double * @ param v2 * double * @ return double * * / public double add (double v1, double v2) {BigDecimal b1=new BigDecimal (Double.toString (v1)); BigDecimal b2 = new BigDecimal (Double.toString (v2)); return b1.add (b2). DoubleValue ();} comparison of two and double additions

+, strictfp,BigDecimel

Strictfp-- Java keyword.

Can be applied to a class, interface, or method.

When you declare a method using the strictfp keyword, all float and double expressions in the method strictly follow the restrictions of FP-strict and conform to the IEEE-754 specification.

Strict constraints mean that the results of all expressions must be what the IEEE 754 algorithm expects for operands, expressed in single-precision and double-precision formats.

Public class MathDemo {/ * @ param args * / public static void main (String [] args) {System.err.println ("ordinary" + addNormal (12353.21)); System.err.println ("strictfp" + addDouble (12353.21)); System.err.println ("BigDEcimel:" + add (12353.21));} public static double addNormal (12353.21) V1) {double res = 0; for (int I = 0; I

< v1.length; i++) { res += v1[i]; } return res; } public static strictfp double addDouble(double... v) { double res = 0; for (int i = 0; i < v.length; i++) { res += v[i]; } return res; } /** * @param b1 * double * @param v2 * double * @return double */ public static double add(double... v) { BigDecimal b = new BigDecimal(Double.toString(v[0])); for (int i = 1; i < v.length; i++) { BigDecimal b2 = new BigDecimal(Double.toString(v[i])); b=b.add(b2); } return b.doubleValue(); }} 输入 12353.21,21334.24,154435.03三个类型的数据时候 结果: 普通 188122.47999999998 strictfp 188122.47999999998 BigDEcimel: 188122.48 输入 3.21, 4.24,5.03 结果 普通 12.48 strictfp 12.48 BigDEcimel: 12.48 输入: 12353.21,21334.24 结果: 普通 33687.45 strictfp 33687.45 BigDEcimel: 33687.45 结论是: BigDecimal的算法精度比较好。 其余两种方法 都存在缺点。至于strictfp 这个关键字 是去平台化影响。比如32为机器和64位机器结果都一样。 对于精度计算结果影响不大。 附录:. //使用double类型创建BigDecimal public BigDecimal(doubleval) { if (Double.isInfinite(val) || Double.isNaN(val)) throw new NumberFormatException("Infinite or NaN"); // Translate the double into sign, exponent and significand, according // to the formulae in JLS, Section 20.10.22. long valBits = Double.doubleToLongBits(val); int sign = ((valBits >

> 63) = 0? 1:-1); int exponent= (int) ((valBits > > 52) & 0x7ffL); long significand = (exponent==0? (valBits & (1L)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report