Answer the question
In order to leave comments, you need to log in
Why does it output a different number of decimal places?
Wrote a program
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main(){
setlocale(LC_ALL, "Russian");
float t,y=0;
int a=10, c=5;
cout<<"Введите значение t от 1 до 8"<<endl;
cout<<"t=";
cin>>t;
cout<<" ________________________________"<<endl;
cout<<"|Значения функции y в зависимости|"<<endl;
cout<<"| от занчения t с шагом 0,5 |"<<endl;
cout<<" --------------------------------"<<endl;
do{
if (t>5){
y=log10(t)-log(c);
cout<<"| t="<<setw(3)<<t<<setw(8)<<"|"<<setw(8)<<"y="<<setprecision(4)<<y<<" |"<<endl;
cout<<" --------------------------------"<<endl;
}
else {
if (t==5){
y=pow(a,3)+c;
cout<<"| t="<<setw(3)<<t<<setw(8)<<"|"<<setw(11)<<"y="<<setprecision(4)<<y<<" |"<<endl;
cout<<" --------------------------------"<<endl;
}
else {
if (t<5){
y=exp(-2*t)*sqrt(a*c);
cout<<"| t="<<setw(3)<<t<<setw(8)<<"|"<<setw(10)<<"y="<<setprecision(4)<<y<<"|"<<endl;
cout<<" --------------------------------"<<endl;
}
}
}
t+=0.5;
}while(t>=1 && t<=8);
}
Answer the question
In order to leave comments, you need to log in
Setprecision sets not the number of decimal places, but the number of significant digits.
double pi =3.1415926;
std::cout << std::setprecision(5) << pi << '\n';
// 3.1416
std::cout << std::setprecision(5) << pi*100 << '\n';
// 314.16
std::cout << std::setprecision(5) << pi/100 << '\n';
// 0.031416
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question