Java Power of 2 nextPowerOfTwoValue(double value)

Here you can find the source of nextPowerOfTwoValue(double value)

Description

This method will return the next power of two for the given value, e.g.

License

Open Source License

Parameter

Parameter Description
value to use

Return

the next power of two of the given value

Declaration

public static int nextPowerOfTwoValue(double value) 

Method Source Code

//package com.java2s;
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:// w  w w .  j a  v  a  2 s . c  om
 Department of Geography, University of Bonn
 and
 lat/lon GmbH
    
 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 of the License, or (at your option)
 any later version.
 This library is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    
 Contact information:
    
 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/
    
 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/
    
 e-mail: info@deegree.org
 ----------------------------------------------------------------------------*/

public class Main {
    /**
     *
     */
    public static final double EPSILON = 1E-10;

    /**
     * This method will return the next power of two for the given value, e.g. if value is 511 it will return 512, if
     * value is 513 it will return 1024.
     * 
     * @param value
     *            to use
     * @return the next power of two of the given value
     */
    public static int nextPowerOfTwoValue(double value) {
        int power = previousPowerOfTwo(value);
        return 1 << power;
    }

    /**
     * This method will return the next power, two must be raised too to give the next power of two, e.g. if value is
     * 511 it will return 9, if value is 513 it will return 10.
     * 
     * @param value
     *            to use
     * @return the next power to the base of two the given value has
     */
    public static int previousPowerOfTwo(double value) {
        int result = 0;
        int power = 1;
        while (power <= value && Math.abs(value - power) > EPSILON) {
            power = power << 1;
            result++;
        }
        return result;
    }
}

Related

  1. nextPowerOfTwo(int x)
  2. nextPowerOfTwo(int x)
  3. nextPowerOfTwo(int x)
  4. nextPowerOfTwo(int x)
  5. nextPowerOfTwoExact(int n)
  6. nextPowerTwo(int initialNum)