Home Dashboard Directory Help

VS2010 C++ wrong subtraction result (1.0f - 1.0f == 1753.0) when code optimization is on by szpilewski


 as Fixed Help for as Fixed

Sign in
to vote
Type: Bug
ID: 696766
Opened: 10/26/2011 4:00:00 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
User(s) can reproduce this bug


//This code result is 1.0f - 1.0f == 1753.0

#include "stdafx.h" //empty
#include <tchar.h>
#include <iostream>
#include <stdlib.h>

struct Struct
float a;
float b;

int _tmain(int argc, _TCHAR* argv[])
Struct* ptr = new Struct;

float x;
float y = rand() ? 1.0f : 2.0f;

x = y + 1753;

ptr->a = x;
ptr->b = x - y;

x = y;

std::cout << "x = " << x << std::endl;
std::cout << "y = " << y << std::endl;

float result = x - y;

std::cout << "x - y = " << result << std::endl;

delete ptr;
return 0;
Sign in to post a comment.
Posted by Microsoft on 11/21/2011 at 3:40 PM
Thank you for reporting this issue. I can confirm that the compiler is generating incorrect code for the example code you provided. This is a known issue and will be fixed in the next release of Visual Studio. You should be able to verify that it is fixed using the already released developer preview for VS 11: http://www.microsoft.com/download/en/details.aspx?id=27543.

ian Bearman
VC++ Code Generation and Optimization Team
Posted by MS-Moderator08 [Feedback Moderator] on 10/26/2011 at 7:08 PM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 10/26/2011 at 4:48 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by szpilewski on 10/26/2011 at 7:02 AM
Set code optimization to Disabled (/Od)
File Name Submitted By Submitted On File Size  
Bug_0.zip 10/26/2011 78 KB