Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2^3
- pow(2,3) == 8
- // 5^5
- pow(5,5) == 3125
- int ipow(int base, int exp)
- {
- int result = 1;
- while (exp)
- {
- if (exp & 1)
- result *= base;
- exp >>= 1;
- base *= base;
- }
- return result;
- }
- x^15 = (x^7)*(x^7)*x
- x^7 = (x^3)*(x^3)*x
- x^3 = x*x*x
- n*n = n^2
- n^2*n = n^3
- n^3*n^3 = n^6
- n^6*n^6 = n^12
- n^12*n^3 = n^15
- struct IeeeFloat
- {
- unsigned int base : 23;
- unsigned int exponent : 8;
- unsigned int signBit : 1;
- };
- union IeeeFloatUnion
- {
- IeeeFloat brokenOut;
- float f;
- };
- inline float twoToThe(char exponent)
- {
- // notice how the range checking is already done on the exponent var
- static IeeeFloatUnion u;
- u.f = 2.0;
- // Change the exponent part of the float
- u.brokenOut.exponent += (exponent - 1);
- return (u.f);
- }
- 2 ** 3 == 1 << 3 == 8
- 2 ** 30 == 1 << 30 == 1073741824 (A Gigabyte)
- int pow( int base, int exponent)
- {
- if (exponent == 0) return 1; // base case;
- int temp = pow(base, exponent/2);
- if (exponent % 2 == 0)
- return temp * temp;
- else
- return (base * temp * temp);
- }
- int pow(int base, int pow) {
- int res = 1;
- for(int i=pow; i<pow; i++)
- res *= base;
- return res;
- }
Add Comment
Please, Sign In to add comment