I checked the difference between abs
and fabs
on python here
As I understand there are some difference regarding the speed and the passed types, but my question related to native c++ on V.S.
Regarding the V.S.
I tried the following on Visual Studio 2013 (v120)
:
float f1= abs(-9.2); // f = 9.2
float f2= fabs(-9); // Compile error [*]
So fabs(-9)
it will give me a compiler error, but when I tried to do the following:
double i = -9;
float f2= fabs(i); // This will work fine
What I understand from the first code that it will not compile because fabs(-9)
need a double, and the compiler could not convert -9 to -9.0, but in the second code the compiler will convert i=-9
to i=-9.0
at compile time so fabs(i)
will work fine.
Any better explanation?
Another thing, why the compiler can't accept fabs(-9)
and convert the int value to double automatically like what we have in c#?
[*]:
Error: more than one instance of overloaded function "fabs" matches the argument list:
function "fabs(double _X)"
function "fabs(float _X)"
function "fabs(long double _X)"
argument types are: (int)
See Question&Answers more detail:os