-1

This is the code

This is the Output I am getting

Console.Write("Enter angle in degrees: ");

// angle == 90
float angle = float.Parse(Console.ReadLine());
float radian = angle * (MathF.PI / 180);

Console.WriteLine("Sine is " + MathF.Sin(radian) );

// Expected "Consine is 0"
// Actual   "Cosine is -4.371139e-08"
Console.WriteLine("Cosine is " + MathF.Cos(radian));

Why do I have -4.371139e-08 instead of 0?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215

1 Answers1

2

Let's do a simple math:

cos(pi/2 + x) = -sin(x) = - x + x^3/3! - x^5/5! + ... ~ -x # for small x

In our case:

90 * (MathF.PI / 180) == 1.5707963705062866 
PI / 2                == 1.5707963267948966... # note the difference

So

90 * (MathF.PI / 180) == PI / 2 + 4.371139006309477E-08

That's why MathF.Cos(90 * (MathF.PI / 180)) expected to be -4.371139006309477E-08, not 0.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215