Why the plus operation doesn't work ?

I have a code like below. I am wonder why the plus (+) operation for error doesn't work.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

void calnormal()
{
	long numcal;
	float indexNC0,indexNC1;
	float error;
    
	float aa0 = 0, ab0 = 0, ac0 = 0, ad0 = 0;
	float          bb0 = 0, bc0 = 0, bd0 = 0;
	float                   cc0 = 0, cd0 = 0;
	float                            dd0 = 0;
    
	float aa, ab, ac, ad;
	float     bb, bc, bd;
	float         cc, cd;
	float             dd;
    
	for(int i=0;i<noofvert;i++)
	{
		numcal = vlist[i].returnsizef();
		for(int j=0;j<numcal;j=j+2)
		{
			indexNC0 = vlist[i].returnindexf(j);
			u.ax = vlist[i].returnx() - vlist[indexNC0].returnx();
			u.ay = vlist[i].returny() - vlist[indexNC0].returny();
			u.az = vlist[i].returnz() - vlist[indexNC0].returnz();
			if(j == 0){v0 = u;}
            
			indexNC1 = vlist[i].returnindexf(j+1);
			v.ax = vlist[i].returnx() - vlist[indexNC1].returnx();
			v.ay = vlist[i].returny() - vlist[indexNC1].returny();
			v.az = vlist[i].returnz() - vlist[indexNC1].returnz();
            
			normal.ax = u.ay * v.az - u.az * v.ay;
			normal.ay = u.az * v.ax - u.ax * v.az;
			normal.az = u.ax * v.ay - u.ay * v.ax;
			
			normal.D = - vlist[i].returnx() * normal.ax -    vlist[i].returny() * normal.ay - vlist[i].returnz() * normal.az;
            
			aa = normal.ax * normal.ax;
			ab = normal.ax * normal.ay;
			ac = normal.ax * normal.az;
			ad = normal.ax * normal.D;
            
			bb = normal.ay * normal.ay;
			bc = normal.ay * normal.az;
			bd = normal.ay * normal.D;
            
			cc = normal.az * normal.az;
			cd = normal.az * normal.D;
            
			dd = normal.D * normal.D;
            
			aa0 = aa0 + aa;
			ab0 = ab0 + ab;    bb0 = bb0 + bb;
			ac0 = ac0 + ac;    bc0 = bc0 + bc;    cc0 = cc0 + cc;
			ad0 = ad0 + ad;    bd0 = bd0 + bd;    cd0 = cd0 + cd;    dd0 = dd0 + dd;
           
            
		}
        
        float error1,error2,error3;
       
        error1 = cd0 * vlist[i].returnz();
        error2 = dd0 ;
        
        error3 = error2 + error1;

        cout  <<  error1 << " " << error2 << " " << error3 << endl;
    
        
        cout << endl;
	}
	cout << endl;
	
}


these is the example for the outcome:

-7.03125e-06 7.03125e-06 -4.54747e-13

the actual operation should be :

-7.03125e-06 + 7.03125e-06

and the result should be 0. However, it gives me -4.54747e-13. I am not sure where it comes. :((

could someone, help me to figure out what is actually wrong in my coding? :(
Last edited on
Well, since I can't compile your code and I don't know what noovert and the several functions you call are (although I can guess), I can't actually say if there's anything wrong with your code

In general the floating point formats could be the problem. Depending on how you've manipulated the iostream, the level of precision stored internally may be different from that output to screen. Also the numbers themselves are an approximation, and although the variations are usually very small, when you hope for 2 floating point numbers to be exactly equal in magnitude (like your sum above), you may encounter some surprises.

Try this for fun with varying values of precision, and see if you get the same values every time:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int main()
{	
	long double ld = 1.0l / 3.0l;
	float f = 1.0f / 3.0f;
	cout.precision(8);
	cout << "ld = " << ld << endl;
	cout << "f = " << f << endl;
	return 0;
}
Topic archived. No new replies allowed.