HOME

TheInfoList



OR:

Single-precision floating-point format (sometimes called FP32 or float32) is a
computer number format A computer number format is the internal representation of numeric values in digital device hardware and software, such as in programmable computers and calculators. Numerical values are stored as groupings of bits, such as bytes and words. The ...
, usually occupying 32 bits in
computer memory In computing, memory is a device or system that is used to store information for immediate use in a computer or related computer hardware and digital electronic devices. The term ''memory'' is often synonymous with the term '' primary storag ...
; it represents a wide
dynamic range Dynamic range (abbreviated DR, DNR, or DYR) is the ratio between the largest and smallest values that a certain quantity can assume. It is often used in the context of signals, like sound and light. It is measured either as a ratio or as a base ...
of numeric values by using a floating radix point. A floating-point variable can represent a wider range of numbers than a fixed-point variable of the same bit width at the cost of precision. A signed 32-bit
integer An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the languag ...
variable has a maximum value of 231 − 1 = 2,147,483,647, whereas an
IEEE 754 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found ...
32-bit base-2 floating-point variable has a maximum value of (2 − 2−23) × 2127 ≈ 3.4028235 × 1038. All integers with 7 or fewer decimal digits, and any 2''n'' for a whole number −149 ≤ ''n'' ≤ 127, can be converted exactly into an IEEE 754 single-precision floating-point value. In the
IEEE 754-2008 The Institute of Electrical and Electronics Engineers (IEEE) is a 501(c)(3) professional association for electronic engineering and electrical engineering (and associated disciplines) with its corporate office in New York City and its operation ...
standard Standard may refer to: Symbols * Colours, standards and guidons, kinds of military signs * Standard (emblem), a type of a large symbol or emblem used for identification Norms, conventions or requirements * Standard (metrology), an object th ...
, the 32-bit base-2 format is officially referred to as binary32; it was called single in
IEEE 754-1985 IEEE 754-1985 was an industry standard for representing floating-point numbers in computers, officially adopted in 1985 and superseded in 2008 by IEEE 754-2008, and then again in 2019 by minor revision IEEE 754-2019. During its 23 years, it was th ...
. IEEE 754 specifies additional floating-point types, such as 64-bit base-2 ''
double precision Double-precision floating-point format (sometimes called FP64 or float64) is a floating-point number format, usually occupying 64 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point. Flo ...
'' and, more recently, base-10 representations. One of the first
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s to provide single- and double-precision floating-point data types was Fortran. Before the widespread adoption of IEEE 754-1985, the representation and properties of floating-point data types depended on the
computer manufacturer Current notable computer hardware manufacturers: Cases List of computer case manufacturers: * Aigo * Antec * AOpen * ASRock * Asus * be quiet! * CaseLabs (defunct) * Chassis Plans * Cooler Master * Corsair * Dell * Deepcool * DFI * ...
and computer model, and upon decisions made by programming-language designers. E.g.,
GW-BASIC GW-BASIC is a dialect of the BASIC programming language developed by Microsoft from IBM BASICA. Functionally identical to BASICA, its BASIC interpreter is a fully self-contained executable and does not need the Cassette BASIC ROM found in the ...
's single-precision data type was the
32-bit MBF 3 (three) is a number, numeral and digit. It is the natural number following 2 and preceding 4, and is the smallest odd prime number and the only prime preceding a square number. It has religious or cultural significance in many societie ...
floating-point format. Single precision is termed ''REAL'' in Fortran, ''SINGLE-FLOAT'' in
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fr ...
, ''float'' in C,
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, C#,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, ''Float'' in
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
and
Swift Swift or SWIFT most commonly refers to: * SWIFT, an international organization facilitating transactions between banks ** SWIFT code * Swift (programming language) * Swift (bird), a family of birds It may also refer to: Organizations * SWIFT, ...
, and ''Single'' in
Object Pascal Object Pascal is an extension to the programming language Pascal that provides object-oriented programming (OOP) features such as classes and methods. The language was originally developed by Apple Computer as ''Clascal'' for the Lisa Worksh ...
(
Delphi Delphi (; ), in legend previously called Pytho (Πυθώ), in ancient times was a sacred precinct that served as the seat of Pythia, the major oracle who was consulted about important decisions throughout the ancient classical world. The orac ...
),
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic .NET (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic ( ...
, and
MATLAB MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementat ...
. However, ''float'' in Python,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, PHP, and
OCaml OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. ...
and ''single'' in versions of
Octave In music, an octave ( la, octavus: eighth) or perfect octave (sometimes called the diapason) is the interval between one musical pitch and another with double its frequency. The octave relationship is a natural phenomenon that has been refer ...
before 3.2 refer to
double-precision Double-precision floating-point format (sometimes called FP64 or float64) is a floating-point number format, usually occupying 64 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point. F ...
numbers. In most implementations of
PostScript PostScript (PS) is a page description language in the electronic publishing and desktop publishing realm. It is a dynamically typed, concatenative programming language. It was created at Adobe Systems by John Warnock, Charles Geschke, Do ...
, and some
embedded systems An embedded system is a computer system—a combination of a computer processor, computer memory, and input/output peripheral devices—that has a dedicated function within a larger mechanical or electronic system. It is ''embedded'' ...
, the only supported precision is single.


IEEE 754 standard: binary32

The IEEE 754 standard specifies a ''binary32'' as having: *
Sign bit In computer science, the sign bit is a bit in a signed number representation that indicates the sign of a number. Although only signed numeric data types have a sign bit, it is invariably located in the most significant bit position, so the term ...
: 1 bit *
Exponent Exponentiation is a mathematical operation, written as , involving two numbers, the '' base'' and the ''exponent'' or ''power'' , and pronounced as " (raised) to the (power of) ". When is a positive integer, exponentiation corresponds to r ...
width: 8 bits *
Significand The significand (also mantissa or coefficient, sometimes also argument, or ambiguously fraction or characteristic) is part of a number in scientific notation or in floating-point representation, consisting of its significant digits. Depending on ...
precision: 24 bits (23 explicitly stored) This gives from 6 to 9 significant decimal digits precision. If a decimal string with at most 6 significant digits is converted to the IEEE 754 single-precision format, giving a normal 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. The sign bit determines the sign of the number, which is the sign of the significand as well. The exponent is an 8-bit unsigned integer from 0 to 255, in biased form: an exponent value of 127 represents the actual zero. Exponents range from −126 to +127 because exponents of −127 (all 0s) and +128 (all 1s) are reserved for special numbers. The true significand includes 23 fraction bits to the right of the binary point and an ''implicit leading bit'' (to the left of the binary point) with value 1, unless the exponent is stored with all zeros. Thus only 23 fraction bits of the
significand The significand (also mantissa or coefficient, sometimes also argument, or ambiguously fraction or characteristic) is part of a number in scientific notation or in floating-point representation, consisting of its significant digits. Depending on ...
appear in the memory format, but the total precision is 24 bits (equivalent to log10(224) ≈ 7.225 decimal digits). The bits are laid out as follows: The real value assumed by a given 32-bit ''binary32'' data with a given ''sign'', biased exponent ''e'' (the 8-bit unsigned integer), and a ''23-bit fraction'' is : (-1)^ \times 2^ \times (1.b_b_ \dots b_0)_2, which yields : \text = (-1)^\text \times 2^ \times \left(1 + \sum_^ b_ 2^ \right). In this example: * \text = b_ = 0, * (-1)^\text = (-1)^ = +1 \in \, * E = (b_b_ \dots b_)_2 = \sum_^ b_ 2^ = 124 \in \ = \, * 2^ = 2^ = 2^ \in \ , * 1.b_b_...b_ = 1 + \sum_^ b_ 2^ = 1 + 1\cdot 2^ = 1.25 \in \ \subset ; 2 - 2^ \subset
offset-binary Offset binary, also referred to as excess-K, excess-''N'', excess-e, excess code or biased representation, is a method for signed number representation where a signed number n is represented by the bit pattern corresponding to the unsigned numb ...
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. The minimum positive normal value is 2^ \approx 1.18 \times 10^ and the minimum positive (subnormal) value is 2^ \approx 1.4 \times 10^.


Converting decimal to binary32

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 and normalize the integer part into binary * 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 \times 2 = 0.750 = 0 + 0.750 \Rightarrow b_ = 0, the integer part represents the binary fraction digit. Re-multiply 0.750 by 2 to proceed : 0.750 \times 2 = 1.500 = 1 + 0.500 \Rightarrow b_ = 1 : 0.500 \times 2 = 1.000 = 1 + 0.000 \Rightarrow b_ = 1, fraction = 0.011, terminate We see that (0.375)_ can be exactly represented in binary as (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)_ = (12)_ + (0.375)_ = (1100)_2 + (0.011)_2 = (1100.011)_2 Since IEEE 754 binary32 format requires real values to be represented in (1.x_1x_2...x_)_2 \times 2^ format (see Normalized number, Denormalized number), 1100.011 is shifted to the right by 3 digits to become (1.100011)_2 \times 2^ Finally we can see that: (12.375)_ = (1.100011)_2 \times 2^ From which we deduce: * The exponent is 3 (and in the biased form it is therefore (127+3)_ = (130)_ = (1000\ 0010)_ * The fraction is 100011 (looking to the right of the binary point) From these we can form the resulting 32-bit IEEE 754 binary32 format representation of 12.375: : (12.375)_ = (0\ 10000010\ 10001100000000000000000)_ = (41460000)_ Note: consider converting 68.123 into IEEE 754 binary32 format: Using the above procedure you expect to get (\text)_ with the last 4 bits being 1001. However, due to the default rounding behaviour of IEEE 754 format, what you get is (\text)_, whose last 4 bits are 1010. Example 1: Consider decimal 1. We can see that: (1)_ =(1.0)_2 \times 2^ From which we deduce: * The exponent is 0 (and in the biased form it is therefore (127+0)_=(127)_ = (0111\ 1111)_ * The fraction is 0 (looking to the right of the binary point in 1.0 is all 0 = 000...0) From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 1: : (1)_ = (0\ 01111111\ 00000000000000000000000)_ = (\text)_ Example 2: Consider a value 0.25. We can see that: (0.25)_ =(1.0)_2 \times 2^ From which we deduce: * The exponent is −2 (and in the biased form it is (127+(-2))_ = (125)_ = (0111\ 1101)_) * The fraction is 0 (looking to the right of binary point in 1.0 is all zeroes) From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 0.25: : (0.25)_ = (0\ 01111101\ 00000000000000000000000)_ = (\text)_ Example 3: Consider a value of 0.375. We saw that 0.375 = = \times 2^ Hence after determining a representation of 0.375 as \times 2^ we can proceed as above: * The exponent is −2 (and in the biased form it is (127+(-2))_ = (125)_ = (0111\ 1101)_) * The fraction is 1 (looking to the right of binary point in 1.1 is a single 1 = x_1) From these we can form the resulting 32-bit IEEE 754 binary32 format representation of real number 0.375: : (0.375)_ = (0\ 01111101\ 10000000000000000000000)_ = (\text)_


Converting binary32 to decimal

If the binary32 value, in this example, is in hexadecimal we first convert it to binary: : \text_ = 0100\ 0001\ 1100\ 1000\ 0000\ 0000\ 0000\ 0000_ then we break it down into three parts: sign bit, exponent, and significand. * Sign bit: 0_2 * Exponent: 1000\ 0011_2 = 83_ = 131_ * Significand: 100\ 1000\ 0000\ 0000\ 0000\ 0000_2 = 480000_ We then add the implicit 24th bit to the significand: * Significand: \mathbf100\ 1000\ 0000\ 0000\ 0000\ 0000_2 = \text_ and decode the exponent value by subtracting 127: * Raw exponent: 83_ = 131_ * Decoded exponent: 131 - 127 = 4 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 = 1 bit 22 = 0.5 bit 21 = 0.25 bit 20 = 0.125 bit 19 = 0.0625 bit 18 = 0.03125 . . bit 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. * Decoded significand: 1 + 0.5 + 0.0625 = 1.5625 = \text/2^ Then we need to multiply with the base, 2, to the power of the exponent, to get the final result: : 1.5625 \times 2^4 = 25 Thus : \text = 25 This is equivalent to: : n = (-1)^s \times (1+m*2^)\times 2^ where is the sign bit, is the exponent, and is the significand.


Precision limitations on decimal values (between 1 and 16777216)

* 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

* 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

These examples are given in bit ''representation'', in
hexadecimal In mathematics and computing, the hexadecimal (also base-16 or simply hex) numeral system is a positional numeral system that represents numbers using a radix (base) of 16. Unlike the decimal system representing numbers using 10 symbols, he ...
and binary, 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 10000000 100100100001111110110112 = 4049 0fdb16 ≈ 3.14159274101257324 ≈ π ( pi ) 0 01111101 010101010101010101010112 = 3eaa aaab16 ≈ 0.333333343267440796 ≈ 1/3 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 like
double precision Double-precision floating-point format (sometimes called FP64 or float64) is a floating-point number format, usually occupying 64 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point. Flo ...
, because of the even number of bits in the significand. The bits of 1/3 beyond the rounding point are 1010... which is more than 1/2 of a
unit in the last place In computer science and numerical analysis, unit in the last place or unit of least precision (ulp) is the spacing between two consecutive floating-point numbers, i.e., the value the least significant digit (rightmost digit) represents if it is 1 ...
. Encodings of qNaN and sNaN are not specified in
IEEE 754 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found ...
and implemented differently on different processors. The x86 family and the ARM family processors use the most significant bit of the significand field to indicate a quiet NaN. The
PA-RISC PA-RISC is an instruction set architecture (ISA) developed by Hewlett-Packard. As the name implies, it is a reduced instruction set computer (RISC) architecture, where the PA stands for Precision Architecture. The design is also referred to as ...
processors use the bit to indicate a signalling NaN.


Optimizations

The design of floating-point format allows various optimisations, resulting from the easy generation of a base-2 logarithm approximation from an integer view of the raw bit pattern. Integer arithmetic and bit-shifting can yield an approximation to reciprocal square root ( fast inverse square root), commonly required in
computer graphics Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great de ...
.


See also

*
IEEE 754 The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found ...
* ISO/IEC 10967, language independent arithmetic *
Primitive data type In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled pr ...
*
Numerical stability In the mathematical subfield of numerical analysis, numerical stability is a generally desirable property of numerical algorithms. The precise definition of stability depends on the context. One is numerical linear algebra and the other is algori ...
*
Scientific notation Scientific notation is a way of expressing numbers that are too large or too small (usually would result in a long string of digits) to be conveniently written in decimal form. It may be referred to as scientific form or standard index form, o ...


References


External links


Live floating-point bit pattern editor





C source code to convert between IEEE double, single, and half precision
{{data types Binary arithmetic Computer arithmetic Floating point types