简体   繁体   中英

For float and double, why it is asymmetric for the negative and positive numbers?

阅读MSDN,浮动范围从1E-45到1E38,从1E-324到1E308的两倍范围,我想知道为什么它对负和正指数不对称?

Your confusion stems from thinking that "1E-45" is a negative number. It is not - it is in fact a very small positive number: 1 x 10^-45 or rather, 0.000...0001 - which has 44 "0"s between the "0." and the "1"

This represents the minimum unit of accuracy that a float can store (or similar - the articles that other people have linked to will explain in detail, if you need to know).

The other number, "1E38" is an indication of the largest number that can be stored in this datatype. This is 1 x 10^38 or rather 10000...0000 ie 1 with 38 0s after it.

They're not positive and negative numbers - they're positive and negative exponents . The difference is due to the way that normalization works, basically. You end up being able to store "smaller" numbers because of denormal numbers and exponent biasing.

Ultimately, you basically don't need to worry about that - but you do need to understand that the range is the same for positive and negative numbers (there's just a sign bit).

All numeric ranges for 2's complement encoding are unbalanced by design due to the presence of 0. You see this back in the .NET framework, like int.MaxValue = 2,147,483,647, int.MinValue = -2,147,483,648. This is why Math.Abs(int.MinValue) throws an exception.

The exponent of a floating point number is encoded with an offset. For float it is 8 bits with an offset of 127, providing a range of 2 ^ -126 to 2 ^ 127 or 1.18E-38 to 3.40E+38 (all mantissa bits = 1). For double it is 11 bits with an offset of 1023, 2 ^ -1022 through 2 ^ 1023 or 2.23E-308 through 1.79E+308.

The range on the bottom end is further extended by allowing a floating value to be denormal . A normalized floating point value always starts with an implicit 1, not encoded in the mantissa. When a value drops below the smallest representable value (all zeros in the mantissa, encoded exponent = 1) then the exponent is set to 0 to indicate a denormal, the implicit 1 is not there anymore. The smallest possible non-zero float has a 1 in the least significant bit of the mantissa. With 23 bits in the mantissa for a float that's 2 ^ -23 = 1.19E-7. Yielding a smallest possible value of 1.19E-7 * 1.18E-38 = 1.40E-45. The value of Single.Epsilon

You never actually want to get close to denormals, they lose significant digits in a hurry. They really only help to avert division-by-zero problems, at a price.

That is due to the IEEE 754 standard which defines the coding of both (float & double). Nothing strange when you understand how it works.

Reading the article may be sufficient to understand it.

For Single precision (float) coded on 32 bits, read : http://en.wikipedia.org/wiki/Single_precision

For Double precision (double) coded on 64 bits, read : http://en.wikipedia.org/wiki/Double_precision

Good reading

I think you must be looking in the wrong place. The documentation I am looking at indicates that they are symmetrical as expected

http://msdn.microsoft.com/en-us/library/system.double.minvalue.aspx

Fur double this is positive to negative...

1.7976931348623157E+308.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM