Can anyone explain representation of float in memory?

Can anyone help me in understanding how float values are stored in the memory.

My doubt is here float values contain ‘.' (for example 3.45) how the '.' will be represented in the memory?

Can anyone please clarify me with a diagram?

6

The decimal point is not explicitly stored anywhere; that’s a display issue.

The following explanation is a simplification; I’m leaving out a lot of important details and my examples aren’t meant to represent any real-world platform. It should give you a flavor of how floating-point values are represented in memory and the issues associated with them, but you will want to find more authoritative sources like What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Start by representing a floating-point value in a variant of scientific notation, using base 2 instead of base 10. For example, the value 3.14159 can be represented as

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0.7853975 * 2<sup>2</sup>
0.7853975 * 2<sup>2</sup>
    0.7853975 * 22

0.7853975 is the significand, a.k.a. the mantissa; it’s the part of the number containing the significant digits. This value is multiplied by the base 2 raised to the power of 2 to get 3.14159.

Floating-point numbers are encoded by storing the significand and the exponent (along with a sign bit).

A typical 32-bit layout looks something like the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 3 32222222 22211111111110000000000
1 09876543 21098765432109876543210
+-+--------+-----------------------+
| | | |
+-+--------+-----------------------+
^ ^ ^
| | |
| | +-- significand
| |
| +------------------- exponent
|
+------------------------ sign bit
</code>
<code> 3 32222222 22211111111110000000000 1 09876543 21098765432109876543210 +-+--------+-----------------------+ | | | | +-+--------+-----------------------+ ^ ^ ^ | | | | | +-- significand | | | +------------------- exponent | +------------------------ sign bit </code>
 3 32222222 22211111111110000000000
 1 09876543 21098765432109876543210
+-+--------+-----------------------+
| |        |                       |
+-+--------+-----------------------+
 ^    ^                ^
 |    |                |
 |    |                +-- significand 
 |    |
 |    +------------------- exponent 
 |
 +------------------------ sign bit

Like signed integer types, the high-order bit indicates sign; 0 indicates a positive value, 1 indicates negative.

The next 8 bits are used for the exponent. Exponents can be positive or negative, but instead of reserving another sign bit, they’re encoded such that 10000000 represents 0, so 00000000 represents -128 and 11111111 represents 127.

The remaining bits are used for the significand. Each bit represents a negative power of 2 counting from the left, so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
01101 = 0 * 2<sup>-1</sup> + 1 * 2<sup>-2</sup> + 1 * 2<sup>-3</sup> + 0 * 2<sup>-4</sup> + 1 * 2<sup>-5</sup>
= 0.25 + 0.125 + 0.03125
= 0.40625
01101 = 0 * 2<sup>-1</sup> + 1 * 2<sup>-2</sup> + 1 * 2<sup>-3</sup> + 0 * 2<sup>-4</sup> + 1 * 2<sup>-5</sup> = 0.25 + 0.125 + 0.03125 = 0.40625
    01101 = 0 * 2-1 + 1 * 2-2 + 1 * 2-3 + 0 * 2-4 + 1 * 2-5 
          = 0.25 + 0.125 + 0.03125 
          = 0.40625

Some platforms assume a “hidden” leading bit in the significand that’s always set to 1, so values in the significand are always between [0.5, 1). This allows these platforms to store values with a slightly greater precision (more on that below). My example doesn’t do this.

So our value of 3.14159 would be represented as something like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0 10000010 11001001000011111100111
^ ^ ^
| | |
| | +--- significand = 0.7853975...
| |
| +------------------- exponent = 2 (130 - 128)
|
+------------------------- sign = 0 (positive)
value= -1<sup>(sign)</sup> * 2<sup>(exponent)</sup> * (significand)
value= -1<sup>0</sup> * 2<sup>2</sup> * 0.7853975...
value= 3.14159...
0 10000010 11001001000011111100111 ^ ^ ^ | | | | | +--- significand = 0.7853975... | | | +------------------- exponent = 2 (130 - 128) | +------------------------- sign = 0 (positive) value= -1<sup>(sign)</sup> * 2<sup>(exponent)</sup> * (significand) value= -1<sup>0</sup> * 2<sup>2</sup> * 0.7853975... value= 3.14159...
    0 10000010 11001001000011111100111
    ^     ^               ^
    |     |               |
    |     |               +--- significand = 0.7853975...
    |     |
    |     +------------------- exponent = 2 (130 - 128)
    |
    +------------------------- sign = 0 (positive)

    value= -1(sign) * 2(exponent) * (significand)
    value= -10 * 22 * 0.7853975...
    value= 3.14159...

Now, something you’ll notice if you add up all the bits in the significand is that they don’t total 0.7853975; they actually come out to 0.78539747. There aren’t quite enough bits to store the value exactly; we can only store an approximation. The number of bits in the significand determines the precision, or how many significant digits you can store. 23 bits gives us roughly 6 decimal digits of precision. 64-bit floating point types offer enough bits in the significand to give roughly 12 to 15 digits of precision. But be aware that there are values that cannot be represented exactly no matter how many bits you use. Just as values like 1/3 cannot be represented in a finite number of decimal digits, values like 1/10 cannot be represented in a finite number of bits. Since values are approximate, calculations with them are also approximate, and rounding errors accumulate.

The number of bits in the exponent determines the range (the minimum and maximum values you can represent). But as you move towards your minimum and maximum values, the size of the gap between representable values increases. That is, if you can’t exactly represent values between 0.785397 and 0.785398, then you can’t exactly represent values between 7.85397 and 7.85398 either, or values between 78.5397 and 78.5398, or values between 785397.0 and 785398.0. Be careful when multiplying very large (in terms of magnitude) numbers by very small numbers.

1

The . is not stored at all. First, you should understand engineering notation, which has a fixed-precision factor and an integer exponent: 1 is 1.0 · 100 = 1.0E0, 2 is 2.0E0, 10 is 1.0E1 etc. This allows for very short notation of large numbers. One billion is 1.0E9. The factor before the E is usually notated as a fixed-precision number: 1.00000E9. A result from this is that the number one billion and one = 1,000,000,001 and one billion are both the same in this notation, when the precision isn’t large enough. Also note that the factor never needs a leading zero. Instead, the exponent can be decremented until that is no longer the case.

In memory, a floating point number is represented similarly: One bit has the sign, some bits form the factor as a fixed-precision number (“mantissa”), the remaining bits form the exponent. Significant differences to base-10 engineering notation is that of course now the exponent has base 2. The exact size of each part depends on the exact floating-point standard you are using.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật