Chcę rozważyć kod. Pierwszy to:Różnica między sqrt i sqrtf
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main() {
int s = 25;
cout << sqrt(s) << endl;
return 0;
}
Dało mi ten błąd:
>c:\users\datuashvili\documents\visual studio 2010\projects\training\training\training.cpp(9): error C2668: 'sqrt' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 10.0\vc\include\math.h(589): could be 'long double sqrt(long double)'
1> c:\program files\microsoft visual studio 10.0\vc\include\math.h(541): or 'float sqrt(float)'
1> c:\program files\microsoft visual studio 10.0\vc\include\math.h(127): or 'double sqrt(double)'
1> while trying to match the argument list '(int)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Jeśli dodam typu float w nawiasach przed s tak:
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main() {
int s = 25;
cout << sqrt((float)s) << endl;
return 0;
}
mam jak I zgaduję, 5
. A inny wariant jest to, że zamiast sqrt
, jeśli piszę sqrtf
:
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;
int main(){
int s=25;
cout << sqrtf((float)s) << endl;
return 0;
}
Dostałam też 5
.
Jaka jest różnica między nimi? Czy to oznacza, że sqrtf jest taki sam jak sqrt dla typu float?
Ale nie dostaniesz błędu kompilujesz go w g ++ (kompilator GNU) – vivek