Here you can find the source of pow(double a, int b)
Parameter | Description |
---|---|
a | base for exponentiation |
b | integral value of exponent |
public static final double pow(double a, int b)
//package com.java2s; /*// w ww . jav a2 s. c o m * Copyright 2014 Jon N. Marsh. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Computes {@code a^b} for integer exponents. Works for both positive and * negative values of the exponent {@code b}. * * @param a base for exponentiation * @param b integral value of exponent * @return {@code a} raised to the {@code b}<sup>th</sup> power */ public static final double pow(double a, int b) { if (b < 0.0) { a = 1.0 / a; b *= -1; } double result = 1.0; while (b != 0) { if ((b & 1) == 1) { result *= a; } b >>= 1; a *= a; } return result; } }