Namespaces
Variants

rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
rint lrint llrint
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
Definiert in Header <math.h>
float rintf ( float arg ) ;
(1) (seit C99)
double rint ( double arg ) ;
(2) (seit C99)
long double rintl ( long double arg ) ;
(3) (seit C99)
Definiert in Header <tgmath.h>
#define rint( arg )
(4) (seit C99)
Definiert in Header <math.h>
long lrintf ( float arg ) ;
(5) (seit C99)
long lrint ( double arg ) ;
(6) (seit C99)
long lrintl ( long double arg ) ;
(7) (seit C99)
Definiert in Header <tgmath.h>
#define lrint( arg )
(8) (seit C99)
Definiert in Header <math.h>
long long llrintf ( float arg ) ;
(9) (seit C99)
long long llrint ( double arg ) ;
(10) (seit C99)
long long llrintl ( long double arg ) ;
(11) (seit C99)
Definiert in Header <tgmath.h>
#define llrint( arg )
(12) (seit C99)
1-3) Rundet das Fließkomma-Argument arg auf einen ganzzahligen Wert im Fließkommaformat, unter Verwendung des aktuellen Rundungsmodus.
5-7, 9-11) Rundet das Fließkomma-Argument arg auf einen ganzzahligen Wert im Integer-Format, unter Verwendung des aktuellen Rundungsmodus.
4,8,12) Typgenerische Makros: Wenn arg den Typ long double hat, werden rintl , lrintl , llrintl aufgerufen. Andernfalls, wenn arg einen Ganzzahltyp oder den Typ double hat, werden rint , lrint , llrint aufgerufen. Andernfalls werden entsprechend rintf , lrintf , llrintf aufgerufen.

Inhaltsverzeichnis

Parameter

arg - Gleitkommawert

Rückgabewert

Wenn keine Fehler auftreten, wird der nächstgelegene ganzzahlige Wert zu arg , entsprechend dem aktuellen Rundungsmodus , zurückgegeben.

Fehlerbehandlung

Fehler werden gemeldet, wie in math_errhandling spezifiziert.

Wenn das Ergebnis von lrint oder llrint außerhalb des durch den Rückgabetyp darstellbaren Bereichs liegt, kann ein Domänenfehler oder ein Bereichsfehler auftreten.

Wenn die Implementierung IEEE-Gleitkommaarithmetik (IEC 60559) unterstützt,

Für die rint Funktion:
  • Wenn arg ±∞ ist, wird es unverändert zurückgegeben.
  • Wenn arg ±0 ist, wird es unverändert zurückgegeben.
  • Wenn arg NaN ist, wird NaN zurückgegeben.
Für lrint und llrint Funktionen:
  • Wenn arg ±∞ ist, wird FE_INVALID ausgelöst und ein implementierungsdefinierter Wert zurückgegeben.
  • Wenn das Ergebnis der Rundung außerhalb des Wertebereichs des Rückgabetyps liegt, wird FE_INVALID ausgelöst und ein implementierungsdefinierter Wert zurückgegeben.
  • Wenn arg NaN ist, wird FE_INVALID ausgelöst und ein implementierungsdefinierter Wert zurückgegeben.

Hinweise

POSIX spezifiziert , dass alle Fälle, in denen lrint oder llrint FE_INEXACT auslösen, Domänenfehler sind.

Wie in math_errhandling spezifiziert, kann FE_INEXACT (muss aber auf Nicht-IEEE-Fließkomma-Plattformen nicht) von rint ausgelöst werden, wenn ein nicht-ganzzahliger endlicher Wert gerundet wird.

Der einzige Unterschied zwischen rint und nearbyint ist, dass nearbyint niemals FE_INEXACT auslöst.

Die größten darstellbaren Gleitkommawerte sind exakte Ganzzahlen in allen Standard-Gleitkommaformaten, daher rint überläuft niemals allein; jedoch kann das Ergebnis jeden Ganzzahltyp (einschließlich intmax_t ) überlaufen, wenn es in einer Ganzzahlvariable gespeichert wird.

Wenn der aktuelle Rundungsmodus ist...

Beispiel

