Precise floating-point operations in JAVA __java

Source: Internet
Author: User

Floating-point operations in Java are represented by the nearest number of values that they can represent for many value floating-point numbers, which may lead to imperceptible errors in the calculation.

As the following example:

public class SSSs {public

	static void Main (string[] ages) {
		double d1=2.07;
		Double d2=1.03;
		System.out.println (D1+D2);
	}


Results:

Although the calculation result is very small, the error is not accurate. This is unacceptable in areas like financial computing where precision is required, but this is the problem with binary itself, and computers generally use binary representations that cannot be solved using basic data types.

In order to solve the problem that the basic data type floating-point number cannot be calculated accurately, the Java.math.BigDecimal class is specially provided in Java, which provides the accurate calculation function of floating-point number. Same as the BigInteger class, whose operations are performed using method calls

Demo

public class SSSs {public

	static void Main (string[] ages) {
		BigDecimal b1=new BigDecimal ("2.07");
		BigDecimal b2=new BigDecimal ("1.03");
		System.out.println ("Add Operation:");
		System.out.println ("B1:" +B1);
		System.out.println ("B2:" +b2);
		System.out.println ("Add Value:" +b1.add (B2));
		
	}


Output value:

The rest of the function parameters create the following table

BigDecimal ABS ()
Returns BigDecimal with the value of the BigDecimal, whose scale is This.scale ().
BigDecimal ABS (Mathcontext MC)
Returns the BigDecimal for this BigDecimal absolute value (rounded according to the context setting).
BigDecimal Add (BigDecimal augend)
Returns a BigDecimal with a value of (this + augend) with a scale of Max (This.scale (), Augend.scale ()).
BigDecimal Add (BigDecimal augend, Mathcontext MC)
Returns the BigDecimal whose value is (this + augend) (rounded according to the context setting).
Byte Bytevalueexact ()
Convert this BigDecimal to byte to check for missing information.
int CompareTo (BigDecimal val)
Compares this BigDecimal to the specified BigDecimal.
BigDecimal Divide (BigDecimal divisor)
Returns a BigDecimal with a value of (this/divisor) whose preferred scale is (This.scale ()-Divisor.scale ()), or if the exact quotient value cannot be represented (because it has an infinite decimal extension) ArithmeticException.
BigDecimal Divide (BigDecimal divisor, int roundingmode)
Returns a BigDecimal with a value of (this/divisor) with a scale of This.scale ().
BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)
Returns a BigDecimal with a value of (this/divisor) whose scale is the specified scale.
BigDecimal Divide (BigDecimal divisor, int scale, Roundingmode roundingmode)
Returns a BigDecimal with a value of (this/divisor) whose scale is the specified scale.
BigDecimal Divide (BigDecimal divisor, Mathcontext MC)
Returns the BigDecimal whose value is (this/divisor) (rounded according to the context setting).
BigDecimal Divide (BigDecimal divisor, Roundingmode roundingmode)
Returns a BigDecimal with a value of (this/divisor) with a scale of This.scale ().
Bigdecimal[] Divideandremainder (BigDecimal divisor)
Returns a two-element BigDecimal array that contains the results of the dividetointegralvalue followed by the remainder of the two operand calculations.
Bigdecimal[] Divideandremainder (BigDecimal divisor, Mathcontext MC)
Returns a two-element BigDecimal array that contains the results of the dividetointegralvalue, followed by the remainder result of rounding the two operands based on the context setting.
BigDecimal Dividetointegralvalue (BigDecimal divisor)
Returns BigDecimal, whose value is the integer portion of the quotient value (this/divisor) that is rounded down.
BigDecimal Dividetointegralvalue (BigDecimal divisor, Mathcontext MC)
Returns the BigDecimal, whose value is the integer portion of (this/divisor).
Double Doublevalue ()
Converts this BigDecimal to double.
Boolean Equals (Object x)
Compares the equality of this BigDecimal with the specified Object.
Float Floatvalue ()
Converts this BigDecimal to float.
int Hashcode ()
Returns the hash code for this BigDecimal.
int Intvalue ()
Converts this BigDecimal to an int.
int Intvalueexact ()
Convert this BigDecimal to int to check for missing information.
Long Longvalue ()
Converts this BigDecimal to long.
Long Longvalueexact ()
Converts this BigDecimal to long to check for missing information.
BigDecimal Max (BigDecimal val)
Returns the maximum value for this BigDecimal and Val.
BigDecimal min (BigDecimal val)
Returns the minimum value for this BigDecimal and Val.
BigDecimal movepointleft (int n)
Returns a BigDecimal, which is equivalent to moving the decimal point of the value to the left n bit.
BigDecimal movepointright (int n)
Returns a BigDecimal, which is equivalent to moving the decimal point of the value to the right n bit.
BigDecimal Multiply (BigDecimal multiplicand)
Returns a BigDecimal with a value of (Thisxmultiplicand) with a scale of (This.scale () + Multiplicand.scale ()).
BigDecimal Multiply (BigDecimal multiplicand, Mathcontext MC)
Returns the BigDecimal whose value is (Thisxmultiplicand) (rounded according to the context setting).
BigDecimal negate ()
Returns BigDecimal with a value of (-this) with a scale of This.scale ().
BigDecimal negate (Mathcontext MC)
Returns the BigDecimal whose value is (-this) (rounded according to the context setting).
BigDecimal Plus ()
Returns BigDecimal with a value of (+this) with a scale of This.scale ().
BigDecimal Plus (Mathcontext MC)
Returns the BigDecimal whose value is (+this) (rounded according to the context setting).
BigDecimal pow (int n)
Returns the BigDecimal whose value is (THISN) and accurately calculates the power so that it has infinite precision.
BigDecimal Pow (int n, Mathcontext MC)
Returns the BigDecimal whose value is (THISN).
int precision ()
Returns the precision of this BigDecimal.
BigDecimal remainder (BigDecimal divisor)
Returns the BigDecimal whose value is (this% divisor).
BigDecimal remainder (BigDecimal divisor, Mathcontext MC)
Returns the BigDecimal whose value is (this% divisor) (rounded according to the context setting).
BigDecimal round (Mathcontext MC)
Returns the BigDecimal after rounding based on the Mathcontext setting.
int scale ()
Returns the scale of this BigDecimal.
BigDecimal Scalebypoweroften (int n)
Returns the BigDecimal whose value is equal to (this * 10n).
BigDecimal setscale (int newscale)
Returns a BigDecimal whose scale is the specified value, and its value is the value of this BigDecimal.
BigDecimal setscale (int newscale, int roundingmode)
Returns a BigDecimal whose scale is a specified value, and its nonstandard value is determined by multiplying or dividing the BigDecimal value of the scale by the appropriate power of 10 to maintain its total value.
BigDecimal setscale (int newscale, Roundingmode roundingmode)
Returns BigDecimal, whose scale is the specified value, and its nonstandard value is determined by multiplying or dividing the BigDecimal value of the scale by the appropriate power of 10 to maintain its total value.
Short Shortvalueexact ()
Convert this BigDecimal to short to check for missing information.
int Signum ()
Returns the positive and negative function of this BigDecimal.
BigDecimal Striptrailingzeros ()
Returns a value that is superior to this decimal, but removes all trailing zeros from the BigDecimal.
BigDecimal Subtract (BigDecimal subtrahend)
Returns a BigDecimal with a value of (this-subtrahend) with a scale of Max (This.scale (), Subtrahend.scale ()).
BigDecimal Subtract (BigDecimal subtrahend, Mathcontext MC)
Returns the BigDecimal whose value is (this-subtrahend) (rounded according to the context setting).
BigInteger Tobiginteger ()
Converts this BigDecimal to BigInteger.
BigInteger Tobigintegerexact ()
Convert this BigDecimal to BigInteger to check for missing information.
String toengineeringstring ()
Returns the string representation of this BigDecimal, when an exponent is required, the engineering count method is used.
String toplainstring ()
Returns a string representation of this BigDecimal without referring to a numeric segment.
String toString ()
Returns the string representation of this BigDecimal, using scientific notation if an exponent is required.
BigDecimal ULP ()
Returns the size of the ULP (last unit) of this BigDecimal.
BigInteger Unscaledvalue ()
Returns the BigInteger of its value for the BigDecimal value for this index.
Static BigDecimal valueof (double val)
Converts a double to BigDecimal by using the string representation of the double specification provided by the double.tostring (double) method.
Static BigDecimal valueof (long Val)
Converts a Long value to a BigDecimal with a 0 scale.
Static BigDecimal valueof (long unscaledval, int scale)
Converts a long non-scale value and an int scale to BigDecimal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.