Question about division and double values

Hi,

I'm a newbie playing with fire C++ for the very first time, (I'm a VB 6 developer) I have been reading a beginners book on C++ and I'm trying to create a modest application to do some quick and dirty calculations, I'm using Visual Studio 2008 Express.

I know floating point in C++ is not accurate, and I'm not implying there is anything wrong with it, I do have a basic understanding of how a CPU handles floating point and the trade-off it does have in order to be quick.

I know it is not meant to be precise but when I try to run this code;

#include <iostream>

using namespace std;

void main(){

long double a = 2;
long double b = 32768;
long double r = 0;

r = a / b;

cout << "r = " << r << "\n\n";
}

If I use any calculator a/b gives: 0.00006103515625, however in the program "r" equals to 6.10352e-005... That is a terrible result, way beyond non-accuracy and more on the land of completely useless.

I know I must be doing doing something wrong, can anyone please give me some orientation or advice about how this kind of operation should be accomplished? I'm puzzled and I have spent three days with this issue, and it is making me nuts.

Thanks.



closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>

using namespace std;

void main()
{

    long double a = 2;
    long double b = 32768;
    long double r = 0;

    r = a / b;

    cout << "r = " << setprecision (20)<< r << " = " << fixed << r << endl;

}


r = 6.103515625e-005 = 0.00006103515625000000


http://www.cplusplus.com/reference/iostream/manipulators/setprecision/
http://www.cplusplus.com/reference/iostream/manipulators/fixed/
http://www.cplusplus.com/reference/iostream/manipulators/scientific/
Last edited on
however in the program "r" equals to 6.10352e-005... That is a terrible result, way beyond non-accuracy and more on the land of completely useless.


Nothing is wrong with the division. 'r' is just being printed in scientific notation. (6.10352e-005 = 6.10352 * 10^-5 = 0.0000610352)

You can change the output format of the printed number with 'fixed' and 'scientific' modifiers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>  // for iostream manipulators

using namespace std;

int main(){  // main returns an int!  not a void!

long double a = 2;
long double b = 32768;
long double r = 0;

r = a / b;

// note the addition of 'fixed' and 'setprecision' here
cout << fixed << setprecision(5) << "r = " << r << "\n\n";

return 0;  // return a value.
}


edit: blah, too slow XD
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>

using namespace std;

int main(){
	
	long double a = 2.0;
	long double b = 32768.0;
	long double r = 0.0;
	
	r = (a / b);
	
	cout.precision(14);
	cout << "r = " << fixed << r << "\n\n";
	
	return 0;
}
Dammit, too slow
BTW, just to respond to a few of the OP's assumptions.

VB uses exactly the same floating-point as C++. In fact, all programming languages bound to the FPU use exactly the same floating-point.

There is no trade-off for speed. The fact is that a binary system cannot accurately represent every real number. It can only approximate.


The long double type is an extension in many C and C++ compilers (such as GNU), since the standards (until C99) did not provide for it. On Intel hardware, it usually represents the 80-bit extended precision double, but it is not required to be any larger than a plain double (64-bit). Just so you know that such a construct is not technically portable at the moment. So unless you are using more than 16 digits of precision just stick with a double.

Here is a good read:
What Every Computer Scientist Should Know About Floating-Point Arithmetic
http://docs.sun.com/source/806-3568/ncg_goldberg.html

Hope this helps.
Just as a minor correction to Duoas, an n-ary system can represent any real number, but some numbers (e.g. pi) require infinite storage, which a computer obviously doesn't have.
Hi,

Many thanks to all, as I suspected it was my own wrongdoings. If I have the correct understanding it is all down to the way cout displays the results by default as scientific notation, the resulting value itself is stored properly in memory and it is really less than 1. 0.0000610...

What was confusing me is the fact that the "watch" window in Visual studio displays the var values in scientific notation too.

Does anybody know if this can be changed? For me it would be great just to see regular numbers instead of scientific notation, ie: 0.00006103515625000000 instead of 6.103515625e-005

Again many thanks, I wasn't expecting such a good and quick response! :-D
Topic archived. No new replies allowed.