Module: CMath

Defined in:
mruby/mrbgems/mruby-cmath/src/cmath.c

Class Method Summary collapse

Class Method Details

.acosObject

.acoshObject

.asinObject

.asinhObject

.atanObject

.atanhObject

.cosObject

.coshObject

.expObject

.logObject

log(z): return the natural logarithm of z, with branch cut along the negative real axis



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'mruby/mrbgems/mruby-cmath/src/cmath.c', line 143

DEF_CMATH_METHOD(exp)

/* log(z): return the natural logarithm of z, with branch cut along the negative real axis */
static mrb_value
cmath_log(mrb_state *mrb, mrb_value self) {
  mrb_value z;
  mrb_float base;
  mrb_float real, imag;

  mrb_int n = mrb_get_args(mrb, "o|f", &z, &base);

#ifndef M_E
#define M_E F(exp)(1.0)
#endif

  if (n == 1) base = M_E;
  if (cmath_get_complex(mrb, z, &real, &imag) || real < 0.0) {
    mrb_complex c = CX(real,imag);
    c = FC(log)(c);
    if (n == 2) c = CXDIVc(c, FC(log)(CX(base,0)));
    return mrb_complex_new(mrb, creal(c), cimag(c));
  }
  if (n == 1) return mrb_float_value(mrb, F(log)(real));
  return mrb_float_value(mrb, F(log)(real)/F(log)(base));
}

.log10Object

log10(z): return the base-10 logarithm of z, with branch cut along the negative real axis



170
171
172
173
174
175
176
177
178
179
180
# File 'mruby/mrbgems/mruby-cmath/src/cmath.c', line 170

static mrb_value
cmath_log10(mrb_state *mrb, mrb_value self) {
  mrb_value z = mrb_get_arg1(mrb);
  mrb_float real, imag;
  if (cmath_get_complex(mrb, z, &real, &imag) || real < 0.0) {
    mrb_complex c = CX(real,imag);
    c = CXDIVf(FC(log)(c),log(10));
    return mrb_complex_new(mrb, creal(c), cimag(c));
  }
  return mrb_float_value(mrb, F(log10)(real));
}

.log2Object

log2(z): return the base-2 logarithm of z, with branch cut along the negative real axis



183
184
185
186
187
188
189
190
191
192
193
# File 'mruby/mrbgems/mruby-cmath/src/cmath.c', line 183

static mrb_value
cmath_log2(mrb_state *mrb, mrb_value self) {
  mrb_value z = mrb_get_arg1(mrb);
  mrb_float real, imag;
  if (cmath_get_complex(mrb, z, &real, &imag) || real < 0.0) {
    mrb_complex c = CX(real,imag);
    c = CXDIVf(FC(log)(c),log(2.0));
    return mrb_complex_new(mrb, creal(c), cimag(c));
  }
  return mrb_float_value(mrb, F(log2)(real));
}

.sinObject

.sinhObject

.sqrtObject

sqrt(z): return square root of z



196
197
198
199
200
201
202
203
204
205
206
# File 'mruby/mrbgems/mruby-cmath/src/cmath.c', line 196

static mrb_value
cmath_sqrt(mrb_state *mrb, mrb_value self) {
  mrb_value z = mrb_get_arg1(mrb);
  mrb_float real, imag;
  if (cmath_get_complex(mrb, z, &real, &imag) || real < 0.0) {
    mrb_complex c = CX(real,imag);
    c = FC(sqrt)(c);
    return mrb_complex_new(mrb, creal(c), cimag(c));
  }
  return mrb_float_value(mrb, F(sqrt)(real));
}

.tanObject

.tanhObject