#include <fenv.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
#pragma STDC FENV_ACCESS ON
    fesetround(FE_TONEAREST);
    printf("rounding to nearest (halfway cases to even):\n"
           "rint(+2.3) = %+.1f  ", rint(2.3));
    printf("rint(+2.5) = %+.1f  ", rint(2.5));
    printf("rint(+3.5) = %+.1f\n", rint(3.5));
    printf("rint(-2.3) = %+.1f  ", rint(-2.3));
    printf("rint(-2.5) = %+.1f  ", rint(-2.5));
    printf("rint(-3.5) = %+.1f\n", rint(-3.5));
    fesetround(FE_DOWNWARD);
    printf("rounding down: \nrint(+2.3) = %+.1f  ", rint(2.3));
    printf("rint(+2.5) = %+.1f  ", rint(2.5));
    printf("rint(+3.5) = %+.1f\n", rint(3.5));
    printf("rint(-2.3) = %+.1f  ", rint(-2.3));
    printf("rint(-2.5) = %+.1f  ", rint(-2.5));
    printf("rint(-3.5) = %+.1f\n", rint(-3.5));
    printf("rounding down with lrint: \nlrint(+2.3) = %ld  ", lrint(2.3));
    printf("lrint(+2.5) = %ld  ", lrint(2.5));
    printf("lrint(+3.5) = %ld\n", lrint(3.5));
    printf("lrint(-2.3) = %ld  ", lrint(-2.3));
    printf("lrint(-2.5) = %ld  ", lrint(-2.5));
    printf("lrint(-3.5) = %ld\n", lrint(-3.5));
    printf("lrint(-0.0) = %ld\n", lrint(-0.0));
    printf("lrint(-Inf) = %ld\n", lrint(-INFINITY)); // FE_INVALID raised
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("rint(1.1) = %.1f\n", rint(1.1));
    if (fetestexcept(FE_INEXACT))
        puts("    FE_INEXACT was raised");
    feclearexcept(FE_ALL_EXCEPT);
    printf("lrint(LONG_MIN-2048.0) = %ld\n", lrint(LONG_MIN-2048.0));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID was raised");
}

Mögliche Ausgabe:

rounding to nearest (halfway cases to even):
rint(+2.3) = +2.0  rint(+2.5) = +2.0  rint(+3.5) = +4.0
rint(-2.3) = -2.0  rint(-2.5) = -2.0  rint(-3.5) = -4.0
rounding down:
rint(+2.3) = +2.0  rint(+2.5) = +2.0  rint(+3.5) = +3.0
rint(-2.3) = -3.0  rint(-2.5) = -3.0  rint(-3.5) = -4.0
rounding down with lrint:
lrint(+2.3) = 2  lrint(+2.5) = 2  lrint(+3.5) = 3
lrint(-2.3) = -3  lrint(-2.5) = -3  lrint(-3.5) = -4
lrint(-0.0) = 0
lrint(-Inf) = -9223372036854775808
rint(1.1) = 1.0
    FE_INEXACT was raised
lrint(LONG_MIN-2048.0) = -9223372036854775808
    FE_INVALID was raised

Referenzen

  • C23-Standard (ISO/IEC 9899:2024):
  • 7.12.9.4 Die rint-Funktionen (S.: TBD)
  • 7.12.9.5 Die lrint- und llrint-Funktionen (S.: TBD)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S.: TBD)
  • F.10.6.4 Die rint-Funktionen (S.: TBD)
  • F.10.6.5 Die lrint- und llrint-Funktionen (S.: TBD)
  • C17-Standard (ISO/IEC 9899:2018):
  • 7.12.9.4 Die rint-Funktionen (S. 184)
  • 7.12.9.5 Die lrint- und llrint-Funktionen (S. 184)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S. 272-273)
  • F.10.6.4 Die rint-Funktionen (S. 384)
  • F.10.6.5 Die lrint- und llrint-Funktionen (S. 384)
  • C11-Standard (ISO/IEC 9899:2011):
  • 7.12.9.4 Die rint-Funktionen (S. 252)
  • 7.12.9.5 Die lrint- und llrint-Funktionen (S. 252)
  • 7.25 Typgenerische Mathematik <tgmath.h> (S. 373-375)
  • F.10.6.4 Die rint-Funktionen (S. 527)
  • F.10.6.5 Die lrint- und llrint-Funktionen (S. 527)
  • C99-Standard (ISO/IEC 9899:1999):
  • 7.12.9.4 Die rint-Funktionen (S. 232-233)
  • 7.12.9.5 Die lrint- und llrint-Funktionen (S. 233)
  • 7.22 Typgenerische Mathematik <tgmath.h> (S. 335-337)
  • F.9.6.4 Die rint-Funktionen (S. 463)
  • F.9.6.5 Die lrint- und llrint-Funktionen (S. 463)

Siehe auch

(C99) (C99) (C99)
rundet zur nächstgelegenen Ganzzahl, die nicht größer im Betrag als der gegebene Wert ist
(Funktion)
rundet zu einer Ganzzahl unter Verwendung des aktuellen Rundungsmodus
(Funktion)
ruft Rundungsrichtung ab oder legt sie fest
(Funktion)