C ++ lround () - C ++ standaardbibliotheek

De functie lround () in C ++ rondt de gehele waarde af die het dichtst bij het argument ligt, waarbij hoofdletters halverwege worden afgerond vanaf nul. De geretourneerde waarde is van het type long int.

De functie lround () in C ++ rondt de gehele waarde af die het dichtst bij het argument ligt, waarbij hoofdletters halverwege worden afgerond vanaf nul. De geretourneerde waarde is van het type long int. Het is vergelijkbaar met de functie round (), maar retourneert een lange int terwijl round hetzelfde gegevenstype retourneert als de invoer.

lround () prototype (vanaf C ++ 11-standaard)

lange int lround (dubbele x); long int lround (float x); lange int lround (lange dubbele x); lange int lround (T x); // Voor integraal type

De functie lround () gebruikt één argument en retourneert een waarde van het type long int. Deze functie is gedefinieerd in het header-bestand.

lround () Parameters

De functie lround () gebruikt een enkele argumentwaarde om af te ronden.

lround () Retourwaarde

De functie lround () retourneert de integrale waarde die het dichtst bij x ligt, waarbij hoofdletters halverwege van nul zijn afgerond. De geretourneerde waarde is van het type long int.

Voorbeeld 1: Hoe werkt lround () in C ++?

 #include #include using namespace std; int main() ( long int result; double x = 11.16; result = lround(x); cout << "lround(" << x << ") = " << result << endl; x = 13.87; result = lround(x); cout << "lround(" << x << ") = " << result << endl; x = 50.5; result = lround(x); cout << "lround(" << x << ") = " << result << endl; x = -11.16; result = lround(x); cout << "lround(" << x << ") = " << result << endl; x = -13.87; result = lround(x); cout << "lround(" << x << ") = " << result << endl; x = -50.5; result = lround(x); cout << "lround(" << x << ") = " << result << endl; return 0; )

Wanneer u het programma uitvoert, is de uitvoer:

 lround (11.16) = 11 lround (13.87) = 14 lround (50.5) = 51 lround (-11.16) = -11 lround (-13.87) = -14 lround (-50.5) = -51

Voorbeeld 2: functie lround () voor integrale typen

 #include #include using namespace std; int main() ( int x = 15; long int result; result = lround(x); cout << "lround(" << x << ") = " << result << endl; return 0; ) 

Wanneer u het programma uitvoert, is de uitvoer:

 lround (15) = 15 

Voor integrale waarden retourneert het toepassen van de functie lround dezelfde waarde als de invoer. Het wordt dus in de praktijk niet vaak gebruikt voor integrale waarden.

Interessante artikelen...