For situations where we *definitely* want rounding to zero regardless of
any environment settings there is a variant of the truncf method in place. As I
work on areas I'm converting these so we have a single function for all cases,
and if it ever needs to be modified it only has to be done in globals.h
However there are quite a lot of times where we want rounding to nearest, and
from what I've read, to avoid an anomaly about zero, 0.5 needs to go towards 1,
and -0.5 needs to go to zero (or the other way round). i.e. everything moves in
the same direction.
What I'm proposing is the following:
inline int F2SI(float n) // float to symetrical int
if (n >= 0.0f)
return int(n + 0.5f);
return int(n - 0.49999995f);
This function runs very slightly faster than lrint(), without without compiler
optimisation and considerably faster with -O3. It appears to check out
numerically to reasonable precision, and handling large groups of borderline
additions gives exactly the same result.
0.5f -> 1
0.49999995f -> 0
-0.5f -> 0
-0.5000001f -> -1
I haven't actually applied this, so if you think there is something I've missed
please do shout out!
Finally, let me know if you want to examine my proof of concept code.
Will J Godfrey
Say you have a poem and I have a tune.
Exchange them and we can both have a poem, a tune, and a song.
Yoshimi source code is available from either:
Our list archive is at: https://www.freelists.org/archive/yoshimi
To post, email to yoshimi@xxxxxxxxxxxxx