Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- //x^n evaluation
- int main() {
- double x = -10;
- int n = 10;
- int bc_n = n; //Save n for correct output;
- bool invert = (n < 0);
- if (invert) {
- n = -n;
- }
- double res = 1;
- double pw = x;
- while (n) {
- if (n & 1) res *= pw;
- pw *= pw;
- n >>= 1;
- }
- if (invert) {
- res = 1 / res;
- n = -n;
- }
- printf("%lf^%d = %lf\n", x, bc_n, res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement