Hi everyone,
I am having this problem to define a function which raises a float y eg 3.5 which is not a whole number to the base x.
Heres the code which gives domain error as function pow takes only int values.

//Program to raise a number to power when both are floating nos
#include<stdio.h>
#include<conio.h>
#include<math.h>
void power(float x,float i)
{
 float y;
 y=pow(x,i);
 printf("%d",y);
}

void main()
{
 float x,i;
 printf("Enter any number");
 scanf("%d",&x);
 printf("Enter its power");
 scanf("%d",&i);
 power(x,i);
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

I'll be glad if you help me out.
Thanks
:confused:

Dani AI

Generated

Short answer: yes — C can compute x^y when y is non‑integer. The usual causes of the "domain error" in the code above are using the wrong scanf/printf format specifiers and passing the wrong types to pow, and (on some toolchains) forgetting to link the math library. As noted, the standard pow() works with double (there is powf() for float), and as showed you may need to link libm on Unix-like systems.

Correct, minimal patterns (do not use %d for floating input):

#include <stdio.h>
#include <math.h>

int main(void) {
    double x, y;
    if (scanf("%lf %lf", &x, &y) != 2) return 1;   /* %lf for double */
    double r = pow(x, y);                          /* pow returns double */
    printf("%g\n", r);                             /* %g is handy for output */
    return 0;
}

If you prefer float (smaller, slightly faster on some platforms), use powf and the float scanf specifier:

float xf, yf, rf;
scanf("%f %f", &xf, &yf);
rf = powf(xf, yf);
printf("%f\n", rf);

Troubleshooting checklist:

  • Use "%lf" in scanf for double and "%f" for float; printf uses "%f" (floats are promoted to double).
  • On gcc/clang link with -lm if you see unresolved math symbols; MSVC normally links math functions automatically.
  • A real-domain "domain error" often means a negative base with a non-integer exponent (e.g. pow(-2, 0.5) is not a real number). Handle that by checking the sign and exponent (only integer exponents allowed for negative bases), or use C99 complex (cpow in <complex.h>) if complex results are acceptable.
  • If pow() returns NaN, check errno (EDOM) or use isnan/isfinite to detect errors.

Fixing the format specifiers and using the right pow variant will resolve the behavior you observed in 's original post.

Recommended Answers

All 4 Replies

I am having this problem to define a function which raises a float y eg 3.5 which is not a whole number to the base x.
Heres the code which gives domain error as function pow takes only int values.

Pow, in the standard C library for mathematical operations, takes two double values by default. You could also look into the powerful powf function.

-Fredric

I want to run this program in C where I can compute 2^(3.5).Is this possible in C?

This file:
#include <math.h>
int main(int argc, char *argv[])
{
        printf("%f\n",pow(2., 3.5) );
        return 0;
}
compiled this way (This is unix does not matter)
kcsdev:/home/jmcnama> cc t.c -lm
Gives this result:
kcsdev:/home/jmcnama> a.out
11.313708
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.