I just wonder:
When I run this program, why is it that the result is a little incorrect ?
Like 0.2 * 1 == 0.20000000000000001
and
0.2 * 2 == 0.40000000000000002
This is a discussion on Floating Point Accuracy... within the C++ Programming forums, part of the General Programming Boards category; I just wonder: When I run this program, why is it that the result is a little incorrect ? Like ...
I just wonder:
When I run this program, why is it that the result is a little incorrect ?
Like 0.2 * 1 == 0.20000000000000001
and
0.2 * 2 == 0.40000000000000002
Studying programming languages,
you'll ALWAYS be a student ;-)
Why not tell the compiler to show the decimal two spaces from the decimal point using set precision?
Code:#include <iostream> using std::fixed; using std::cout; #include <iomanip> using std::setprecision; int main ( void ) { double val = 5.5; double res = val + val; cout << Result: " << fixed << setprecision( 2 ) << res; return 0; }
Hi swgh
Sure I could !
But that didn't answer the question... ;-)
regards,
The SharK
Studying programming languages,
you'll ALWAYS be a student ;-)
Floating point numbers in computers are never going to be infinitely precise. To have that level of precision would require unlimited memory. If my memory is right, single-precision numbers are accurate to seven decimal places while double-precision are accurate to fifteen.
I see, thanks Frobozz ;-)
Studying programming languages,
you'll ALWAYS be a student ;-)
Single precision is 24 bits of precision (including leading 1) while double is 53, which yes, works out to about 7 and 15 decimal places
And that's all the accuracy computers provide us
"The Internet treats censorship as damage and routes around it." - John Gilmore
http://en.wikipedia.org/wiki/IEEE_fl...point_standard
Especially read David Goldberg's paper at the end
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
You could use BCD instead of binary if you want decimal precision ie. storing the number as base 10 digits instead of base 2 bits.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
That doesn't eliminate the problem. Try to compute 1/7 (as a real value, not using integer arithmetic) and store than within a finite number of decimal places.Originally Posted by jafet