开发者

sqrt() pow() fabs() do not work

开发者 https://www.devze.com 2023-02-24 16:00 出处:网络
i am trying to compile my program where i am using the functions like sqrt pow and fab开发者_开发问答s. I do have math.h included but for some reason i get errors like:

i am trying to compile my program where i am using the functions like sqrt pow and fab开发者_开发问答s. I do have math.h included but for some reason i get errors like:

    error C2668: 'fabs' : ambiguous call to overloaded function

same for the rest of the functions i have this included:

    #include "stdafx.h"
    #include "math.h"

i tried including but still same errors. Does anyone know why they are not being recognized? my file is .cpp not .c but it is an MFC project.

thanks


It's because those functions are overloaded for several types: float, double and long double. Thus, if you pass in an integer, the compiler doesn't which one to choose. An easy fix is to simply pass a double (or multiply what you pass in by 1.0), this should fix the problem.

      int value = rand();
      int result1 = (int)fabs(value*1.0); 
      printf("%d, %d\n", result1, result1);

otherwise:

     int value = rand();
     int result1 = (int)fabs((double)value); 
     printf("%d, %d\n", result1, result1);
0

精彩评论

暂无评论...
验证码 换一张
取 消