2

I try to solve a math problem and my output sensitivity has a little different such as 0.07. And then I compare pow() and powf() in my code and I see this sensitivity. The code as follows:

int main()
{

    int terms, sign=-1;
    double x, operation=0.0;

    printf("Please enter the number of terms : ");
    scanf_s("%d", &terms);

    while (terms <= 0)
    {
        printf("Please re-enter the number of terms :");
        scanf_s("%d", &terms);
    }

    printf("Please enter a value for x :");
    scanf_s("%lf", &x);

    for (int i = 1; i <= terms; i++)
    {
    sign = sign * (-1);
    operation = operation + sign * powf(x + i / 10.0, (2 * i) - 1) / (2 * i);
    }
    printf("The result is : %.2lf\n", operation);

    system("pause");
    return 0;

}

Sample:

terms  : 10

x : 1.1

output  : `-59783.61` with `powf`

output  : `-59783.67` with `pow`

What is the difference between to these functions?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
00rhn
  • 39
  • 1
  • 2

5 Answers5

5

pow operates on doubles. powf operates on floats. This is a fairly standard notation in C, where the base name of a function will be for operands (and return values) of the default type (like int and double), while prefixed and suffixed versions are for other types (like long, float, etc). Here is a reference: http://pubs.opengroup.org/onlinepubs/9699919799/.

This difference in data types fully explains the differences you see in your result.

doubles contain 53 bits of precision in the mantissa, which translates to ~16 decimal digits of precision. This exceeds the precision with which you display your result, so is probably accurate enough for your purposes.

floats, on the other hand, have only 24 bits in the mantissa, which translates to ~7 decimal digits. Any combination of operations will cause roundoff errors do creep in almost immediately to within your display precision.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
2

The C language did not have "generic functions" until C11. Instead, there are different functions in the standard library for different data types.

The *f functions of <math.h> operate on single-precision floating point numbers (floats using single precision internally. double stands for ... double precision. Always use the non-f functions for double arguments.

0

The difference is :

float powf( float base, float exponent );
double pow( double base, double exponent );

DESCRIPTION in here

The pow functions compute base raised to the power of exponent.

0

As said here , pow takes a double, where powf takes a float rendering it much less precise.

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

powf returns and takes arguments of float.

pow returns double and takes arguments of double. If a float number is given as parameter, it is converted to double by the usual promotion rules.See here

The difference you are seeing in the result is due to the higher precision of double

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31