diff options
| author | Paolo Bosetti <[email protected]> | 2012-08-06 15:02:03 +0200 |
|---|---|---|
| committer | Paolo Bosetti <[email protected]> | 2012-08-06 15:02:56 +0200 |
| commit | aa0d2f91447c49363059f2e95cb9023f65a6fbef (patch) | |
| tree | 2cfa325956e62648f2161564adfdf6dddc45b737 /src/math.c | |
| parent | fd097b8aff7b91bd105fc1daec5a4050a947b763 (diff) | |
| parent | 193c98ae540d43d082795fd77ea81a4f6f7fd0f6 (diff) | |
| download | mruby-aa0d2f91447c49363059f2e95cb9023f65a6fbef.tar.gz mruby-aa0d2f91447c49363059f2e95cb9023f65a6fbef.zip | |
Updated Xcode project build settings in conformity with 10.8/Xcode 4.4
Diffstat (limited to 'src/math.c')
| -rw-r--r-- | src/math.c | 119 |
1 files changed, 74 insertions, 45 deletions
diff --git a/src/math.c b/src/math.c index b0d911573..9aae87acd 100644 --- a/src/math.c +++ b/src/math.c @@ -5,15 +5,19 @@ */ #include "mruby.h" +#include "mruby/array.h" + +#ifdef ENABLE_MATH #include <math.h> #define domain_error(msg) \ mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg); -#define MATH_TOLERANCE 1E-12 - /* math functions not provided under Microsoft Visual C++ */ #ifdef _MSC_VER + +#define MATH_TOLERANCE 1E-12 + #define asinh(x) log(x + sqrt(pow(x,2.0) + 1)) #define acosh(x) log(x + sqrt(pow(x,2.0) - 1)) #define atanh(x) (log(1+x) - log(1-x))/2.0 @@ -85,10 +89,6 @@ erfc(double x) #endif - -mrb_value -mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr); - /* TRIGONOMETRIC FUNCTIONS */ @@ -410,11 +410,14 @@ math_exp(mrb_state *mrb, mrb_value obj) static mrb_value math_log(mrb_state *mrb, mrb_value obj) { - mrb_float x; + mrb_float x, base; + int argc; - mrb_get_args(mrb, "f", &x); + argc = mrb_get_args(mrb, "f|f", &x, &base); x = log(x); - + if (argc == 2) { + x /= log(base); + } return mrb_float_value(x); } @@ -465,6 +468,25 @@ math_log10(mrb_state *mrb, mrb_value obj) /* * call-seq: + * Math.sqrt(numeric) -> float + * + * Returns the square root of <i>numeric</i>. + * + */ +static mrb_value +math_sqrt(mrb_state *mrb, mrb_value obj) +{ + mrb_float x; + + mrb_get_args(mrb, "f", &x); + x = sqrt(x); + + return mrb_float_value(x); +} + + +/* + * call-seq: * Math.cbrt(numeric) -> float * * Returns the cube root of <i>numeric</i>. @@ -612,47 +634,54 @@ mrb_init_math(mrb_state *mrb) struct RClass *mrb_math; mrb_math = mrb_define_module(mrb, "Math"); - mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(MATH_TOLERANCE)); - #ifdef M_PI - mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(M_PI)); - #else - mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(atan(1.0)*4.0)); - #endif - - #ifdef M_E - mrb_define_const(mrb, mrb_math, "E", mrb_float_value(M_E)); - #else - mrb_define_const(mrb, mrb_math, "E", mrb_float_value(exp(1.0))); - #endif +#ifdef M_PI + mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(M_PI)); +#else + mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(atan(1.0)*4.0)); +#endif - mrb_define_module_function(mrb, mrb_math, "sin", math_sin, 1); - mrb_define_module_function(mrb, mrb_math, "cos", math_cos, 1); - mrb_define_module_function(mrb, mrb_math, "tan", math_tan, 1); - - mrb_define_module_function(mrb, mrb_math, "asin", math_asin, 1); - mrb_define_module_function(mrb, mrb_math, "acos", math_acos, 1); - mrb_define_module_function(mrb, mrb_math, "atan", math_atan, 1); - mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, 2); +#ifdef M_E + mrb_define_const(mrb, mrb_math, "E", mrb_float_value(M_E)); +#else + mrb_define_const(mrb, mrb_math, "E", mrb_float_value(exp(1.0))); +#endif + +#ifdef MRB_USE_FLOAT + mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-5)); +#else + mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(1e-12)); +#endif + + mrb_define_module_function(mrb, mrb_math, "sin", math_sin, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "cos", math_cos, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "tan", math_tan, ARGS_REQ(1)); + + mrb_define_module_function(mrb, mrb_math, "asin", math_asin, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "acos", math_acos, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "atan", math_atan, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, ARGS_REQ(2)); - mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, 1); - mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, 1); - mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, 1); + mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, 1); - mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, 1); - mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, 1); + mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "exp", math_exp, 1); - mrb_define_module_function(mrb, mrb_math, "log", math_log, -1); - mrb_define_module_function(mrb, mrb_math, "log2", math_log2, 1); - mrb_define_module_function(mrb, mrb_math, "log10", math_log10, 1); - mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, 1); + mrb_define_module_function(mrb, mrb_math, "exp", math_exp, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "log", math_log, ARGS_REQ(1)|ARGS_OPT(1)); + mrb_define_module_function(mrb, mrb_math, "log2", math_log2, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "log10", math_log10, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, ARGS_REQ(1)); - mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, 1); - mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, 2); + mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, ARGS_REQ(2)); - mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, 2); + mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, ARGS_REQ(2)); - mrb_define_module_function(mrb, mrb_math, "erf", math_erf, 1); - mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, 1); + mrb_define_module_function(mrb, mrb_math, "erf", math_erf, ARGS_REQ(1)); + mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, ARGS_REQ(1)); } +#endif /* ENABLE_MATH */ |
