O
O
Ogurchik-0072021-10-05 07:30:15
C++ / C#
Ogurchik-007, 2021-10-05 07:30:15

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);

}


and each time the values ​​\u200b\u200bof "y" display a different number of decimal places after the comma

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Ocelot, 2021-10-05
@Ocelot

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 question

Ask a Question

731 491 924 answers to any question