算術(shù)運算
目前創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計、景寧畬族自治網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。BigDecimalpowabsnegatepow
BigDecimalpublic
1, public BigDecimal add(BigDecimal augend)
BigDecimal:
result.intValue/intCompact = this.intValue/intCompact + augend. intValue/intCompact result.scale = max(this.scale, augend.scale) result.precision = 0
2, public BigDecimal add(BigDecimal augend, MathContext mc)
MathContextRounding
BigDecimalpublicnegate
public BigDecimal subtract(BigDecimal subtrahend, MathContext mc) { if (mc.precision == 0) return subtract(subtrahend); // share the special rounding code in add() return add(subtrahend.negate(), mc); }
result.intValue/intCompact = this.intValue/intCompact * multiplicand. intValue/intCompact result.scale = sum(this.scale, multiplicand.scale) result.precision = 0
mutiplymultiplyAndRound
result.intValue/intCompact = this.intValue/intCompact divisor. intValue/intCompact
result.scale = this.scale - divisor.scale
BigDecimal5public
1, public BigDecimal divide(BigDecimal divisor)
MathContext mc = new MathContext( (int)Math.min(this.precision() + (long)Math.ceil(10.0*divisor.precision()/3.0), Integer.MAX_VALUE), RoundingMode.UNNECESSARY); BigDecimal quotient; try { quotient = this.divide(divisor, mc); } catch (ArithmeticException e) { throw new ArithmeticException("Non-terminating decimal expansion; " + "no exact representable decimal result."); }
MathContextMathContextprecision
Math.min(this.precision() + (long)Math.ceil(10.0*divisor.precision()/3.0),Integer.MAX_VALUE)
RoundingModeUNNECESSARY
precisionprecisionRoundingModeRounding
2, public BigDecimal divide(BigDecimal divisor, MathContext mc)
longBigIntegerdivideAndRound
private static BigDecimal divideAndRound(long ldividend, long ldivisor, int scale, int roundingMode, int preferredScale)
longquotientremainder
long q = ldividend / ldivisor; long r = ldividend % ldivisor;
qsign = ((ldividend < 0) == (ldivisor < 0)) ? 1 : -1;
remainder0rounding
if (r != 0) { boolean increment = needIncrement(ldivisor, roundingMode, qsign, q, r); return valueOf((increment ? q + qsign : q), scale); }
remainder0scale
private static BigDecimal divideAndRound(BigInteger bdividend, BigInteger bdivisor, int scale, int roundingMode, int preferredScale)
BigIntegerlongBigIntegerlong
quotientremainderlongBigIntegerMutableBigIntegerdivide
MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag); MutableBigInteger mq = new MutableBigInteger(); MutableBigInteger mdivisor = new MutableBigInteger(bdivisor.mag); MutableBigInteger mr = mdividend.divide(mdivisor, mq);
0
qsign = (bdividend.signum != bdivisor.signum) ? -1 : 1;
longBigInteger
public
1, public BigDecimal pow(int n)
unscaled valuescaleBigDecimal
unscaled valueBigIntegerpowscalethis.scale *n
if (n < 0 || n > 999999999) throw new ArithmeticException("Invalid operation"); // No need to calculate pow(n) if result will over/underflow. // Don't attempt to support "supernormal" numbers. int newScale = checkScale((long)scale * n); return new BigDecimal(this.inflated().pow(n), newScale);
n
2, public BigDecimal pow(int n, MathContext mc)
BigDecimalroundingX3.274-1996
int mag = Math.abs(n); // ready to carry out power calculation... BigDecimal acc = ONE; // accumulator boolean seenbit = false; // set once we've seen a 1-bit for (int i=1;;i++) { // for each bit [top bit ignored] mag += mag; // shift left 1 bit if (mag < 0) { // top bit is set seenbit = true; // OK, we're off acc = acc.multiply(lhs, workmc); // acc=acc*x } if (i == 31) break; // that was the last bit if (seenbit) acc=acc.multiply(acc, workmc); // acc=acc*acc [square] // else (!seenbit) no point in squaring ONE } // if negative n, calculate the reciprocal using working precision if (n < 0) // [hence mc.precision>0] acc=ONE.divide(acc, workmc);
mag += magn
if(mag <0) 1BigDecimalseenbit
1 = 2^0*1i=31
n<01
125
1, 5101000…i=29
2, mag <0 => seenbit = true, acc = 1*12 = 12(12^1)
3, seenbit = true => acc = 12 * 12 = 144(12^2)
4, i= 30, mag 01000…
5, mag>0 => seenbittrue
6, seenbit =true => acc = acc * acc = 144* 144(12^4)
7, i=31mag1000…
8, mag<0=>seenbit = true,acc = acc * 12 = 144*144*12(12^5)
9, i = 31 =>
doRound
setScaleBigDecimalBigDecimalintVal, intCompact,precision,scalescaleunscaled value
BigDecimal
public BigDecimal setScale(int newScale, int roundingMode)
unscaled value
1, intCompactunscaled value
scaleLong.MAX_VALUEdivideAndRound
2, intValunscaled value
intCompactBigIntegerlong
compareToBigDecimal
if (scale == val.scale) { long xs = intCompact; long ys = val.intCompact; if (xs != INFLATED && ys != INFLATED) return xs != ys ? ((xs > ys) ? 1 : -1) : 0; }
BigDecimalunscaled valuescalescaleunscaled valueintCompactlong
int xsign = this.signum(); int ysign = val.signum(); if (xsign != ysign) return (xsign > ysign) ? 1 : -1;
if (xsign == 0) return 0;
00
int cmp = compareMagnitude(val);
compareMagnitude
long xae = (long)this.precision() - this.scale; // [-1] long yae = (long)val.precision() - val.scale; // [-1] if (xae < yae) return -1; if (xae > yae) return 1;
precision – scale
sdiffscalescaleBigIntegercompareMagnitude
for (int i = 0; i < len1; i++) { int a = m1[i]; int b = m2[i]; if (a != b) return ((a & LONG_MASK) < (b & LONG_MASK)) ? -1 : 1; }
intLONG_MASK0.
return (xsign > 0) ? cmp : -cmp;
equalsArrayListcontainsBigDecimalequals
if (scale != xDec.scale) return false;
BigDecimalscaleBigDecimalBigDecimal(“0”)BigDecimal(“0.0”)
BigDecimal a = new BigDecimal("0.0"); BigDecimal b = BigDecimal.ZERO; System.out.print(a.equals(b));//false
compareToequals
BigDecimalBigDecimal
BigDecimalunscaled valuescaleBigDecimalBigIntegerint
– doubleDoubletoString
BigIntegerLongunscaled valuescalescaleMathContextroundingMathContextX3.274-1996
doRoundsetScalecompareToequalsdoRoundsetScalecompareToequalsequalscompareTo0
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文題目:BigDecimal類的用法-創(chuàng)新互聯(lián)
文章分享:http://www.rwnh.cn/article30/ddcopo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化、網(wǎng)站策劃、用戶體驗、企業(yè)網(wǎng)站制作、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)