5. How FLOAT and DOUBLE Number Stored in Memory? | IEEE 754 Representation
By Concept && Coding - by Shrayansh
Share:
Key Concepts
- IEEE 754 Standard: Standard for representing floating-point numbers in computers.
- Sign Bit: Indicates the sign of the number (0 for positive, 1 for negative).
- Exponent: Represents the scale of the number.
- Mantissa (Significand): Represents the precision bits of the number.
- Bias: A value added to the exponent to allow representation of both positive and negative exponents without using a sign bit.
- Binary Conversion: Converting decimal numbers to their binary equivalents.
- Big Decimal: A class used for precise decimal arithmetic, avoiding the inaccuracies of float and double.
Float Representation in Memory (IEEE 754)
- Floats are stored as 32-bit values according to the IEEE 754 standard.
- The 32 bits are divided into three parts:
- Sign Bit (1 bit): 0 for positive, 1 for negative.
- Exponent (8 bits): Represents the power of 2 by which the mantissa is multiplied.
- Mantissa (23 bits): The fractional part of the number.
- Example: The video explains how the decimal number 4.125 is stored as a float.
Step-by-Step Conversion of 4.125 to IEEE 754 Float
- Convert to Binary:
- Convert the integer part (4) to binary: 100.
- Convert the fractional part (0.125) to binary: 0.001.
- Combine: 4.125 = 100.001 in binary.
- Normalize:
- Express the binary number in the form 1.xxxxx * 2^exponent.
- 100.001 = 1.00001 * 2^2.
- Add Bias to Exponent:
- The bias for floats is 127.
- Add the bias to the exponent: 2 + 127 = 129.
- Store in Memory:
- Sign Bit: 0 (positive).
- Exponent: Convert 129 to binary (8 bits): 10000001.
- Mantissa: The fractional part after the leading 1 (00001), padded with zeros to fill 23 bits: 00001000000000000000000.
Reverse Conversion (IEEE 754 Float to Decimal)
- The video explains how to convert a float back to its decimal representation.
- Formula:
(-1)^sign_bit * (1 + mantissa) * 2^(exponent - bias) - Example: Converting the stored representation of 4.125 back to decimal.
- Sign Bit: 0.
- Exponent: 129.
- Mantissa: 0.00001 (binary).
- Calculation:
(-1)^0 * (1 + 0.00001) * 2^(129 - 127)1 * (1 + 0.03125) * 2^21.03125 * 4 = 4.125
The Issue with 0.7f
- The video demonstrates why representing 0.7 as a float results in an approximate value (0.69999988...).
- Conversion to Binary: 0.7 in decimal is a repeating fraction in binary (0.101100110011...).
- Normalization: 1.01100110011... * 2^-1
- Storage: The mantissa has limited space (23 bits), so the repeating binary fraction is truncated, leading to an approximation.
- Reverse Conversion: When converting back, the truncated mantissa results in a decimal value close to, but not exactly, 0.7.
Step-by-Step Conversion of 0.7 to IEEE 754 Float and Back
- Convert to Binary:
- 0.7 = 0.101100110011... (repeating).
- Normalize:
- 0. 101100110011... = 1.01100110011... * 2^-1.
- Add Bias to Exponent:
- -1 + 127 = 126.
- Store in Memory:
- Sign Bit: 0 (positive).
- Exponent: Convert 126 to binary (8 bits): 01111110.
- Mantissa: The fractional part after the leading 1 (01100110011...), truncated to 23 bits.
- Reverse Conversion:
- Using the formula, the truncated mantissa and exponent result in a value close to 0.7, but not exact (approximately 0.69999988...).
Double Representation
- Doubles are stored as 64-bit values, providing higher precision than floats.
- Format:
- Sign Bit (1 bit)
- Exponent (11 bits)
- Mantissa (52 bits)
- Bias: The bias for doubles is 1023 (2^10 - 1).
- Doubles offer more precision but still suffer from approximation issues with certain decimal values.
Big Decimal as a Solution
- The video recommends using
BigDecimalin Java for precise decimal arithmetic. BigDecimalavoids the approximation issues offloatanddoubleby storing decimal numbers as exact values.- Example: Storing 4.7 as a
BigDecimalwill result in exactly 4.7, not an approximation.
Conclusion
The video provides a detailed explanation of how floating-point numbers (floats and doubles) are stored in memory according to the IEEE 754 standard. It highlights the approximation issues that can arise due to the limited precision of these data types and recommends using BigDecimal for precise decimal arithmetic when necessary. The step-by-step examples of converting decimal numbers to their IEEE 754 representation and back provide a clear understanding of the underlying principles.
Chat with this Video
AI-PoweredLoad the transcript when you're ready to chat so the initial page stays lighter.
Related Videos

Shocking video shows moment paramedics are hit by Israel in 'double-tap' strike
Sky News

Inside Jeffrey Epstein's Network of Power
Bloomberg Originals

Every Kind of Volcano | SciShow Kids
SciShow Kids

Pokemon goes prehistoric at Chicago's Field Museum
Reuters

Pokemon goes prehistoric at Chicago's Field Museum
Reuters

Trump's supporters furious over Trump smartphone scam.
ABC News In-depth

Throwing out the first pitch for the Rockies for STEM Day!
Sick Science!