devxlogo

A Power Function for Integers

A Power Function for Integers

Need a quick and dirty way to exponentiate integers? Or perhaps you are working on a number library of your own. This will work in all cases.

 templateTYPE power(TYPE x, TYPE y) {        if (y == 0) {           // Identity                return 1;        } else if (y > 0) {     //                if (y % 2) {    // Odd                        return x * power(x * x, y / 2);                } else {        // Even                        return power(x * x, y / 2);                }        } else {        // Not Implemented                return 0;        }} // TYPE power(TYPE,TYPE)
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist