Why Am I Getting "NaN" Output ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why Am I Getting "NaN" Output ?

I am trying to code a permutation calculator for math hw.. The problem is that when I use big numbers like 555,55 or 999,99 etc. the proggramme gives me the "NaN" output :( I am the noobest noob and just don't get it plz help ! using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Permütasyon { class Program { static void Main(string[] args) { // degiskenler ve sonucun tanimlarini yazdim (sonuca ulasmak icin 2 asama/fonksiyon kullandim): double nege = double.Parse(Console.ReadLine()); double rege = double.Parse(Console.ReadLine()); double sonuc = nPermutasyon(nege, rege); Console.WriteLine(sonuc + " Doğru mu ? :D"); Console.WriteLine("Çıkmak için Enter tuşuna basın..."); Console.ReadLine(); } // permutasyon isleminin fonksiyonunu yazdim (asagidaki fonksiyondan alinan degerler ile bu fonksiyondaki islem yapilarak permutasyon yapmis oluyoruz): static double nPermutasyon(double n, double r) { if (n == r) return faktoriyel(n); else return faktoriyel(n) / faktoriyel(n - r); } // faktoriyel isleminin fonksiyonunu yazdim (bu islemler yapilip yukaridaki fonksiyona yaziliyor): static double faktoriyel(double n) { if (n == 1) return 1; else return n * faktoriyel(n - 1); } } }

27th Nov 2017, 7:18 PM
Esas Çocuk
Esas Çocuk - avatar
2 Answers
+ 2
If you WriteLine() nege and rege, you'll notice 555,55 is parsed as 55555. That many options quickly overflows your data types. For example, this input is already 2.48 x 10^105: 111 55 [In the future you can link your code with the sideways < symbol with three dots on it]
27th Nov 2017, 7:37 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
NaN means 'Not a Number', it can help
27th Nov 2017, 7:53 PM
Elegis Sossou
Elegis Sossou - avatar