1. ## Modulus question!

Hi ppl! ...I have a quick question! ...I am designing an audio effect algorithm; flanger. I am currently working on the problem of interpolation. Now my problem lies here:

If sample rate is 22.4 kHz, I can work out that the current delay will map exactly to any one sample in a 1second buffer by:

SAMPLE_RATE / 1000 - to give number of samples per second.

So: every ms, 22.4 samples are saved. If my delay time is [for example] 5ms, I have:

5 * 22.4 = 112 - this is a whole number! - can take the 112th sample and return!

BUT! If my delay time is 4ms:

4 * 22.4 = 89.6 - not a whole number!

SO, in this case, i need to take an average of the 89th sample and the 90th sample in the buffer!

My problem is this! how can I check to see if the number calculated is a whole number?

Code:
```tmp = currentDelay * (SAMPLE_RATE / 1000);
if (tmp = whole number) {
/* can simply return the sample at point currentDelay time from current sample! */
/* find where this sample is in the buffer - currentDelay samples from writePtr */
return audioBuffer[pointer - tmp];
}
else {
/* Have to return the average of the two nearest neighbours - get whole number part */
return(average(audioBuffer[pointer - tmp], audioBuffer[pointer-(tmp+1)]));
}

/* NOTE: Please - don't say my return statements are false because I am using array wrap around checks in my code! i.e:
if (tmp < pointer){
sample is at other end of array! calculate its position!
}
I just haven't put it here because it would be about 10 lines longer!!! */```
Thanks for any help!!! Matt.

2. I would do something like:

Code:
```if((float)((int)tmp + 1) - tmp >= 0.1f)
{
...
}```
The int cast drops the decimals if there are any, then you add one, convert back to float and substract the original number from it. If you had for example 89.6f, converting to an int would give you 89, adding one would get 90 and converting back to float would get 90f. Then you substract the original number (89.6f) to 90f which gives you 0.4f and compare that with your degree of acceptability which I set to 0.1f. If you want more precision, just make it smaller.

3. Originally Posted by Desolation
I would do something like:

Code:
```if((float)((int)tmp + 1) - tmp >= 0.1f)
{
...
}```
AH!!! Very clever!!! I like what you have done there!!! would never have thought of that!!!! many thanks!!!!!

tmp + 1

casting to a float

then subtracting tmp

which would give a value between 0-1 if it was not a whole number!

I guess it could be better to do:

Code:
`if((float)((int)tmp + 1) - tmp != 1)`
would this work better? Thanks!

4. Originally Posted by peterchen
this all so works

Code:
```if(tmp>(int)tmp) {
}```
ah! I like that better! far more effiecient!!! especially when doing Real Time DSP! I need as much efficiency as i can get!!! thanks a lot!!!!

5. I don't think so because of the way floating integers are represented by the computer (or whatever). You can do the following and never get the first message:

Code:
```float a = 0.7f;
if(a == 0.7f)
printf("First");
else
printf("Second");```
This is because floating point numbers are represented as very close approximations, if I recall. Like I said, you can still make your "degree of acceptability" way smaller like 0.001f which is more than acceptable I think. I doubt the difference could be heard.

Edit: Yep, definitely better, peterchen. =)

6. Cheers Guys For Your Help!!!!

7. note just realized it doesn't work for negative nums

8. Time can't be negative anyway ;-)

Edit: My flanger (on my multi-fx pedal for my guitar) ranges from 0 to 100.

9. HAHA!!! Yes... of course!!! ...I guess I'm lucky in this case that I will never have a negative delay time! ...this would be impossible as it would mean going into the future! and those samples have not even been taken!!!!

Thanks again!

10. regarding
Code:
```float a = 0.7f;
if(a == 0.7f)
printf("First");
else
printf("Second");```
i did get first message

11. Ha ! I tried to reproduce the behavior I told you guys and the following code did it:
Code:
```	float a = 0.5415;
if(a == 0.5415)
std::cout << 1;```
It doesn't output anything. (0.5415 is just some random number by the way)

12. Code:
```	float a = 0.5415;
if(a == 0.5415f)
std::cout << 1;```
Now try this.
Kurt
EDIT:
Reason 0.5415 is not a float ( its a double literal ).

13. Oops. My mistake. I swear I'd seen something like that and the equation wasn't true. Mmm.. Oh well, let's just forget it =P