Floating Point Math >Single precision 32-bit floating point value Format: SEEE EEEE EMMM MMMM MMMM MMMM MMMM MMMM S=sign E=exponent M=mantissa Sign bit is 0 for positive, 1 for negative. Exponent is biased by 127. Power Bit pattern Decimal value 0 01111111 127 +1 10000000 128 -1 01111110 126 +127 11111110 254 -126 00000001 1 Special conditons: Sign Exponent Mantissa Meaning 0 All 0's All 0's +0 1 All 0's All 0's -0 0 All 0's Nonzero +Denormal 1 All 0's nonzero -Denormal 0 All 1's All 0's +Infinity 1 All 1's All 0's -Infinity 0 All 1's nonzero NaN 1 All 1's nonzero NaN Implied Mantissa bit: The Mantissa is 23 bits long with an implied bit left of the radix. The implied bit is 1 for almost all circumstances. The implied bit is 0 only if the number is denormal. Convert from decimal to binary floating point: 173.7 = b? Sign = 0 (positive) To get Exponent, repeatedly divide/multiply by two, while counting how many iterations, until there's there's only 1 to the left of the decimal point. Final result will be a normalised number. The number of iterations will be our exponent (positive for division, negative for multiplication). 173.7/2=86.85 2^1 86.85/2=43.425 2^2 43.425/2=21.7125 2^3 21.7125/2=10.85625 2^4 10.85625/2=5.428125 2^5 5.428125/2=2.7140625 2^6 2.7140625/2=1.35703125 2^7 Finished, power is 7. Power is 7. Our binary exponent is: 7+127=134=b1000_0110. To get our binary Mantissa (for normals), we multiply the fractional part of our normalised number by two and allocate a 1 if the result is greater than 1, and 0 otherwise. When we get a 1, we subtract 1 from the number. Normalised number into Mantissa: Number X2 Result Binary result 0.35703125 0.7140625 0 ;Since we didn't get a 1, we only copy the number from the x2 Result column to the Number ;column of the next row. 0.7140625 1.428125 1 ;Since we got a 1 here, when we copy the number over from the x2 Result column to the Number 0.428125 0.85625 0 ;column of the next row, we subtract 1. 0.85625 1.7125 1 0.7125 1.425 1 0.425 0.85 0 0.85 1.7 1 0.7 1.4 1 0.4 0.8 0 0.8 1.6 1 0.6 1.2 1 0.2 0.4 0 ;We've just got a number that we got before, meaning a recursive sequence. We can take a 0 ;shortcut and copy this sequence until we fill up the mantissa. 1 1 0 0 1 1 0 0 1 1 We now have our full floating point number: 0,100_0011_0,010_1101_1011_0011_0011_0011=0x432DB333 ;commas separate bit fields Now try a negative number: -0.75 Sign = 1 We can, from this point, act as if it's a positive number. Normalise number: 0.75*2=1.5 2^-1 Get exponent: 127-1 = 126 = 01111110 Normalised number, so implied bit is 1. Get Mantissa bit pattern: 0.5*2 = 1 ;Nothing left, so fill in rest of mantissa with 0. Our final floating point binary number is: 1,011_1111_0,100_0000_0000_0000_0000_0000=0xBF400000 >Double Precision 64-bit floating point value Format: SEEEEEEEEEEEMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM ;1 sign bit, 11 exponent bits, 52 mantissa bits Exponent has bias of 1,023. Rest of process is exactly the same. Practice: Convert Pi to single precision floating point 3.1415926535897932384... Sign is 0, since it's positive. Normalize and acquire power '' / 2 = 1.5707963267948966192313216916398 x 2^1 ;done Get bit pattern of exponent 127+1=128=10000000 ;done Get Mantissa pattern Number x2 Bit 0.5707963267948966192313216916398 1.1415926535897932384626433832795 1 0.1415926535897932384626433832795 0.283185307179586476925286766559 0 0.283185307179586476925286766559 0.566370614359172953850573533118 0 0.566370614359172953850573533118 1.132741228718345907701147066236 1 0.132741228718345907701147066236 0.265482457436691815402294132472 0 0.265482457436691815402294132472 0.530964914873383630804588264944 0 0.530964914873383630804588264944 1.061929829746767261609176529888 1 0.061929829746767261609176529888 0.123859659493534523218353059776 0 0.123859659493534523218353059776 0.247719318987069046436706119552 0 0.247719318987069046436706119552 0.495438637974138092873412239104 0 0.495438637974138092873412239104 0.990877275948276185746824478208 0 0.990877275948276185746824478208 1.981754551896552371493648956416 1 0.981754551896552371493648956416 1.963509103793104742987297912832 1 0.963509103793104742987297912832 1.927018207586209485974595825664 1 0.927018207586209485974595825664 1.854036415172418971949191651328 1 0.854036415172418971949191651328 1.708072830344837943898383302656 1 0.708072830344837943898383302656 1.416145660689675887796766605312 1 0.416145660689675887796766605312 0.832291321379351775593533210624 0 0.832291321379351775593533210624 1.664582642758703551187066421248 1 0.664582642758703551187066421248 1.329165285517407102374132842496 1 0.329165285517407102374132842496 0.658330571034814204748265684992 0 0.658330571034814204748265684992 1.316661142069628409496531369984 1 0.316661142069628409496531369984 0.633322284139256818993062739968 1 ;Final bit, next bit would be a 1, so we round up We now have our mantissa pattern: 10010010000111111011011 Concatenate them all together: 0,100_0000_0,100_1001_0000_1111_1101_1011=0x40490FDB Compare with Pi value from Propeller manual Pg. 93: 0x40490FDB