[C / C ++ syntax] -pow function application

pow function and should pay attention to the problem

pow (x, y) function is used to solve the required X Y problem
header file for the function pow <math.h>

Problem Description encounter

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

int main(void)
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        int ans;
        ans = pow(n, m);
        printf("%d\n", ans);
    }
    return 0;
}

Results are as follows
The results should not be surprising run 25,125,100 thing!

Function definition follows the function pow

double pow  (double base, double exponent);

It can be seen pow function parameters and return values are double type!

Solution

So how do we solve it
very simple to change the parameters and return values of type double like spicy!

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

int main(void)
{
    double n, m;
    while(~scanf("%lf%lf", &n, &m))
    {
        double ans;
        ans = pow(n, m);
        printf("%d\n", (int)ans);
    }
    return 0;
}

Results are as follows!
Is not it a success!
In fact, many function return values and parameters are double type
used with caution to avoid unnecessary mistakes we will do should consult the documentation
Reference: https://blog.csdn.net/qq_20504945/article/details/84964954
(for personal learning use )

Published 20 original articles · won praise 2 · Views 949

Guess you like

Origin blog.csdn.net/zhbbbbbb/article/details/103382635