I'm trying to write a function that accepts two type double parameters. The function is suppose to be calculate and return a base number raised to an exponent (using recursion), but I can't use the pow() function itself.

I've got it working fine without using decimal format numbers, but i'm stuck on what to do since both parameters accept type double.

This is what i got so far :

#include <iostream>

using namespace std;

double recursion (double x, double y)
{

	if(y > 1)
		return x * recursion(x, y - 1);

	else
		return x;
}

int main()
{
	double base;
	double exp;

	cout << "(A) raised to the (B) power is ..." << endl;
	cout << "Enter input (A): ";
	cin >> base;
	cout << "Enter input (B): ";
	cin >> exp;
	// Recursion call

	cout << base << " raised to the " << exp <<" power is " << recursion(base, exp) << endl;

	return 0;
}

any suggestions?

Dani AI

Generated

: the routine in your first post only multiplies while subtracting 1 each call, so it correctly implements integer exponentiation but cannot produce a correct real (non‑integer) power. is right that fixed affects only output formatting. is also correct that approximations (series) are one route, but there is a much simpler and standard identity to use for fractional exponents.

For real exponents the usual identity is
x^y = exp(y * log(x)).
That is the reliable way to get non‑integer powers (assuming a real result). For integer exponents you can keep a recursive routine (better: recursive exponentiation by squaring for speed and fewer recursive calls). If the base is negative and the exponent is not an integer the real result does not exist (the result is complex) — either reject that input or switch to std::complex.

Example hybrid implementation (recursive for integer exponents, exp/log for fractional exponents):

#include <cmath>
#include <limits>

static double int_pow_rec(double x, unsigned long long n) {
    if (n == 0) return 1.0;
    double half = int_pow_rec(x, n >> 1);
    return (n & 1) ? x * half * half : half * half;
}

double pow_custom(double x, double y) {
    const double eps = 1e-12;
    double y_round = std::round(y);
    if (std::fabs(y - y_round) < eps) {
        long long n = static_cast<long long>(y_round);
        if (n == 0) return 1.0;
        bool neg = n < 0;
        unsigned long long un = neg ? static_cast<unsigned long long>(-n) : static_cast<unsigned long long>(n);
        double r = int_pow_rec(x, un);
        return neg ? 1.0 / r : r;
    }
    if (x <= 0.0) return std::numeric_limits<double>::quiet_NaN(); // switch to complex if needed
    return std::exp(y * std::log(x));
}

Notes: pick an eps appropriate to your range when testing "integerness"; watch for 0^negative, overflow, and precision limits. For complex results use std::complex<double> and the same exp(y*log(z)) approach. See std::exp, std::log and std::complex for details.

Recommended Answers

All 3 Replies

I don't understand what the problem is. Your program runs ok for me. If you are concerned about it displaying the return value in scientific notation, add << fixed before the call to recursion function.

For example, in my program if you try 2.34 raised to the 3.52 power, you will get a result of 29.9822. This is wrong. The correct answer is 19.9360.

I don't know what I must do so the exponential number can be a decimal and still return the correct answer.

Your approach indeed doesn't work for non-integer exponents. It is just not applicable.
Check out Taylor series, or some other approximation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.