473,756 Members | 3,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Floating Point Bug in Visual Studio 2003

We are using Visual Studio 2003.NET (C++) for the development
of our software in the fields digital signal processing and
numerical acoustics.

One of our programs was working correctly if we are using the
Debug-Version of the program, but it fails (or leads to false
results) if we are using the Release-Version. After a long
debugging session we found out, that our program was working
correctly, but the floating point processing (accuracy) of the
Debug-Version and the Release-Version is different and leads to
different numerical results.

This bug occurs independend of the optimization level which is
used for the Release-Version (even if optimization is disabled).

We have dedected this bug only in the C++ compiler of Visual
Studio 2003.NET. With Visual Studio 6 both versions of our
program are working (with the same results).

I have included a test program in this mail. If the program
below is compiled with Visual Studio 2003.NET the output of
the Debug- and the Release-Version is differnt.

Note: There is also a problem with casting floats to int. This
means, in some cases the result of "(int)x" in the Debug- and
the Release-Vesion is not the same.

I have tried to report this bug/problem to the microsoft company,
but after visiting a lot of web-pages without any chance to send
a note to microsoft i gave up.
Anton Noll
Austrian Academy of Sciences, Acoustics Research Institut
1010 Vienna, Reichsratstrass e 17, Austria
E-Mail: An********@oeaw .ac.at
Test program (a part of a fir filter development algorithm)

----- BEGIN OF NUMTEST.CPP -----
// numtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>

