我在计算处理百分比时做错了什么?
What did I do wrong with calculating percent in processing?
我正在编写一个简单的处理脚本,它执行一个随机布尔值并计算它出现 1 而不是 0 的频率。
然后我添加了一个随机完成的总金额的计数器。
我想用它来计算 1ns 的百分比。
到目前为止一切正常。
然后我想做计算。无论我做什么,它都显示 0。
代码就像
bool r; //boolean for random
int t; //t fr true or 1
int f; //f for false or 0
int cnt; //cnt for total count
float p; //for the percentage
Void draw(){
r = int(random(2));
if (r==0){int(f++);}
if (r==1){int(t++);}
if (r<100){cnt++;}
p = t / cnt * 100; //calculating percentage.
text(p,10,100); //draws text on sceen at x=10 and y=100 but it always draws 0
}
这有什么问题吗?我做错了什么?
我认为是这个问题:Why dividing two integers doesn't get a float?
所以你可以这样写:
(t * 100.0f)/cnt
我正在编写一个简单的处理脚本,它执行一个随机布尔值并计算它出现 1 而不是 0 的频率。
然后我添加了一个随机完成的总金额的计数器。 我想用它来计算 1ns 的百分比。 到目前为止一切正常。 然后我想做计算。无论我做什么,它都显示 0。 代码就像
bool r; //boolean for random
int t; //t fr true or 1
int f; //f for false or 0
int cnt; //cnt for total count
float p; //for the percentage
Void draw(){
r = int(random(2));
if (r==0){int(f++);}
if (r==1){int(t++);}
if (r<100){cnt++;}
p = t / cnt * 100; //calculating percentage.
text(p,10,100); //draws text on sceen at x=10 and y=100 but it always draws 0
}
这有什么问题吗?我做错了什么?
我认为是这个问题:Why dividing two integers doesn't get a float?
所以你可以这样写:
(t * 100.0f)/cnt