|
-
September 20th, 2006, 03:11 PM
#1
0.999999 = 1
I am reading the book C++ for Dummies to get a clearer picture on some of the more simple C++ concepts and I have come across a paragraph that I can't make sence of.
"C++ can correct for many forms of round-off error. For example, in output, C++ can determine that instead of 0.999999, that the user really meant 1...."
Can anyone demonstrate what this means in a small section of code?
Much appreciated
Tom 
As a quick addition -> Is a double different from a float other than the storage capacity?
Thanks
Last edited by AnotherMuggle; September 20th, 2006 at 03:14 PM.
-
September 20th, 2006, 03:21 PM
#2
Re: 0.999999 = 1
 Originally Posted by tomkear2006
I am reading the book C++ for Dummies to get a clearer picture on some of the more simple C++ concepts
Well this concept really isn't a "C++" concept. It is a general concept with computers that most floating point numbers cannot be represented exactly in a binary number system.
For example, 1.6 cannot be represented exactly in binary (it is a repeating, non-terminating binary floating point number). What the various "output" functions do in C++ is round-off the numbers for only display purposes -- the number internally is (for this case), not really 1.6, but maybe 1.5999999231 or something of that nature.
Back when hand-held calculators were first available, for some models if you entered "1/3x3" you would get "0.99999999", and not 1. Same reason as above.
So it is more of a "numerical analysis" concept as to why you can't represent most floating point numbers exactly in binary, and not a "language" concept.
Edit:
The floating point numbers that can be represented exactly are ones that are a (negative) powers of 2 or sums of (negative) powers of 2. For example 0.5, 0.25, 0.75, 0.125, (or 1/2, 1/4, 3/4, 1/8), etc. can be represented exactly in a binary system.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 20th, 2006 at 03:37 PM.
-
September 20th, 2006, 03:27 PM
#3
Re: 0.999999 = 1
 Originally Posted by Paul McKenzie
Well this concept really isn't a "C++" concept. It is a general concept with computers that floating point numbers cannot be represented exactly in a binary number system.
For example, 1.6 cannot be represented exactly in binary (it is a repeating, non-terminating binary floating point number). What the various "output" functions do in C++ is round-off the numbers for only display purposes -- the number internally is (for this case), not really 1.6, but maybe 1.5999999231 or something of that nature.
Back when hand-held calculators were first available, for some models if you entered "1/3x3" you would get "0.99999999", and not 1. Same reason as above.
So it is more of a "numerical analysis" concept as to why you can't represent most floating point numbers exactly in binary, and not a "language" concept.
Regards,
Paul McKenzie
Increadible 
Thanks Paul,
I pay a lot of money to go to University and I would never get an answer as in depth as that from a lecturer...possibly no answer at all.
Your time is much appreciated!
-
September 20th, 2006, 03:48 PM
#4
Re: 0.999999 = 1
You'll find a great divide between academia and business, between professors and those that know what they're talking about. There are some exceptions, although I haven't found any recently.
-
September 20th, 2006, 04:04 PM
#5
Re: 0.999999 = 1
Yeah I'm impressed anyhow 
What about this paragraph?
"If an application requires a string, you've gotta provide one, even if the string contains only a single character. Providing nothing but the character just won't do the job"
Thanks guys
-
September 20th, 2006, 04:29 PM
#6
Re: 0.999999 = 1
 Originally Posted by tomkear2006
"If an application requires a string, you've gotta provide one, even if the string contains only a single character. Providing nothing but the character just won't do the job"
I don't quite understand this statement, however a character type is not a string. Typically, a string is an array of characters, so I would think that the "applciation" is expecting an array.
Viggy
-
September 20th, 2006, 05:16 PM
#7
Re: 0.999999 = 1
 Originally Posted by tomkear2006
"If an application requires a string, you've gotta provide one, even if the string contains only a single character. Providing nothing but the character just won't do the job"
It means that functions expecting "strings" except that you provide a const (or non-const) char* argument (or a std::string argument if they use a more modern style).
That is : A pointer to a character which must be interpreted as pointing at the start of a sequence of characters having contiguous addresses and terminated by a nul character.
In that case, you can't pass a simple char.
If you want to pass a string that contains a single character, you must pass a pointer p to a char such as *p points to this character and *(p+1) points to a nul character.
You can create such pointer like that:
Code:
char character_array[2]={'H', '\0'};
function_that_expect_a_C_style_string(&character_array[0]);
// or you can use a string litteral:
function_that_expect_a_C_style_string("H");
// but you can't pass a character:
function_that_expect_a_C_style_string('H');
// Since a decent compiler would refuse converting a char to a pointer (a pointer represents an address)!
I hope it makes sense for you, tomkear.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
September 21st, 2006, 02:52 AM
#8
Re: 0.999999 = 1
 Originally Posted by tomkear2006
"If an application requires a string, you've gotta provide one, even if the string contains only a single character. Providing nothing but the character just won't do the job"
I don't have the context but in general this seems to be in reference to type-safety. There are certain implicit promotion and conversion rules.. but otherwise, C++ is very type-safe. That means, if a function is to work on string type - it will work with string typed variables only and not with individual characters. If you want character support - provide an overload that works with input arguments of character-type.
Additionally - it also says that an array of certain type of elements (string - an array of characters) is different from one such element of which the array is made of (one character).
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|