Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbye quetion: Why a double can store more number than a long ?

Reply
Thread Tools

Newbye quetion: Why a double can store more number than a long ?

 
 
jose luis fernandez diaz
Guest
Posts: n/a
 
      05-03-2004
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.
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      05-03-2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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();

[snip]

Well, since you are using a different language from C, we in the comp.lang.c
newsgroup cannot assist you. Your question does not relate to C, and should be
(presumably, from the crossposting, was) asked in a forum related to the
language in which you wrote your example. This appears to be C++, so that forum
would be comp.lang.c++.


- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAljf6agVFX4UWr64RAnujAJwJ4mOsZZ9THxxul4vwzr vTo/acYwCcDLiJ
+EqfYBtEqbJjlf/u1b7p2DQ=
=7HZL
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-03-2004
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 ?


Well.

1.79769E308

does not mean that the number is accurate to the last digit. It means

1797690000000000000000....000000.....000000
and the next smallest number is probably something like
1797680000000000000000....000000.....000000

so there are large gaps between numbers. Those gaps get smaller when
the numbers get smaller.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      05-03-2004
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 <iostream> // No ".h" extension.

> #include <limits>
>
> int main()
> {


using namespace std;

> 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 ?


Typically, some of the bits representing a double are interpreted as an
exponent, and the rest as a multiplier. Floating-point math results in
approximate results, whereas any operation closed over the field of
integers will give an exact result when applied to long int's (if we
discount {over,under}flow).

http://en.wikipedia.org/wiki/IEEE_fl...point_standard
 
Reply With Quote
 
James McIninch
Guest
Posts: n/a
 
      05-03-2004
<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.
 
Reply With Quote
 
Terry
Guest
Posts: n/a
 
      05-03-2004

"Lew Pitcher" <(E-Mail Removed)> wrote in message
news:MHqlc.17455$(E-Mail Removed). ..
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> 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();

> [snip]
>
> Well, since you are using a different language from C, we in the

comp.lang.c
> newsgroup cannot assist you. Your question does not relate to C, and

should be
> (presumably, from the crossposting, was) asked in a forum related to the
> language in which you wrote your example. This appears to be C++, so that

forum
> would be comp.lang.c++.
>
>
> - --
> Lew Pitcher
> IT Consultant, Enterprise Application Architecture,
> Enterprise Technology Solutions, TD Bank Financial Group
>


Not very helpful.

Regards


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-03-2004
jose luis fernandez diaz wrote:
>
> 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;
> }


C++ is off topic on c.l.c. Please refrain from any such
cross-postings.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)


 
Reply With Quote
 
joe durusau
Guest
Posts: n/a
 
      05-03-2004


CBFalconer wrote:

> jose luis fernandez diaz wrote:
> >
> > 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;
> > }

>
> C++ is off topic on c.l.c. Please refrain from any such
> cross-postings.
>
> --
> Some useful references:
> <http://www.ungerhu.com/jxh/clc.welcome.txt>
> <http://www.eskimo.com/~scs/C-faq/top.html>
> <http://benpfaff.org/writings/clc/off-topic.html>
> <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)


Well, the question doesn't have anything to do with unix, either, but
you might see whether your sompiler has sizeof() in it. That could tell
you the size of long and of double, and might provide a clue.

The best place to persue this sort of thing is a textbook. There are
very few newsgroups that devote themselves to trying to run down
questions relating to problems that probably pertain to a specific
compiler/platform issue such as this one. Too bad, but that's the
way it is.

Speaking only for myself,

Joe Durusau



 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-03-2004
On Mon, 3 May 2004 17:09:27 +0100
"Terry" <(E-Mail Removed)> wrote:

<snip C++ code and redirect to comp.lang.c++>

> Not very helpful.


Seemed helpful to me. It contained no incorrect information and told the
OP where help would be available.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      05-04-2004
On Mon, 3 May 2004 17:09:27 +0100, "Terry"
<(E-Mail Removed)> wrote in comp.lang.c:

[snip]

> Not very helpful.
>
> Regards


Nor was your follow up.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Newbye quetion: Why a double can store more number than a long ? jose luis fernandez diaz C Programming 11 05-04-2004 05:27 PM



Advertisments