Calculation differences between Debug and Release mode

Jeremy DeHaan dehaan.jeremiah at gmail.com
Fri Apr 12 23:07:39 PDT 2013


I have a function that will calculate a random point on a circle 
based on a specified radius and total number of points. The only 
point in question is the first point. I get different values when 
the code compiles in Release and Debug mode.

Here is some code:

Vector2f getPoint(uint index)
{
		
	static const(float) pi = 3.141592654f;
		
	float angle = index * 2 * pi / m_pointCount - pi / 2;

		
	float x = cos(angle) * m_radius;
	float y = sin(angle) * m_radius;
		

	return Vector2f(m_radius + x, m_radius + y);
}

Vector2f is simply a struct that has 2 floats.

In debug mode this works as expected. Let's say the radius is 50. 
getPoint(0) returns a vector that prints X: 50 Y: 0. For some 
reason, the same function will return a vector that prints X: 50 
Y: 4.77673e-14. Now, 4.77673e-14 is a crazy small number that 
might as well be 0, but why the difference?

Also, consider the following change in the function:

Vector2f getPoint(uint index)
{
		
	static const(float) pi = 3.141592654f;
		
	float angle = index * 2 * pi / m_pointCount - pi / 2;

	
	float x = cos(angle) * m_radius;
	float y = sin(angle) * m_radius;

	Vector2f temp = Vector2f(m_radius + x, m_radius + y);
		
	return temp;
}

Surprisingly, when calling getpoint(0), this will return a 
Vector2 that will print X: 50 Y: 0 even in Release mode.

Again, it's probably not that big of a deal since 4.77673e-14 is 
so small, but I'm curious about this. Anyone wanna shed some 
light on this?


More information about the Digitalmars-d-learn mailing list