Strange Number Output in std::cout

I'm trying to print array elements to the console via std::cout.

Most of the numbers output normally, but there are some in which a strange behaivour seems to be occuring:

m_CircleVertices[4].x = 0.154509
m_CircleVertices[4].y = 0.475528
m_CircleVertices[4].z = 0

m_CircleVertices[5].x = -5.17058e-14
m_CircleVertices[5].y = 0.5
m_CircleVertices[5].z = 0


On "m_CircleVertices[5].x", the number is outputted in this alternative format. Why is this happening and what can I do to stop it happening?

Here is the code:

1
2
3
4
5
6
7
8
9
10
for (int i = 1; i < noOftriangles; i++)
{
   m_CircleVertices[i].x = (float)(origin + radius * cos(i * 6.28318530718 / noOftriangles));
   m_CircleVertices[i].y = (float)(origin + radius * sin(i * 6.28318530718 / noOftriangles));
   m_CircleVertices[i].z = 0.0F;
		
   std::cout << "m_CircleVertices[" << i << "].x = " << m_CircleVertices[i].x << "\n";
   std::cout << "m_CircleVertices[" << i << "].y = " << m_CircleVertices[i].y << "\n";
   std::cout << "m_CircleVertices[" << i << "].z = " << m_CircleVertices[i].z << "\n\n";
}
Last edited on
What should it print instead?
'm_CircleVertices[5].x' should just output the result of this:

(float)(origin + radius * cos(5 * 6.28318530718 / noOftriangles));

Which is essentially equal to 0, or to be exact 0.0000000000000517057768.

'-5.17058e-14' is actually this number in standard form: -5.17058 * 10^-14
You can change the precision and format of std::cout

Great, thanks.
Last edited on
Topic archived. No new replies allowed.