473,564 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

double variable question

Can some body explain to me this:

Consider the following code:

int main(int argc, char* argv[]) {
double d1;

d1 = 11.0;
d1 -= 1.8;
d1 -= 3.0; //HERE THE DEBBUGER SHOWS 6.1999999999999 993 !!!

printf("%f", d1); //THE OUTPUT IS 6.200000 (ok, we have less digits)
getch();

return 0;
}

I don't understand it. Is it the debuger shows me wrong data, or
something i don't know?
Nov 14 '05 #1
6 2776
polymedes wrote:
Can some body explain to me this:

Consider the following code:

int main(int argc, char* argv[]) {
double d1;

d1 = 11.0;
d1 -= 1.8;
d1 -= 3.0; //HERE THE DEBBUGER SHOWS 6.1999999999999 993 !!!

printf("%f", d1); //THE OUTPUT IS 6.200000 (ok, we have less digits)
getch();

return 0;
}

I don't understand it. Is it the debuger shows me wrong data, or
something i don't know?


The debugger shows you the decimal representation of the binary fraction
stored in the double. If you ask for full precision when using printf(),
you will get the same result.

6.2 is 6+1.0/5, 1.0/5 has no finite representation as binary fraction,
thus you get only an approximation of it. The decimal representation of
0.2 is only finite because 5 is a product of the prime numbers building
up the base 10 (namely, 2 and 5). So, 1.0/(5*5*2) has a finite
representation, but 1.0/3 or 1.0/7 etc has not.

In order to understand the issues at hand better, I suggest some heavy
reading:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

The first sections should be enough for a start :-)
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2
Michael Mair wrote:
polymedes wrote:
Can some body explain to me this:

Consider the following code:

int main(int argc, char* argv[]) {
double d1;

d1 = 11.0;
d1 -= 1.8;
d1 -= 3.0; //HERE THE DEBBUGER SHOWS 6.1999999999999 993 !!!

printf("%f", d1); //THE OUTPUT IS 6.200000 (ok, we have less digits)
getch();

return 0;
}

I don't understand it. Is it the debuger shows me wrong data, or
something i don't know?

The debugger shows you the decimal representation of the binary fraction
stored in the double. If you ask for full precision when using printf(),
you will get the same result.

6.2 is 6+1.0/5, 1.0/5 has no finite representation as binary fraction,
thus you get only an approximation of it. The decimal representation of
0.2 is only finite because 5 is a product of the prime numbers building
up the base 10 (namely, 2 and 5). So, 1.0/(5*5*2) has a finite
representation, but 1.0/3 or 1.0/7 etc has not.

In order to understand the issues at hand better, I suggest some heavy
reading:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

The first sections should be enough for a start :-)
Cheers
Michael


Thanks a lot Michael. This is really interesting.
Nov 14 '05 #3

"polymedes" <po*******@nosp am.com> wrote in message
news:cq******** **@nic.grnet.gr ...
Can some body explain to me this:

Consider the following code:

int main(int argc, char* argv[]) {
double d1;

d1 = 11.0;
d1 -= 1.8;
d1 -= 3.0; //HERE THE DEBBUGER SHOWS 6.1999999999999 993 !!!

printf("%f", d1); //THE OUTPUT IS 6.200000 (ok, we have less digits)
getch();

return 0;
}

I don't understand it. Is it the debuger shows me wrong data, or
something i don't know?


Probably the latter.

Floating point numbers (float, double) are not the same as mathematical Real
numbers. Mor specifically, they have a limited precision.

Usually they are represented as mantissa*2^expo nent (instead
ofmantissa*10^e xponent). Now if a certain figure (say 6.2) cannot be
represented that way, the result will be the nearest *) number that can. In
this case 6.1999999999999 993. Now it may look dramatic in your debugger, but
the difference is actually very small.

If you are writing programs using floats or doubles, you should take this
into account. For instance,
comparing two floating point numbers for equality using a blunt "=="
operator is (usually) not a good idea, for the reasons stated above.
Instead, check wether the absolute difference between the two
numbers is less than a specific limit (usually called epsilon).

So nothing bad is happening in your program.

*) Not sure, could be rounded up/down and be implementations specific.
Nov 14 '05 #4
polymedes wrote:
Can some body explain to me this:

Consider the following code:

int main(int argc, char* argv[]) {
double d1;

d1 = 11.0;
d1 -= 1.8;
d1 -= 3.0; //HERE THE DEBBUGER SHOWS 6.1999999999999 993 !!!

printf("%f", d1); //THE OUTPUT IS 6.200000 (ok, we have less digits)
getch();

return 0;
}

I don't understand it. Is it the debuger shows me wrong data, or
something i don't know?


This is the output of a tiny program I just wrote. I am not including
the program, since it would be useful and easy for you to write one for
yourself, just as checking the FAQ before posting is easy. Look
carefully at the output (if you prefer hex, make your program output hex):

On this implementation,
FLT_DIG = 6, FLT_MANT_DIG=24
DBL_DIG = 15, DBL_MANT_DIG=53
LDBL_DIG = 18, LDBL_MANT_DIG=6 4

Showing 6.2f (float) as octal:
6.1463146
Showing 6.1999999999999 993f (float) as octal:
6.1463146

Showing 6.2 (double) as octal:
6.1463146314631 4632
Showing 6.1999999999999 993 (double) as octal:
6.1463146314631 463

Showing 6.2L (long double) as octal:
6.1463146314631 4631463
Showing 6.1999999999999 993L (long double) as octal:
6.1463146314631 4630014
Nov 14 '05 #5
>Consider the following code:

int main(int argc, char* argv[]) {
double d1;

d1 = 11.0;
d1 -= 1.8;
d1 -= 3.0; //HERE THE DEBBUGER SHOWS 6.1999999999999 993 !!!

printf("%f", d1); //THE OUTPUT IS 6.200000 (ok, we have less digits)
getch();

return 0;
}

I don't understand it. Is it the debuger shows me wrong data, or
something i don't know?


There is no exact value of 6.2 in binary floating point (nor of
1.8). 0.1 is an infinite repeating decimal in binary. Also,
floating point operations are subject to round-off error.
For example, (1 + 100000000000000 000000) - 100000000000000 000000 has
a darn good chance of coming out zero.

If it's not an exact integer, and it doesn't end in 5 (with trailing
0's removed) it doesn't have an exact representation in binary
floating point.

If it's not an exact integer, has at least two decimal places, and
it doesn't end in 25 or 75 (with trailing 0's removed), it doesn't
have an exact representation in binary floating point.

If it's not an exact integer, has at least three decimal places,
and it doesn't end in 125, 375, 625, or 875 (with trailing 0's
removed), it doesn't have an exact representation in binary floating
point.

It appears that the double value you printed had roundoff error of 1 in the
least significant bit, making it match the 'before' value below:

6.2 as double:
Before: 6.1999999999999 992894572642398 998141288757324 218750000000000 00
Value: 6.2000000000000 001776356839400 250464677810668 945312500000000 00
After: 6.2000000000000 010658141036401 502788066864013 671875000000000 00

6.2 as float:
Before: 6.1999993324279 785156250000000 000000000000000 000000000000000 00
Value: 6.1999998092651 367187500000000 000000000000000 000000000000000 00
After: 6.2000002861022 949218750000000 000000000000000 000000000000000 00

For IEEE doubles, which I suspect you are using, DBL_DIG is 15, and the printed
result is 1 off in the 16th place, so you have nothing to complain about.

Gordon L. Burditt
Nov 14 '05 #6
polymedes <po*******@nosp am.com> writes:
Can some body explain to me this:

Consider the following code:

int main(int argc, char* argv[]) {
double d1;

d1 = 11.0;
d1 -= 1.8;
d1 -= 3.0; //HERE THE DEBBUGER SHOWS 6.1999999999999 993 !!!

printf("%f", d1); //THE OUTPUT IS 6.200000 (ok, we have less digits)
getch();

return 0;
}


The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>. Read
question 14.1. Then read the rest of section 14. Then read the whole
thing.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7

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

Similar topics

5
8252
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to understand.)
31
6591
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
2
2099
by: Tobias Froehlich | last post by:
Hello! I have a short question. I have a TextBox in which the user can enter a number, which then has to be saved into a double variable. The line I have looks like this: gpm.Configuration.LearnRate = TextBox1.Text;
5
2818
by: Rouben Rostamian | last post by:
Umfpack is a C library for computations dealing with sparse matrices. Several examples in the User's Guide use a certain cast that puzzles me. Here is an example. The prototype of the function umfpack_di_symbolic() is: int umfpack_di_symbolic ( int n_row, int n_col,
52
5949
by: lcw1964 | last post by:
Greetings, all, I am trying to port a little bit of math code to gcc, that in the original version used the long double version of several functions (in particular, atanl, fabsl, and expl). I get a complie-time "unidentified reference" error to the expl() calls, but gcc seems to digest atanl and fabsl just fine. Changing expl to exp...
116
35731
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going...
6
2909
by: Alexander Stoyakin | last post by:
Hello, please advise on the following issue. I need to check that difference between two double values is not higher than defined limit. int main() { double limit = 0.3; double val1 = 0.5, val2 = 0.2; if ( (val1 - val2) limit )
20
6894
by: hippomedon | last post by:
Hello everyone, I'm looking for some advice on whether I should break the normalization rule. Normally, I would not consider it, but this seems to be a special case. I have created an "Outcomes Database" used to store response data from measures/ questionnaires for a longitudinal health study. It is essentially derived from Duane...
5
1858
compman9902
by: compman9902 | last post by:
First of all, thank you for taking the time to look at my post. My question is pretty plain: "How do I convert a double variable into an int variable?" For example, say I have a double variable and its value is 88.92. Then I have an empty int variable. How would I make that int variable equal 88? Would that same method work if the double...
22
2748
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was...
0
7665
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...
0
7583
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...
0
7888
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
7950
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...
0
3643
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
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
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.