The inner workings of the granary is still abit of a mystery to me too, but there are alot of resources.
This one seem to be old, but I think all the mechanics are the same.
https://www.civfanatics.com/civ4/st...echanics-of-food-growth-granary-and-whipping/
It appears that first food is harvested/stored and only then is production evaluated.
So the food you harvest the turn granary is finished is "lost".
If you have a food bar at 18/24 at pop 2 and have a granary ready to 1-pop whip at 35/60. And your food source is a dry rice (4f).
You can then whip and be at 18/22, and with +4 ready to harvest your city will grow.
However, if you use "avoid growth" for one turn, you will start the next turn with a finished (empty) granary and 22/22 food in the bar.
If you then resume growth, you will grow to pop2, and the4 food you harvested will be stored in the granary and applied to the food bar, landing you at 8/24 the next turn.
T0: 18/24 (pop2, whipped to 18/22 pop1)
T1: 22/22 (pop1)
T2: 8/24
If you had not stopped growth:
T0: 18/24 (pop2, whipepd to 18/22 pop1)
T1: 0/24 (pop2)
T2: 4/24 (pop2) PLUS whatever yield that extra pop could have provided.
If you are looking for a general guideline, then always whip the granary when you are at half a food bar or slightly above.