Search code examples
c#doubleinfinity

Double value being set to +Infinity or NaN


I am currently making an AI that plays liars dice, and want to calculate the chance of a bid being true, so I am using this formula (with d = sum of all dices and n = my bet of how many equal dices are (like 4 2)): Liars Dice Chances Formula

Only problem is the values get so long on the decimal end that it becomes +infinity or NaN if I try anything like Math.Round If it helps this is the code(Players * 5 for the 5 dice each player has) :

ChanceOfTruth = 0
    for (int i = CurrentBid; i <= Players * 5; i++) 
        {
         ChanceOfTruth += (Factorial(Players * 5)/(Factorial(i)*Factorial(Players*5-i)))*Math.Pow(1f/6f,i)*Math.Pow(5f/6f,Players*5-i);   
         }
    public static double Factorial(double num) 
            { 
                for(int i = (int)num - 1; i > 0; i--) 
                {
                    num *= i;
                }
                return num;
            }

Solution

  • Checking again the problem was my factorial function didn't have a proper result for 0, so this fixes it

    public static double Factorial(double num) 
                { 
                    if(num == 0)
                    {
                        return 1;
                    }
                    for(int i = (int)num - 1; i > 0; i--) 
                    {
                        num *= i;
                    }
                    return num;
                }