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

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 696766 Comments
Status Closed Workarounds
Type Bug Repros 3
Opened 10/26/2011 4:00:00 AM
Access Restriction Public


//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)