int getTempo()
{
int tempo;
//User can enter tempo in bpm
tempo = (aserveGetControl(07) / 127) * 250;
//equation to convert tempo in bpm to an integer in ms to use with aserveSleep
return ((1000/tempo) * 60);
}
程序无法运行超过此函数,请获取以下错误:
线程1:EXC_算术(代码=EXC_I386_DIV,子代码=0x0)
最佳答案
当你使用整数数学时,你应该在除法之前做乘法运算。
示例1(截断为零)
(100 / 127) * 250
= (0) * 250
= 0
另一方面
(100 * 256) / 127
= 25600 / 127
= 201
例2(精度损失)
(1000 / 27) * 60
= (370) * 60
= 2220
另一方面
(1000 * 60) / 27
= (60000) / 127
= 2222
试试这个:
int getTempo()
{
int tempo;
//User can enter tempo in bpm
tempo = (aserveGetControl(07) * 250) / 127;
//equation to convert tempo in bpm to an integer in ms to use with aserveSleep
return (60 * 1000) / tempo ;
}