Precision of floating point - C++03 - c++

Suppose i do the following:
Example A:
float i = 123.0f;
std::cout << i;
I'll get the output 123
My question is when the default precision of float is 6 for std::cout why do I get the output 123 ?

You want std::fixed to show these insignificant zeroes. By default, extra zeroes are cropped for readability.
double f = 3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
Outputs:
3.1416
3.14159
3.14159
3.141590000

Related

What is the purpose of the setprecision() used in this C++ program?

This example program was created with the sole purpose of showing what setprecision and setw does. I dont understand the purpose of the third line that says "setprecision(5)". I commented the line out to see the difference but it looks the exact same. Is there no purpose?
cout << "\nSales Figures\n";
cout << "-------------\n";
cout << setprecision(5);
cout << "Day 1: " << setw(8) << day1 << endl;
cout << "Day 2: " << setw(8) << day2 << endl;
cout << "Day 3: " << setw(8) << day3 << endl;
cout << "Total: " << setw(8) << total << endl;
The setprecision() function is part of the "iomanip" library and is used when you need to output a float to a certain number of decimal places. This is good for displaying money amounts and other things that typically are shown with a set number of digits after the decimal point (even if those digits are 0).
Say you have a float called price: If you stored 10.0 in that float, C++ would not know how many decimal points to output when you print into the screen; setprecision(2) would make the output 10.00.
You can find the documentation at this link: https://cplusplus.com/reference/iomanip/setprecision/.
It includes the following code as an example of how setprecision() works.
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n'; // This outputs 3.1415
std::cout << std::setprecision(9) << f << '\n'; // This outputs 3.14159
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n'; // This outputs 3.14159
std::cout << std::setprecision(9) << f << '\n'; // This outputs 3.141590000
return 0;
}
Note that setprecision() is only applicable to data types with decimal points such as floats and doubles.

C++ How to use relative error with `cout`?

While preparing for my first ever coding interview I found a question where I was requested to print a double number with an absolute or relative error of 10^(-6).
How can I do something like that with cout?
double test_var; // Holding Some value which changes according to my program.
cout << test_var << endl;
Plus, In the expected output I saw numbers like 1000.00 how can I print those .00 too?
You could have used std::setprecision.
#include <iostream>
#include <iomanip>
int main()
{
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n'; //prints 3.1416
std::cout << std::setprecision(9) << f << '\n'; //prints 3.14159
std::cout << std::fixed; //Modifies the default formatting for floating-point input/output.
std::cout << std::setprecision(5) << f << '\n'; //prints 3.1416
std::cout << std::setprecision(9) << f << '\n'; //prints 3.141590000
}
so i think its complicated with those set or fixes
there is a simple way to acheive your goal
printf("%.6lf",test_var);
so %.6lf means to keep 6 demicals of the varible

I want to align my results in columns based on the decimal place

I want to display my result aligned on the column by the decimal place.
I have tried to just put setw(7) and setw(6) in parts of the display but it seems to not change the output at all.
int main()
{
double x;
char more = 'y';
while(more=='y' || more=='Y')
{
cout << "\n\t\t\tInput x:";
cin >> x;
cout << "\n\n\t\t\t LibraryResult\tMyResult" << endl;
cout << setprecision(2) << fixed << "\n\t\tsin(" << x << ")\t"
<< setprecision(6) << sin(x) << "\t" << mySin(x) << endl;
cout << setprecision(2) << fixed << "\n\t\tcos(" << x << ")\t"
<< setprecision(6) << cos(x) << "\t" << myCos(x) << endl;
cout << setprecision(2) << fixed << "\n\t\texp(" << x << ")\t"
<< setprecision(6) << exp(x) << "\t" << myExp(x) << endl;
}
}
I want the resultants of the program to be aligned by decimal, so when you put in a number like 2 the decimals are all in the same column.
If you set a precision of 6, a width of 6 or 7 is simply not enough to contain the number. You have to either reduce the precision or increase the width.
Try playing around with the stream manipulators in the following snippet
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
int main()
{
const double period = 2 * 3.141592653589793;
const int steps = 20;
std::cout << std::string(44, '=') << '\n';
std::cout << std::setw(3) << "x" << std::setw(12) << "sin(x)"
<< std::setw(12) << "cos(x)" << std::setw(14) << "tan(x)" << '\n';
std::cout << std::string(44, '-') << '\n';
for(int i = 0; i <= steps; ++i)
{
double x = i * period / steps;
std::cout << std::setprecision(2) << std::fixed << x
<< std::setprecision(6) << std::setw(12) << std::sin(x)
<< std::setprecision(6) << std::setw(12) << std::cos(x)
<< std::setprecision(6)
<< std::setw(16) // <- Try to decrease it
<< std::scientific // <- Try to keep it as std::fixed
<< std::tan(x) << '\n';
}
std::cout << std::string(44, '-') << '\n';
}
You must use several manipulators (one is not enough)
You might want to try with the following combination, that right-aligns your numbers, with a fixed number of decimals.
std::cout.width(15);
std::cout << std::fixed << std::setprecision(6) << std::right << exp(x) << std::endl;
You can find information about manipulators here

std::setprecision not showing trailing decimals [duplicate]

This question already has answers here:
Correct use of std::cout.precision() - not printing trailing zeros
(3 answers)
Closed 6 years ago.
I am trying to print double and integers as double. For doing so, I wrote the following program:
int main()
{
string object="1";
std::stringstream objectString;
objectString << std::setprecision(8) << atof(object.c_str());
cout<<"ObjectString="<<objectString.str()<< " "<<std::setprecision(10) << double(atof(object.c_str())) <<"\n";
}
I expected the output to be:
ObjectString=1.0 1.0
However, I am getting the output as:
ObjectString=1 1
Can someone please suggest as to where am I going wrong?
To force trailing zeros, use std::fixed:
std::string object = "1";
std::stringstream objectString;
objectString << std::fixed << std::setprecision(8) << atof(object.c_str());
std::cout << "ObjectString=" << objectString.str() << " ";
std::cout << std::fixed << std::setprecision(10) << double(atof(object.c_str())) << "\n";
Output:
ObjectString=1.00000000 1.0000000000
The way to force the output to always show the decimal point is to use std::showpoint:
#include <iostream>
#include <iomanip>
int main() {
double d = 1.0;
std::cout << std::setprecision(1) << d << '\n';
std::cout << std::setprecision(1) << std::showpoint << d << '\n';
return 0;
}
[temp]$ ./a.out
1
1.
[temp]$

How can I print a double in C++ up to a fixed precision? [duplicate]

This question already has answers here:
How can I change the precision of printing with the stl?
(2 answers)
Closed 7 years ago.
If I have stored a value in a double variable like
double d = pow(...)
and this function, let's say, evaluates to an integer, and if I print it using cout, it prints only the integer without trailing zeroes.
How can I print the trailing zeroes as well?
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
return 0;
}
Output:
3.1416
3.14159
3.14159
3.141590000
setprecision(int n) sets the specified precision
You are probably looking for std::setprecision. Something like this:
std::cout << std::setprecision (10) << d << std::endl;