1.0 is the only case that can go wrong, so handle that case separately:
b = floor(f >= 1.0 ? 255 : f * 256.0)
Also, it might be worth forcing that f really is 0<=f<=1 to avoid incorrect behaviour due to rounding errors (eg. f=1.0000001).
f2 = max(0.0, min(1.0, f))
b = floor(f2 == 1.0 ? 255 : f2 * 256.0)
Alternative safe solutions:
b = (f >= 1.0 ? 255 : (f <= 0.0 ? 0 : (int)floor(f * 256.0)))
or
b = max(0, min(255, (int)floor(f * 256.0)))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…