IEEE 754 single-precision binary floating-point format: binary32
No 3200 byte textual header and no extended textual headers.
No binary header.
The data must be formatted as IEEE.
The IEEE 754 standard specifies a binary32 as having:
- Sign bit: 1 bit 符号位,0表示正,1表示负,通常用s标识
- Exponent width: 8 bits 幂数,通常用e表示其数值
- Significand precision: 24 bits (23 explicitly stored) 二进制纯小数,通常用x标识
In this example:
,
,
,
,
.
thus:
.
3、Converting from decimal representation to binary32 format
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 re-multiply 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 x 2 = 0.750 = 0 + 0.750 => b−1 = 0, the integer part represents the binary fraction digit. Re-multiply 0.750 by 2 to proceed
0.750 x 2 = 1.500 = 1 + 0.500 => b−2 = 1
0.500 x 2 = 1.000 = 1 + 0.000 => b−3 = 1, fraction = 0.000, terminate
We see that (0.375)10 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. So it is only approximated.
Therefore, (12.375)10 = (12)10 + (0.375)10 = (1100)2 + (0.011)2 = (1100.011)2
From which we deduce:
- The exponent is 3 (and in the biased form it is therefore 130 = 1000 0010) 二进制小数(1100.011)左移3位 ,3+127=130=e
- The fraction is 100011 (looking to the right of the binary point) 二进制小数左移3位后为(1.100011)整数部分减1为(0.100011),即x
- 所以s=0,e=十进制130=二进制(1000 0010),x=二进制(100011)
- (12.375)10= (0 1000 0010 1000 1100 0000 0000 0000 000)2
4、舍入误差的影响
Note: consider converting 68.123 into IEEE 754 binary32 format: Using the above procedure you expect to get 42883EF9 H with the last 4 bits being 1001. However, due to the default rounding behaviour of IEEE 754 format, what you get is 42883EFA H , whose last 4 bits are 1010.
Precision limits on integer values[edit]
- Integers in
can be exactly represented
- Integers in
or in
round to a multiple of 2
- Integers in
or in
round to a multiple of 4
- ....
- Integers in
or in
round to a multiple of
- Integers in
or in
round to a multiple of
- Integers larger than or equal to
or smaller than or equal to
are rounded to "infinity".