Click to See Complete Forum and Search --> : 0.999999 = 1


tomkear2006
September 20th, 2006, 03:11 PM
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

Paul McKenzie
September 20th, 2006, 03:21 PM
I am reading the book C++ for Dummies to get a clearer picture on some of the more simple C++ conceptsWell 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

tomkear2006
September 20th, 2006, 03:27 PM
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!

jfaust
September 20th, 2006, 03:48 PM
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.

tomkear2006
September 20th, 2006, 04:04 PM
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

MrViggy
September 20th, 2006, 04:29 PM
"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

SuperKoko
September 20th, 2006, 05:16 PM
"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:

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.

exterminator
September 21st, 2006, 02:52 AM
"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).