Using cout to produce output -
double space = 4.0 - 1.9999999;
cout << space << endl;
result: 2
Why am I not getting 2.0000001 instead? What should the type be or the numbers to make this 2.0000001?
Thanks
Using cout to produce output -
double space = 4.0 - 1.9999999;
cout << space << endl;
result: 2
Why am I not getting 2.0000001 instead? What should the type be or the numbers to make this 2.0000001?
Thanks
cout << fixed << setprecision(7) << space << endl;
Don't forget to include <iomanip>, and take care with expectations when it comes to floating point arithmetic. The results are often approximations and can suffer from accumulated errors.
Thank you! That works, problem is this was on a test I took today and apparently there is a way to do it without using setprecision. Any thoughts?
There's another way to set the flags, or you can use another output method, but that's essentially the normal way to override cout's convenience behavior. I can't think of any clever way off the top of my head that isn't too awkward to be reasonable.
Any thoughts?
If it was on a test then you would have learned it in class or through homework. Though I can't help much unless you provide the exact question rather than paraphrasing it.
Below is the exact question seen in the comment below. I'll post the answer when I have it next week.
#include <iostream>
using namespace std;
int main()
{
//change the line below as little as possible to have a result of 2.0000001
float c = 4.0 - 1.99999990;
cout << "c = " << c << endl;
return 0;
}
deceptikon, you were right on. I missed that section some how and have been pulling my hair out. Thanks
As the code stands, the two literal values in the calculation are implicitly treated as doubles. Have you tried changing c to a double, or you can change the literals to floats by putting an f at the end of each e.g. 4.0f - 1.99999990f.
Those are about the only changes I can think of to that particular line of code that might make a difference. Changing c to a double would be my bet!
Changing c to a double would be my bet!
That's not a bad notion, but the problem isn't in precision of the type, it's in cout's behavior. cout acts much like printf() when printing floating point in that the default precision is 6, and if the fractional part is all zeros it's simply ignored. Since the first nonzero digit is the 7th in the value's precision, it gets truncated along with the zeros whether it's a rounding error or not.
I'm curious what the expected answer is, actually, since I still can't think of a good way to override count's default behavior without changing the value or the call to cout.
@Deceptikon: I know about couts precision, but the OP was saying that they weren't allowed to use setprecision and the line they specified was the line to modify. Those two suggestions were the only things I could think of that could make a difference to that line of code. Changing the literals to floats would increase the risk that the values would be truncated, so changing c to a double would be the best bet. But as you say it doesn't get around the issue with couts default precision.
I've had a little play with this during some long builds at work and TBH, I can't see a way of getting the intended result without using setprecision!
But setting the precision isn't the only step required, you still need to declare c as a double.
To illustrate:
Compiling and running this under Visual Studio 2008:
#include <iostream>
#include <iomanip>
int main()
{
float c = 4.0 - 1.99999990;
std::cout << "c = " << std::fixed << std::setprecision(7) << c << std::endl;
return 0;
}
outputs:
c = 2.0000000
I'm guessing this is due to the range of values that float can approximate, so there are almost certainly some rounding errors. So the expected value is not displayed.
But if you change c to a double, recompile and run:
#include <iostream>
#include <iomanip>
int main()
{
double c = 4.0 - 1.99999990;
std::cout << "c = " << std::fixed << std::setprecision(7) << c << std::endl;
return 0;
}
The output is:
c = 2.0000001
Which is the output the OP is looking for.
So possibly, the OPs teacher meant that setprecision alone was not the answer.
Otherwise, I don't know if it's possible that the OPs teacher is using a compiler with a different default precision for std::cout and perhaps changing c to a double is the answer he or she is looking for. Or perhaps the question itself is just flawed.
Otherwise, there must be something else that we've all missed!
Either way, changing c to a double AND using setprecision seems to do the trick. That's the best answer I can come up with!
This has really piqued my interest though. It's a pretty simple problem! Like you, I would also be very interested to see what the expected answer is!
Where is Narue when you need her? Heh heh! If she can't solve it, nobody can! except perhaps Donald Knuth or Bjarne Stroustrup, heh heh!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.