# Problem with dividing

**URL:** <https://discourse.processing.org/t/problem-with-dividing/16071>\
**Category:** Beginners\
**Created:** [December 3, 2019, 8:24am UTC](https://discourse.processing.org/t/problem-with-dividing/16071 "2019-12-03T08:24:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [December 3, 2019, 8:24am UTC](https://discourse.processing.org/t/problem-with-dividing/16071/1 "2019-12-03T08:24:38Z")

</div>

Hi! I am currently making a program that draws a variety of graphs, and I’ve finished with the bar graph and line graph. They work well, and I am trying to add another function: a pie graph.  
I am making the drawing part first before I put it together with the entire code, but it does not work well. I think there is a problem with the dividing part. However, I cannot find out the problem. I saved the results as a float variable just in case int can not hold the answer, but it still always ends up with 0. Can someone help me find the problem? The code is below. I boldened the part that causes the problem.

```auto

import javax.swing.JOptionPane;
PFont f;

void setup()
{
  background(0);
  size(2000, 4000);
  textSize(30);
  arr = new IntList();
  deg = new IntList();
  arr.append(8);
  arr.append(9);
  arr.append(6);
  arr.append(8);
  arr.append(9);
  arr.append(6);
  arr.append(6);
  num=7;
  //title = 
  title = "dss";
 
}

int num=0;
String title = "Untitled #1";
IntList arr;
IntList deg;

int a = 0;

void draw()
{
  int hap = 0;//Is the sum(hap means sum in Korean:))
  
  for(int i = 0; i < num; i++){
    hap += arr.get(i);
  }
//////////////////////////////////////////////////////////////problem part starts//////////////////////////////////////////////////////////////////////
  float a;
  for(int i = 0; i < num; i++){
    a = arr.get(i) / hap;
    deg.append((int)a * 360);
    println(i+"- a : "+a);
    println(i+" : "+deg.get(i));
  }
//////////////////problem part ends////////////////////////////
  println("hap ="+hap);
  int before = deg.get(0);
  arc(350, 350, 380, 380, radians(0), radians(deg.get(0)));
  for(int i = 1; i < num; i++){
    fill(i*107%255, i*61%255, i*162%255);
    arc(350, 350, 380, 380, radians(before), radians(deg.get(i)));
    before = deg.get(i);
  }
  
  
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 3, 2019, 10:55am UTC](https://discourse.processing.org/t/problem-with-dividing/16071/2 "2019-12-03T10:55:06Z")

</div>

> [@24stevek](#):
>
> a = arr.get(i) / hap;

if it’s the integer division problem use

```auto
a = arr.get(i)/(float)hap; // PCT as 0 .. 1.0

```

* * *

```auto
// integer division problem

float f;
int a = 3, b = 2;

f = a/b;
println(f);

f = a/(float)b;
println(f);

/* console:
1.0
1.5
*/

```

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [December 3, 2019, 11:51am UTC](https://discourse.processing.org/t/problem-with-dividing/16071/3 "2019-12-03T11:51:32Z")

</div>

> [@kll](#):
>
> (float)

Thank you, but it still doesn’t work…  
I think it’s supposed to be the right method of calculating the degrees, isn’t it?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 3, 2019, 12:29pm UTC](https://discourse.processing.org/t/problem-with-dividing/16071/4 "2019-12-03T12:29:11Z")

</div>

no, rad or deg?

```auto
deg = a * 360;
rad = a * TAU;

```

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [December 3, 2019, 2:04pm UTC](https://discourse.processing.org/t/problem-with-dividing/16071/5 "2019-12-03T14:04:00Z")

</div>

Umm… I don’t know. Maybe I should try it both.(Although I eventually change into rad later on)  
Thank you for your replies!

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 3, 2019, 4:25pm UTC](https://discourse.processing.org/t/problem-with-dividing/16071/6 "2019-12-03T16:25:00Z")

</div>

You have 2 times the variable a. This might cause problems. Once a global int a and then the float a in your method…

What i assume happens :

```auto
//Global
int a = 0;

//method

float a;
  for(int i = 0; i < num; i++){
    a = arr.get(i) / hap; // float version of a is set. 
    deg.append((int)a * 360);//int version of a (meaning it‘s never set, so only gives 0)
    println(i+"- a : "+a); //these might also be the Int version. 
    println(i+" : "+deg.get(i)); // deg was only set to Int a, which was always 0
  }

```

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [December 4, 2019, 8:19am UTC](https://discourse.processing.org/t/problem-with-dividing/16071/7 "2019-12-04T08:19:27Z")

</div>

Oh! Thanks for that, I didn’t realize it. But it doesn’t seem to be the problem…  
I am about to get nuts trying to find this thing… Maybe it’s the whole function itself that causes the problem?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 4, 2019, 10:26am UTC](https://discourse.processing.org/t/problem-with-dividing/16071/8 "2019-12-04T10:26:42Z")

</div>

possibly

```auto
(int)a * 360

```

is not same as

```auto
(int)( a * 360 )

```

esp. if  
a = 0 … 1.0

* * *

```auto
float a = 0.3;
println("(int)a * 360 "+((int)a * 360) );
println("(int)( a * 360 ) "+((int)( a * 360 )) );

/*

(int)a * 360 0
(int)( a * 360 ) 108

*/

```

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [December 4, 2019, 12:03pm UTC](https://discourse.processing.org/t/problem-with-dividing/16071/9 "2019-12-04T12:03:12Z")

</div>

> [@kll](#):
>
> float a = 0.3; println("(int)a \* 360 “+((int)a \* 360) ); println(”(int)( a \* 360 ) "+((int)( a \* 360 )) );

Real thanks! I didn’t know that!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 4, 2019, 12:16pm UTC](https://discourse.processing.org/t/problem-with-dividing/16071/10 "2019-12-04T12:16:25Z")

</div>

sorry, where is a JAVA documentation what talks about that?

- first ()a
- second a \* b
- third c + d

* * *

anyhow if a program / logic / function not does what you think it should  
use heavy diagnostic printing  
`println("function name : variable "+variable);`  
a good trick is

```auto
boolean diagp = true; // false;

//..

if ( diagp ) println("function name : variable "+variable);

```

and when testing finished disable all printing

---

<div class="post-metadata">

**Author:** ![hellonearthis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hellonearthis/32/3482_2.png) [@hellonearthis](https://discourse.processing.org/u/hellonearthis)\
**Post date:** [December 4, 2019, 7:56pm UTC](https://discourse.processing.org/t/problem-with-dividing/16071/11 "2019-12-04T19:56:42Z")

</div>

> [@24stevek](#):
>
> / hap;

Instead of using divide (and hitting divide by zero problem) you could multiply.  
So if you wanted to do width/2 you could put width\*0.5

Or if you don;t what to work out the percentage. You could try this hack hap = 0.000001

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 4, 2019, 8:18pm UTC](https://discourse.processing.org/t/problem-with-dividing/16071/12 "2019-12-04T20:18:10Z")

</div>

To change Division to multiplication with values that were intended for Division you need to take the inverse, which is a division, and therefore basically comes down to the same.

In your example, you have the Original of :  
width/2, where 2 is the factor. To get width/0.5, you need to take the inverse of /2, which is 1/2 = 0.5, so you could just as well use width/2 from the beginning, since it ends up with dividing through the value Anyway.

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [December 5, 2019, 8:03am UTC](https://discourse.processing.org/t/problem-with-dividing/16071/13 "2019-12-05T08:03:00Z")

</div>

Oh, Thanks! i didn’t think of that.

---

<div class="post-metadata">

**Author:** ![24stevek](https://avatars.discourse-cdn.com/v4/letter/2/edb3f5/32.png) [@24stevek](https://discourse.processing.org/u/24stevek)\
**Post date:** [December 5, 2019, 8:03am UTC](https://discourse.processing.org/t/problem-with-dividing/16071/14 "2019-12-05T08:03:29Z")

</div>

Oh, Thanks! That was useful.
