473,809 Members | 2,620 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
12 2985
jose luis fernandez diaz wrote:

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


Your program won't help you in understanding what's the deal with
floating point numbers and why they have a greater range then eg. long.

Let us simplify the whole thing in a more familiar environment.
Assume you are my 'computer'. But I will allow you to calulate
with 5 digits only (and no sign for simplicity)

If you use those 5 digits for 'long' only , you can use those 5
digits to count from 00000 to 99999, so your range is 100000 numbers.
But you can do different also. You can divide the available 5 digits
into a mantissa and an exponent. Say you use 2 digits for the exponent
and the remaining 3 digits for the mantissa (you are familiar with
sientific notation, are you?). So eg. a number 100 can be represented
in various ways:

number scientific not. our notation
*************** *************** ********
100 100 * 10^0 100 00
100 10 * 10^1 010 01
100 1 * 10^2 001 02

Note: in the column 'our notation', no representation uses more then
5 digits which fits perfectly to what I allowed to you :-)

Now lets see some other numbers.
Say you need to represent 1000 in 'out notation'. How can you do that?
Well you can eg. do 001 03. That would be 1 * 10^3. Or you could do
010 02 (10 * 10^2), or 100 01 (100 * 10^1). Fine. But now try to represent
1001 in 'out notation'. You can't. That's because the only way to get there
would be to increment the mantissa. Let's see whats happening then:

(1000) 001 03 -> (~1001) 002 03 -> 2 * 10^3 -> 2000
(1000) 010 02 -> (~1001) 011 02 -> 11 * 10^2 -> 1100
(1000) 100 01 -> (~1001) 101 01 -> 101 * 10^1 -> 1010

You see, using only 5 digits, it is impossible to get at the number 1001. You can
get 1000 and you can get 1010, but no numbers inbetween. It follows from the fact
that you reserve some of the available 5 digits to represent an exponent, which
drops the 'range' of numbers representable with the remaining 3 digits. But you
get the freedome to move the comma around by fiddeling with the exponent. Using
those 5 digit 'scientific notation' it is easy to represent

1000 -> 100 01
10000 -> 100 02
100000 -> 100 03
1000000 -> 100 04
10000000 -> 100 05

Right now we are way 'over' the upper limit for 5-digit-long, which was
99999. But we bought this by reducing the granularity. We cannot represent
all numbers in 10 millions, just some of them.

100 05 -> 10000000
101 05 -> 10100000

All numbers between 10000000 and 10100000 are not representable by our 5 digit
scheme but need to be approximated by either 10000000 or 10100000.

Back to your computer. It doesn't use only 5 digits, but much more. Also the
exponent is not taken to base 10, but to base 2. This reduces the effect you
just saw. But the principle is still the same. You buy the greater range
by reducing the granularity.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #11
Ok. I understand.

Regards,
Jose Luis.

Karl Heinz Buchegger <kb******@gasca d.at> wrote in message news:<40******* ********@gascad .at>...
jose luis fernandez diaz wrote:

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


Your program won't help you in understanding what's the deal with
floating point numbers and why they have a greater range then eg. long.

Let us simplify the whole thing in a more familiar environment.
Assume you are my 'computer'. But I will allow you to calulate
with 5 digits only (and no sign for simplicity)

If you use those 5 digits for 'long' only , you can use those 5
digits to count from 00000 to 99999, so your range is 100000 numbers.
But you can do different also. You can divide the available 5 digits
into a mantissa and an exponent. Say you use 2 digits for the exponent
and the remaining 3 digits for the mantissa (you are familiar with
sientific notation, are you?). So eg. a number 100 can be represented
in various ways:

number scientific not. our notation
*************** *************** ********
100 100 * 10^0 100 00
100 10 * 10^1 010 01
100 1 * 10^2 001 02

Note: in the column 'our notation', no representation uses more then
5 digits which fits perfectly to what I allowed to you :-)

Now lets see some other numbers.
Say you need to represent 1000 in 'out notation'. How can you do that?
Well you can eg. do 001 03. That would be 1 * 10^3. Or you could do
010 02 (10 * 10^2), or 100 01 (100 * 10^1). Fine. But now try to represent
1001 in 'out notation'. You can't. That's because the only way to get there
would be to increment the mantissa. Let's see whats happening then:

(1000) 001 03 -> (~1001) 002 03 -> 2 * 10^3 -> 2000
(1000) 010 02 -> (~1001) 011 02 -> 11 * 10^2 -> 1100
(1000) 100 01 -> (~1001) 101 01 -> 101 * 10^1 -> 1010

You see, using only 5 digits, it is impossible to get at the number 1001. You can
get 1000 and you can get 1010, but no numbers inbetween. It follows from the fact
that you reserve some of the available 5 digits to represent an exponent, which
drops the 'range' of numbers representable with the remaining 3 digits. But you
get the freedome to move the comma around by fiddeling with the exponent. Using
those 5 digit 'scientific notation' it is easy to represent

1000 -> 100 01
10000 -> 100 02
100000 -> 100 03
1000000 -> 100 04
10000000 -> 100 05

Right now we are way 'over' the upper limit for 5-digit-long, which was
99999. But we bought this by reducing the granularity. We cannot represent
all numbers in 10 millions, just some of them.

100 05 -> 10000000
101 05 -> 10100000

All numbers between 10000000 and 10100000 are not representable by our 5 digit
scheme but need to be approximated by either 10000000 or 10100000.

Back to your computer. It doesn't use only 5 digits, but much more. Also the
exponent is not taken to base 10, but to base 2. This reduces the effect you
just saw. But the principle is still the same. You buy the greater range
by reducing the granularity.

Jul 22 '05 #12


Hi,

Long stores the integers as a normal bit representation and a double is like
a bigger float, in that it stores values in IEEE Floating point format.

You should know more about it if you want to do any serious programming in
any language. Please check google for IEEE Floating point format.

"jose luis fernandez diaz" <jo************ **********@yaho o.es> 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.

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.670 / Virus Database: 432 - Release Date: 4/27/2004
Jul 22 '05 #13

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

Similar topics

32
22646
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
4187
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
7240
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
3553
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
8754
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
2669
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
13353
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
2563
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 bits
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.