# Factorial not working "cannot convert from float to int"

**URL:** <https://discourse.processing.org/t/factorial-not-working-cannot-convert-from-float-to-int/16115>\
**Category:** Coding Questions\
**Created:** [December 4, 2019, 2:34pm UTC](https://discourse.processing.org/t/factorial-not-working-cannot-convert-from-float-to-int/16115 "2019-12-04T14:34:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsunii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tsunii/32/6822_2.png) [@tsunii](https://discourse.processing.org/u/tsunii)\
**Post date:** [December 4, 2019, 2:34pm UTC](https://discourse.processing.org/t/factorial-not-working-cannot-convert-from-float-to-int/16115/1 "2019-12-04T14:34:58Z")

</div>

I’m trying to make a simple 2d application for the “Traveller Salesmanship Problem” i’m configuring the program, i was working on everything in int, but i thougth float would be more accurate, even since i’m working with distance between 2 points.

I made a little function that gives me back the factorial, that represents the amount of paths that exist, when it was int it worked just fine, but when i changed everything to float it didn’t worked anymore, and i don’t know why…

```auto
//Quantidade de circulo]
float ct = 4;
//Comprimento e Largura de Circulo
float c1 = 6, a1 = 6;
//Contador
int i, j;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[fact(ct)];

PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
  println(c);
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final float fact(float num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculos

  if (fase==0) {
    for (i=0; i<ct; i++) {
      p[i] = new PVector(random(c1, width - c1), random(a1, height - a1));
      ellipse(p[i].x, p[i].y, c1, a1);
      //ct - 1 para a quantidade de circulos sempre ser "ct^2"
      if (cfase1 >= ct - 1) {
        fase++;
      }
    }
    cfase1++;
  } else if (fase==1) {
    for (i=0; i < fact(ct); i++) {
      for (j = 0; j < ct; j++) {
        //Calculo entre duas distancias
        c[i] = sqrt(pow(p[i+1].x - p[i].x, 2) + pow(p[i+1].y - p[i].y, 2));
        if (cfase2 >= ct) {
          fase++;
        }
        cfase2++;
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![tsunii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tsunii/32/6822_2.png) [@tsunii](https://discourse.processing.org/u/tsunii)\
**Post date:** [December 4, 2019, 2:45pm UTC](https://discourse.processing.org/t/factorial-not-working-cannot-convert-from-float-to-int/16115/2 "2019-12-04T14:45:25Z")

</div>

I just figured it out! Just needed to think a little about what the data was for!!!

```auto
//Quantidade de circulo]
int ct = 4;
//Comprimento e Largura de Circulo
float c1 = 6, a1 = 6;
//Contador
int i, j;
//Separador de Fase
float fase = 0, cfase1 = 0, cfase2 = 0, cfase3 = 0;
//Quantidade de caminhos
float[] c = new float[fact(ct)];

PVector[] p = new PVector[ct];

void setup() {
  size (200, 200);
  background(255);
  //Quantidade de caminhos
  println(c);
}

//Quantidade de caminhos, fatorial da quantidade de circulos
static final int fact(int num) {
  return num == 1? 1: fact(num - 1)*num;
}

void draw() {

  //Desenhando os Círculos

  if (fase==0) {
    for (i=0; i<ct; i++) {
      p[i] = new PVector(random(c1, width - c1), random(a1, height - a1));
      ellipse(p[i].x, p[i].y, c1, a1);
      //ct - 1 para a quantidade de circulos sempre ser "ct^2"
      if (cfase1 >= ct - 1) {
        fase++;
      }
    }
    cfase1++;
  } else if (fase==1) {
    for (i=0; i < fact(ct); i++) {
      for (j = 0; j < ct; j++) {
        //Calculo entre duas distancias
        c[i] = sqrt(pow(p[i+1].x - p[i].x, 2) + pow(p[i+1].y - p[i].y, 2));
        if (cfase2 >= ct) {
          fase++;
        }
        cfase2++;
      }
    }
  }
}

```
