C ++ lrint () - biblioteka standardowa C ++

Funkcja lrint () w C ++ zaokrągla argument do wartości całkowitej przy użyciu bieżącego trybu zaokrąglania.

Funkcja lrint () w C ++ zaokrągla argument do wartości całkowitej przy użyciu bieżącego trybu zaokrąglania. Bieżący tryb zaokrąglania jest określany przez funkcję fesetround(). Jest podobny do rint (), ale zwraca long int.

lrint () prototyp (w standardzie C ++ 11)

long int lrint (double x); long int lrint (float x); long int lrint (long double x); long int lrint (T x); // Dla typu całkowitego

Funkcja lrint () przyjmuje pojedynczy argument i zwraca wartość typu long int. Ta funkcja jest zdefiniowana w pliku nagłówkowym.

lrint () Parametry

Funkcja lrint () przyjmuje wartość pojedynczego argumentu do zaokrąglenia.

lrint () Zwracana wartość

Funkcja lrint () zaokrągla argument x do wartości całkowitej, używając kierunku zaokrąglania określonego przez fegetround () i zwraca wartość w long int.

Domyślnie kierunek zaokrąglania jest ustawiony na „do najbliższego”. Kierunek zaokrąglania można ustawić na inne wartości za pomocą funkcji fesetround ().

Przykład 1: Jak lrint () działa w C ++?

 #include #include #include using namespace std; int main() ( // by default, rounding direction is to-nearest i.e. fesetround(FE_TONEAREST) double x = 11.87; long int result; result = lrint(x); cout << "Rounding to-nearest (" << x << ") = " << result << endl; // mid-way values are rounded off to higher integer x = 11.5; result = lrint(x); cout << "Rounding to-nearest (" << x << ") = " << result << endl; // setting rounding direction to DOWNWARD fesetround(FE_DOWNWARD); x = 11.87; result = lrint(x); cout << "Rounding downward (" << x << ") = " << result << endl; // setting rounding direction to UPWARD fesetround(FE_UPWARD); x = 33.32; result = lrint(x); cout << "Rounding upward (" << x << ") = " << result << endl; return 0; )

Po uruchomieniu programu wynik będzie następujący:

 Zaokrąglenie do najbliższego (11,87) = 12 Zaokrąglenie do najbliższego (11,5) = 12 Zaokrąglenie w dół (11,8699) = 11 Zaokrąglenie w górę (33,3201) = 34

Przykład 2: funkcja lrint () dla typów całkowitych

 #include #include #include using namespace std; int main() ( int x = 15; long int result; // setting rounding direction to DOWNWARD fesetround(FE_DOWNWARD); result = lrint(x); cout << "Rounding downward (" << x << ") = " << result << endl; return 0; ) 

Po uruchomieniu programu wynik będzie następujący:

 Zaokrąglanie w dół (15) = 15 

W przypadku wartości całkowitych zastosowanie funkcji lrint zwraca tę samą wartość co dane wejściowe. Dlatego w praktyce nie jest powszechnie używany dla wartości całkowitych.

Interesujące artykuły...