C++ Manipülatör ayarı hassasiyeti

C++ manipülatör ayarı hassasiyeti işlevi, kayan noktalı bir değerin çıkış akışı gösteriminin basamak sayısını kontrol etmek için kullanılır.

Bu manipülatör başlık dosyasında bildirildi.

Sözdizimi

 /*unspecified*/ setprecision (int n);  

Parametre

N : ondalık hassasiyet için yeni değer.

Geri dönüş değeri

Bu işlev belirtilmemiş türde bir nesne döndürür. Setbase işlevi yalnızca akış manipülatörü olarak kullanılmalıdır.

Veri Yarışları

Üzerine eklendiği veya çıkarıldığı akış nesnesi değiştirilir ve aynı akış nesnesine eşzamanlı erişim, veri yarışlarına neden olabilir.

İstisnalar

Herhangi bir istisna atılırsa nesne geçerli bir durumdadır.

örnek 1

Setprecision kullanımını gösteren basit örneği görelim:

 #include // std::cout, std::fixed #include // std::setprecision using namespace std; int main () { double f =3.14159; cout << setprecision(5) << f << '
'; cout << setprecision(9) << f << '
'; cout << fixed; cout << setprecision(5) << f << '
'; cout << setprecision(9) << f << '
'; return 0; }  

Çıktı:

 3.1416 3.14159 3.14159 3.141590000  

Örnek 2

Başka bir basit örnek görelim:

 #include #include #include #include using namespace std; int main() { const long double pi = acos(-1.L); cout << 'default precision (6): ' << pi << '
' << 'setprecision(10): ' << setprecision(10) << pi << '
' << 'max precision:' << setprecision(numeric_limits::digits10 + 1) << pi << '
'; return 0; }  

Çıktı:

 default precision (6): 3.14159 setprecision(10): 3.141592654 max precision:3.141592653589793239  

Örnek 3

Başka bir basit örnek görelim:

 #include #include using namespace std; int main (void) { float a,b,c; a = 5; b = 3; c = a/b; cout << setprecision (1) << c << endl; cout << setprecision (2) << c << endl; cout << setprecision (3) << c << endl; cout << setprecision (4) << c << endl; cout << setprecision (5) << c << endl; cout << setprecision (6) << c << endl; return 0; }  

Çıktı:

 2 1.7 1.67 1.667 1.6667 1.66667