Inaccurate calculation caused by inaccurate use of data type

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

int main(){
  int num,onum,numl=0,tem,nnum=0;    

  printf("Enter num: ");
  scanf("%d", &num);

  onum =num;

  while (num!=0){
    num = num/10;
    ++numl;
  }
  num = onum;

  while (num!=0){
    tem = num%10;

    nnum += pow(tem,numl);
    printf("%d",nnum);
    num /= 10    ;
  }

  if (nnum == onum){
    printf("is");
  }
  else{
    printf("not");
  }
}

works great with other narcissistic numbers.

but if input 153, 5 ** 3 is erroneously counted to 124, why? enter image description here


This question duplicates many questions . And the answer below is the easiest for the user to understand.

In one sentence, an inaccurate calculation is called {inaccurate {using a data type} or {compiler} or {FPU}}.

I avoid the error by updating my compiler to gcc 7.2. But the solution to the problem requires the pow function for an integer.

+2
source share
1 answer

pow (in pow(tem,numl)) - . , double, ( pow 124.999999, , , - )

, pow, ( , FPU, ). , " ", .

, , , ( function pow (int, int))

+3

All Articles