Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Single-precision floating-point format

From Wikipedia, the free encyclopedia
(Redirected fromSingle precision floating-point format)
32-bit computer number format
This article mayrequirecleanup to meet Wikipedia'squality standards. The specific problem is:
 • This article doesn't provide a good structure to lead users from easy to deeper understanding
 • Some points are 'explained' by lengthy examples instead of concise description of the concept.
Please helpimprove this article if you can.(January 2025) (Learn how and when to remove this message)

Single-precision floating-point format (sometimes calledFP32 orfloat32) is acomputer number format, usually occupying32 bits incomputer memory; it represents a widedynamic range of numeric values by using afloating radix point.

A floating-point variable can represent a wider range of numbers than afixed-point variable of the same bit width at the cost of precision. Asigned 32-bitinteger variable has a maximum value of 231 − 1 = 2,147,483,647, whereas anIEEE 754 32-bit base-2 floating-point variable has a maximum value of (2 − 2−23) × 2127 ≈ 3.4028235 × 1038. All integers with seven or fewer decimal digits, and any 2n for a whole number −149 ≤n ≤ 127, can be converted exactly into an IEEE 754 single-precision floating-point value.

In the IEEE 754standard, the 32-bit base-2 format is officially referred to asbinary32; it was calledsingle inIEEE 754-1985. IEEE 754 specifies additional floating-point types, such as 64-bit base-2double precision and, more recently, base-10 representations.

One of the firstprogramming languages to provide single- and double-precision floating-point data types wasFortran. Before the widespread adoption of IEEE 754-1985, the representation and properties of floating-point data types depended on thecomputer manufacturer and computer model, and upon decisions made by programming-language designers. E.g.,GW-BASIC's single-precision data type was the32-bit MBF floating-point format.

Single precision is termedREAL(4) orREAL*4 inFortran;[1]SINGLE-FLOAT inCommon Lisp;[2]float binary(p) with p≤21,float decimal(p) with the maximum value of p depending on whether the DFP (IEEE 754 DFP) attribute applies, in PL/I;float inC with IEEE 754 support,C++ (if it is in C),C# andJava;[3]Float inHaskell[4] andSwift;[5] andSingle inObject Pascal (Delphi),Visual Basic, andMATLAB. However,float inPython,Ruby,PHP, andOCaml andsingle in versions ofOctave before 3.2 refer todouble-precision numbers. In most implementations ofPostScript, and someembedded systems, the only supported precision is single.

Floating-pointformats
IEEE 754
Other
Alternatives
Tapered floating point

IEEE 754 standard: binary32

[edit]

The IEEE 754 standard specifies abinary32 as having:

This gives from 6 to 9significant decimal digits precision. If a decimal string with at most 6 significant digits is converted to the IEEE 754 single-precision format, giving anormal number, and then converted back to a decimal string with the same number of digits, the final result should match the original string. If an IEEE 754 single-precision number is converted to a decimal string with at least 9 significant digits, and then converted back to single-precision representation, the final result must match the original number.[6]

The sign bit determines the sign of the number, which is the sign of the significand as well. "1" stands for negative. The exponent field is an 8-bit unsigned integer from 0 to 255, inbiased form: a value of 127 represents the actual exponent zero. Exponents range from −126 to +127 (thus 1 to 254 in the exponent field), because the biased exponent values 0 (all 0s) and 255 (all 1s) are reserved for special numbers (subnormal numbers,signed zeros,infinities, andNaNs).

The true significand of normal numbers includes 23 fraction bits to the right of the binary point and animplicit leading bit (to the left of the binary point) with value 1. Subnormal numbers and zeros (which are the floating-point numbers smaller in magnitude than the least positive normal number) are represented with the biased exponent value 0, giving the implicit leading bit the value 0. Thus only 23 fraction bits of thesignificand appear in the memory format, but the total precision is 24 bits (equivalent to log10(224) ≈ 7.225 decimal digits) for normal values; subnormals have gracefully degrading precision down to 1 bit for the smallest non-zero value.

The bits are laid out as follows:

The real value assumed by a given 32-bitbinary32 data with a givensign, biased exponentE (the 8-bit unsigned integer), and a23-bit fraction is

(1)b31×2(b30b29b23)2127×(1.b22b21b0)2{\displaystyle (-1)^{b_{31}}\times 2^{(b_{30}b_{29}\dots b_{23})_{2}-127}\times (1.b_{22}b_{21}\dots b_{0})_{2}},

which yields

