C++, output of one digit after the decimal -


i'm new in c++ , help. don't understand why i'm getting output of 1 digit after decimal on sum below. have tried solve no success.

int main() {     double alt, t;      t = 4.5;     // function calculating altitude on 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 print 6 significant digits of floating points. can change with:

cout.precision(10); cout << alt << endl; 

which gives output:

12019.2925 

which seems correct solution.

you should not try set precision higher 15, because precision limit of double type (typically). can use numeric_limits<double>::digits10 <limits> make sure precision have.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -