restore original value after setprecision? [duplicate] - c++

This question already has answers here:
Set back default floating point print precision in C++
(5 answers)
Closed 7 years ago.
I have search the web but couldn't find what I need.
Some people recommend using
streamsize ss = std::cout.precision();
but I couldn't get it to work.
How do I set a double value back to the original state after setprecision?
#include <iostream>
using namespace std;
int main()
{
double a;
cout << "enter a double value: ";
cin >> a;
cout << "your value in 3 decimals is " << setprecision(3) << fixed << a << endl;
cout << "your original value is " << a << endl;
return 0;
}
Obviously the code above will not return the original value of a.
My intended output is: if user enter 1.267432
your value in 3 decimals is 1.267
your original value is 1.267432

How do I set a double value back to the original state after
setprecision?
To do so, you have to get the precision before you use setprecision(). In your question you already mentioned it by the following line:
streamsize ss = std::cout.precision();
but I couldn't get it to work.
Here how you use it:
streamsize ss = std::cout.precision();
double a = 1.267432;
std::cout << "a = " << a << '\n';
std::cout.precision (3);
std::cout << "a becomes = " << a << '\n';
std::cout.precision (ss);
std::cout << "Original a= " << a << '\n';
And the output will be like:
a = 1.26743
a becomes = 1.27
Original a= 1.26743
Reference: setprecision.
Run live.

You can try like this:
#include <iomanip>
#include <iostream>
int main()
{
double a = 1.267432;
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(3);
std::cout << a << endl;
return 0;
}

Related

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

Displaying Long Numbers C++ [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 2 years ago.
I don't need to do any calculations just print out the numbers.
double n1 = 1000000.5985;
double n2 = 9999999.0;
double n3 = 678300.893;
When I try
cout << n1 << n2 << n3;
I get 1e+006 1e+007 678301
How do I get it to print the whole number without converting to a string?
Use setprecision() from <iomanip>.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double f = 1000000.5985;
cout << f << endl;
cout << fixed << setprecision(6) << f <<endl;
cout << fixed << setprecision(5) << f << endl;
cout << fixed << setprecision(9) << f << endl;
return 0;
}
Output is
1e+06
1000000.598500
1000000.59850
1000000.598500000

Converting string to double keeps rounding to the whole number

I am trying to convert a string decimal number into a double, however when I use the atof() function, my number ends up rounding to the whole number.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string num = "135427.7000";
double r = atof(num.c_str());
cout << r << endl;
}
The output is:
135428
I want:
135427.7
cout does that, not atof().
More precisely, operator<<, which inserts the formated data into the std::ostream.
You can use std::setprecision() from the <iomanip> standard library to print the decimals:
cout << setprecision(7) << r << endl;
or
cout << fixed << setprecision(1) << r << endl;
If you want to print the whole 135427.7000:
cout << fixed << setprecision(4) << r << endl;

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]$

c++ passing pointers to a function

I am trying to create a console app in C++ that prompts the user to enter a floating point number and then takes that number and separates out the integer part and the fraction part.
Example output would be:-
Please enter a floating point number:
800.589
The integer part is 800 and the fraction part is .589
My solution is shown below:
#include <iostream>
#include <cmath>
using namespace std;
void spliceAnyNumber (double anyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
fractionPart = fmod(anyNumber,1);
integerPart = anyNumber - fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}
int main()
{
cout << "Please enter a floating point number: ";
double anyNumber = 0;
cin >> anyNumber;
cout << endl;
spliceAnyNumber(anyNumber);
system("Pause");
return 0;
}
I wrote the program but I am also being asked to pass pointers to the function and manipulate the dereferenced values. I tried to do that below but I am getting a bunch of errors back from the compiler.
#include <iostream>
#include <cmath>
using namespace std;
void spliceAnyNumber (double *pAnyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
&fractionPart = fmod(&anyNumber,1);
&integerPart = &anyNumber - &fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n"; *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}
int main()
{
cout << "Please enter a floating point number: ";
double *pAnyNumber = &anyNumber;
cin >> *pAnyNumber;
cout << endl;
spliceAnyNumber(*pAnyNumber);
system("Pause");
return 0;
}
Where am I going wrong with adding in pointers? Version 1 works but version 2 does not.
I've notated this inline.
#include <iostream>
#include <cmath>
using namespace std;
void spliceAnyNumber (double *pAnyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
&fractionPart = fmod(&anyNumber,1); // <- you should dereference pAnyNumber instead, and assign to fractionPart (i.e. "fractionPart = fmod(*pAnyNymber, 1);
&integerPart = &anyNumber - &fractionPart; // <- similar as above
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n"; *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}
int main()
{
cout << "Please enter a floating point number: ";
double *pAnyNumber = &anyNumber; // <- you haven't declared an 'anyNumber' variable to take the address of
cin >> *pAnyNumber;
cout << endl;
spliceAnyNumber(*pAnyNumber);
system("Pause");
return 0;
}
The & operator takes the address of a variable, so typeof(&anyNumber) == double**. You want the * operator instead.
You should read double *pAnyNumber as "When I apply the * operator, I get a double". (You actually get an lvalue reference, but that doesn't roll off the tongue and will probably confuse you...)
Your main function is a mess; leave it the same as the original and change spliceAnyNumber(pAnyNumber); to spliceAnyNumber(&pAnyNumber);.
I assume when you wrote anyNumber you actually meant pAnyNumber. If you have a pointer
double* p;
You dereference by *p, not &p. The former gives you a double while the latter gives you a double**.
You have to declare anyNumber before you can dereference it:
double *pAnyNumber = &anyNumber; // references an undeclared variable
Just take the address when passing to the function. Before that, you can use normal variables - no need for pointers:
double anyNumber;
cin >> anyNumber;
cout << endl;
spliceAnyNumber(&anyNumber);
Additionally, you're using the wrong operator in your function. It should be like this:
*pFractionPart = fmod(*pAnyNumber,1);
*pIntegerPart = *pAnyNumber - fractionPart;
The other thing is invalid syntax: &variable = ... literally means "address of variable = ", which results in a double**.
So the only change you have to make is the function parameter, and accessing it. No need for all those pointers inside the function..