Commit 8574c0815cfab3952c8cf4c97a5c4467a1a18f55

Sam Lantinga 2013-11-27T00:29:46

Fixed bug 2274 - SDL_ceil is incorrectly implemented when HAVE_LIBC is not defined Ghassan Al-Mashareqa The SDL_ceil function is implemented incorrectly when HAVE_CEIL is not defined (HAVE_LIBC not defined). The following code: double val = SDL_ceil(2.3); printf("%g", val); prints "2.0", as STD_ceil is defined as: double SDL_ceil(double x) { #ifdef HAVE_CEIL return ceil(x); #else return (double)(int)((x)+0.5); #endif /* HAVE_CEIL */ } This functions is used in the SDL_BuildAudioResampleCVT function of the audio subsystem (SDL_audiocvt.c), and causes a bug in that function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
diff --git a/src/stdlib/SDL_stdlib.c b/src/stdlib/SDL_stdlib.c
index 2ee62be..aa57d41 100644
--- a/src/stdlib/SDL_stdlib.c
+++ b/src/stdlib/SDL_stdlib.c
@@ -52,7 +52,12 @@ SDL_ceil(double x)
 #ifdef HAVE_CEIL
     return ceil(x);
 #else
-    return (double)(int)((x)+0.5);
+    double integer = SDL_floor(x);
+    double fraction = x - integer;
+    if (fraction > 0.0) {
+        integer += 1.0;
+    }
+    return integer;
 #endif /* HAVE_CEIL */
 }