| | |
iomanip, setprecision - inprecise?
![]() |
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
Learning about all functions in <iomanip> and was testing out setprecision() and found that something weird is going on...? I did try to do a search on setprecision() limitatations or similar keywords but can't seem to find the right site... can someone please tell me what's going on?
outputs:
C++ Syntax (Toggle Plain Text)
double a = 0.1, b = 0.2, c = 0.3, d = 0.4, e = 0.5; double f = 0.6, g = 0.7, h = 0.8, i = 0.9; cout << setprecision(20) << a << endl; cout << setprecision(20) << b << endl; cout << setprecision(20) << c << endl; cout << setprecision(20) << d << endl; cout << setprecision(20) << e << endl; cout << setprecision(20) << f << endl; cout << setprecision(20) << g << endl; cout << setprecision(20) << h << endl; cout << setprecision(20) << i << endl << endl; float a2 = 0.1, b2 = 0.2, c2 = 0.3, d2 = 0.4, e2 = 0.5; float f2 = 0.6, g2 = 0.7, h2 = 0.8, i2 = 0.9; cout << setprecision(20) << a2 << endl; cout << setprecision(20) << b2 << endl; cout << setprecision(20) << c2 << endl; cout << setprecision(20) << d2 << endl; cout << setprecision(20) << e2 << endl; cout << setprecision(20) << f2 << endl; cout << setprecision(20) << g2 << endl; cout << setprecision(20) << h2 << endl; cout << setprecision(20) << i2 << endl;
C++ Syntax (Toggle Plain Text)
0.10000000000000001000 0.20000000000000001000 0.29999999999999999000 0.40000000000000002000 0.50000000000000000000 0.59999999999999998000 0.69999999999999996000 0.80000000000000004000 0.90000000000000002000 0.10000000149011612000 0.20000000298023224000 0.30000001192092896000 0.40000000596046448000 0.50000000000000000000 0.60000002384185791000 0.69999998807907104000 0.80000001192092896000 0.89999997615814209000
Floating point numbers are not stored precisely in memory. They are usually stored according to the IEEE 754 standard. If you want exact precision, you need to use a 3rd party library or write your own. For most calculations, however, the small degree of error is acceptable.
![]() |
Similar Threads
- Basic doubt (C++)
- C++ question(Decimal places)very weird (C++)
- C++ homework (C++)
- how to call this function...using menu..! (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with C++ project
- Next Thread: operator - (minus) overloading problem
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






