user705414
user705414

Reputation: 21200

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

Reading MSDN, float ranges from 1E-45 to 1E38, double ranges from 1E-324 to 1E308, I am wondering why it is asymmetric for negative and positive exponents?

Upvotes: 1

Views: 1354

Answers (5)

Hans Passant
Hans Passant

Reputation: 941605

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.

Upvotes: 1

perfectionist
perfectionist

Reputation: 4346

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 i.e. 1 with 38 0s after it.

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1500805

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).

Upvotes: 1

ColinE
ColinE

Reputation: 70142

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.

Upvotes: 0

Arnaud F.
Arnaud F.

Reputation: 8452

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

Upvotes: 0

Related Questions