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;
    }
    

      

  • 相关阅读:
    12迭代器
    11(2)Vector(向量)
    11(1) LinkList ---链表
    11集合(Collection<E>) Arraylist
    10异常
    乘法计算过程的模拟
    10 Date详解
    详细的OA系统学习
    8 math类
    Java开发中的23种设计模式详解
  • 原文地址:https://www.cnblogs.com/yapp/p/14386114.html
Copyright © 2011-2022 走看看