static float _dfunci0(float x)
{
float sa, uh, u=1, s=1;
int i = 1;
while(true)
{
sa = s;
uh = x / 2 / (float)i++;
u = u * uh * uh;
s = s + u;
if(sa == s) return s;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
float delta = (float)1e-6, freq = (float)1;
float fe = (float)0.05, fg = (float)0.1;

int k, n;
float pi, a, df, fs, alpha, wn, hilf;
float *cw, *w,*c;

pi = (float)atan2((f loat)0,(float)-1);
a = (float)(-20 * log10(delta));
df = (fg-fe) / freq;
fs = (fg+fe) / 2 / freq;
n = int((a - 7.95) / 28.72 / df) + 1;

cw = new float[n+1];
c = new float[n+1];
w = new float[n+1];

if(a > 50)
alpha = (float)(0.1102 * (a-8.7));
else if(a > 21)
alpha = (float)(pow(0.5 842*(a-21),0.4) + 0.07886*(a-21));
else
alpha = 0;

c[0] = 2*fs;
for(k = 1;k <= n; k++)
c[k] = sin(2 * pi * k * fs) / (pi * k);

wn = _dfunci0(alpha) ;
for(k = 0;k <= n; k++)
{
hilf = (float)1 - (float)(k*k) / (float)(n*n);
if(hilf <= 0)
hilf = 0;
else
hilf = alpha * sqrt(hilf);
w[k] = _dfunci0(hilf) / wn;
}

for(k = 0; k <= n; k++)
cw[k] = (float)(c[k] * w[k]);

FILE *test = fopen("numtest. txt","wt");
for(k = 0; k <= n; k++)
fprintf(test,"% g,%g,%g\n", c[k], w[k], cw[k]);
fclose(test);

return 0;
}
----- END OF NUMTEST.CPP -----

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #1
5 3750
On 1 Jun 2005 00:51:38 -0500, Anton Noll <An********@oea w.ac.at> wrote
in comp.lang.c++:
We are using Visual Studio 2003.NET (C++) for the development
of our software in the fields digital signal processing and
numerical acoustics.

One of our programs was working correctly if we are using the
Debug-Version of the program, but it fails (or leads to false
results) if we are using the Release-Version. After a long
debugging session we found out, that our program was working
correctly, but the floating point processing (accuracy) of the
Debug-Version and the Release-Version is different and leads to
different numerical results.
If you are concerned with accuracy, why the heck are you using the
type float, which by its very nature has a limited accuracy?

And for that matter, why are you using any version of Visual C++? Use
a compiler that provides the full hardware precision of the FPU for
long double, instead of throwing it away for marketing reasons? Try
using gcc for Windows (the cygwin port, not mingw). Or Borland, or
any other compiler that does not throw away 16 bits of precision.

And looking through your code sample, I see a large number of
constants scattered through it, a very bad coding practice. These
should be defined as const values with meaningful names. But the most
important thing is that none of them has more than four significant
digits, even though the limited format you are using provides for six
significant decimal digits. Why not improve the accuracy of your
calculations by improving the accuracy of your constants?

On the other hand, why not just build your programs in debug mode, if
it gives you the result you want and you don't want to use a better
compiler for numerics, or even the most accurate floating point type
that your compiler provides?

Do you understand the limitations and compromises of floating point
arithmetic, especially using the very low precision Intel single
precision type? See this standard reference on the subject:

http://docs.sun.com/db/doc/800-7895/6hos0aou4
This bug occurs independend of the optimization level which is
used for the Release-Version (even if optimization is disabled).
The question I have is, what do you expect anyone in this group to do
for you? We can't fix Microsoft's compiler, if indeed it is broken.
Is there anything you expect to read here that can be of any use to
you other than telling you to use double for more precision or switch
to a different compiler?

The actual cause of the difference you see is most likely a difference
in the way some intermediate values are handled. Most likely in the
release version, more intermediate values are kept in FPU registers as
64-bit doubles instead of being truncated to 32 bits and stored to
memory.
We have dedected this bug only in the C++ compiler of Visual
What proof do you have that this is a bug?
Studio 2003.NET. With Visual Studio 6 both versions of our
program are working (with the same results).

I have included a test program in this mail. If the program
below is compiled with Visual Studio 2003.NET the output of
the Debug- and the Release-Version is differnt.

Note: There is also a problem with casting floats to int. This
means, in some cases the result of "(int)x" in the Debug- and
the Release-Vesion is not the same.
Do you understand these two things:

1. Casting a floating point value to int is not necessary and a
completely useless waste of typing? Assigning a floating point value
to an int does the conversion automatically, no cast required. If the
integral part of the float value is outside the range of int, you get
the same undefined behavior whether you use the cast or not?

2. Assignment of a floating point value to an integer type, with or
without a cast, is a truncating operation. It does not round. So if
you have a value that after having passed through several operations
is equal to 2.999999999, it will convert to the integer value 2, but
if you have 3.000000001, it will convert to the integer value 3?
I have tried to report this bug/problem to the microsoft company,
but after visiting a lot of web-pages without any chance to send
a note to microsoft i gave up.
Try asking in one of Microsoft's support groups in the
news:microsoft. public.* family, on the server msnews.microsof t.com.
Someone there can tell you how to file a report with Microsoft.
Anton Noll
Austrian Academy of Sciences, Acoustics Research Institut
1010 Vienna, Reichsratstrass e 17, Austria
E-Mail: An********@oeaw .ac.at
Test program (a part of a fir filter development algorithm)

----- BEGIN OF NUMTEST.CPP -----
// numtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>

static float _dfunci0(float x)
{
float sa, uh, u=1, s=1;
From a C++ point of view, or in fact from just about any programming
language, this program is terrible. Variable names like 'sa', 'uh',
'u', and 's' are inexcusable. Are you aware that using more
characters in the names of the variables does not make the program
larger or slower, and does not change the output?
int i = 1;
while(true)
{
sa = s;
uh = x / 2 / (float)i++;
u = u * uh * uh;
s = s + u;
if(sa == s) return s;
Please read a decent reference on floating point math in computer
programs. Especially what it has to say on comparing floating point
values for exact equality. For some values of 'x', this function will
never return.
}
}

int _tmain(int argc, _TCHAR* argv[])
{
float delta = (float)1e-6, freq = (float)1;
float fe = (float)0.05, fg = (float)0.1;
Are you aware that numbers like 0.05 and 0.1 cannot be represented
exactly in binary floating point representation?
int k, n;
float pi, a, df, fs, alpha, wn, hilf;
float *cw, *w,*c;

pi = (float)atan2((f loat)0,(float)-1);
a = (float)(-20 * log10(delta));
df = (fg-fe) / freq;
fs = (fg+fe) / 2 / freq;
n = int((a - 7.95) / 28.72 / df) + 1;

cw = new float[n+1];
c = new float[n+1];
w = new float[n+1];

if(a > 50)
alpha = (float)(0.1102 * (a-8.7));
else if(a > 21)
alpha = (float)(pow(0.5 842*(a-21),0.4) + 0.07886*(a-21));
else
alpha = 0;

c[0] = 2*fs;
for(k = 1;k <= n; k++)
c[k] = sin(2 * pi * k * fs) / (pi * k);

wn = _dfunci0(alpha) ;
for(k = 0;k <= n; k++)
{
hilf = (float)1 - (float)(k*k) / (float)(n*n);
if(hilf <= 0)
hilf = 0;
else
hilf = alpha * sqrt(hilf);
w[k] = _dfunci0(hilf) / wn;
}

for(k = 0; k <= n; k++)
cw[k] = (float)(c[k] * w[k]);

FILE *test = fopen("numtest. txt","wt");
for(k = 0; k <= n; k++)
fprintf(test,"% g,%g,%g\n", c[k], w[k], cw[k]);
fclose(test);

return 0;
}
----- END OF NUMTEST.CPP -----


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #2
* Jack Klein:

Try asking in one of Microsoft's support groups in the
news:microsoft. public.* family, on the server msnews.microsof t.com.
Someone there can tell you how to file a report with Microsoft.


Alas, although I agree with your other comments & advice, the problem
of reporting a "problem" (in MS speak even an Internal Compiler Error is at
worst a "problem") to Microsoft is a real one and of general interest even
in this group, because it's so widely used a compiler, and it's not solved
by posting to a Microsoft public or internal group.

Currently it's possible to report bugs in the Visual C++ 8.0 compiler, and
that was a great improvement on earlier (marketing-driven) practice.

Microsoft have had and probably still has reporting pages for earlier
versions, but these pages have, AFAIK, _never actually worked_; they have
just been very misleading, dishonest window dressing, a marketing effort.
For Visual C++ prior to version 8.0, there are AFAIK only three ways to
report a bug, such as an ICE: (1) be an MVP (Most Valued Professional),
which essentially means top-posting inane answers to inane questions in the
Microsoft group for a period of months or years until MVP-ship is endowed,
however that works, (2) work in firm with an established professional
relationship to Microsoft, or (3) shelve out some good money for no return.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
Anton Noll wrote:
....

We have dedected this bug only in the C++ compiler of Visual
Studio 2003.NET. With Visual Studio 6 both versions of our
program are working (with the same results).
It's more than likely not a "bug" as such with the compiler. This kind
of thing happens with floating point code. Floating point arithmetic
has many many issues with "non exactness".

See

http://support.microsoft.com/kb/q42980/

You could try using "double" instead of "float". double has 48 mantissa
bits on most architectures and "float" only has 24 (on most modern
machines). If this does not work, you will need to deal with issues
relating to round-off errors.

.... // numtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h" stdafx.h is not a standard C++ header - nuke this - you're not using it.

....
int _tmain(int argc, _TCHAR* argv[])
int main( int, char ** )
{
float delta = (float)1e-6, freq = (float)1;
float fe = (float)0.05, fg = (float)0.1;
Have you thought of using this code structure ?

float delta = 1e-6f;
float freq = 1.0f;
float fe = 0.05f;
float fg = 0.1f;

Much easier to read :0)

int k, n;
float pi, a, df, fs, alpha, wn, hilf;
float *cw, *w,*c;
A more acceptible C++ style is defining and initializing. This looks
like C style code (no need to do this in C++).

pi = (float)atan2((f loat)0,(float)-1);
Why all the conversions from int to float (and then a hidden conversion
to double) ?

float pi = atan2( 0, -1 );

So much easier to read....

a = (float)(-20 * log10(delta));
df = (fg-fe) / freq;
fs = (fg+fe) / 2 / freq;
Huge potential source of errors here if fg and fe are "close".
n = int((a - 7.95) / 28.72 / df) + 1;

cw = new float[n+1];
c = new float[n+1];
w = new float[n+1];


use std::vector ... much nicer.
....

You could also use a template for your code so you can switch it from
float to double (or even long double on some compilers) and check how
this behaves with your code changes.
Jul 23 '05 #4
You could try using "double" instead of "float". double has 48 mantissa
bits on most architectures and "float" only has 24 (on most modern
machines). If this does not work, you will need to deal with issues
relating to round-off errors.


Where people follow the IEEE 754 (floating points) standard a float has
a 23 bits mantissa and a double has a 52 bits mantissa.
Due to a layout trick this is improved to 24 for float and 53 for
double (for people wondering how/why
http://stevehollasch.com/cgindex/coding/ieeefloat.html)

Jul 23 '05 #5
"Anton Noll" <An********@oea w.ac.at> wrote in message
news:Xn******** *************** ********@38.119 .71.40
We are using Visual Studio 2003.NET (C++) for the development
of our software in the fields digital signal processing and
numerical acoustics.

One of our programs was working correctly if we are using the
Debug-Version of the program, but it fails (or leads to false
results) if we are using the Release-Version. After a long
debugging session we found out, that our program was working
correctly, but the floating point processing (accuracy) of the
Debug-Version and the Release-Version is different and leads to
different numerical results.

This bug occurs independend of the optimization level which is
used for the Release-Version (even if optimization is disabled).

We have dedected this bug only in the C++ compiler of Visual
Studio 2003.NET. With Visual Studio 6 both versions of our
program are working (with the same results).

I have included a test program in this mail. If the program
below is compiled with Visual Studio 2003.NET the output of
the Debug- and the Release-Version is differnt.

Note: There is also a problem with casting floats to int. This
means, in some cases the result of "(int)x" in the Debug- and
the Release-Vesion is not the same.

I have tried to report this bug/problem to the microsoft company,
but after visiting a lot of web-pages without any chance to send
a note to microsoft i gave up.

For what it is worth, I believe that if you choose the /Op compiler option
for both Debug and Release --- set in the IDE under

C/C++->Optimization->Floating-Point Consistency

--- then you will get the same results under Debug and Release. Reading the
documentation on this switch will provide further information.

--
John Carson

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7269
by: angelag | last post by:
I am currently taking a college course in Visual Basic.Net and I am a beginner. I bought Visual Studio.Net 2003 to do my homework at home. I built my first project and e-mailed it to myself at school. When I tried to open it in the lab, I got a message saying I couldn't open it because it was created with a newer version. Evidently the lab is using Visual Studio.Net 2002. My professor doesn't just want the executable file, he wants...
6
6177
by: Martin Bless | last post by:
The good news: Along with Python-2.4 comes really good news to Windows users. Yes, you now CAN build extension modules yourself using the SAME C++ compiler and linker Python is built with itself. Everything you need is available at no costs (except download hassle and installation time). Once your system is set up properly its just a matter of running 'python setup.py build'. No longer waiting for someone else to build binaries and a...
0
6136
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug file as folows. I need help to resolve them ASAP: cl /c /nologo /MDd /W3 /Od /GR /GM /Zi /GX /D "_DEBUG" /D " WIN32" /D "_W INDOWS" /D "_WINDLL" /D "_AFXDLL" /D "_MBCS" /D "_USRDLL" /
0
1865
by: Ricardo Dias Marques | last post by:
Hi, I have a development machine with Visual Studio .Net 2003 which, as far as I know, targets the 1.1 .Net Framework. Now I need to open some solutions that were created in another machine with Visual Studio .Net 2002 (against the 1.0 Framework). I have already read:
4
3313
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can interact with a rich 3D environment. Seem to be 90% of the way there! My problems are related to calculations where the result tends to zero (or another defined limit.) Have loads of cases where this kind of interaction occurs but this one
13
4150
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain amount of floating point work was approximately. To attempt to do this I made a program that...
13
5172
by: Chris Stankevitz | last post by:
Hi, I have a very large Visual c++ .net 2003 7.1 native c application (approximately 500,000 lines of code). This application is a simulation that frequently works with floating point numbers. From time to time I do something bad -- for example divide by zero or try to grow a float beyond numeric_limits<float>::max() I told MSVC .net 2003 7.1 to "Break into the debugger" for ALL exceptions in
10
18769
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
0
1969
by: induriprakash | last post by:
Hi, We are working on a scientifical related product which is most sensitive towards mathematical functions (even 7th decimal place chang can cause considereble differences in the output values). We’ve observed that the trignometric functions are behaving differently in Visual C++ 2003 and Visual C++ 2005 versions. We've recently upgraded IDE from VS2003 to VS2005 and compiled the unmanaged vc++ code in both versions. To explain this in...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9273
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9841
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.