วันอาทิตย์ที่ 31 สิงหาคม พ.ศ. 2557

Java java.math BigDecimal pow(int n, MathContext mc)

Description Of BigDecimal: pow(int n, MathContext mc)

The java.math.BigDecimal.pow(int n, MathContext mc) returns a BigDecimal whose value is (thisn ) . The current implementation uses the core algorithm defined in ANSI standard X3.274-1996 with rounding according to the context settings. In general, the returned numerical value is within two ulps of the exact numerical value for the chosen precision. Note that future releases may use a different algorithm with a decreased allowable error bound and increased allowable exponent range.

The X3.274-1996 algorithm is:

-An ArithmeticException exception is thrown if
--abs(n) > 999999999
--mc.precision == 0 and n < 0
--mc.precision > 0 and n has more than mc.precision decimal digits
-if n is zero, ONE is returned even if this is zero, otherwise
--if n is positive, the result is calculated via the repeated squaring technique into a single accumulator. The individual multiplications with the accumulator use the same math context settings as in mc except for a precision increased to mc.precision + elength + 1 where elength is the number of decimal digits in n .
--if n is negative, the result is calculated as if n were positive; this value is then divided into one using the working precision specified above.
--The final value from either the positive or negative case is then rounded to the destination precision.

BigDecimal.pow(int n, MathContext mc) method returns this n using the ANSI standard X3.274-1996 algorithm

Code Example Java BigDecimal

import java.math.BigDecimal;
import java.math.MathContext;

public class BigDecimalExam {
 public static void main(String[] args) {
  BigDecimal bg1 = new BigDecimal("2.242");

  MathContext mc = new MathContext(4); // 4 precision

  BigDecimal bg2 = bg1.pow(2, mc);

  System.out.println(bg2);
 }
}


yengo หรือ buzzcity

ไม่มีความคิดเห็น :

แสดงความคิดเห็น