value=(1)sign×2(E127)×(1+i=123b23i2i).{\displaystyle {\text{value}}=(-1)^{\text{sign}}\times 2^{(E-127)}\times \left(1+\sum _{i=1}^{23}b_{23-i}2^{-i}\right).}

In this example:

thus:

Note:

Exponent encoding

[edit]

The single-precision binary floating-point exponent is encoded using anoffset-binary representation, with the zero offset being 127; also known as exponent bias in the IEEE 754 standard.

  • Emin = 01H−7FH = −126
  • Emax = FEH−7FH = 127
  • Exponent bias = 7FH = 127

Thus, in order to get the true exponent as defined by the offset-binary representation, the offset of 127 has to be subtracted from the stored exponent.

The stored exponents 00H and FFH are interpreted specially.

Exponentfraction = 0fraction ≠ 0Equation
00H = 000000002±zerosubnormal number(1)sign×2126×0.fraction{\displaystyle (-1)^{\text{sign}}\times 2^{-126}\times 0.{\text{fraction}}}
01H, ..., FEH = 000000012, ..., 111111102normal value(1)sign×2exponent127×1.fraction{\displaystyle (-1)^{\text{sign}}\times 2^{{\text{exponent}}-127}\times 1.{\text{fraction}}}
FFH = 111111112±infinityNaN (quiet, signaling)

The minimum positive normal value is21261.18×1038{\displaystyle 2^{-126}\approx 1.18\times 10^{-38}} and the minimum positive (subnormal) value is21491.4×1045{\displaystyle 2^{-149}\approx 1.4\times 10^{-45}}.

Converting decimal to binary32

[edit]
This sectionpossibly containsoriginal research. Pleaseimprove it byverifying the claims made and addinginline citations. Statements consisting only of original research should be removed.(February 2020) (Learn how and when to remove this message)
This sectionmay beconfusing or unclear to readers. In particular, the examples are simple particular cases (simple values exactly representable in binary, without an exponent part). This section is also probably off-topic: this is not an article about conversion, and conversion from decimal using decimal arithmetic (as opposed to conversion from a character string) is uncommon. Please helpclarify the section. There might be a discussion about this onthe talk page.(February 2020) (Learn how and when to remove this message)

In general, refer to the IEEE 754 standard itself for the strict conversion (including the rounding behaviour) of a real number into its equivalent binary32 format.

Here we can show how to convert a base-10 real number into an IEEE 754 binary32 format using the following outline:

  • Consider a real number with an integer and a fraction part such as 12.375
  • Convert andnormalize the integer part intobinary
  • Convert the fraction part using the following technique as shown here
  • Add the two results and adjust them to produce a proper final conversion

Conversion of the fractional part:Consider 0.375, the fractional part of 12.375. To convert it into a binary fraction, multiply the fraction by 2, take the integer part and repeat with the new fraction by 2 until a fraction of zero is found or until the precision limit is reached which is 23 fraction digits for IEEE 754 binary32 format.

0.375×2=0.750=0+0.750b1=0{\displaystyle 0.375\times 2=0.750=0+0.750\Rightarrow b_{-1}=0}, the integer part represents the binary fraction digit. Re-multiply 0.750 by 2 to proceed
0.750×2=1.500=1+0.500b2=1{\displaystyle 0.750\times 2=1.500=1+0.500\Rightarrow b_{-2}=1}
0.500×2=1.000=1+0.000b3=1{\displaystyle 0.500\times 2=1.000=1+0.000\Rightarrow b_{-3}=1}, fraction = 0.011, terminate

We see that(0.375)10{\displaystyle (0.375)_{10}} can be exactly represented in binary as(0.011)2{\displaystyle (0.011)_{2}}. Not all decimal fractions can be represented in a finite digit binary fraction. For example, decimal 0.1 cannot be represented in binary exactly, only approximated. Therefore:

(12.375)10=(12)10+(0.375)10=(1100)2+(0.011)2=(1100.011)2{\displaystyle (12.375)_{10}=(12)_{10}+(0.375)_{10}=(1100)_{2}+(0.011)_{2}=(1100.011)_{2}}

Since IEEE 754 binary32 format requires real values to be represented in(1.x1x2...x23)2×2e{\displaystyle (1.x_{1}x_{2}...x_{23})_{2}\times 2^{e}} format (seeNormalized number,Denormalized number), 1100.011 is shifted to the right by 3 digits to become(1.100011)2×23{\displaystyle (1.100011)_{2}\times 2^{3}}

Finally we can see that:(12.375)10=(1.100011)2×23{\displaystyle (12.375)_{10}=(1.100011)_{2}\times 2^{3}}

From which we deduce:

