zoukankan      html  css  js  c++  java
  • 数值的整数次方*

    bool g_bInvalidInput = false;
    double power(double base, int exponent)
    {
    	if (equal(base, 0.0) && exponent < 0)
    	{
    		g_bInvalidInput = true;
    		return 0.0;
    	}
    
    	unsigned int absExponent = (unsigned int)exponent;
    	if (exponent < 0)
    		absExponent = (unsigned int)(-exponent);
    
    	double result = PowerWithUnsignedInt(base, absExponent);
    	if (exponent < 0)
    		result = 1.0 / result;
    	return result;
    }
    
    bool equal(double value1, double value2)
    {
    	if ((value1 - value2 > -0.0000001) &&
    		(value1 - value2 < 0.0000001))
    		return true;
    	else
    		return false;
    }
    
    double PowerWithUnsignedInt(double base, unsigned int absExponent)
    {
    	if (absExponent == 0)
    		return 1;
    	if (absExponent == 1)
    		return base;
    
    	double result = PowerWithUnsignedInt(base, absExponent >> 1);
    	result *= result;
    	if (absExponent & 0x1 == 1)
    		result *= base;
    	return result;
    }
    

      

  • 相关阅读:
    csrf跨站请求伪造
    IO 之 InputStream 和 Reader
    javadoc tags
    java this
    递归
    java 文件中 定义一个字符串,它的默认编码是什么?
    合并数组
    << 移位运算
    final static T
    Base64.java 工具类
  • 原文地址:https://www.cnblogs.com/yapp/p/14386114.html
Copyright © 2011-2022 走看看