frexp ist definiert in der math
, die in C über math.h
, bzw. in C++ über cmath
eingebunden wird.
frex teilt eine Fließkommazahl in Faktor und Potenz zur Basis zwei auf. (floatingPoint = factor * 2exp
)
#include <math.h> double frexp( double floatingPoint, int * exp ); float frexp( float floatingPoint, int * exp ); // nur C++ long double frexp( long double floatingPoint, int * exp ); // nur C++
factor:
exp: Ausgabeparameter: Adresse für ein Integer, um den Exponent zur Basis zwei abzulegen
Return value: Wert, mit dem die Potenz von exp zur Basis 2 multipliziert wird, um dem Parameter floatingPoint zu entsprechen
exp wird hier ausschließlich als Ausgabeparameter verwendet.
#include <stdlib.h> // für EXIT_SUCCESS #include <math.h> // für log10 #include <stdio.h> // für printf() int main( void ) { double factor; int exp; double floatingPoint = 12; factor = ldexp( floatingPoint, &exp ); printf( "%f * 2^%f entspricht %f\n", factor, exp, floatingPoint ); return EXIT_SUCCESS; }
Ausgabe:
3.000000 * 2^2.000000 entspricht 12.000000
frexp lässt sich mit ldexp() umkehren. Zu beachten ist, dass der Faktor entsprechend einer Fließkommazahl immer zwischen größer -1.0 und kleiner 1.0 liegt, aber nie -1.0 oder +1.0 erreicht.