sqrt() pow() fabs() do not work
-
Hello everyone, i am trying to compile my program where i am using the functions like sqrt pow and fabs. 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
-
Hello everyone, i am trying to compile my program where i am using the functions like sqrt pow and fabs. 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 (multiply what you pass in by 1.0), this should fix the problem.
int result = (int)fabs(value*1.0);
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
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 (multiply what you pass in by 1.0), this should fix the problem.
int result = (int)fabs(value*1.0);
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Cedric Moonen wrote:
int result = (int)fabs(value*1.0);
Better could be int result = (int)fabs((double)value); Because it avoids a multiplication. Am I right?
- ns ami -
Yes you're right. It's just because I am used to multiply by 1.0 because in general I use that in calculation (where I don't want to put a cast inside because it looks ugly) :)
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Cedric Moonen wrote:
int result = (int)fabs(value*1.0);
Better could be int result = (int)fabs((double)value); Because it avoids a multiplication. Am I right?
- ns ami -
ns ami wrote:
Am I right?
Only if the code were in a huge loop would the difference in time even be noticeable.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Cedric Moonen wrote:
int result = (int)fabs(value*1.0);
Better could be int result = (int)fabs((double)value); Because it avoids a multiplication. Am I right?
- ns ami -
the compiler will optimize that multiplication away.
int value = rand();
int result1 = (int)fabs(value*1.0);
printf("%d, %d\n", result1, result1);VC6, release mode gives the following assembly:
; 10 : int value = rand();
call \_rand
; 11 : int result1 = (int)fabs(value*1.0);
mov DWORD PTR -4+\[esp+4\], eax fild DWORD PTR -4+\[esp+4\] fabs call \_\_ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax push eax push OFFSET FLAT:??\_C@\_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; \`string' call \_printf
no fmul. if you do this:
int value = rand();
int result1 = (int)fabs((double)value);
printf("%d, %d\n", result1, result1);you get this:
; 10 : int value = rand();
call \_rand
; 11 : int result1 = (int)fabs((double)value);
mov DWORD PTR -4+\[esp+4\], eax fild DWORD PTR -4+\[esp+4\] fabs call \_\_ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax push eax push OFFSET FLAT:??\_C@\_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; \`string' call \_printf
exactly the same.
-
ns ami wrote:
Am I right?
Only if the code were in a huge loop would the difference in time even be noticeable.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
it won't matter at all. the multiplication will be optimized away, and the two different calls will end with exactly the same object code.
-
it won't matter at all. the multiplication will be optimized away, and the two different calls will end with exactly the same object code.
nevertheless it's a bad style as many people won't understand it.
Don't try it, just do it! ;-)
-
the compiler will optimize that multiplication away.
int value = rand();
int result1 = (int)fabs(value*1.0);
printf("%d, %d\n", result1, result1);VC6, release mode gives the following assembly:
; 10 : int value = rand();
call \_rand
; 11 : int result1 = (int)fabs(value*1.0);
mov DWORD PTR -4+\[esp+4\], eax fild DWORD PTR -4+\[esp+4\] fabs call \_\_ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax push eax push OFFSET FLAT:??\_C@\_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; \`string' call \_printf
no fmul. if you do this:
int value = rand();
int result1 = (int)fabs((double)value);
printf("%d, %d\n", result1, result1);you get this:
; 10 : int value = rand();
call \_rand
; 11 : int result1 = (int)fabs((double)value);
mov DWORD PTR -4+\[esp+4\], eax fild DWORD PTR -4+\[esp+4\] fabs call \_\_ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax push eax push OFFSET FLAT:??\_C@\_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; \`string' call \_printf
exactly the same.