Funkcja nextafter () w C ++ pobiera dwa argumenty i zwraca następną reprezentowalną wartość po x w kierunku y.
Funkcja jest zdefiniowana w pliku nagłówkowym.
nextafter () prototyp (w standardzie C ++ 11)
double nextafter (double x, double y); float nextafter (float x, float y); long double nextafter (long double x, long double y); Promowany nextafter (Type1 x, Type2 y); // Dodatkowe przeciążenia
Od C ++ 11, jeśli jakikolwiek argument przekazany do funkcji nextafter () jest long double
, typem zwracanym Promoted
jest long double
. Jeśli nie, zwracany typ Promoted
to double
.
Parametry nextafter ()
- x : wartość podstawowa.
- y : wartość, do której przybliżana jest wartość zwracana.
nextafter () Zwracana wartość
Funkcja nextafter () zwraca następną możliwą do reprezentacji wartość po x w kierunku y.
Przykład 1: Jak funkcja nextafter () działa w C ++?
#include #include using namespace std; int main() ( double x = 0.0, y = 1.0; double resultInDouble = nextafter(x,y); cout << "nextafter(x, y) = " << resultInDouble << endl; return 0; )
Po uruchomieniu programu wynik będzie następujący:
nextafter (x, y) = 4,94066e-324
Przykład 2: funkcja nextafter () dla argumentów różnych typów
#include #include using namespace std; int main() ( float y = 1.0; double x = INFINITY; double result = nextafter(x,y); cout << "nextafter(x, y) = " << result << endl; return 0; )
Po uruchomieniu programu wynik będzie następujący:
nextafter (x, y) = 1,79769e + 308