C ++ trunc () - C ++ standaardbibliotheek

De functie trunc () in C ++ rondt het argument af naar nul en retourneert de dichtstbijzijnde integrale waarde die niet groter is dan het argument.

De functie trunc () in C ++ rondt het argument af naar nul en retourneert de dichtstbijzijnde integrale waarde die niet groter is dan het argument.

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

dubbele trunc (dubbele x); zweven trunc (zweven x); lange dubbele trunc (lange dubbele x); dubbele trunc (T x); // Voor integrale typen

De functie trunc () heeft een enkel argument en retourneert een waarde van het type double, float of long double type. Deze functie is gedefinieerd in het header-bestand.

trunc () Parameters

De functie trunc () accepteert één argument waarvan de trunc-waarde moet worden berekend.

trunc () Retourwaarde

De functie trunc () rondt x af naar nul en retourneert de dichtstbijzijnde integrale waarde die niet groter is dan x.

Simpel gezegd, de functie trunc () kapt de waarde af na decimaal en retourneert alleen het gehele deel.

Voorbeeld 1: hoe werkt trunc () in C ++?

 #include #include using namespace std; int main() ( double x = 10.25, result; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; x = -34.251; result = trunc(x); cout << "trunc(" << x << ") = " << result << endl; return 0; )

Wanneer u het programma uitvoert, is de uitvoer:

 trunc (10,25) = 10 trunc (-34,251) = -34

Voorbeeld 2: trunc () functie voor integrale typen

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

Wanneer u het programma uitvoert, is de uitvoer:

 afkapping (15) = 15 

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

Interessante artikelen...