The important thing is, for each range, to look at the signs of the
exponents.
The sign of the exponent indicates the direction in which to move the
decimal point.
+ means move the decimal to the right
- means move the decimal point to the left
To simplfy it for demonstration purposes let's take a floating point type
that allows the ranges:
-1.79E+3 to -4.94E-3
and
4.94E-3 to 1.79E+3
-1.79E+3 equates to -1790
-4.94E-3 equates to -0.00494
4.94E-3 equates to 0.00494
1.79E+3 equates to 1790
Remembering that not every number can be represented exactly in floating
point, you can see that we have ranges of:
-1790 through -0.00494 negative values
and
0.00494 through 1790 negative values
1790 is the upper limit, -1790 is the lower limit and the closest
representations of 0 that we can achieve are 0.00494 and -0.00494.
Now, extrapolate that example up to the Double type and you can see that the
ranges are:
-1.79769313486231570E+308 through -4.94065645841246544E-324 for negative
values
and
4.94065645841246544E-324 through 1.79769313486231570E+308 for positive
values
This can be rewritten as:
-179769313486231570[...300 zeroes...]00000000
through
-0.000000[...310 zeroes...]000494065645841246544
for negative values
and
0.000000[...310 zeroes...]000494065645841246544
through
179769313486231570[...300 zeroes...]00000000
for positive values
This shows that 0 cannot be represented exactly as a Double and the closest
representations of 0 that can be achieved are actually:
-0.000000[...310 zeroes...]000494065645841246544
and
0.000000[...310 zeroes...]000494065645841246544
Now, when it comes to dealing with the Decimal type, it holds a signed
128-bit (16-byte) value representing a 96-bit (12-byte) integer number
scaled by a variable power of 10.
The scaling factor specifies the number of digits to the right of the
decimal point and ranges from 0 through 28.
With a scale of 0 (no decimal places), the largest possible value is
+/-79,228,162,514,264,337,593,543,950,335
(+/-7.9228162514264337593543950335E+28).
With 28 decimal places, the largest value is
+/-7.9228162514264337593543950335, and the smallest nonzero value is
+/-0.0000000000000000000000000001 (+/-1E-28).
As you can see, for each decimal place that you have, you lose one integral
place and vice-versa.
With no decimal places you can have the equivalent of a 29 digit number with
a range of +79,228,162,514,264,337,593,543,950,335
to -79,228,162,514,264,337,593,543,950,335.
This is 10 digits wider than an Int64 (Long) and the maximum value is
approximately 8.5 bollions times larger.
cj said:
While I'm on the subject why would anyone use a floating point number
(double or single) if they're inaccurate? Why not use decimal? Other
than the decimal takes 16 bytes vs the doubles 8.
Also I'm still not sure why double and single list the values they accept
in two ranges one for each side of 0. Do they not accept values around 0?
I'm sure they do but the documentation doesn't make sense to me.
ie: -1.79769313486231570E+308 through