From these we can form the resulting 32-bit IEEE 754 binary32 format representation of 12.375:

(12.375)10=(0 10000010 10001100000000000000000)2=(41460000)16{\displaystyle (12.375)_{10}=(0\ 10000010\ 10001100000000000000000)_{2}=(41460000)_{16}}

Note: consider converting 68.123 into IEEE 754 binary32 format: Using the above procedure you expect to get(42883EF9)16{\displaystyle ({\text{42883EF9}})_{16}} with the last 4 bits being 1001. However, due to the default rounding behaviour of IEEE 754 format, what you get is(42883EFA)16{\displaystyle ({\text{42883EFA}})_{16}}, whose last 4 bits are 1010.

Example 1:Consider decimal 1. We can see that:(1)10=(1.0)2×20{\displaystyle (1)_{10}=(1.0)_{2}\times 2^{0}}

From which we deduce:

From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 1:

(1)10=(0 01111111 00000000000000000000000)2=(3F800000)16{\displaystyle (1)_{10}=(0\ 01111111\ 00000000000000000000000)_{2}=({\text{3F800000}})_{16}}

Example 2:Consider a value 0.25. We can see that:(0.25)10=(1.0)2×22{\displaystyle (0.25)_{10}=(1.0)_{2}\times 2^{-2}}

From which we deduce:

From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 0.25:

(0.25)10=(0 01111101 00000000000000000000000)2=(3E800000)16{\displaystyle (0.25)_{10}=(0\ 01111101\ 00000000000000000000000)_{2}=({\text{3E800000}})_{16}}

Example 3:Consider a value of 0.375. We saw that0.375=(0.011)2=(1.1)2×22{\displaystyle 0.375={(0.011)_{2}}={(1.1)_{2}}\times 2^{-2}}

Hence after determining a representation of 0.375 as(1.1)2×22{\displaystyle {(1.1)_{2}}\times 2^{-2}} we can proceed as above:

From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 0.375:

(0.375)10=(0 01111101 10000000000000000000000)2=(3EC00000)16{\displaystyle (0.375)_{10}=(0\ 01111101\ 10000000000000000000000)_{2}=({\text{3EC00000}})_{16}}

Converting binary32 to decimal

[edit]
This sectionpossibly containsoriginal research. Pleaseimprove it byverifying the claims made and addinginline citations. Statements consisting only of original research should be removed.(February 2020) (Learn how and when to remove this message)
This sectionmay beconfusing or unclear to readers. In particular, there is only a very simple example, without rounding. This section is also probably off-topic: this is not an article about conversion, and conversion to decimal, using decimal arithmetic, is uncommon. Please helpclarify the section. There might be a discussion about this onthe talk page.(February 2020) (Learn how and when to remove this message)

If the binary32 value,41C80000 in this example, is in hexadecimal we first convert it to binary:

41C8 000016=0100 0001 1100 1000 0000 0000 0000 00002{\displaystyle {\text{41C8 0000}}_{16}=0100\ 0001\ 1100\ 1000\ 0000\ 0000\ 0000\ 0000_{2}}

then we break it down into three parts: sign bit, exponent, and significand.

We then add the implicit 24th bit to the significand:

and decode the exponent value by subtracting 127:

Each of the 24 bits of the significand (including the implicit 24th bit), bit 23 to bit 0, represents a value, starting at 1 and halves for each bit, as follows:

bit 23 = 1bit 22 = 0.5bit 21 = 0.25bit 20 = 0.125bit 19 = 0.0625bit 18 = 0.03125bit 17 = 0.015625..bit 6 = 0.00000762939453125bit 5 = 0.000003814697265625bit 4 = 0.0000019073486328125bit 3 = 0.00000095367431640625bit 2 = 0.000000476837158203125bit 1 = 0.0000002384185791015625bit 0 = 0.00000011920928955078125

The significand in this example has three bits set: bit 23, bit 22, and bit 19. We can now decode the significand by adding the values represented by these bits.

Then we need to multiply with the base, 2, to the power of the exponent, to get the final result:

1.5625×24=25{\displaystyle 1.5625\times 2^{4}=25}

Thus

41C8 0000=25{\displaystyle {\text{41C8 0000}}=25}

This is equivalent to:

n=(1)s×(1+m223)×2x127{\displaystyle n=(-1)^{s}\times (1+m*2^{-23})\times 2^{x-127}}

wheres is the sign bit,x is the exponent, andm is the significand.

Precision limitations on decimal values (between 1 and 16777216)

