As shown in the following code, there is a difference between cout
and printf
when outputting data.
double ans = 0, max = 135.349, min = 3.88633;
ans = max * 2086458231 / min;
cout << ans << endl;
printf("%lf", ans);
Output
7.26652e+010
72665192664.000000
Why is there such a difference?
Formatted output problem of c++, cout’s default output format of floating point numbers is not %lf. If you want to set the output format, you can refer to the following link
http://en.cppreference.com/w/...
cout is the syntax of C++, printf is the C language, but C is retained. In cstdio, the results are different because cout defaults to retaining N bits + scientific calculation method for over-long floating point numbers, but cout can also be used Use parameters to format the output, such as
cout << setiosflags(ios::fixed) << f
No need for scientific notation. You can consult the manual for more parameters.
printf can also be formatted, very Convenient
cout
The default stream output valid bits are 6 digits. If it exceeds 6 digits, it will be automatically formatted. If the integer length exceeds 6 digits, it will be automatically formatted into scientific notation.cin and cout are c++ codes, printf and scanf are c codes. %f in C language outputs floating-point data in decimal form.
cout is the output method in the c++ iostream standard library, while printf is retained by the c language. The default formatted output of cout is different from %lf