Fix corner cases like 0.251f in seconds-to-milliseconds conversion
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Mon, 24 Jan 2011 10:34:03 +0000 (10:34 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Mon, 24 Jan 2011 10:34:03 +0000 (10:34 +0000)
Do this by rounding the final floating-point value to the nearest
integer rather than simply casting to int.  Thanks to Elviz for
finding and debugging this.

(Also, it sucks to have to reimplement parts of C99.)

git-svn-id: https://s.snth.net/svn/neverball/trunk@3460 78b8d119-cf0a-0410-b17c-f493084dd1d7

share/common.h

index 7f1c2f6..9d2b8c1 100644 (file)
                                    (src), \
                                    MAX(0, MAXSTRLEN(dst) - strlen(dst))))
 
-#define TIME_TO_MS(t) ((int) ((t) * 1000.0f))
+#define SIGN(n) ((n) < 0 ? -1 : ((n) ? +1 : 0))
+#define ROUND(f) ((int) ((f) + 0.5f * SIGN(f)))
+
+#define TIME_TO_MS(t) ROUND((t) * 1000.0f)
 #define MS_TO_TIME(m) ((m) * 0.001f)
 
 int   read_line(char **, fs_file);