error on simple math

I wrote a program and find and this error and I don't know why.
Visual C++ 2010

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "stdafx.h"
#include <iostream>

using namespace std;
		
void main()
{

	double a = 2.09, b=0;

	cout << a << endl;
	
	a = a - 2.09;
	cout << "a - 2.09 = " << a << endl;

	a = 2.09;
	
	a = a - .09;
	cout << "a - .09 = " << a << endl;
	
	a = a - 2;
	cout << "a - 2 = " << a << endl;

	cin >> b;	
}

Output:

2.09
a - 2.09 = 0
a - .09 = 2
a - 2 = -2.22045e-016


why 'a' becomes -2.22045e-016
If instead of 2.09 I use 1.09, 3.09 or 4.09 the final output is fine > a - 2 = 0;
Try a = a - 2.0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "stdafx.h"
#include <iostream>

using namespace std;
			
void main()
{

	double a = 2.09, b=0;

	cout << a << endl;
	
	a = a - 2.09;
	cout << "a - 2.09 = " << a << endl;

	a = 2.09;
	
	a = a - .09;
	cout << "a - .09 = " << a << endl;
	
	a = a - 2.0;
	cout << "a - 2.0 = " << a << endl;

	cin >> b;	
}


2.09
a - 2.09 = 0
a - .09 = 2
a - 2.0 = -2.22045e-016


Same error.
That is not an error. It's a lack of precision, of approximately 0.000000000000000222045

Welcome to floating point numbers. This is what they do.

http://floating-point-gui.de/


As an aisde, void main() is not C++. In c++, main returns an int. int main()
Last edited on
Come to think of it, I think this may be "working as intended."

I'm pretty sure that floats, doubles, etc have a limited amount of precision, based on your architecture. As you approach 0, you end with an infinitely smaller number, which the compiler attempts to print out to it's best ability, where with a definable number, that kind of imprecision is actually ignored by the compiler in favor of the gross precision values that are determined by the user/program editing.

Edit:
Blast your faster fingers, Moschops!
Last edited on
Just add

1
2
std::cout << std::fixed ;
std::cout.precision(2) ;


right at the beginning of main() and run the program again. That would take care of things like 0.000000000000000222045 at least as far as the displayed output is concerned.


Oh, and int main(), not void main().
http://www2.research.att.com/~bs/bs_faq2.html#void-main
That is not an error. It's a lack of precision, of approximately 0.000000000000000222045

Welcome to floating point numbers. This is what they do.

http://floating-point-gui.de/


As an aisde, void main() is not C++. In c++, main returns an int. int main()


ok. I am reading floating-point-gui.de.

I wrote void main(), and it still compile.
It is supposed to use int main() always for C++?
, and it still compile.

Well then you must have something that is not a C++ compiler. It's probably very similar to a C++ compiler. Most of the time, it won't make any difference, I expect. Still, it's important to know when you're writing C++ and when you're not - if you ever have to work with other coders, or you swicth compiler, you'll run into problems if you don't know.

It is supposed to use int main() always for C++?

Either that or int main(int argc, char* argv[])
Last edited on
I will do some research on this issue and reply later.

And I understood, int main() always for C++.


std::cout << std::fixed ;
std::cout.precision(2) ;


It seems to work, on display, but the value is not 0. I am working in a function, it'll take some time,I'll post it when finished.
Last edited on
So far, this error appears when the remainder is assumed to be 0.
Well, I wrote a function to make it 0.

It will display;

To use all that remains enter > 000
Otherwise, enter the quantity to use.
:


Topic archived. No new replies allowed.