On Sat, 24 Nov 2018 11:14:06 +0100 Fons Adriaensen <f...@linuxaudio.org> wrote: <snip> >The dangerous thing to do is: > >// given float p > >int i = (int) floorf (p); >float f = fmodf (p, 1.0f); > >as you could end up with i + f != p. > >The safe way is of course: > >int i = (int) floorf (p); >float f = p - i; > > >Ciao, >
Thanks very much for that Fons :) I'd been mulling over *exactly* that point for some time. My reasoning being that in the latter case, if the integer was slightly wrong then using it for the subtraction should give a remainder that was also slightly wrong, but in a direction that tends to compensate for the error. The other thing, is why do we need the floorf? Or in the original example (which was taken from working code) truncf? A straight cast to int seems to give exactly the same result, and is at least twice as fast with -O3 and about 4 times as fast unoptimised. -- Will J Godfrey http://www.musically.me.uk Say you have a poem and I have a tune. Exchange them and we can both have a poem, a tune, and a song. _______________________________________________ Linux-audio-dev mailing list Linux-audio-dev@lists.linuxaudio.org https://lists.linuxaudio.org/listinfo/linux-audio-dev