Permalink
Browse files

allow float/double switch

  • Loading branch information...
1 parent c8f8f83 commit ee30c5a4a44fc26a8b5b8d6a0e35a7a4b6a6be50 @matz matz committed Apr 27, 2012
Showing with 15 additions and 32 deletions.
  1. +9 −2 include/mrbconf.h
  2. +6 −30 src/numeric.c
View
@@ -8,12 +8,19 @@
#define MRUBYCONF_H
#include <stdint.h>
+#define MRB_USE_FLOAT
+
+#ifdef MRB_USE_FLOAT
+typedef float mrb_float;
+#define readfloat(p) strtof((p),NULL)
+#else
typedef double mrb_float;
+#define readfloat(p) strtod((p),NULL)
+#endif
+
typedef int32_t mrb_int;
typedef intptr_t mrb_sym;
-
#define readint(p,base) strtol((p),NULL,(base))
-#define readfloat(p) strtod((p),NULL)
#undef INCLUDE_ENCODING /* not use encoding classes (ascii only) */
#define INCLUDE_ENCODING /* use UTF-8 encoding classes */
View
@@ -101,29 +101,14 @@ const unsigned char mrb_nan[] = "\x00\x00\xc0\x7f";
const unsigned char mrb_nan[] = "\x7f\xc0\x00\x00";
#endif
-extern double round(double);
-
-#ifndef HAVE_ROUND
-double
-round(double x)
-{
- double f;
-
- if (x > 0.0) {
- f = floor(x);
- x = f + (x - f >= 0.5);
- }
- else if (x < 0.0) {
- f = ceil(x);
- x = f - (f - x >= 0.5);
- }
- return x;
-}
+#ifdef MRB_USE_FLOAT
+#define round(f) roundf(f)
+#define floor(f) floorf(f)
+#define ceil(f) ceilf(f)
+#define floor(f) floorf(f)
+#define fmod(x,y) fmodf(x,y)
#endif
-
-
-
void mrb_cmperr(mrb_state *mrb, mrb_value x, mrb_value y);
void
@@ -459,16 +444,7 @@ flodivmod(mrb_state *mrb, mrb_float x, mrb_float y, mrb_float *divp, mrb_float *
mrb_float div, mod;
if (y == 0.0) mrb_num_zerodiv(mrb);
-#ifdef HAVE_FMOD
mod = fmod(x, y);
-#else
- {
- mrb_float z;
-
- modf(x/y, &z);
- mod = x - z * y;
- }
-#endif
if (isinf(x) && !isinf(y) && !isnan(y))
div = x;
else

4 comments on commit ee30c5a

@takeru
Contributor
takeru commented on ee30c5a Apr 27, 2012

この変更以降OSXでビルドできなくなりました。

@takeru
Contributor
takeru commented on ee30c5a Apr 28, 2012

寝ぼけていた、Linuxでした。
2.6.26-2-xen-686 #1 SMP Thu Nov 25 02:32:31 UTC 2010 i686 GNU/Linux

% gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.2-1.1' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-cld --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.3.2 (Debian 4.3.2-1.1)

@takeru
Contributor
takeru commented on ee30c5a Apr 28, 2012

% make
make -C mrblib --no-print-directory CC=gcc LL=gcc
make -C ../tools/mrbc --no-print-directory CC=gcc LL=gcc
gcc -Wall -Werror-implicit-function-declaration -g -MMD -I../../src -I../../src/../include -c ../../src/../tools/mrbc/mrbc.c -o ../../src/../tools/mrbc/mrbc.o
gcc -Wall -Werror-implicit-function-declaration -g -MMD -I../../src -I../../src/../include -c ../../src/array.c -o ../../src/array.o
gcc -Wall -Werror-implicit-function-declaration -g -MMD -I../../src -I../../src/../include -c ../../src/ascii.c -o ../../src/ascii.o
gcc -Wall -Werror-implicit-function-declaration -g -MMD -I../../src -I../../src/../include -c ../../src/cdump.c -o ../../src/cdump.o
gcc -Wall -Werror-implicit-function-declaration -g -MMD -I../../src -I../../src/../include -c ../../src/class.c -o ../../src/class.o
gcc -Wall -Werror-implicit-function-declaration -g -MMD -I../../src -I../../src/../include -c ../../src/codegen.c -o ../../src/codegen.o
../../src/codegen.c: In function 'codegen':
../../src/codegen.c:1517: error: implicit declaration of function 'strtof'
make[2]: *** [../../src/codegen.o] Error 1
make[1]: *** [../bin/mrbc] Error 2
make: *** [src/mrblib/mrblib.o] Error 2

@matz
Member
matz commented on ee30c5a Apr 28, 2012

strtof() seem to not be available on all platforms. I will use strtod() even when float is chosen.
Fixed in 0c5eba8.

Please sign in to comment.