[edit]
  • Decimals between 1 and 2: fixed interval 2−23 (1+2−23 is the next largest float after 1)
  • Decimals between 2 and 4: fixed interval 2−22
  • Decimals between 4 and 8: fixed interval 2−21
  • ...
  • Decimals between 2n and 2n+1: fixed interval 2n−23
  • ...
  • Decimals between 222=4194304 and 223=8388608: fixed interval 2−1=0.5
  • Decimals between 223=8388608 and 224=16777216: fixed interval 20=1

Precision limitations on integer values

[edit]
  • Integers between 0 and 16777216 can be exactly represented (also applies for negative integers between −16777216 and 0)
  • Integers between 224=16777216 and 225=33554432 round to a multiple of 2 (even number)
  • Integers between 225 and 226 round to a multiple of 4
  • ...
  • Integers between 2n and 2n+1 round to a multiple of 2n−23
  • ...
  • Integers between 2127 and 2128 round to a multiple of 2104
  • Integers greater than or equal to 2128 are rounded to "infinity".

Notable single-precision cases

[edit]

These examples are given in bitrepresentation, inhexadecimal andbinary, of the floating-point value. This includes the sign, (biased) exponent, and significand.

0 00000000 000000000000000000000012 = 0000 000116 = 2−126 × 2−23 = 2−149 ≈ 1.4012984643 × 10−45
                                      (smallest positive subnormal number)

0 00000000 111111111111111111111112 = 007f ffff16 = 2−126 × (1 − 2−23) ≈ 1.1754942107 × 10−38
                                      (largest subnormal number)

0 00000001 000000000000000000000002 = 0080 000016 = 2−126 ≈ 1.1754943508 × 10−38
                                      (smallest positive normal number)

0 11111110 111111111111111111111112 = 7f7f ffff16 = 2127 × (2 − 2−23) ≈ 3.4028234664 × 1038
                                      (largest normal number)

0 01111110 111111111111111111111112 = 3f7f ffff16 = 1 − 2−24 ≈ 0.999999940395355225
                                      (largest number less than one)

0 01111111 000000000000000000000002 = 3f80 000016 = 1 (one)

0 01111111 000000000000000000000012 = 3f80 000116 = 1 + 2−23 ≈ 1.00000011920928955
                                      (smallest number larger than one)

1 10000000 000000000000000000000002 = c000 000016 = −2
0 00000000 000000000000000000000002 = 0000 000016 = 0
1 00000000 000000000000000000000002 = 8000 000016 = −0

0 11111111 000000000000000000000002 = 7f80 000016 = infinity
1 11111111 000000000000000000000002 = ff80 000016 = −infinity

0 01111101 010101010101010101010112 = 3eaa aaab16 ≈ 0.333333343267440796 ≈ 1/3
0 10000000 100100100001111110110112 = 4049 0fdb16 ≈ 3.14159274101257324 ≈ π (pi)

x 11111111 100000000000000000000012 = ffc0 000116 = qNaN (on x86 and ARM processors)
x 11111111 000000000000000000000012 = ff80 000116 = sNaN (on x86 and ARM processors)

By default, 1/3 rounds up, instead of down likedouble-precision, because of the even number of bits in the significand. The bits of 1/3 beyond the rounding point are1010... which is more than 1/2 of aunit in the last place.

Encodings of qNaN and sNaN are not specified inIEEE 754 and implemented differently on different processors. Thex86 family and theARM family processors use the most significant bit of the significand field to indicate aquiet NaN. ThePA-RISC processors use the bit to indicate asignaling NaN.

Optimizations

[edit]

The design of floating-point format allows various optimisations, resulting from the easy generation of abase-2 logarithm approximation from an integer view of the raw bit pattern. Integer arithmetic and bit-shifting can yield an approximation toreciprocal square root (fast inverse square root), commonly required incomputer graphics.

See also

[edit]

References

[edit]
  1. ^"REAL Statement".scc.ustc.edu.cn. Archived fromthe original on 2021-02-24. Retrieved2013-02-28.
  2. ^"CLHS: Type SHORT-FLOAT, SINGLE-FLOAT, DOUBLE-FLOAT..."www.lispworks.com.
  3. ^"Primitive Data Types".Java Documentation.
  4. ^"6 Predefined Types and Classes".haskell.org. 20 July 2010.
  5. ^"Float".Apple Developer Documentation.
  6. ^William Kahan (1 October 1997)."Lecture Notes on the Status of IEEE Standard 754 for Binary Floating-Point Arithmetic"(PDF). p. 4. Archived fromthe original(PDF) on 8 February 2012.

External links

[edit]
Uninterpreted
Numeric
Pointer
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Single-precision_floating-point_format&oldid=1310757563"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp