Understanding of float, double, long double precision and numerical range in C language

Source: Internet
Author: User

IEEE754 the representation of floating-point numbers. The expression range of float type data in C language is -3.4*10^38~+3.4*10^38. Double is -1.7*10^-308~1.7*10^308,long double for -1.2*10^-4932~1.2*10^4932.

Type

Number of bits (bits)

Valid numbers

Range of values

Float

32

6~7

-3.4*10^38~+3.4*10^38

Double

64

15~16

-1.7*10^-308~1.7*10^308

Long double

128/

18~19

-1.2*10^-4932~1.2*10^4932

Exactly how the range is calculated is analyzed as follows:

For single-precision floating-point numbers (float), the sign bit one, which refers to the digit 8 bits, the mantissa 23 bits. The index can be expressed in the range of -128~127. The mantissa is 23 bits.

The precision of float and double is determined by the number of bits in the mantissa. Floating-point numbers are stored in memory by scientific notation, and the integer part is always an implied "1", because it is invariant and therefore cannot affect precision. float:2^23 = 8388608, a total of seven bits, which means that there can be up to 7 valid digits, but it is guaranteed to be 6 bits, that is, the precision of float is 6~7 bit valid number; double:2^52 = 4503599627370496, altogether 16 bits, The precision of the double is 15~16 bit.

The negative exponent determines the absolute minimum non-zero that the floating point can express, and the positive exponent determines the maximum number of absolute value that the floating point can express, which determines the range of the floating-point value. The range of float is -2^128 ~ +2^128, that is, the range of -3.40E+38 ~ +3.40e+38;double is -2^1024 ~ +2^1024, or -1.79E+308 ~+1.79e+308.

Take float for example, as shown in the table below

Symbol

Tail

Index

1

23

8

Number (+ +)

Decimal parts (Determination of accuracy)

-127~128 Index (decision Range)

For example:

+1.1111111111111111111111*2^127 (23 1 after the decimal point, because the range of the mantissa is one or two, its highest bit is always 1, so just access the fractional part, so the decimal is 23 bits 1), approximately equal to 2*2^127=3.4*10^38. The 3.4*10^38 negative number is the same.

A double is similar to this, with a double with a sign bit of 63 bits and an exponent of 62~52 bits and a total of 11 bits. The range represented is -1024~1023. The mantissa is 51~0. The range represented is +1.1111111111111111..11111*2^1023 (52 1 after the decimal point) to 1.7*10^308. Negative numbers are the same.

Understanding of float, double, long double precision and numerical range in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.