Namespaces
Variants

modf, modff, modfl

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
(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 im Header <math.h>
float modff ( float arg, float * iptr ) ;
(1) (seit C99)
double modf ( double arg, double * iptr ) ;
(2)
long double modfl ( long double arg, long double * iptr ) ;
(3) (seit C99)
1-3) Zerlegt den gegebenen Gleitkommawert arg in einen ganzzahligen und einen gebrochenen Teil, die jeweils denselben Typ und dasselbe Vorzeichen wie arg besitzen. Der ganzzahlige Teil (im Gleitkommaformat) wird in dem Objekt gespeichert, auf das iptr zeigt.

Inhaltsverzeichnis

Parameter

arg - Gleitkommawert
iptr - Zeiger auf Gleitkommawert, um den ganzzahligen Teil zu speichern

Rückgabewert

Wenn keine Fehler auftreten, gibt den Bruchteil von arg mit demselben Vorzeichen wie arg zurück. Der ganzzahlige Teil wird in den Wert geschrieben, auf den iptr zeigt.

Die Summe des zurückgegebenen Werts und des in * iptr gespeicherten Werts ergibt arg (unter Berücksichtigung von Rundungsfehlern).

Fehlerbehandlung

Diese Funktion unterliegt keinen der in math_errhandling spezifizierten Fehler.

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

  • Wenn arg ±0 ist, wird ±0 zurückgegeben und ±0 wird in * iptr gespeichert.
  • Wenn arg ±∞ ist, wird ±0 zurückgegeben und ±∞ wird in * iptr gespeichert.
  • Wenn arg NaN ist, wird NaN zurückgegeben und NaN wird in * iptr gespeichert.
  • Der zurückgegebene Wert ist exakt, der aktuelle Rundungsmodus wird ignoriert.

Hinweise

Diese Funktion verhält sich, als wäre sie wie folgt implementiert:

double modf(double value, double *iptr)
{
#pragma STDC FENV_ACCESS ON
    int save_round = fegetround();
    fesetround(FE_TOWARDZERO);
    *iptr = std::nearbyint(value);
    fesetround(save_round);
    return copysign(isinf(value) ? 0.0 : value - (*iptr), value);
}

Beispiel

#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
    double f = 123.45;
    printf("Given the number %.2f or %a in hex,\n", f, f);
    double f3;
    double f2 = modf(f, &f3);
    printf("modf() makes %.2f + %.2f\n", f3, f2);
    int i;
    f2 = frexp(f, &i);
    printf("frexp() makes %f * 2^%d\n", f2, i);
    i = ilogb(f);
    printf("logb()/ilogb() make %f * %d^%d\n", f / scalbn(1.0, i), FLT_RADIX, i);
    // special values
    f2 = modf(-0.0, &f3);
    printf("modf(-0) makes %.2f + %.2f\n", f3, f2);
    f2 = modf(-INFINITY, &f3);
    printf("modf(-Inf) makes %.2f + %.2f\n", f3, f2);
}

Mögliche Ausgabe:

Given the number 123.45 or 0x1.edccccccccccdp+6 in hex,
modf() makes 123.00 + 0.45
frexp() makes 0.964453 * 2^7
logb()/ilogb() make 1.92891 * 2^6
modf(-0) makes -0.00 + -0.00
modf(-Inf) makes -INF + -0.00

Referenzen

  • C23-Standard (ISO/IEC 9899:2024):
  • 7.12.6.12 Die modf-Funktionen (S: TBD)
  • F.10.3.12 Die modf-Funktionen (S: TBD)
  • C17-Standard (ISO/IEC 9899:2018):
  • 7.12.6.12 Die modf-Funktionen (S.: TBD)
  • F.10.3.12 Die modf-Funktionen (S.: TBD)
  • C11-Standard (ISO/IEC 9899:2011):
  • 7.12.6.12 Die modf-Funktionen (S. 246-247)
  • F.10.3.12 Die modf-Funktionen (S. 523)
  • C99-Standard (ISO/IEC 9899:1999):
  • 7.12.6.12 The modf functions (S. 227)
  • F.9.3.12 The modf functions (S. 460)
  • C89/C90-Standard (ISO/IEC 9899:1990):
  • 4.5.4.6 Die modf-Funktion

Siehe auch

(C99) (C99) (C99)
rundet zur nächstgelegenen Ganzzahl, die nicht größer im Betrag als der gegebene Wert ist
(Funktion)