Title Description
The integer exponent of the floating-point base and int type given a double type. The exponent of the base of the second party.
/*** Consider the situation: The index may be positive, negative, 0. * Base is 0, exponent is 0 or negative, throw exception handling *@paramBase Radix *@paramExponent Index *@return */ Public Static DoublePowerDoubleBaseintexponent) { //The index and the base cannot be 0 at the same time, mathematically meaningless . if(Equal (base, 0.0) && exponent <= 0) Throw NewRuntimeException ("Invalid iuput. Base and exponent both is zero "); //index is 0, return 1 if(Exponent = = 0) return1.0; intExp =exponent; if(Exponent < 0) Exp= -exp; //seeking power to the square Doubleresult =powerwithunsignedexponent (base, exp); //The exponent is negative and the countdown is if(Exponent < 0) {result= 1.0/result; } returnresult; } Private Static DoublePowerwithunsignedexponent (DoubleBaseintexp) { Doubleresult = 1.0; for(intI=1; i<=exp; i++) {result*=Base; } returnresult; } //Comparison of two double type data Private Static BooleanEqualDoubleNUM1,Doublenum2) { if((Num1-num2 > -0.0000001) && (Num1-num2 < 0.0000001)) return true; return false; }
Optimization method:
Private Static DoublePowerWithUnsignedExponent1 (DoubleBaseintexp) { if(exp = = 0) return1.0; if(exp = = 1) returnBase; Doubleresult = Powerwithunsignedexponent (base, exp >> 1);//shift right instead of dividing by 2Result *=result; if(exp & 1) = = 1) {//determining the power of odd and even times, which is substituted for the remainder operationResult *=Base; } returnresult; }
The whole number of points of the offer-value of the sword