473,573 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Hi,

My OS is:

cronos:jdiaz:tm p>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:tm p>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:tm p>a.out
8 8
-922337203685477 5808 922337203685477 5807 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.
Jul 22 '05 #1
12 2916
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jose luis fernandez diaz wrote:
Hi,

My OS is:

cronos:jdiaz:tm p>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:tm p>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)

iD8DBQFAljf6agV FX4UWr64RAnujAJ wJ4mOsZZ9THxxul 4vwzrvTo/acYwCcDLiJ
+EqfYBtEqbJjlf/u1b7p2DQ=
=7HZL
-----END PGP SIGNATURE-----
Jul 22 '05 #2
jose luis fernandez diaz wrote:

Hi,

My OS is:

cronos:jdiaz:tm p>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:tm p>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:tm p>a.out
8 8
-922337203685477 5808 922337203685477 5807 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

179769000000000 0000000....0000 00.....000000
and the next smallest number is probably something like
179768000000000 0000000....0000 00.....000000

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

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3
jose luis fernandez diaz wrote:
Hi,

My OS is:

cronos:jdiaz:tm p>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:tm p>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:tm p>a.out
8 8
-922337203685477 5808 922337203685477 5807 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}flo w).

http://en.wikipedia.org/wiki/IEEE_fl...point_standard
Jul 22 '05 #4
<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:tm p>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:tm p>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:tm p>a.out
8 8
-922337203685477 5808 922337203685477 5807 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.
Jul 22 '05 #5

"Lew Pitcher" <Le*********@td .com> wrote in message
news:MH******** ************@ne ws20.bellglobal .com...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jose luis fernandez diaz wrote:
Hi,

My OS is:

cronos:jdiaz:tm p>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:tm p>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
Jul 22 '05 #6
jose luis fernandez diaz wrote:

I compile in 64-bits mode the program below:

cronos:jdiaz:tm p>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)
Jul 22 '05 #7


CBFalconer wrote:
jose luis fernandez diaz wrote:

I compile in 64-bits mode the program below:

cronos:jdiaz:tm p>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

Jul 22 '05 #8
On Mon, 3 May 2004 17:09:27 +0100, "Terry"
<te***@tbean.fr eeserve.co.uk> 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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #9
Hi,

I have made the program below to try to understand how a floating
point number is made by my computer (HP-UX cronos B.11.11 U 9000/800):

#include <stdio.h>
#include <math.h>

union
{
float f;
unsigned char uc[sizeof(float)];
} f_union;
int main()
{
f_union.f=1.0F;

unsigned char u1=1, u2;
u1 <<= sizeof(unsigned char)*8-1;

for (int i=0; i < sizeof(float); ++i)
{
u2=f_union.uc[i];
for(int j=0; j<sizeof(unsign ed char)*8; j++)
{
printf("%d", ((u1 & u2)!=0));
u2 <<= 1;
}
}
printf("\n");

int exp;
double man = frexp(f_union.f , &exp);
printf("%lf %d\n%f\n", man, exp,f_union.f);
return 0;
}
but I didn't. Perhaps the program is wrong. Here are some outputs:

001111111000000 000000000000000 00
0.500000 1
1.000000

010000000000000 000000000000000 00
0.500000 2
2.000000

010000000100000 000000000000000 00
0.750000 2
3.000000
010000001000000 000000000000000 00
0.500000 3
4.000000

010000001010000 000000000000000 00
0.625000 3
5.000000

010000010000000 000000000000000 00
0.500000 4
8.000000


Any hint are welcome.

Thanks,
Jose Luis.

jo************* *********@yahoo .es (jose luis fernandez diaz) wrote in message news:<c2******* *************** ****@posting.go ogle.com>...
Hi,

My OS is:

cronos:jdiaz:tm p>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:tm p>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:tm p>a.out
8 8
-922337203685477 5808 922337203685477 5807 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.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
22608
by: f | last post by:
I have this double sum, a, b, c; sum = a + b + c; printf("%.20f = %.20f, %.20f, %.20f", sum, a, b, c); I found that the debug version and release version of the same code give me different result. I am using VC++ 6.0. In debug version, the print out is:
11
393
by: jose luis fernandez diaz | last post by:
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:
4
4175
by: Michael Mair | last post by:
Hi there, actually, I have posted the same question in g.g.help. As there were no answers, I am still not sure whether this is a bug or only something open to the compiler that is seemingly inconsistent or whether my understanding of C is not complete enough. I would appreciate answers or pointers to answers very much.
60
7175
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
11
3521
by: aisling.cronin | last post by:
Hi I am using ctime to convert the following string 1144412677847 .... Please could some one to double check if they get the same result as me (The Time is Sun Nov 02 09:11:51 2031). It seems incorrect and would like a second opion. #include <time.h> #include <stdio.h>
3
8727
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; this produced an error
11
2652
by: Steven Woody | last post by:
long i = nnn; long j; double d; d = i; j = ( long )d; in this case, i == j ? thanks.
206
13176
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) It then updates the value with numberOfPrecisions after the decimal
0
2548
by: Charles Coldwell | last post by:
James Kanze <james.kanze@gmail.comwrites: True, with some additional considerations. The commonly used IEEE 754 floating point formats are single precision: 32 bits including 1 sign bit, 23 significand bits (with an implicit leading 1, for 24 total), and 8 exponent bits double precision: 64 bits including 1 sign bit, 52 significand...
0
7986
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8179
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5556
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5258
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3701
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3699
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.