Value range and precision of float in C #

Source: Internet
Author: User

Original: Value range and precision of float in C #

Expression of type float:

By default, the real number to the right of the assignment operator is treated as double. Therefore, the floating-point variable should be initialized with the suffix F or f, as shown in the following example:

float x = 3.5F;

If you do not use a suffix in the above declaration, a compilation error occurs because you try to store a double value in a float variable.

Value range of float

Float occupies 4 bytes, which is the same as int, which is 32bit.

1bit (sign bit) 8bits (digit digit) 23bits (trailing digit)

Storage methods such as:

The basic expression method of the range of values

(floating point) value = mantissa x base ^ exponent, (plus sign)----------------

Thus, the index range of float is -127~128, and the double index range is -1023~1024, and the digits are divided in the form of complement. The negative exponent determines the minimum number of absolute values that the floating point can express, and the positive exponent determines the maximum number of absolute values that the floating point can express, which determines the range of the floating-point numbers.

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.

Other special representations

1. When the exponential and fractional portions are all 0 o'clock, representing 0 values, +0 and 0 (the symbol bit is determined), and 0x00000000 indicates that positive 0,0x80000000 represents negative 0. 2. The index portion is 1, the fractional part is full 0 o'clock, which indicates infinity, positive infinity and negative infinity, 0x7f800000 represents positive infinity, and 0xff800000 represents negative infinity. 3. The index portion is full 1, fractional part of the 0 o'clock, indicating nan, divided into Qnan and Snan,java are both Nan.

Conclusion: It can be seen that the value range of floating-point number is: 2^ (-149) ~ ~ (2-2^ (-23)) *2^127, namely Float.min_value and Float.max_value.

Precision

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, which means that there can be up to 7 valid digits, but the absolute guarantee of 6-bit, that is, the precision of float is 6~7 bit valid number;

Value range and precision of float in C #

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.