Calculation differences between Debug and Release mode

Jeremy DeHaan dehaan.jeremiah at gmail.com
Sat Apr 13 09:36:21 PDT 2013


On Saturday, 13 April 2013 at 11:59:12 UTC, Simen Kjaeraas wrote:
> 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?

After playing around I discovered that Mono-D automatically uses 
-O for release builds and it looks like that is what is causing 
this. After compiling using that switch just from the command 
line I reproduced the problem.

I'm on Windows, and I my compilation was nothing more than "dmd 
-O -release main.d" to get the issue I described.



More information about the Digitalmars-d-learn mailing list