On Tue, Apr 07, 2009 at 03:38:36PM +0900, Teika Kazura wrote: > I doubt if I can implement float -> int. (I'll deal with the doc.) > I don't know either what's the recommended way to do so in C. Let f a > float var. > 1. Check that the abs(f) is small enough to be int. > 2. Do (int) round(f). > Is this so?
C99 and modern Posix have lround() and llround() to round doubles to longs and long longs, respectively (lroundf() and llroundf() for floats). I don't know about portability to older systems, though. There is no function to round specifically to an int; I suppose you could take the smaller of lround(f) and (long)INT_MAX , and cast that to an int.
