简体   繁体   中英

Floating Point Addition Behavior - CPP

I have a question regarding floating point addition- I understand errors are easy to come by, but this one has me stumped. In my code, I compute 3 values that are floating point- z1, z2, and z3. They are interpolated z values for z-buffering.

To get the final z coordinate, I have float z = z1 + z2 + z3. When I print z, I realized it is always coming out to the same number- which is resulting in substantial z-fighting in the images generated by my program.

                    float z1 = (bry[0] * (1.0 / v1.m_pos[2]));
                    float z2 = (bry[1] * (1.0 / v2.m_pos[2]));
                    float z3 = (bry[2] * (1.0 / v3.m_pos[2]));

                    cout << "z1 is " << z1 << endl;
                    cout << "z2 is " << z2 << endl;
                    cout << "z3 is " << z3 << endl;

                    float z = z1 + z2 + z3;
                    cout << " z computed to  be " << z << cout;

For context, here are resulting print statements.

z1 is 0.59306
z2 is 0.156332
z3 is 0.250608
z computed to  be 10xa45504

z1 is 0.700896
z2 is 0.0484997
z3 is 0.250605
z computed to  be 10xa45504

I'm not exactly sure why the printing of the z value is the way it is, or what I may be doing incorrectly to add these values together. Any help/guidance would be appreciated.

That last line is printing two things: The value of z (1), and the address of the cout object (0xa45504). Perhaps you meant endl instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM