N
N
novichok1514132020-12-14 18:18:51
C++ / C#
novichok151413, 2020-12-14 18:18:51

Why does it output int y and not double?

I'm doing the task, display all the values ​​of the function y = sin(4x) - 2 on the segment [-p, p] with a step p/8.

Wrote code:

GetPut.cpp file code

#include <iostream>
using namespace std;

void GetAB(float& a, float& b)
{
  setlocale(LC_ALL, "rus");
  cout << "Отрезок [-P, P]" << endl;
  cout << "Шаг табулирования: P / 8" << endl;
}

void Put(double y)
{
  setlocale(LC_ALL, "rus");
  cout << endl << "Последнее значение функции равно: " << y << endl;
}


resh.cpp file code

#include <cmath>
#include <iostream>
using namespace std;

double mainFunc(double x)
{
  return sin(4 * x) - 2;
}

void cycle(double& y, float a, float b)
{
  int n = 16; // максимальное число входов, т.к. Пи/8 помещается в заданный отрезок 16 раз
  double xMin = -acos(-1); 
  double xMax = acos(-1);
  cout << "Таблица значений функции" << endl;
  cout << "\tx=\ty=" << endl;
  for (xMin; xMin < xMax; xMin += 0.392699)
  {
    y = mainFunc(xMin); // вычисление по функции
    cout << endl << '\t' << xMin << '\t' << y; // вывод и вычисления
  }
}


main.cpp

#include <iostream>

void GetAB(float& a, float& b);
void Put(double y);
void cycle(double&, float, float);

int main()
{
  float a, b;
  double y;
  GetAB(a, b);
  cycle(y, a, b);
  Put(y);
  system("PAUSE");
  return 0;
}


The output of x is normal (with an accuracy of 5 digits after the decimal point), but y fails for some reason. Why?
It is worth adding that the last value of y is written with decimal places (2.00001)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2020-12-14
@novichok151413

What did you want to get?
Sin(4x) values ​​in pi/8 increments are 0, 1, -1.
If you want to output with zeros (-2.0000000) - read the docs on cout (cout.precision, std::fixed)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question