算术运算
目前创新互联公司已为1000+的企业提供了网站建设、域名、网站空间、网站托管维护、企业网站设计、景宁畲族自治网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。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
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:BigDecimal类的用法-创新互联
文章分享:https://www.cdcxhl.com/article30/ddcopo.html
成都网站建设公司_创新互联,为您提供网站导航、搜索引擎优化、网站策划、用户体验、企业网站制作、外贸建站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联