Re: Newbye quetion: Why a double can store more number than a long ?
From: James McIninch (james.mcininch_at_comcast.net.spam)
Date: 05/03/04
- Next message: John Harrison: "Re: arrays of objects"
- Previous message: Allan Bruce: "Re: Plotting XY data"
- In reply to: jose luis fernandez diaz: "Newbye quetion: Why a double can store more number than a long ?"
- Next in thread: CBFalconer: "Re: Newbye quetion: Why a double can store more number than a long ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 03 May 2004 14:24:16 GMT
<posted & mailed>
The code below isn't C language code, but the question still applies.
A double can represent larger numbers because it lacks the resolution of the
long type. For a long integer, you have one number with absolute precision
for each integer within the range. For a double, you have exactly the same
number of values as the long, but on average, the difference between each
individual value and the next is much greater. Instead of counting by 1's,
you are counting by tens of thousands.
It may be easier to understand if you think about it this way, a integer
simply stores a number. A double uses the same number of bits to store two
numbers: one an integer, and one to tell it where to stick the decimal
point. The range of integers (where there is precision) is much smaller
because only a portion of the double is used to store the integral part
(mantissa) while another portion of the bits is used to store the position
of the decimal point (exponent). That's a simplification, but you get the
idea.
Integers are perfectly precise with a narrow range, and doubles are, on
average, very imprecise but cover a much wider range. Both doubles and
integers (in this case, since they are the same number of bits on your
system) have the same number of values.
jose luis fernandez diaz wrote:
> Hi,
>
> My OS is:
>
> cronos:jdiaz:tmp>uname -a
> HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license
>
>
> I compile in 64-bits mode the program below:
>
> cronos:jdiaz:tmp>cat kk.C
> #include <iostream.h>
> #include <limits>
>
> int main()
> {
> long l_min = numeric_limits<long>::min();
> long l_max = numeric_limits<long>::max();
> double d_min = numeric_limits<double>::min();
> double d_max = numeric_limits<double>::max();
> cout << sizeof(long) << " " << sizeof(double) << endl;
> cout << l_min << " " << l_max << " " << d_min << " " << d_max <<
> endl;
>
>
> return 0;
> }
> cronos:jdiaz:tmp>a.out
> 8 8
> -9223372036854775808 9223372036854775807 2.22507e-308 1.79769e+308
>
>
>
> The double and long types have the same size, but the double limits
> are bigger. Can anyone explein this to me ?
>
> Thanks,
> Jose Luis.
-- remove .spam from address to reply by e-mail.
- Next message: John Harrison: "Re: arrays of objects"
- Previous message: Allan Bruce: "Re: Plotting XY data"
- In reply to: jose luis fernandez diaz: "Newbye quetion: Why a double can store more number than a long ?"
- Next in thread: CBFalconer: "Re: Newbye quetion: Why a double can store more number than a long ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|