T
T
TgFox2020-11-18 14:17:26
C++ / C#
TgFox, 2020-11-18 14:17:26

How to shorten to 4 decimal places, for cos2 and sin2?

class SinCos
{
public readonly double Sin2;
public readonly double Cos2;

public SinCos(double x)
{
Sin2 = square(Math.Sin(x));
Cos2 = square(Math.Cos(x));
Math.Round(Sin2, 4);
}

private double square(double x)
{
return x * x;
}

public override string ToString()
{
return "Sin2a = " + Sin2 + " ; Cos2a = " + Cos2;
}
}

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Table of values ​​from –pi to pi in increments of pi/4.");
Console.WriteLine("\n");
double begin = -Math.PI;
double end = Math.PI;
double step = Math.PI / 4.0;
end += step / 2.0;
for (double i = begin; i < end; i += step)
{
Console.WriteLine("i = {0:f4}", i);
Console.WriteLine(new SinCos(i));
Console.WriteLine("");
}
}
}
}5fb502b0bb166773452291.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cicatrix, 2020-11-18
@TgFox

The solution to your problem lies in your own code.
Meditate on these lines and ask yourself why i is output in the right format but SinCos is unformatted:

Console.WriteLine("i = {0:f4}", i);
Console.WriteLine(new SinCos(i));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question