The following code in C++:
double x = 500000.6;
printf("%f\n", x);
cout << x << endl;
prints the following:
500000.600000
500001
Why cout isn't printing the correct value (500000.6)?
Putting the following line as the beginning of the code makes cout prints correct result:
cout.precision(7);
However, setting the precision to 6 (which is the default anyways) doesn't print the expected result.
Why this happen? I have only 1 digit after the decimal point, not 7 digits!
By default, the precision is the total number of significant digits. It's not the number of digits after the decimal point. 500000.6 has seven significant digits, 500001 has only six.
Try this instead
cout << fixed << setprecision(1) << x << endl;
When used for fixed format, the precision is the number of digits after the decimal point.
Related
#include <iostream> using namespace std;
int main()
{
double x=5.0,y=4.0,z;
z=x+y;
cout<<x<<endl<<y<<endl<<z;
return 0;
}
The above program gives me the following output:
5
4
9
When I have declared the variables to be double and even z as double why do I get the output as integer value(9)??
cout is being helpful here: if the double value is a whole number, then it, by default, does not display a decimal separator followed by an arbitrary number of zeros.
If you want to display as many numbers as the precision that your particular double on your platform has, then use something on the lines of
cout.precision(std::numeric_limits<double>::max_digits10);
cout << fixed << x << endl;
Floating point numbers with no digits after the floating point are printed as integers by default.
To always show the floating point, use setiosflags(ios::showpoint).
You can combine that with fixed and setprecision(n) I/O flags to limit how many digits to print after the floating point. For example:
double d = 5.0;
cout << setiosflags(ios::showpoint) << d << endl; // prints 5.00000
cout << setiosflags(ios::showpoint) << fixed << setprecision(1)
<< d << endl; // prints 5.0
I am having trouble getting my program to output the correct precision for my doubles. Say I have a
double d;
After running some algorithm, I get d as my output. I want to set the precision of my d variable to 4 points of precision after decimal. I should be able to do this like so:
cout << setprecision(4) << d;
The problem is that if I if d is an integer, then no decimal placed are printed.
For example, if d == 120, then my program will print 120. I want it to print 120.0000 instead. How do I get my program to always print 4 decimal places?
You need to be using fixed precision to output a fixed number of decimal points. You should to tell the stdout stream (cout) that you want to use fixed precision.
std::cout << std::fixed << std::setprecision(4) << d;
This is a small program that calculates the gaps of 'interval' size that are beetween two numbers 'from' 'to'. Then I calculate the 'size' (number of gaps) and store it in an int variable, and give me a smaller value sometimes.
Here is the code:
double from=0, to=1, interval=0.1;
cout << "WORKING WITH VARIABLES: " << endl;
double operation = (to-from)/interval +1;
cout << "Size: " << operation << endl;
int size = operation;
cout << "Size after storing: " << size << endl << endl;
cout << "WORKING WITHOUT VARIABLES: " << endl;
cout << "Size: " << (to-from)/interval +1 << endl;
size = (to-from)/interval +1;
cout << "Size after storing: " << size << endl << endl;
Problem seems to be in how it's stored interval. If interval=1 everything is good, but if is 0.1, as in the example it give me 10 instead 11 in the "Size after storing" of the second case.
I've found out that it works well with interval=0.25 (2^-2).
EDIT: I haven't found that it fails in the first case, always does in the second.
Floating point numbers are stored with a finite precision, and in binary. 0.25 is easy. That's just 1/4, so 0.01 binary. 0.1 is 1/10, which cannot be represented by a finite binary string. It's 1/16+1/32+ ...
So 1/10 is rounded down, and 10 * 1/10 is slightly less than 1.
As for the different results in the first and second case, that's probably because intermediate values are rounded to more digits than double has.
You are suffering from the inaccuracies inherent in floating point arithmetic. What you probably get in the 0.1 case is 10.999.... instead of 11. Converting double to int truncates instead of rounding so you get 10. Add a small value before converting to int to combat this.
int size=operation+0.0000000001;
If you want to convert a double that is close to an integer (due to rounding errors, as explained by others) to an int, always round the value to the nearest integer, e.g. with round(), before converting the value to int, since double to int conversion truncates the value and will yield an incorrect result if the error is negative.
I'm new in C++ and would like to get some help.
I don't understand why I'm getting an output of only one digit after the decimal on the sum below.
I have tried to solve this with no success.
int main()
{
double alt, t;
t = 4.5;
// function for calculating the altitude over time.
alt = (-0.12)*pow(t, 4) +(12.0)*pow(t, 3) -(380.0)*pow(t, 2) +(4100.0)*t +220.0;
cout << alt << endl;
return 0;
}
The default behaviour of cout is to print six significant digits of floating points. You can change that with:
cout.precision(10);
cout << alt << endl;
which gives the output:
12019.2925
which seems to be the correct solution.
You should not try to set the precision to anything higher than roughly 15, because that is about the precision limit of the double type (typically). You can use the numeric_limits<double>::digits10 from <limits> to make sure what precision you actually have.
Well, basically I was using setprecision(3), but that is rounding up the last number, for example if we do like this -
double x = 5;
x = (double) x / 3;
cout << fixed << setprecision(3) << x << endl;
It will show 1.667
But, if we do it with calculator, it will show - 1.666666666...67
So basically, what I mean is, is there any chance to output in file, just the first 3 digits after the comma, and not to round it up?
1.666666666...67 rounded to three decimal places is 1.667
If you just want to truncate the output then send it to a string with strstream, search the string for the position of "." and truncate the string 3 places beyond that
Or if you simply want to always round down, multiply the result by 1000, use floor() to round down and then divide by 1000.0 again.
A cast to long truncates the fraction part :
int main()
{
double x;
x= -100.666666666666666;
x = static_cast<double> ( static_cast<long>(x * 1000) )/1000;
cout << x << endl;
}
We could use floor(double) from cmath, which is more preferable, but it's rounds negatives to negative side either.
cout << fixed << setprecision(3) << double(int(x*1000))/1000 << endl;
we use int() to truncate the tailing digits.