De functie fegetround () in C ++ wordt gebruikt om de waarde van de afrondingsmacro met drijvende komma te verkrijgen die overeenkomt met de huidige afrondingsrichting.
De functie fegetround () is gedefinieerd in het header-bestand.
fegetround () prototype
int fegetround ();
fegetround () Parameters
- Geen
fegetround () Retourwaarde
- Bij succes retourneert de functie fegetround () de afrondingsmacro met drijvende komma die de huidige afrondingsrichting beschrijft.
- Bij een storing wordt een negatieve waarde geretourneerd.
Macro | Omschrijving |
---|---|
FE_DOWNWARD | Rond naar beneden af |
FE_TONEAREST | Rond af op de dichtstbijzijnde waarde |
FE_TOWARDZERO | Rond naar nul |
FE_UPWARD | Rond omhoog |
Voorbeeld: hoe werkt de functie fegetround ()?
#include #include #include #pragma STDC FENV_ACCESS ON using namespace std; void print_current_rounding_direction() ( cout << "Current rounding method: "; switch (fegetround()) ( case FE_TONEAREST: cout << "FE_TONEAREST"; break; case FE_DOWNWARD: cout << "FE_DOWNWARD"; break; case FE_UPWARD: cout << "FE_UPWARD"; break; case FE_TOWARDZERO: cout << "FE_TOWARDZERO"; break; default: cout << "unknown"; ); cout << endl; ) int main(void) ( print_current_rounding_direction(); cout < " << rint(6.2) << endl; cout < " << rint(18.7) << endl; fesetround(FE_UPWARD); print_current_rounding_direction(); cout < " << rint(6.2) << endl; cout < " << rint(19.7) << endl; fesetround(FE_DOWNWARD); print_current_rounding_direction(); cout < " << rint(6.2) << endl; cout < " << rint(19.7) << endl; return 0; )
Wanneer u het programma uitvoert, is de uitvoer:
Huidige afrondingsmethode: FE_TONEAREST 6,2 -> 6 18,7 -> 19 Huidige afrondingsmethode: FE_UPWARD 6,2 -> 7 19,7 -> 20 Huidige afrondingsmethode: FE_DOWNWARD 6,2 -> 6 19,7 -> 19