Issue
Intuitively I would think that long doubles have more decimal places than doubles and doubles have more decimal places than floats, however, if so why does std::cout print out floats, doubles, and long doubles at the same decimal precision, even when they have overloads for all primitive data types?
C++ source code:
#include <iostream>
#define PI 3.1415926535897932384626433832;
int main()
{
float f = PI;
double d = PI;
long double ld = PI;
std::cout << f << std::endl;
std::cout << d << std::endl;
std::cout << ld << std::endl;
return 0;
}
output:
3.14159
3.14159
3.14159
Solution
Values have a fixed precision depending on the type and you can't change it. It's implementation defined. You can only change the precision of the output and there is just one setting for all types of floats. You can't have different output precision for float, double and long double. The default output precision for all types of float is 6.
It's technically not possible to set a different output precision for e.g. float
than e.g. double
.
There are no overloads for std::setprecision
. Each std::basic_ostream
can hold only one value for precision.
Answered By - Thomas Sablik Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.