# Problem with dividing

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.

``````
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);
for(int i = 1; i < num; i++){
fill(i*107%255, i*61%255, i*162%255);
before = deg.get(i);
}

}
``````
1 Like

if it’s the integer division problem use

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

``````// 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
*/

``````
2 Likes

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?

``````deg = a * 360;
``````
1 Like

Umm… I don’t know. Maybe I should try it both.(Although I eventually change into rad later on)

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 :

``````//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
}
``````
1 Like

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?

possibly

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

is not same as

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

esp. if
a = 0 … 1.0

``````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

*/
``````
2 Likes

Real thanks! I didn’t know that!

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

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

//..

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

``````

and when testing finished disable all printing

1 Like

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

2 Likes

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.

2 Likes

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

Oh, Thanks! That was useful.

1 Like