English: Aliasing a positive IEEE-754floating point number x to an integer Ix (like in C: float x = ...; int32_t i = * (int32_t *) &x;) yields a scaled and shifted approximation of its base-2 logarithm. This is the basis of the fast inverse square root algorithm.
Français : Si un nombre positif x représenté en virgule flottante au format IEEE 754 est interprété comme un entier Ix (comme en C : float x = ...; int32_t i = * (int32_t *) &x;) cela produit, à une transformation linéaire près, une approximation de son logarithme en base 2. Cette approximation est le fondement de l'algorithme de racine carrée inverse rapide.
Warning: This source contains non-ASCII characters in label 3, in the title of the first curve and in comments.
#!/usr/bin/gnuplot# Aliasing a float32 to an int32 yields an approximation of its base-2# logarithm.# Parameters for IEEE-754 single-precision floatsL=2**23# mantissa scaling factorB=127# exponent bias# For m ∈ [0, 1), log₂(1 + m) ≈ 1 + σsigma=0.0430357# Assume x is a positive normal float32.# Compute I(x): the int32 aliased to x.log_of_2=log(2)log2(x)=log(x)/log_of_2round(x)=floor(x+0.5)e(x)=floor(log2(x))# exponentm(x, e_x)=x/2**e_x-1# mantissa, without the leading 1M(x, e_x)=round(m(x,e_x)*L)# scaled and rounded mantissaI2(x, e_x)=(e_x+B)*L+M(x,e_x)I(x)=I2(x,e(x))# Best fit to a logarithmscaled_log(x)=L*log2(x)+L*(B-sigma)# Do the plotsetterminalsvgsize640,480enhancedsetoutput'Log_by_aliasing_to_int.svg'setxrange[0:10]setyrange[0x3d800000:]setformaty"0x%x"setyticsLsetkeytopleftLeftreverseinvertspacing1.4setsamples200setstyleline1lw3lcrgb"gray80"setstyleline2lw1lcrgb"blue"setstyleincrementusersetlabel1"L = 2^{23}"at6,0x3fc00000setlabel2"B = 127"at6,0x3f600000setlabel3"σ = 0.0430357"at6,0x3f000000plotscaled_log(x)title"L log_2(x) + L (B − σ)",\
I(x)title'I_x'lt3
Licencja
Ja, właściciel praw autorskich do tego dzieła, udostępniam je na poniższej licencji
dzielić się – kopiować, rozpowszechniać, odtwarzać i wykonywać utwór
modyfikować – tworzyć utwory zależne
Na następujących warunkach:
uznanie autorstwa – musisz określić autorstwo utworu, podać link do licencji, a także wskazać czy utwór został zmieniony. Możesz to zrobić w każdy rozsądny sposób, o ile nie będzie to sugerować, że licencjodawca popiera Ciebie lub Twoje użycie utworu.
na tych samych warunkach – Jeśli zmienia się lub przekształca niniejszy utwór, lub tworzy inny na jego podstawie, można rozpowszechniać powstały w ten sposób nowy utwór tylko na podstawie tej samej lub podobnej licencji.