十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
算術(shù)運(yùn)算
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元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。