Jump to content

File:Log by aliasing to int.svg

Page contents not supported in other languages.
This is a file from the Wikimedia Commons
From Wikipedia, the free encyclopedia

Original file (SVG file, nominally 640 × 480 pixels, file size: 13 KB)

Summary

Description
English: Aliasing a positive IEEE-754 floating 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.
Date
Source Own work
Author Edgar Bonet
 
This W3C-unspecified plot was created with Gnuplot.

Gnuplot source

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 floats
L = 2**23   # mantissa scaling factor
B = 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_2
round(x) = floor(x + 0.5)
e(x) = floor(log2(x))               # exponent
m(x, e_x) = x / 2**e_x - 1          # mantissa, without the leading 1
M(x, e_x) = round(m(x, e_x) * L)    # scaled and rounded mantissa
I2(x, e_x) = (e_x + B) * L + M(x, e_x)
I(x) = I2(x, e(x))

# Best fit to a logarithm
scaled_log(x) = L * log2(x) + L * (B - sigma)

# Do the plot
set terminal svg size 640, 480 enhanced
set output 'Log_by_aliasing_to_int.svg'
set xrange [0:10]
set yrange [0x3d800000:]
set format y "0x%x"
set ytics L
set key top left Left reverse invert spacing 1.4
set samples 200
set style line 1 lw 3 lc rgb "gray80"
set style line 2 lw 1 lc rgb "blue"
set style increment user
set label 1 "L = 2^{23}"    at 6, 0x3fc00000
set label 2 "B = 127"       at 6, 0x3f600000
set label 3 "σ = 0.0430357" at 6, 0x3f000000
plot scaled_log(x) title "L log_2(x) + L (B − σ)", \
     I(x) title 'I_x' lt 3

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

25 July 2014

image/svg+xml

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current11:20, 25 July 2014Thumbnail for version as of 11:20, 25 July 2014640 × 480 (13 KB)Edgar.bonetUser created page with UploadWizard

The following page uses this file:

Global file usage

Metadata