11

I'm using Visual Studio 2015 on Windows 10, I'm still a new coder, I've just started to learn C#, and while I was in the process, I discovered the Math class and was just having fun with it, till the console outputted: " ∞ "

It's a Console Application

Here's the code:

var k = Math.Sqrt((Math.Pow(Math.Exp(5), Math.E)));
var l = Math.Sqrt((Math.Pow(Math.PI, Math.E)));
Console.WriteLine("number 1 : " + k);
Console.WriteLine("number 2 : " + l);
Console.ReadKey();
var subject = Math.Pow(Math.Sqrt((Math.Pow(Math.PI, Math.E))), Math.Sqrt((Math.Pow(Math.Exp(5), Math.E))));
Console.WriteLine(k + " ^ " + l + " = " + subject);
Console.ReadKey();
//output  :
/*number 1 : 893.998923601492
 number 2 : 4.73910938029088
 893.998923601492 ^ 4.73910938029088 = ∞*/

Why is this happening? using normal calculator the result is: 96985953901866.7

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ahmed Alani
  • 137
  • 4

1 Answers1

19

Because you are doing

var subject = Math.Pow(l, k);

instead of

var subject = Math.Pow(k, l);

You are inverting base with exponent!

And you should really reuse your variables, instead of recalculating everything! (had you reused the variables, the problem would have been more apparent).

xanatos
  • 109,618
  • 12
  • 197
  • 280