hi
While i was learning the new VC Express i encountered a problem:
klasse.h-------------------------------------------------------------------
class
complex { float re, im;public
:complex(
float r, float i) : re(r), im(i) {};complex
operator+(complex c);complex
operator*(complex c);};
----------------------------------------------------------klasse.h-------
klasse.cpp----------------------------------------------------------------
#include
"klasse.h"complex complex::operator+(complex c){
complex b = complex(0, 0);
b.im = im + c.im;
b.re = re + c.re;
return b;
}
complex complex::operator*(complex c){
complex b = complex(0, 0);
b.im = im * c.im;
b.re = re * c.re;
return b;
}
--------------------------------------------------------------------------------klasse.cpp
main.cpp-----------------------------------------------------------------------------------
#include
"klasse.h"int
main(){
complex a = complex(1.0f, 3.1f);complex b = complex(1.2f, 2.0f);
complex c = b;
a = b + c; // BREAKPOINT SET IN THIS LINE
b = b + c*a;
c = a*b + complex(1,2);
return 0;
}
-----------------------------------------------------------------------------main.cpp
After checking the values of a,b,c I had a problem with variable a.
a={re=1.0000000 im=3.0999999 }
b={re=1.2000000 im=2.0000000 }
c={re=1.2000000 im=2.0000000 }
Could anybody give an explanation of the value 3.0999999 cause its supposed to be 3.1000000
Please help me

Floating point problem
Warboy
To expand on Christian's response a little,
1. Generally, it is preferable to use double. It is the default and forcing float tends to be unproductive.
2. Regardless, the numbers 1.2 and 3.1 cannot be represented exactly in binary floating point. Fractions like 0.1 and 0.2 are not exactly represented because 1/10 is not exactly represented. It is the same thing we are accustomed to with 1/3 in decimal arithmetic. In binary systems there are just more cases, including 1/5 and fractions containing 1/5 as a factor (e.g., 1/10, 1/20, 1/100)..
3. Sometimes it appears that the numbers are represented exactly. This can be because of rounding that happened when the value is displayed in decimal (i.e., 1.2 worked but 3.1 didn't). In your program, you might find that comparisons might work or might not, because of the ways rounding is happening.
4. So special care is needed when using floating--point and you want to work with exact values.
guinea13
Because there are so many ways that what you see in output may also be different from what is held in the computer, I made a little program to demonstrate some things about floats (and the fact that calculations are always done in double by default, regardless of how the result is stored).
Here is the little program (in tiny type to avoid line wrap):
/* float5.c 1.10 Demonstrate incommensurate floats with rounding and
compiler oddities. */
#include <stdio.h>
int main( )
{ /* Show how binary floating-point has many approximation differences
that can surprise you in C/C++ programs. */
typedef union
{ float f; /* A stored float value */
unsigned char x[sizeof(float)]; /* viewed as a sequence of bytes */
} fp;
fp r0, r1, r2, r3;
int i;
printf("\nfloat5> 1.10 Demonstration of float usage hiccups.\n");
printf( " Developed with VC++ Toolkit 2003 compiler.\n");
printf( " Confirmed with VC++ 2005 Express Edition.\n");
r0.f = 1.2f;
r1.f = 3.1f;
r2.f = 31.0f;
r3.f = 10.0 * r1.f;
printf("\n r0 is %f, r1 is %f, r2 is %f, and r3 is %f\n",
r0.f, r1.f, r2.f, r3.f);
printf( " where the r3 = 10.0 * r1 value printed might not\n");
printf( " be the float value actually stored if the code\n");
printf( " optimization is a little too clever.\n");
printf("\n r1 * 10.0 is %f\n", r1.f * 10.0);
printf( " the double value without rounding down to float.\n");
printf("\n r2 == r3 is %d\n", r2.f == r3.f);
printf( " and r3 is rounded exact even though r1 can't be.\n");
printf("\n r0 is 0x");
for (i=sizeof(float); i;)
printf("%2.2X", r0.x[--i]);
printf("\n r1 is 0x");
for (i=sizeof(float); i; )
printf("%2.2X", r1.x[--i]);
printf("\n both approximate with repeating bits to the end.\n");
printf( " Output rounding has them appear to be exact.\n");
printf("\n r2 is 0x");
for (i=sizeof(float); i; )
printf("%2.2X", r2.x[--i]);
printf("\n r3 is 0x");
for (i=sizeof(float); i; )
printf("%2.2X", r3.x[--i]);
printf("\n both exact because r3 is rounded when stored.");
printf("\n\n");
return 0;
} /* main */
/* 1.10 2006-03-23-00:19 confirm identical results with VC++ 2005
Express Edition. Optimization level does not seem to matter
with regard to r3 in the first printf() of the data.
1.00 2006-03-22-22:11 initial version confirmed with the VC++
Toolkit 2003 compiler. This version shows a 3.099999 for the
first output of r3 in the list of values.
$Header: /MyProjects/Float5/float5.c 2 06-03-23 0:47 Orcmid $
/* end of float5.c */
U?ur Gumu?han
Practically every time I am in a class or a setting where there are newcomers, someone stumbles on the kinds of discrepancies that arise in using floating-point arithmetic. This shows up in mystery behavior around conversion of input values, use of the internal and intermediate values in calculations, and then presentation of decimal-notation output.
The sensitivity of floating-point arithmetic to mystery behavior arises in all programming languages, going back to Fortran and continuing into C, C++, Java, and C#. Not even Basic (or JavaScript or ...) is free from these kinds of discrepancies. The puzzle that mack345 encountered is just one more reminder.
I thought it would be useful to create some resources that we can refer people to for an appreciation of what is happening. I'd like to collect clues that help people learn how to mitigate (and trouble-shoot) edge cases when floating-point approximations provide surprising results.
I have refined the earlier float5.c demonstration and made it available for download at C/C++ Floating-Point Models a starting point for describing floating-point gotchas in C/C++ (and other programming languages).
At first the only text is in comments in the code and the outputs from the sample executables. That should be enought for anyone to duplicate the Visual C++ 2005 Express Edition results and gain some sense for what is going on.
There will be more text and links at the Computer Arithmetic Gotchas cover page real soon now. As the Zip-file download manifest says, I am very interested in comments and experiences of others.
William W
Floating point numbers in computers are not exact. double is more precise than float, try that, but even then, this sort of stuff will occur.
Anirudh