mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
mrbgems/mruby-math/math.c@0:158c61bb030f, 2015-03-25 (annotated)
- Committer:
- mzta
- Date:
- Wed Mar 25 17:36:16 2015 +0000
- Revision:
- 0:158c61bb030f
mirb_mbed initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzta | 0:158c61bb030f | 1 | /* |
mzta | 0:158c61bb030f | 2 | ** math.c - Math module |
mzta | 0:158c61bb030f | 3 | ** |
mzta | 0:158c61bb030f | 4 | ** See Copyright Notice in mruby.h |
mzta | 0:158c61bb030f | 5 | */ |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | #include "mruby.h" |
mzta | 0:158c61bb030f | 8 | #include "mruby/array.h" |
mzta | 0:158c61bb030f | 9 | |
mzta | 0:158c61bb030f | 10 | #include <math.h> |
mzta | 0:158c61bb030f | 11 | |
mzta | 0:158c61bb030f | 12 | static void |
mzta | 0:158c61bb030f | 13 | domain_error(mrb_state *mrb, const char *func) |
mzta | 0:158c61bb030f | 14 | { |
mzta | 0:158c61bb030f | 15 | struct RClass *math = mrb_module_get(mrb, "Math"); |
mzta | 0:158c61bb030f | 16 | struct RClass *domainerror = mrb_class_get_under(mrb, math, "DomainError"); |
mzta | 0:158c61bb030f | 17 | mrb_value str = mrb_str_new_cstr(mrb, func); |
mzta | 0:158c61bb030f | 18 | mrb_raisef(mrb, domainerror, "Numerical argument is out of domain - %S", str); |
mzta | 0:158c61bb030f | 19 | } |
mzta | 0:158c61bb030f | 20 | |
mzta | 0:158c61bb030f | 21 | /* math functions not provided by Microsoft Visual C++ 2012 or older */ |
mzta | 0:158c61bb030f | 22 | #if defined _MSC_VER && _MSC_VER < 1800 |
mzta | 0:158c61bb030f | 23 | |
mzta | 0:158c61bb030f | 24 | #include <float.h> |
mzta | 0:158c61bb030f | 25 | |
mzta | 0:158c61bb030f | 26 | #define MATH_TOLERANCE 1E-12 |
mzta | 0:158c61bb030f | 27 | |
mzta | 0:158c61bb030f | 28 | double |
mzta | 0:158c61bb030f | 29 | asinh(double x) |
mzta | 0:158c61bb030f | 30 | { |
mzta | 0:158c61bb030f | 31 | double xa, ya, y; |
mzta | 0:158c61bb030f | 32 | |
mzta | 0:158c61bb030f | 33 | /* Basic formula loses precision for x < 0, but asinh is an odd function */ |
mzta | 0:158c61bb030f | 34 | xa = fabs(x); |
mzta | 0:158c61bb030f | 35 | if (xa > 3.16227E+18) { |
mzta | 0:158c61bb030f | 36 | /* Prevent x*x from overflowing; basic formula reduces to log(2*x) */ |
mzta | 0:158c61bb030f | 37 | ya = log(xa) + 0.69314718055994530942; |
mzta | 0:158c61bb030f | 38 | } |
mzta | 0:158c61bb030f | 39 | else { |
mzta | 0:158c61bb030f | 40 | /* Basic formula for asinh */ |
mzta | 0:158c61bb030f | 41 | ya = log(xa + sqrt(xa*xa + 1.0)); |
mzta | 0:158c61bb030f | 42 | } |
mzta | 0:158c61bb030f | 43 | |
mzta | 0:158c61bb030f | 44 | y = _copysign(ya, x); |
mzta | 0:158c61bb030f | 45 | return y; |
mzta | 0:158c61bb030f | 46 | } |
mzta | 0:158c61bb030f | 47 | |
mzta | 0:158c61bb030f | 48 | double |
mzta | 0:158c61bb030f | 49 | acosh(double x) |
mzta | 0:158c61bb030f | 50 | { |
mzta | 0:158c61bb030f | 51 | double y; |
mzta | 0:158c61bb030f | 52 | |
mzta | 0:158c61bb030f | 53 | if (x > 3.16227E+18) { |
mzta | 0:158c61bb030f | 54 | /* Prevent x*x from overflowing; basic formula reduces to log(2*x) */ |
mzta | 0:158c61bb030f | 55 | y = log(x) + 0.69314718055994530942; |
mzta | 0:158c61bb030f | 56 | } |
mzta | 0:158c61bb030f | 57 | else { |
mzta | 0:158c61bb030f | 58 | /* Basic formula for acosh */ |
mzta | 0:158c61bb030f | 59 | y = log(x + sqrt(x*x - 1.0)); |
mzta | 0:158c61bb030f | 60 | } |
mzta | 0:158c61bb030f | 61 | |
mzta | 0:158c61bb030f | 62 | return y; |
mzta | 0:158c61bb030f | 63 | } |
mzta | 0:158c61bb030f | 64 | |
mzta | 0:158c61bb030f | 65 | double |
mzta | 0:158c61bb030f | 66 | atanh(double x) |
mzta | 0:158c61bb030f | 67 | { |
mzta | 0:158c61bb030f | 68 | double y; |
mzta | 0:158c61bb030f | 69 | |
mzta | 0:158c61bb030f | 70 | if (fabs(x) < 1E-2) { |
mzta | 0:158c61bb030f | 71 | /* The sums 1+x and 1-x lose precision for small x. Use the polynomial |
mzta | 0:158c61bb030f | 72 | instead. */ |
mzta | 0:158c61bb030f | 73 | double x2 = x * x; |
mzta | 0:158c61bb030f | 74 | y = x*(1.0 + x2*(1.0/3.0 + x2*(1.0/5.0 + x2*(1.0/7.0)))); |
mzta | 0:158c61bb030f | 75 | } |
mzta | 0:158c61bb030f | 76 | else { |
mzta | 0:158c61bb030f | 77 | /* Basic formula for atanh */ |
mzta | 0:158c61bb030f | 78 | y = 0.5 * (log(1.0+x) - log(1.0-x)); |
mzta | 0:158c61bb030f | 79 | } |
mzta | 0:158c61bb030f | 80 | |
mzta | 0:158c61bb030f | 81 | return y; |
mzta | 0:158c61bb030f | 82 | } |
mzta | 0:158c61bb030f | 83 | |
mzta | 0:158c61bb030f | 84 | double |
mzta | 0:158c61bb030f | 85 | cbrt(double x) |
mzta | 0:158c61bb030f | 86 | { |
mzta | 0:158c61bb030f | 87 | double xa, ya, y; |
mzta | 0:158c61bb030f | 88 | |
mzta | 0:158c61bb030f | 89 | /* pow(x, y) is undefined for x < 0 and y not an integer, but cbrt is an |
mzta | 0:158c61bb030f | 90 | odd function */ |
mzta | 0:158c61bb030f | 91 | xa = fabs(x); |
mzta | 0:158c61bb030f | 92 | ya = pow(xa, 1.0/3.0); |
mzta | 0:158c61bb030f | 93 | y = _copysign(ya, x); |
mzta | 0:158c61bb030f | 94 | return y; |
mzta | 0:158c61bb030f | 95 | } |
mzta | 0:158c61bb030f | 96 | |
mzta | 0:158c61bb030f | 97 | /* Declaration of complementary Error function */ |
mzta | 0:158c61bb030f | 98 | double |
mzta | 0:158c61bb030f | 99 | erfc(double x); |
mzta | 0:158c61bb030f | 100 | |
mzta | 0:158c61bb030f | 101 | /* |
mzta | 0:158c61bb030f | 102 | ** Implementations of error functions |
mzta | 0:158c61bb030f | 103 | ** credits to http://www.digitalmars.com/archives/cplusplus/3634.html |
mzta | 0:158c61bb030f | 104 | */ |
mzta | 0:158c61bb030f | 105 | |
mzta | 0:158c61bb030f | 106 | /* Implementation of Error function */ |
mzta | 0:158c61bb030f | 107 | double |
mzta | 0:158c61bb030f | 108 | erf(double x) |
mzta | 0:158c61bb030f | 109 | { |
mzta | 0:158c61bb030f | 110 | static const double two_sqrtpi = 1.128379167095512574; |
mzta | 0:158c61bb030f | 111 | double sum = x; |
mzta | 0:158c61bb030f | 112 | double term = x; |
mzta | 0:158c61bb030f | 113 | double xsqr = x*x; |
mzta | 0:158c61bb030f | 114 | int j= 1; |
mzta | 0:158c61bb030f | 115 | if (fabs(x) > 2.2) { |
mzta | 0:158c61bb030f | 116 | return 1.0 - erfc(x); |
mzta | 0:158c61bb030f | 117 | } |
mzta | 0:158c61bb030f | 118 | do { |
mzta | 0:158c61bb030f | 119 | term *= xsqr/j; |
mzta | 0:158c61bb030f | 120 | sum -= term/(2*j+1); |
mzta | 0:158c61bb030f | 121 | ++j; |
mzta | 0:158c61bb030f | 122 | term *= xsqr/j; |
mzta | 0:158c61bb030f | 123 | sum += term/(2*j+1); |
mzta | 0:158c61bb030f | 124 | ++j; |
mzta | 0:158c61bb030f | 125 | } while (fabs(term/sum) > MATH_TOLERANCE); |
mzta | 0:158c61bb030f | 126 | return two_sqrtpi*sum; |
mzta | 0:158c61bb030f | 127 | } |
mzta | 0:158c61bb030f | 128 | |
mzta | 0:158c61bb030f | 129 | /* Implementation of complementary Error function */ |
mzta | 0:158c61bb030f | 130 | double |
mzta | 0:158c61bb030f | 131 | erfc(double x) |
mzta | 0:158c61bb030f | 132 | { |
mzta | 0:158c61bb030f | 133 | static const double one_sqrtpi= 0.564189583547756287; |
mzta | 0:158c61bb030f | 134 | double a = 1; |
mzta | 0:158c61bb030f | 135 | double b = x; |
mzta | 0:158c61bb030f | 136 | double c = x; |
mzta | 0:158c61bb030f | 137 | double d = x*x+0.5; |
mzta | 0:158c61bb030f | 138 | double q1; |
mzta | 0:158c61bb030f | 139 | double q2 = b/d; |
mzta | 0:158c61bb030f | 140 | double n = 1.0; |
mzta | 0:158c61bb030f | 141 | double t; |
mzta | 0:158c61bb030f | 142 | if (fabs(x) < 2.2) { |
mzta | 0:158c61bb030f | 143 | return 1.0 - erf(x); |
mzta | 0:158c61bb030f | 144 | } |
mzta | 0:158c61bb030f | 145 | if (x < 0.0) { /*signbit(x)*/ |
mzta | 0:158c61bb030f | 146 | return 2.0 - erfc(-x); |
mzta | 0:158c61bb030f | 147 | } |
mzta | 0:158c61bb030f | 148 | do { |
mzta | 0:158c61bb030f | 149 | t = a*n+b*x; |
mzta | 0:158c61bb030f | 150 | a = b; |
mzta | 0:158c61bb030f | 151 | b = t; |
mzta | 0:158c61bb030f | 152 | t = c*n+d*x; |
mzta | 0:158c61bb030f | 153 | c = d; |
mzta | 0:158c61bb030f | 154 | d = t; |
mzta | 0:158c61bb030f | 155 | n += 0.5; |
mzta | 0:158c61bb030f | 156 | q1 = q2; |
mzta | 0:158c61bb030f | 157 | q2 = b/d; |
mzta | 0:158c61bb030f | 158 | } while (fabs(q1-q2)/q2 > MATH_TOLERANCE); |
mzta | 0:158c61bb030f | 159 | return one_sqrtpi*exp(-x*x)*q2; |
mzta | 0:158c61bb030f | 160 | } |
mzta | 0:158c61bb030f | 161 | |
mzta | 0:158c61bb030f | 162 | #endif |
mzta | 0:158c61bb030f | 163 | |
mzta | 0:158c61bb030f | 164 | #if (defined _MSC_VER && _MSC_VER < 1800) || defined __ANDROID__ || (defined __FreeBSD__ && __FreeBSD_version < 803000) |
mzta | 0:158c61bb030f | 165 | |
mzta | 0:158c61bb030f | 166 | double |
mzta | 0:158c61bb030f | 167 | log2(double x) |
mzta | 0:158c61bb030f | 168 | { |
mzta | 0:158c61bb030f | 169 | return log10(x)/log10(2.0); |
mzta | 0:158c61bb030f | 170 | } |
mzta | 0:158c61bb030f | 171 | |
mzta | 0:158c61bb030f | 172 | #endif |
mzta | 0:158c61bb030f | 173 | |
mzta | 0:158c61bb030f | 174 | /* |
mzta | 0:158c61bb030f | 175 | TRIGONOMETRIC FUNCTIONS |
mzta | 0:158c61bb030f | 176 | */ |
mzta | 0:158c61bb030f | 177 | |
mzta | 0:158c61bb030f | 178 | /* |
mzta | 0:158c61bb030f | 179 | * call-seq: |
mzta | 0:158c61bb030f | 180 | * Math.sin(x) -> float |
mzta | 0:158c61bb030f | 181 | * |
mzta | 0:158c61bb030f | 182 | * Computes the sine of <i>x</i> (expressed in radians). Returns |
mzta | 0:158c61bb030f | 183 | * -1..1. |
mzta | 0:158c61bb030f | 184 | */ |
mzta | 0:158c61bb030f | 185 | static mrb_value |
mzta | 0:158c61bb030f | 186 | math_sin(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 187 | { |
mzta | 0:158c61bb030f | 188 | mrb_float x; |
mzta | 0:158c61bb030f | 189 | |
mzta | 0:158c61bb030f | 190 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 191 | x = sin(x); |
mzta | 0:158c61bb030f | 192 | |
mzta | 0:158c61bb030f | 193 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 194 | } |
mzta | 0:158c61bb030f | 195 | |
mzta | 0:158c61bb030f | 196 | /* |
mzta | 0:158c61bb030f | 197 | * call-seq: |
mzta | 0:158c61bb030f | 198 | * Math.cos(x) -> float |
mzta | 0:158c61bb030f | 199 | * |
mzta | 0:158c61bb030f | 200 | * Computes the cosine of <i>x</i> (expressed in radians). Returns |
mzta | 0:158c61bb030f | 201 | * -1..1. |
mzta | 0:158c61bb030f | 202 | */ |
mzta | 0:158c61bb030f | 203 | static mrb_value |
mzta | 0:158c61bb030f | 204 | math_cos(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 205 | { |
mzta | 0:158c61bb030f | 206 | mrb_float x; |
mzta | 0:158c61bb030f | 207 | |
mzta | 0:158c61bb030f | 208 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 209 | x = cos(x); |
mzta | 0:158c61bb030f | 210 | |
mzta | 0:158c61bb030f | 211 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 212 | } |
mzta | 0:158c61bb030f | 213 | |
mzta | 0:158c61bb030f | 214 | /* |
mzta | 0:158c61bb030f | 215 | * call-seq: |
mzta | 0:158c61bb030f | 216 | * Math.tan(x) -> float |
mzta | 0:158c61bb030f | 217 | * |
mzta | 0:158c61bb030f | 218 | * Returns the tangent of <i>x</i> (expressed in radians). |
mzta | 0:158c61bb030f | 219 | */ |
mzta | 0:158c61bb030f | 220 | static mrb_value |
mzta | 0:158c61bb030f | 221 | math_tan(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 222 | { |
mzta | 0:158c61bb030f | 223 | mrb_float x; |
mzta | 0:158c61bb030f | 224 | |
mzta | 0:158c61bb030f | 225 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 226 | x = tan(x); |
mzta | 0:158c61bb030f | 227 | |
mzta | 0:158c61bb030f | 228 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 229 | } |
mzta | 0:158c61bb030f | 230 | |
mzta | 0:158c61bb030f | 231 | /* |
mzta | 0:158c61bb030f | 232 | INVERSE TRIGONOMETRIC FUNCTIONS |
mzta | 0:158c61bb030f | 233 | */ |
mzta | 0:158c61bb030f | 234 | |
mzta | 0:158c61bb030f | 235 | /* |
mzta | 0:158c61bb030f | 236 | * call-seq: |
mzta | 0:158c61bb030f | 237 | * Math.asin(x) -> float |
mzta | 0:158c61bb030f | 238 | * |
mzta | 0:158c61bb030f | 239 | * Computes the arc sine of <i>x</i>. Returns -{PI/2} .. {PI/2}. |
mzta | 0:158c61bb030f | 240 | */ |
mzta | 0:158c61bb030f | 241 | static mrb_value |
mzta | 0:158c61bb030f | 242 | math_asin(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 243 | { |
mzta | 0:158c61bb030f | 244 | mrb_float x; |
mzta | 0:158c61bb030f | 245 | |
mzta | 0:158c61bb030f | 246 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 247 | if (x < -1.0 || x > 1.0) { |
mzta | 0:158c61bb030f | 248 | domain_error(mrb, "asin"); |
mzta | 0:158c61bb030f | 249 | } |
mzta | 0:158c61bb030f | 250 | x = asin(x); |
mzta | 0:158c61bb030f | 251 | |
mzta | 0:158c61bb030f | 252 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 253 | } |
mzta | 0:158c61bb030f | 254 | |
mzta | 0:158c61bb030f | 255 | /* |
mzta | 0:158c61bb030f | 256 | * call-seq: |
mzta | 0:158c61bb030f | 257 | * Math.acos(x) -> float |
mzta | 0:158c61bb030f | 258 | * |
mzta | 0:158c61bb030f | 259 | * Computes the arc cosine of <i>x</i>. Returns 0..PI. |
mzta | 0:158c61bb030f | 260 | */ |
mzta | 0:158c61bb030f | 261 | static mrb_value |
mzta | 0:158c61bb030f | 262 | math_acos(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 263 | { |
mzta | 0:158c61bb030f | 264 | mrb_float x; |
mzta | 0:158c61bb030f | 265 | |
mzta | 0:158c61bb030f | 266 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 267 | if (x < -1.0 || x > 1.0) { |
mzta | 0:158c61bb030f | 268 | domain_error(mrb, "acos"); |
mzta | 0:158c61bb030f | 269 | } |
mzta | 0:158c61bb030f | 270 | x = acos(x); |
mzta | 0:158c61bb030f | 271 | |
mzta | 0:158c61bb030f | 272 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 273 | } |
mzta | 0:158c61bb030f | 274 | |
mzta | 0:158c61bb030f | 275 | /* |
mzta | 0:158c61bb030f | 276 | * call-seq: |
mzta | 0:158c61bb030f | 277 | * Math.atan(x) -> float |
mzta | 0:158c61bb030f | 278 | * |
mzta | 0:158c61bb030f | 279 | * Computes the arc tangent of <i>x</i>. Returns -{PI/2} .. {PI/2}. |
mzta | 0:158c61bb030f | 280 | */ |
mzta | 0:158c61bb030f | 281 | static mrb_value |
mzta | 0:158c61bb030f | 282 | math_atan(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 283 | { |
mzta | 0:158c61bb030f | 284 | mrb_float x; |
mzta | 0:158c61bb030f | 285 | |
mzta | 0:158c61bb030f | 286 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 287 | x = atan(x); |
mzta | 0:158c61bb030f | 288 | |
mzta | 0:158c61bb030f | 289 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 290 | } |
mzta | 0:158c61bb030f | 291 | |
mzta | 0:158c61bb030f | 292 | /* |
mzta | 0:158c61bb030f | 293 | * call-seq: |
mzta | 0:158c61bb030f | 294 | * Math.atan2(y, x) -> float |
mzta | 0:158c61bb030f | 295 | * |
mzta | 0:158c61bb030f | 296 | * Computes the arc tangent given <i>y</i> and <i>x</i>. Returns |
mzta | 0:158c61bb030f | 297 | * -PI..PI. |
mzta | 0:158c61bb030f | 298 | * |
mzta | 0:158c61bb030f | 299 | * Math.atan2(-0.0, -1.0) #=> -3.141592653589793 |
mzta | 0:158c61bb030f | 300 | * Math.atan2(-1.0, -1.0) #=> -2.356194490192345 |
mzta | 0:158c61bb030f | 301 | * Math.atan2(-1.0, 0.0) #=> -1.5707963267948966 |
mzta | 0:158c61bb030f | 302 | * Math.atan2(-1.0, 1.0) #=> -0.7853981633974483 |
mzta | 0:158c61bb030f | 303 | * Math.atan2(-0.0, 1.0) #=> -0.0 |
mzta | 0:158c61bb030f | 304 | * Math.atan2(0.0, 1.0) #=> 0.0 |
mzta | 0:158c61bb030f | 305 | * Math.atan2(1.0, 1.0) #=> 0.7853981633974483 |
mzta | 0:158c61bb030f | 306 | * Math.atan2(1.0, 0.0) #=> 1.5707963267948966 |
mzta | 0:158c61bb030f | 307 | * Math.atan2(1.0, -1.0) #=> 2.356194490192345 |
mzta | 0:158c61bb030f | 308 | * Math.atan2(0.0, -1.0) #=> 3.141592653589793 |
mzta | 0:158c61bb030f | 309 | * |
mzta | 0:158c61bb030f | 310 | */ |
mzta | 0:158c61bb030f | 311 | static mrb_value |
mzta | 0:158c61bb030f | 312 | math_atan2(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 313 | { |
mzta | 0:158c61bb030f | 314 | mrb_float x, y; |
mzta | 0:158c61bb030f | 315 | |
mzta | 0:158c61bb030f | 316 | mrb_get_args(mrb, "ff", &x, &y); |
mzta | 0:158c61bb030f | 317 | x = atan2(x, y); |
mzta | 0:158c61bb030f | 318 | |
mzta | 0:158c61bb030f | 319 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 320 | } |
mzta | 0:158c61bb030f | 321 | |
mzta | 0:158c61bb030f | 322 | |
mzta | 0:158c61bb030f | 323 | |
mzta | 0:158c61bb030f | 324 | /* |
mzta | 0:158c61bb030f | 325 | HYPERBOLIC TRIG FUNCTIONS |
mzta | 0:158c61bb030f | 326 | */ |
mzta | 0:158c61bb030f | 327 | /* |
mzta | 0:158c61bb030f | 328 | * call-seq: |
mzta | 0:158c61bb030f | 329 | * Math.sinh(x) -> float |
mzta | 0:158c61bb030f | 330 | * |
mzta | 0:158c61bb030f | 331 | * Computes the hyperbolic sine of <i>x</i> (expressed in |
mzta | 0:158c61bb030f | 332 | * radians). |
mzta | 0:158c61bb030f | 333 | */ |
mzta | 0:158c61bb030f | 334 | static mrb_value |
mzta | 0:158c61bb030f | 335 | math_sinh(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 336 | { |
mzta | 0:158c61bb030f | 337 | mrb_float x; |
mzta | 0:158c61bb030f | 338 | |
mzta | 0:158c61bb030f | 339 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 340 | x = sinh(x); |
mzta | 0:158c61bb030f | 341 | |
mzta | 0:158c61bb030f | 342 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 343 | } |
mzta | 0:158c61bb030f | 344 | |
mzta | 0:158c61bb030f | 345 | /* |
mzta | 0:158c61bb030f | 346 | * call-seq: |
mzta | 0:158c61bb030f | 347 | * Math.cosh(x) -> float |
mzta | 0:158c61bb030f | 348 | * |
mzta | 0:158c61bb030f | 349 | * Computes the hyperbolic cosine of <i>x</i> (expressed in radians). |
mzta | 0:158c61bb030f | 350 | */ |
mzta | 0:158c61bb030f | 351 | static mrb_value |
mzta | 0:158c61bb030f | 352 | math_cosh(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 353 | { |
mzta | 0:158c61bb030f | 354 | mrb_float x; |
mzta | 0:158c61bb030f | 355 | |
mzta | 0:158c61bb030f | 356 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 357 | x = cosh(x); |
mzta | 0:158c61bb030f | 358 | |
mzta | 0:158c61bb030f | 359 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 360 | } |
mzta | 0:158c61bb030f | 361 | |
mzta | 0:158c61bb030f | 362 | /* |
mzta | 0:158c61bb030f | 363 | * call-seq: |
mzta | 0:158c61bb030f | 364 | * Math.tanh() -> float |
mzta | 0:158c61bb030f | 365 | * |
mzta | 0:158c61bb030f | 366 | * Computes the hyperbolic tangent of <i>x</i> (expressed in |
mzta | 0:158c61bb030f | 367 | * radians). |
mzta | 0:158c61bb030f | 368 | */ |
mzta | 0:158c61bb030f | 369 | static mrb_value |
mzta | 0:158c61bb030f | 370 | math_tanh(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 371 | { |
mzta | 0:158c61bb030f | 372 | mrb_float x; |
mzta | 0:158c61bb030f | 373 | |
mzta | 0:158c61bb030f | 374 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 375 | x = tanh(x); |
mzta | 0:158c61bb030f | 376 | |
mzta | 0:158c61bb030f | 377 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 378 | } |
mzta | 0:158c61bb030f | 379 | |
mzta | 0:158c61bb030f | 380 | |
mzta | 0:158c61bb030f | 381 | /* |
mzta | 0:158c61bb030f | 382 | INVERSE HYPERBOLIC TRIG FUNCTIONS |
mzta | 0:158c61bb030f | 383 | */ |
mzta | 0:158c61bb030f | 384 | |
mzta | 0:158c61bb030f | 385 | /* |
mzta | 0:158c61bb030f | 386 | * call-seq: |
mzta | 0:158c61bb030f | 387 | * Math.asinh(x) -> float |
mzta | 0:158c61bb030f | 388 | * |
mzta | 0:158c61bb030f | 389 | * Computes the inverse hyperbolic sine of <i>x</i>. |
mzta | 0:158c61bb030f | 390 | */ |
mzta | 0:158c61bb030f | 391 | static mrb_value |
mzta | 0:158c61bb030f | 392 | math_asinh(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 393 | { |
mzta | 0:158c61bb030f | 394 | mrb_float x; |
mzta | 0:158c61bb030f | 395 | |
mzta | 0:158c61bb030f | 396 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 397 | |
mzta | 0:158c61bb030f | 398 | x = asinh(x); |
mzta | 0:158c61bb030f | 399 | |
mzta | 0:158c61bb030f | 400 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 401 | } |
mzta | 0:158c61bb030f | 402 | |
mzta | 0:158c61bb030f | 403 | /* |
mzta | 0:158c61bb030f | 404 | * call-seq: |
mzta | 0:158c61bb030f | 405 | * Math.acosh(x) -> float |
mzta | 0:158c61bb030f | 406 | * |
mzta | 0:158c61bb030f | 407 | * Computes the inverse hyperbolic cosine of <i>x</i>. |
mzta | 0:158c61bb030f | 408 | */ |
mzta | 0:158c61bb030f | 409 | static mrb_value |
mzta | 0:158c61bb030f | 410 | math_acosh(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 411 | { |
mzta | 0:158c61bb030f | 412 | mrb_float x; |
mzta | 0:158c61bb030f | 413 | |
mzta | 0:158c61bb030f | 414 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 415 | if (x < 1.0) { |
mzta | 0:158c61bb030f | 416 | domain_error(mrb, "acosh"); |
mzta | 0:158c61bb030f | 417 | } |
mzta | 0:158c61bb030f | 418 | x = acosh(x); |
mzta | 0:158c61bb030f | 419 | |
mzta | 0:158c61bb030f | 420 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 421 | } |
mzta | 0:158c61bb030f | 422 | |
mzta | 0:158c61bb030f | 423 | /* |
mzta | 0:158c61bb030f | 424 | * call-seq: |
mzta | 0:158c61bb030f | 425 | * Math.atanh(x) -> float |
mzta | 0:158c61bb030f | 426 | * |
mzta | 0:158c61bb030f | 427 | * Computes the inverse hyperbolic tangent of <i>x</i>. |
mzta | 0:158c61bb030f | 428 | */ |
mzta | 0:158c61bb030f | 429 | static mrb_value |
mzta | 0:158c61bb030f | 430 | math_atanh(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 431 | { |
mzta | 0:158c61bb030f | 432 | mrb_float x; |
mzta | 0:158c61bb030f | 433 | |
mzta | 0:158c61bb030f | 434 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 435 | if (x < -1.0 || x > 1.0) { |
mzta | 0:158c61bb030f | 436 | domain_error(mrb, "atanh"); |
mzta | 0:158c61bb030f | 437 | } |
mzta | 0:158c61bb030f | 438 | x = atanh(x); |
mzta | 0:158c61bb030f | 439 | |
mzta | 0:158c61bb030f | 440 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 441 | } |
mzta | 0:158c61bb030f | 442 | |
mzta | 0:158c61bb030f | 443 | /* |
mzta | 0:158c61bb030f | 444 | EXPONENTIALS AND LOGARITHMS |
mzta | 0:158c61bb030f | 445 | */ |
mzta | 0:158c61bb030f | 446 | |
mzta | 0:158c61bb030f | 447 | /* |
mzta | 0:158c61bb030f | 448 | * call-seq: |
mzta | 0:158c61bb030f | 449 | * Math.exp(x) -> float |
mzta | 0:158c61bb030f | 450 | * |
mzta | 0:158c61bb030f | 451 | * Returns e**x. |
mzta | 0:158c61bb030f | 452 | * |
mzta | 0:158c61bb030f | 453 | * Math.exp(0) #=> 1.0 |
mzta | 0:158c61bb030f | 454 | * Math.exp(1) #=> 2.718281828459045 |
mzta | 0:158c61bb030f | 455 | * Math.exp(1.5) #=> 4.4816890703380645 |
mzta | 0:158c61bb030f | 456 | * |
mzta | 0:158c61bb030f | 457 | */ |
mzta | 0:158c61bb030f | 458 | static mrb_value |
mzta | 0:158c61bb030f | 459 | math_exp(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 460 | { |
mzta | 0:158c61bb030f | 461 | mrb_float x; |
mzta | 0:158c61bb030f | 462 | |
mzta | 0:158c61bb030f | 463 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 464 | x = exp(x); |
mzta | 0:158c61bb030f | 465 | |
mzta | 0:158c61bb030f | 466 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 467 | } |
mzta | 0:158c61bb030f | 468 | |
mzta | 0:158c61bb030f | 469 | /* |
mzta | 0:158c61bb030f | 470 | * call-seq: |
mzta | 0:158c61bb030f | 471 | * Math.log(numeric) -> float |
mzta | 0:158c61bb030f | 472 | * Math.log(num,base) -> float |
mzta | 0:158c61bb030f | 473 | * |
mzta | 0:158c61bb030f | 474 | * Returns the natural logarithm of <i>numeric</i>. |
mzta | 0:158c61bb030f | 475 | * If additional second argument is given, it will be the base |
mzta | 0:158c61bb030f | 476 | * of logarithm. |
mzta | 0:158c61bb030f | 477 | * |
mzta | 0:158c61bb030f | 478 | * Math.log(1) #=> 0.0 |
mzta | 0:158c61bb030f | 479 | * Math.log(Math::E) #=> 1.0 |
mzta | 0:158c61bb030f | 480 | * Math.log(Math::E**3) #=> 3.0 |
mzta | 0:158c61bb030f | 481 | * Math.log(12,3) #=> 2.2618595071429146 |
mzta | 0:158c61bb030f | 482 | * |
mzta | 0:158c61bb030f | 483 | */ |
mzta | 0:158c61bb030f | 484 | static mrb_value |
mzta | 0:158c61bb030f | 485 | math_log(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 486 | { |
mzta | 0:158c61bb030f | 487 | mrb_float x, base; |
mzta | 0:158c61bb030f | 488 | int argc; |
mzta | 0:158c61bb030f | 489 | |
mzta | 0:158c61bb030f | 490 | argc = mrb_get_args(mrb, "f|f", &x, &base); |
mzta | 0:158c61bb030f | 491 | if (x < 0.0) { |
mzta | 0:158c61bb030f | 492 | domain_error(mrb, "log"); |
mzta | 0:158c61bb030f | 493 | } |
mzta | 0:158c61bb030f | 494 | x = log(x); |
mzta | 0:158c61bb030f | 495 | if (argc == 2) { |
mzta | 0:158c61bb030f | 496 | if (base < 0.0) { |
mzta | 0:158c61bb030f | 497 | domain_error(mrb, "log"); |
mzta | 0:158c61bb030f | 498 | } |
mzta | 0:158c61bb030f | 499 | x /= log(base); |
mzta | 0:158c61bb030f | 500 | } |
mzta | 0:158c61bb030f | 501 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 502 | } |
mzta | 0:158c61bb030f | 503 | |
mzta | 0:158c61bb030f | 504 | /* |
mzta | 0:158c61bb030f | 505 | * call-seq: |
mzta | 0:158c61bb030f | 506 | * Math.log2(numeric) -> float |
mzta | 0:158c61bb030f | 507 | * |
mzta | 0:158c61bb030f | 508 | * Returns the base 2 logarithm of <i>numeric</i>. |
mzta | 0:158c61bb030f | 509 | * |
mzta | 0:158c61bb030f | 510 | * Math.log2(1) #=> 0.0 |
mzta | 0:158c61bb030f | 511 | * Math.log2(2) #=> 1.0 |
mzta | 0:158c61bb030f | 512 | * Math.log2(32768) #=> 15.0 |
mzta | 0:158c61bb030f | 513 | * Math.log2(65536) #=> 16.0 |
mzta | 0:158c61bb030f | 514 | * |
mzta | 0:158c61bb030f | 515 | */ |
mzta | 0:158c61bb030f | 516 | static mrb_value |
mzta | 0:158c61bb030f | 517 | math_log2(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 518 | { |
mzta | 0:158c61bb030f | 519 | mrb_float x; |
mzta | 0:158c61bb030f | 520 | |
mzta | 0:158c61bb030f | 521 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 522 | if (x < 0.0) { |
mzta | 0:158c61bb030f | 523 | domain_error(mrb, "log2"); |
mzta | 0:158c61bb030f | 524 | } |
mzta | 0:158c61bb030f | 525 | x = log2(x); |
mzta | 0:158c61bb030f | 526 | |
mzta | 0:158c61bb030f | 527 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 528 | } |
mzta | 0:158c61bb030f | 529 | |
mzta | 0:158c61bb030f | 530 | /* |
mzta | 0:158c61bb030f | 531 | * call-seq: |
mzta | 0:158c61bb030f | 532 | * Math.log10(numeric) -> float |
mzta | 0:158c61bb030f | 533 | * |
mzta | 0:158c61bb030f | 534 | * Returns the base 10 logarithm of <i>numeric</i>. |
mzta | 0:158c61bb030f | 535 | * |
mzta | 0:158c61bb030f | 536 | * Math.log10(1) #=> 0.0 |
mzta | 0:158c61bb030f | 537 | * Math.log10(10) #=> 1.0 |
mzta | 0:158c61bb030f | 538 | * Math.log10(10**100) #=> 100.0 |
mzta | 0:158c61bb030f | 539 | * |
mzta | 0:158c61bb030f | 540 | */ |
mzta | 0:158c61bb030f | 541 | static mrb_value |
mzta | 0:158c61bb030f | 542 | math_log10(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 543 | { |
mzta | 0:158c61bb030f | 544 | mrb_float x; |
mzta | 0:158c61bb030f | 545 | |
mzta | 0:158c61bb030f | 546 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 547 | if (x < 0.0) { |
mzta | 0:158c61bb030f | 548 | domain_error(mrb, "log10"); |
mzta | 0:158c61bb030f | 549 | } |
mzta | 0:158c61bb030f | 550 | x = log10(x); |
mzta | 0:158c61bb030f | 551 | |
mzta | 0:158c61bb030f | 552 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 553 | } |
mzta | 0:158c61bb030f | 554 | |
mzta | 0:158c61bb030f | 555 | /* |
mzta | 0:158c61bb030f | 556 | * call-seq: |
mzta | 0:158c61bb030f | 557 | * Math.sqrt(numeric) -> float |
mzta | 0:158c61bb030f | 558 | * |
mzta | 0:158c61bb030f | 559 | * Returns the square root of <i>numeric</i>. |
mzta | 0:158c61bb030f | 560 | * |
mzta | 0:158c61bb030f | 561 | */ |
mzta | 0:158c61bb030f | 562 | static mrb_value |
mzta | 0:158c61bb030f | 563 | math_sqrt(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 564 | { |
mzta | 0:158c61bb030f | 565 | mrb_float x; |
mzta | 0:158c61bb030f | 566 | |
mzta | 0:158c61bb030f | 567 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 568 | if (x < 0.0) { |
mzta | 0:158c61bb030f | 569 | domain_error(mrb, "sqrt"); |
mzta | 0:158c61bb030f | 570 | } |
mzta | 0:158c61bb030f | 571 | x = sqrt(x); |
mzta | 0:158c61bb030f | 572 | |
mzta | 0:158c61bb030f | 573 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 574 | } |
mzta | 0:158c61bb030f | 575 | |
mzta | 0:158c61bb030f | 576 | |
mzta | 0:158c61bb030f | 577 | /* |
mzta | 0:158c61bb030f | 578 | * call-seq: |
mzta | 0:158c61bb030f | 579 | * Math.cbrt(numeric) -> float |
mzta | 0:158c61bb030f | 580 | * |
mzta | 0:158c61bb030f | 581 | * Returns the cube root of <i>numeric</i>. |
mzta | 0:158c61bb030f | 582 | * |
mzta | 0:158c61bb030f | 583 | * -9.upto(9) {|x| |
mzta | 0:158c61bb030f | 584 | * p [x, Math.cbrt(x), Math.cbrt(x)**3] |
mzta | 0:158c61bb030f | 585 | * } |
mzta | 0:158c61bb030f | 586 | * #=> |
mzta | 0:158c61bb030f | 587 | * [-9, -2.0800838230519, -9.0] |
mzta | 0:158c61bb030f | 588 | * [-8, -2.0, -8.0] |
mzta | 0:158c61bb030f | 589 | * [-7, -1.91293118277239, -7.0] |
mzta | 0:158c61bb030f | 590 | * [-6, -1.81712059283214, -6.0] |
mzta | 0:158c61bb030f | 591 | * [-5, -1.7099759466767, -5.0] |
mzta | 0:158c61bb030f | 592 | * [-4, -1.5874010519682, -4.0] |
mzta | 0:158c61bb030f | 593 | * [-3, -1.44224957030741, -3.0] |
mzta | 0:158c61bb030f | 594 | * [-2, -1.25992104989487, -2.0] |
mzta | 0:158c61bb030f | 595 | * [-1, -1.0, -1.0] |
mzta | 0:158c61bb030f | 596 | * [0, 0.0, 0.0] |
mzta | 0:158c61bb030f | 597 | * [1, 1.0, 1.0] |
mzta | 0:158c61bb030f | 598 | * [2, 1.25992104989487, 2.0] |
mzta | 0:158c61bb030f | 599 | * [3, 1.44224957030741, 3.0] |
mzta | 0:158c61bb030f | 600 | * [4, 1.5874010519682, 4.0] |
mzta | 0:158c61bb030f | 601 | * [5, 1.7099759466767, 5.0] |
mzta | 0:158c61bb030f | 602 | * [6, 1.81712059283214, 6.0] |
mzta | 0:158c61bb030f | 603 | * [7, 1.91293118277239, 7.0] |
mzta | 0:158c61bb030f | 604 | * [8, 2.0, 8.0] |
mzta | 0:158c61bb030f | 605 | * [9, 2.0800838230519, 9.0] |
mzta | 0:158c61bb030f | 606 | * |
mzta | 0:158c61bb030f | 607 | */ |
mzta | 0:158c61bb030f | 608 | static mrb_value |
mzta | 0:158c61bb030f | 609 | math_cbrt(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 610 | { |
mzta | 0:158c61bb030f | 611 | mrb_float x; |
mzta | 0:158c61bb030f | 612 | |
mzta | 0:158c61bb030f | 613 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 614 | x = cbrt(x); |
mzta | 0:158c61bb030f | 615 | |
mzta | 0:158c61bb030f | 616 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 617 | } |
mzta | 0:158c61bb030f | 618 | |
mzta | 0:158c61bb030f | 619 | |
mzta | 0:158c61bb030f | 620 | /* |
mzta | 0:158c61bb030f | 621 | * call-seq: |
mzta | 0:158c61bb030f | 622 | * Math.frexp(numeric) -> [ fraction, exponent ] |
mzta | 0:158c61bb030f | 623 | * |
mzta | 0:158c61bb030f | 624 | * Returns a two-element array containing the normalized fraction (a |
mzta | 0:158c61bb030f | 625 | * <code>Float</code>) and exponent (a <code>Fixnum</code>) of |
mzta | 0:158c61bb030f | 626 | * <i>numeric</i>. |
mzta | 0:158c61bb030f | 627 | * |
mzta | 0:158c61bb030f | 628 | * fraction, exponent = Math.frexp(1234) #=> [0.6025390625, 11] |
mzta | 0:158c61bb030f | 629 | * fraction * 2**exponent #=> 1234.0 |
mzta | 0:158c61bb030f | 630 | */ |
mzta | 0:158c61bb030f | 631 | static mrb_value |
mzta | 0:158c61bb030f | 632 | math_frexp(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 633 | { |
mzta | 0:158c61bb030f | 634 | mrb_float x; |
mzta | 0:158c61bb030f | 635 | int exp; |
mzta | 0:158c61bb030f | 636 | |
mzta | 0:158c61bb030f | 637 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 638 | x = frexp(x, &exp); |
mzta | 0:158c61bb030f | 639 | |
mzta | 0:158c61bb030f | 640 | return mrb_assoc_new(mrb, mrb_float_value(mrb, x), mrb_fixnum_value(exp)); |
mzta | 0:158c61bb030f | 641 | } |
mzta | 0:158c61bb030f | 642 | |
mzta | 0:158c61bb030f | 643 | /* |
mzta | 0:158c61bb030f | 644 | * call-seq: |
mzta | 0:158c61bb030f | 645 | * Math.ldexp(flt, int) -> float |
mzta | 0:158c61bb030f | 646 | * |
mzta | 0:158c61bb030f | 647 | * Returns the value of <i>flt</i>*(2**<i>int</i>). |
mzta | 0:158c61bb030f | 648 | * |
mzta | 0:158c61bb030f | 649 | * fraction, exponent = Math.frexp(1234) |
mzta | 0:158c61bb030f | 650 | * Math.ldexp(fraction, exponent) #=> 1234.0 |
mzta | 0:158c61bb030f | 651 | */ |
mzta | 0:158c61bb030f | 652 | static mrb_value |
mzta | 0:158c61bb030f | 653 | math_ldexp(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 654 | { |
mzta | 0:158c61bb030f | 655 | mrb_float x; |
mzta | 0:158c61bb030f | 656 | mrb_int i; |
mzta | 0:158c61bb030f | 657 | |
mzta | 0:158c61bb030f | 658 | mrb_get_args(mrb, "fi", &x, &i); |
mzta | 0:158c61bb030f | 659 | x = ldexp(x, i); |
mzta | 0:158c61bb030f | 660 | |
mzta | 0:158c61bb030f | 661 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 662 | } |
mzta | 0:158c61bb030f | 663 | |
mzta | 0:158c61bb030f | 664 | /* |
mzta | 0:158c61bb030f | 665 | * call-seq: |
mzta | 0:158c61bb030f | 666 | * Math.hypot(x, y) -> float |
mzta | 0:158c61bb030f | 667 | * |
mzta | 0:158c61bb030f | 668 | * Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle |
mzta | 0:158c61bb030f | 669 | * with sides <i>x</i> and <i>y</i>. |
mzta | 0:158c61bb030f | 670 | * |
mzta | 0:158c61bb030f | 671 | * Math.hypot(3, 4) #=> 5.0 |
mzta | 0:158c61bb030f | 672 | */ |
mzta | 0:158c61bb030f | 673 | static mrb_value |
mzta | 0:158c61bb030f | 674 | math_hypot(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 675 | { |
mzta | 0:158c61bb030f | 676 | mrb_float x, y; |
mzta | 0:158c61bb030f | 677 | |
mzta | 0:158c61bb030f | 678 | mrb_get_args(mrb, "ff", &x, &y); |
mzta | 0:158c61bb030f | 679 | x = hypot(x, y); |
mzta | 0:158c61bb030f | 680 | |
mzta | 0:158c61bb030f | 681 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 682 | } |
mzta | 0:158c61bb030f | 683 | |
mzta | 0:158c61bb030f | 684 | /* |
mzta | 0:158c61bb030f | 685 | * call-seq: |
mzta | 0:158c61bb030f | 686 | * Math.erf(x) -> float |
mzta | 0:158c61bb030f | 687 | * |
mzta | 0:158c61bb030f | 688 | * Calculates the error function of x. |
mzta | 0:158c61bb030f | 689 | */ |
mzta | 0:158c61bb030f | 690 | static mrb_value |
mzta | 0:158c61bb030f | 691 | math_erf(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 692 | { |
mzta | 0:158c61bb030f | 693 | mrb_float x; |
mzta | 0:158c61bb030f | 694 | |
mzta | 0:158c61bb030f | 695 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 696 | x = erf(x); |
mzta | 0:158c61bb030f | 697 | |
mzta | 0:158c61bb030f | 698 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 699 | } |
mzta | 0:158c61bb030f | 700 | |
mzta | 0:158c61bb030f | 701 | |
mzta | 0:158c61bb030f | 702 | /* |
mzta | 0:158c61bb030f | 703 | * call-seq: |
mzta | 0:158c61bb030f | 704 | * Math.erfc(x) -> float |
mzta | 0:158c61bb030f | 705 | * |
mzta | 0:158c61bb030f | 706 | * Calculates the complementary error function of x. |
mzta | 0:158c61bb030f | 707 | */ |
mzta | 0:158c61bb030f | 708 | static mrb_value |
mzta | 0:158c61bb030f | 709 | math_erfc(mrb_state *mrb, mrb_value obj) |
mzta | 0:158c61bb030f | 710 | { |
mzta | 0:158c61bb030f | 711 | mrb_float x; |
mzta | 0:158c61bb030f | 712 | |
mzta | 0:158c61bb030f | 713 | mrb_get_args(mrb, "f", &x); |
mzta | 0:158c61bb030f | 714 | x = erfc(x); |
mzta | 0:158c61bb030f | 715 | |
mzta | 0:158c61bb030f | 716 | return mrb_float_value(mrb, x); |
mzta | 0:158c61bb030f | 717 | } |
mzta | 0:158c61bb030f | 718 | |
mzta | 0:158c61bb030f | 719 | /* ------------------------------------------------------------------------*/ |
mzta | 0:158c61bb030f | 720 | void |
mzta | 0:158c61bb030f | 721 | mrb_mruby_math_gem_init(mrb_state* mrb) |
mzta | 0:158c61bb030f | 722 | { |
mzta | 0:158c61bb030f | 723 | struct RClass *mrb_math; |
mzta | 0:158c61bb030f | 724 | mrb_math = mrb_define_module(mrb, "Math"); |
mzta | 0:158c61bb030f | 725 | |
mzta | 0:158c61bb030f | 726 | mrb_define_class_under(mrb, mrb_math, "DomainError", mrb->eStandardError_class); |
mzta | 0:158c61bb030f | 727 | |
mzta | 0:158c61bb030f | 728 | #ifdef M_PI |
mzta | 0:158c61bb030f | 729 | mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(mrb, M_PI)); |
mzta | 0:158c61bb030f | 730 | #else |
mzta | 0:158c61bb030f | 731 | mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(mrb, atan(1.0)*4.0)); |
mzta | 0:158c61bb030f | 732 | #endif |
mzta | 0:158c61bb030f | 733 | |
mzta | 0:158c61bb030f | 734 | #ifdef M_E |
mzta | 0:158c61bb030f | 735 | mrb_define_const(mrb, mrb_math, "E", mrb_float_value(mrb, M_E)); |
mzta | 0:158c61bb030f | 736 | #else |
mzta | 0:158c61bb030f | 737 | mrb_define_const(mrb, mrb_math, "E", mrb_float_value(mrb, exp(1.0))); |
mzta | 0:158c61bb030f | 738 | #endif |
mzta | 0:158c61bb030f | 739 | |
mzta | 0:158c61bb030f | 740 | #ifdef MRB_USE_FLOAT |
mzta | 0:158c61bb030f | 741 | mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(mrb, 1e-5)); |
mzta | 0:158c61bb030f | 742 | #else |
mzta | 0:158c61bb030f | 743 | mrb_define_const(mrb, mrb_math, "TOLERANCE", mrb_float_value(mrb, 1e-12)); |
mzta | 0:158c61bb030f | 744 | #endif |
mzta | 0:158c61bb030f | 745 | |
mzta | 0:158c61bb030f | 746 | mrb_define_module_function(mrb, mrb_math, "sin", math_sin, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 747 | mrb_define_module_function(mrb, mrb_math, "cos", math_cos, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 748 | mrb_define_module_function(mrb, mrb_math, "tan", math_tan, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 749 | |
mzta | 0:158c61bb030f | 750 | mrb_define_module_function(mrb, mrb_math, "asin", math_asin, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 751 | mrb_define_module_function(mrb, mrb_math, "acos", math_acos, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 752 | mrb_define_module_function(mrb, mrb_math, "atan", math_atan, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 753 | mrb_define_module_function(mrb, mrb_math, "atan2", math_atan2, MRB_ARGS_REQ(2)); |
mzta | 0:158c61bb030f | 754 | |
mzta | 0:158c61bb030f | 755 | mrb_define_module_function(mrb, mrb_math, "sinh", math_sinh, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 756 | mrb_define_module_function(mrb, mrb_math, "cosh", math_cosh, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 757 | mrb_define_module_function(mrb, mrb_math, "tanh", math_tanh, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 758 | |
mzta | 0:158c61bb030f | 759 | mrb_define_module_function(mrb, mrb_math, "asinh", math_asinh, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 760 | mrb_define_module_function(mrb, mrb_math, "acosh", math_acosh, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 761 | mrb_define_module_function(mrb, mrb_math, "atanh", math_atanh, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 762 | |
mzta | 0:158c61bb030f | 763 | mrb_define_module_function(mrb, mrb_math, "exp", math_exp, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 764 | mrb_define_module_function(mrb, mrb_math, "log", math_log, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); |
mzta | 0:158c61bb030f | 765 | mrb_define_module_function(mrb, mrb_math, "log2", math_log2, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 766 | mrb_define_module_function(mrb, mrb_math, "log10", math_log10, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 767 | mrb_define_module_function(mrb, mrb_math, "sqrt", math_sqrt, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 768 | mrb_define_module_function(mrb, mrb_math, "cbrt", math_cbrt, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 769 | |
mzta | 0:158c61bb030f | 770 | mrb_define_module_function(mrb, mrb_math, "frexp", math_frexp, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 771 | mrb_define_module_function(mrb, mrb_math, "ldexp", math_ldexp, MRB_ARGS_REQ(2)); |
mzta | 0:158c61bb030f | 772 | |
mzta | 0:158c61bb030f | 773 | mrb_define_module_function(mrb, mrb_math, "hypot", math_hypot, MRB_ARGS_REQ(2)); |
mzta | 0:158c61bb030f | 774 | |
mzta | 0:158c61bb030f | 775 | mrb_define_module_function(mrb, mrb_math, "erf", math_erf, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 776 | mrb_define_module_function(mrb, mrb_math, "erfc", math_erfc, MRB_ARGS_REQ(1)); |
mzta | 0:158c61bb030f | 777 | } |
mzta | 0:158c61bb030f | 778 | |
mzta | 0:158c61bb030f | 779 | void |
mzta | 0:158c61bb030f | 780 | mrb_mruby_math_gem_final(mrb_state* mrb) |
mzta | 0:158c61bb030f | 781 | { |
mzta | 0:158c61bb030f | 782 | } |
mzta | 0:158c61bb030f | 783 |