Calculation differences between Debug and Release mode

Simen Kjaeraas simen.kjaras at gmail.com
Sat Apr 13 04:50:47 PDT 2013


On Sat, 13 Apr 2013 08:07:39 +0200, Jeremy DeHaan  
<dehaan.jeremiah at gmail.com> wrote:

> 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?

Sounds to me like a bug. I've tried recreating the problem on my machine
(Win7, dmd 2.062 32-bit, no flags other than debug/release), but can't see
it happen here. My (perceived) version of your code:

import std.stdio : writeln;
import std.math : sin, cos;

struct Vector2f {
     float x,y;
}

int m_pointCount = 25;
float m_radius = 50;
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);
}

void main( string[] args ) {
     writeln( getPoint( 0 ) );
}

Could you please post here the minimum code necessary to get the
behavior you describe, as well as the platform and compiler flags
you're using?


-- 
Simen


More information about the Digitalmars-d-learn mailing list