Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: The console is outputting infinite (∞) [closed]

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

like image 512
Ahmed Alani Avatar asked Mar 21 '16 09:03

Ahmed Alani


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


1 Answers

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

like image 63
xanatos Avatar answered Sep 18 '22 17:09

xanatos