Search

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

Closed
as Fixed Help for as Fixed

3
0
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
1
Workaround(s)
3
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;
std::cin.get();

delete ptr;
return 0;
}
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

Visual Studio 2010 SP1

Steps to reproduce

Create C++ Win32 Console project using the code I have submitted.

Select Release configuration

Select 32 bit compilation

Turn code optimization on:
Project->Properties->C/C++->Optimization->Maximize Speed (/O2)

Run application and observe console output

Product Language

English

Operating System

Windows 7

Operating System Language

Polish

Actual results

x = 1
y = 1
x - y = 1753

Expected results

x = 1
y = 1
x - y = 0

or (have rand() returned 0)

x = 2
y = 2
x - y = 0
File Attachments
File Name Submitted By Submitted On File Size  
Bug_0.zip 10/26/2011 78 KB
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.

Thanks,
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)