Skip to content

[50] Unrolling A Recursion Termination Condition #6

@charmhe

Description

@charmhe

The original solution is perfect. Seems to be faster if pulling the condition of the negative outside.

double pow(double x, int n) {
    return n < 0 ? 1/pow(x, (long)-1*n) : pow(x, (long)n);
}
double __pow(double x, long n) {
    if (n == 0) return 1;
    if (n == 1) return x;
    if (n == 2) return x * x;
    if(n % 2)
        return __pow(__pow(x, n / 2), 2);
    else
	return __pow(__pow(x, n / 2), 2) * x;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions