Loi binomiale

leejunki159 Messages postés 4 Date d'inscription mardi 21 avril 2015 Statut Membre Dernière intervention 13 mai 2015 - Modifié par Whismeril le 13/05/2015 à 23:33
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 14 mai 2015 à 09:38
Bonjour,
j'ai un problème dans mon code java lorsque j'entre n=170 le résultât s'affiche mais si j'entre n=171 il me donne "infinity " au lieu d'une résultat numérique pouvez vous m'aider svp



package loi.normale;

import java.math.BigInteger;
import java.util.Scanner;


public class somme {
   

 public static double factorielle(double x) {
 double i;
 double result=1;
 if(x >= 1) {
  for(i=x;i>1;i--) {
   result = result*i;
  }
  return result;
 }
 else if(x == 0) return 1;
 return 0;
}
public static double compter_combinaisons(double k,double n) {
 double z = n-k;
 return factorielle(n)/(factorielle(k)*factorielle(z));
}   
    

   public static double som(float a,float b,int n,float p ) {
 double s = 0;

for(float i=  a;i<=b;i++){

    s=s+compter_combinaisons(i,n)*Math.pow(p, i)*Math.pow((1-p), (n-i));
   
            }
  return s;
}
public static void main(String args[] ) {
    int i;
    int n;
  
            float  b,a;
            double s;
    Scanner sc=new Scanner(System.in);


System.out.println("a=");
   a=sc.nextFloat();
  System.out.println("b="); 
   b=sc.nextFloat();
   System.out.println("n=");
  n=sc.nextInt();


    s= som(a,b,n, (float) 0.5);

        
        System.out.println("binomiale :"+s);


}
     
   }


EDIT: Ajout de la coloration syntaxique.

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 127
14 mai 2015 à 09:38
Bonjour,

Tu trouveras des explications sur les limites du calcul de la factorielle sur http://codes-sources.commentcamarche.net/source/100781-calcul-de-la-factorielle-pour-de-grands-nombres
0
Rejoignez-nous