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);
}
program won't run past this function, get the following error: Thread 1:EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0)
When you use integer maths, you should do the multiplication(s) before division(s).
Example 1 (truncation to zero)
(100 / 127) * 250
= (0) * 250
= 0
On the other hand
(100 * 256) / 127
= 25600 / 127
= 201
Example 2 (loss of precision)
(1000 / 27) * 60
= (370) * 60
= 2220
On the other hand
(1000 * 60) / 27
= (60000) / 127
= 2222
Try this:
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 ;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments