static VALUE rb_gsl_blas_dscal(int argc, VALUE *argv, VALUE obj) { double a; gsl_vector *x = NULL; switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc); Need_Float(argv[0]); CHECK_VECTOR(argv[1]); a = RFLOAT_VALUE(argv[0]); Data_Get_Struct(argv[1], gsl_vector, x); gsl_blas_dscal(a, x); return argv[1]; break; default: if (argc != 1) rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc); Need_Float(argv[0]); a = RFLOAT_VALUE(argv[0]); Data_Get_Struct(obj, gsl_vector, x); gsl_blas_dscal(a, x); return obj; break; } return Qnil; /* never reach here */ }
static void type_to_native(VALUE value, ID data_type, void *native_value) { if (id_type_char == data_type || id_type_uchar == data_type) { if (TYPE(value) == T_FIXNUM) { value = rb_funcall(value, rb_intern("chr"), 0); } *((cl_char *)native_value) = RSTRING_PTR(value)[0]; return; } if (id_type_float == data_type || id_type_double == data_type) { *((cl_float *)native_value) = TYPE(value) == T_FIXNUM ? (cl_float)FIX2INT(value) : RFLOAT_VALUE(value); return; } if (id_type_half == data_type) { *((cl_half *)native_value) = TYPE(value) == T_FIXNUM ? (cl_half)FIX2INT(value) : RFLOAT_VALUE(value); return; } TYPE_TO_NATIVE(bool, char, FIX2INT); TYPE_TO_NATIVE(short, cl_short, FIX2INT); TYPE_TO_NATIVE(ushort, cl_ushort, NUM2UINT); TYPE_TO_NATIVE(int, cl_int, FIX2INT); TYPE_TO_NATIVE(uint, cl_uint, NUM2UINT); TYPE_TO_NATIVE(long, cl_long, NUM2LONG); TYPE_TO_NATIVE(ulong, cl_ulong, NUM2ULONG); TYPE_TO_NATIVE(size_t, cl_uint, NUM2UINT); TYPE_TO_NATIVE(ptrdiff_t, cl_uint, NUM2UINT); TYPE_TO_NATIVE(intptr_t, cl_uint, NUM2UINT); TYPE_TO_NATIVE(uintptr_t, cl_uint, NUM2UINT); }
static VALUE rb_gsl_blas_daxpy2(int argc, VALUE *argv, VALUE obj) { double a; gsl_vector *x = NULL, *y = NULL, *y2 = NULL; switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: get_vector2(argc-1, argv+1, obj, &x, &y); Need_Float(argv[0]); a = RFLOAT_VALUE(argv[0]); break; default: Data_Get_Struct(obj, gsl_vector, x); if (argc != 2) rb_raise(rb_eArgError, "wrong number of arguments (%d for 2)", argc); Need_Float(argv[0]); CHECK_VECTOR(argv[1]); a = RFLOAT_VALUE(argv[0]); Data_Get_Struct(argv[1], gsl_vector, y); break; } y2 = gsl_vector_alloc(y->size); gsl_vector_memcpy(y2, y); gsl_blas_daxpy(a, x, y2); return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, y2); }
static inline double num2dbl_with_to_f(VALUE num) { if (SPECIAL_CONST_P(num)) { if (FIXNUM_P(num)) { if (basic_to_f_p(rb_cFixnum)) return fix2dbl_without_to_f(num); } else if (FLONUM_P(num)) { return RFLOAT_VALUE(num); } } else { switch (BUILTIN_TYPE(num)) { case T_FLOAT: return RFLOAT_VALUE(num); case T_BIGNUM: if (basic_to_f_p(rb_cBignum)) return big2dbl_without_to_f(num); break; case T_RATIONAL: if (basic_to_f_p(rb_cRational)) return rat2dbl_without_to_f(num); break; } } return RFLOAT_VALUE(rb_to_float(num)); }
VALUE rb_num_cbrt( VALUE num) { #if HAVE_FUNC_CBRT return rb_float_new( cbrt( RFLOAT_VALUE( rb_Float( num)))); #else double n; int neg; int i; n = RFLOAT_VALUE( rb_Float( num)); if ((neg = n < 0)) n = -n; n = sqrt( sqrt( n)); i = 2; for (;;) { double w = n; int j; for (j=i;j;--j) w = sqrt( w); i *= 2; w *= n; if (n == w) break; n = w; } return rb_float_new( neg ? -n : n); #endif }
VALUE point_in_poly(VALUE self, VALUE rb_x, VALUE rb_y, VALUE rb_points) { Check_Type(rb_x, T_FLOAT); Check_Type(rb_y, T_FLOAT); Check_Type(rb_points, T_ARRAY); int i = 0; int s = (int)RARRAY_LEN(rb_points); double *vertx; double *verty; vertx = calloc(s, sizeof(double)); verty = calloc(s, sizeof(double)); double test_x = RFLOAT_VALUE(rb_x); double test_y = RFLOAT_VALUE(rb_y); ID i_x = rb_intern("x"); ID i_y = rb_intern("y"); for(i = 0; i < s; i++) { VALUE point = RARRAY_PTR(rb_points)[i]; VALUE x = rb_funcall(point, i_x, 0); VALUE y = rb_funcall(point, i_y, 0); Check_Type(x, T_FLOAT); Check_Type(y, T_FLOAT); vertx[i] = RFLOAT_VALUE(x); verty[i] = RFLOAT_VALUE(y); } return pnpoly(s, vertx, verty, test_x, test_y); }
static VALUE math_atan2(VALUE obj, VALUE y, VALUE x) { double dx, dy; Need_Float2(y, x); dx = RFLOAT_VALUE(x); dy = RFLOAT_VALUE(y); if (dx == 0.0 && dy == 0.0) domain_error("atan2"); if (isinf(dx) && isinf(dy)) domain_error("atan2"); return DBL2NUM(atan2(dy, dx)); }
static VALUE safe_mul(VALUE a, VALUE b, int az, int bz) { double v; if (!az && bz && RB_FLOAT_TYPE_P(a) && (v = RFLOAT_VALUE(a), !isnan(v))) { a = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0); } if (!bz && az && RB_FLOAT_TYPE_P(b) && (v = RFLOAT_VALUE(b), !isnan(v))) { b = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0); } return f_mul(a, b); }
// comparison function for use with qsort int comp_score(const void *a, const void *b) { VALUE a_val = *(VALUE *)a; VALUE b_val = *(VALUE *)b; ID score = rb_intern("score"); double a_score = RFLOAT_VALUE(rb_funcall(a_val, score, 0)); double b_score = RFLOAT_VALUE(rb_funcall(b_val, score, 0)); if (a_score > b_score) return -1; // a scores higher, a should appear sooner else if (a_score < b_score) return 1; // b scores higher, a should appear later else return comp_alpha(a, b); }
VALUE rb_IupPPlotAdd(VALUE node,VALUE v_x,VALUE v_y) { float x,y; VALUE v_ih = rb_iv_get(node,"@ihandle"); if(TYPE(v_x)==T_FIXNUM) x = NUM2INT(v_x); else x = RFLOAT_VALUE(v_x); if(TYPE(v_y)==T_FIXNUM) y = NUM2INT(v_y); else y = RFLOAT_VALUE(v_y); IupPPlotAdd((Ihandle*)NUM2ULONG(v_ih),x,y); return Qnil; }
VALUE rb_num_grammatical( VALUE num, VALUE sing, VALUE plu) { long l; double d; switch (TYPE( num)) { case T_FIXNUM: l = NUM2LONG( num); if (l == 1l || l == -1l) return sing; break; case T_BIGNUM: /* 1 is not a Bignum */ break; case T_FLOAT: d = RFLOAT_VALUE( num); if (d == 1.0 || d == -1.0) return sing; break; default: l = NUM2LONG( rb_funcall( num, id_cmp, 1, INT2FIX( 1))); if (l == 0) return sing; l = NUM2LONG( rb_funcall( num, id_cmp, 1, INT2FIX(-1))); if (l == 0) return sing; break; } return plu; }
VALUE rb_num_neg_p( VALUE num) { VALUE r = Qfalse; switch (TYPE( num)) { case T_FIXNUM: if (NUM2LONG( num) < 0) r = Qtrue; break; case T_BIGNUM: if (!RBIGNUM_SIGN( num)) /* 0 is not a Bignum. */ r = Qtrue; break; case T_FLOAT: if (RFLOAT_VALUE( num) < 0.0) r = Qtrue; break; default: return rb_num_neg_p( rb_funcall( num, id_cmp, 1, INT2FIX( 0))); break; } return r; }
VALUE math_sin(VALUE obj, SEL sel, VALUE x) { Need_Float(x); return DBL2NUM(sin(RFLOAT_VALUE(x))); }
/* * call-seq: * poller.poll(1) => Fixnum * * Multiplexes input/output events in a level-triggered fashion over a set of registered sockets. * * === Examples * * Supported timeout values : * * -1 : block until any sockets are ready (no timeout) * 0 : non-blocking poll * 1 : block for up to 1 second (1000ms) * 0.1 : block for up to 0.1 seconds (100ms) * * poller = ZMQ::Poller.new => ZMQ::Poller * poller.register(req) => true * poller.poll(1) => Fixnum * */ VALUE rb_czmq_poller_poll(int argc, VALUE *argv, VALUE obj) { VALUE tmout; size_t timeout; struct nogvl_poll_args args; int rc; ZmqGetPoller(obj); rb_scan_args(argc, argv, "01", &tmout); if (NIL_P(tmout)) tmout = INT2NUM(0); if (TYPE(tmout) != T_FIXNUM && TYPE(tmout) != T_FLOAT) rb_raise(rb_eTypeError, "wrong timeout type %s (expected Fixnum or Float)", RSTRING_PTR(rb_obj_as_string(tmout))); if (poller->poll_size == 0) return INT2NUM(0); if (poller->dirty == TRUE) { rc = rb_czmq_poller_rebuild_pollset(poller); if (rc == -1) rb_raise(rb_eZmqError, "failed in rebuilding the pollset!"); } timeout = (size_t)(((TYPE(tmout) == T_FIXNUM) ? FIX2LONG(tmout) : RFLOAT_VALUE(tmout)) * 1000); if (timeout < 0) timeout = -1; args.items = poller->pollset; args.nitems = poller->poll_size; args.timeout = (long)timeout; rc = (int)rb_thread_blocking_region(rb_czmq_nogvl_poll, (void *)&args, RUBY_UBF_IO, 0); ZmqAssert(rc); if (rc == 0) { rb_ary_clear(poller->readables); rb_ary_clear(poller->writables); } else { rb_czmq_poller_rebuild_selectables(poller); } return INT2NUM(rc); }
/* * call-seq: to_json(*) * * Returns a JSON string representation for this Float number. */ static VALUE mFloat_to_json(int argc, VALUE *argv, VALUE self) { JSON_Generator_State *state = NULL; VALUE Vstate, rest, tmp, result; double value = RFLOAT_VALUE(self); rb_scan_args(argc, argv, "01*", &Vstate, &rest); if (!NIL_P(Vstate)) Data_Get_Struct(Vstate, JSON_Generator_State, state); if (isinf(value)) { if (!state || state->allow_nan) { result = rb_funcall(self, i_to_s, 0); } else { tmp = rb_funcall(self, i_to_s, 0); rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp)); } } else if (isnan(value)) { if (!state || state->allow_nan) { result = rb_funcall(self, i_to_s, 0); } else { tmp = rb_funcall(self, i_to_s, 0); rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp)); } } else { result = rb_funcall(self, i_to_s, 0); } FORCE_UTF8(result); return result; }
static VALUE t_set_heartbeat_interval (VALUE self, VALUE interval) { float iv = RFLOAT_VALUE(interval); if (evma_set_heartbeat_interval(iv)) return Qtrue; return Qfalse; }
static VALUE t_set_comm_inactivity_timeout (VALUE self, VALUE signature, VALUE timeout) { float ti = RFLOAT_VALUE(timeout); if (evma_set_comm_inactivity_timeout (NUM2ULONG (signature), ti)); return Qtrue; return Qfalse; }
/* * call-seq: * Enumerator.new(size = nil) { |yielder| ... } * Enumerator.new(obj, method = :each, *args) * * Creates a new Enumerator object, which can be used as an * Enumerable. * * In the first form, iteration is defined by the given block, in * which a "yielder" object, given as block parameter, can be used to * yield a value by calling the +yield+ method (aliased as +<<+): * * fib = Enumerator.new do |y| * a = b = 1 * loop do * y << a * a, b = b, a + b * end * end * * p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] * * The optional parameter can be used to specify how to calculate the size * in a lazy fashion (see Enumerator#size). It can either be a value or * a callable object. * * In the second, deprecated, form, a generated Enumerator iterates over the * given object using the given method with the given arguments passed. * * Use of this form is discouraged. Use Kernel#enum_for or Kernel#to_enum * instead. * * e = Enumerator.new(ObjectSpace, :each_object) * #-> ObjectSpace.enum_for(:each_object) * * e.select { |obj| obj.is_a?(Class) } #=> array of all classes * */ static VALUE enumerator_initialize(int argc, VALUE *argv, VALUE obj) { VALUE recv, meth = sym_each; VALUE size = Qnil; if (rb_block_given_p()) { rb_check_arity(argc, 0, 1); recv = generator_init(generator_allocate(rb_cGenerator), rb_block_proc()); if (argc) { if (NIL_P(argv[0]) || rb_obj_is_proc(argv[0]) || (RB_TYPE_P(argv[0], T_FLOAT) && RFLOAT_VALUE(argv[0]) == INFINITY)) { size = argv[0]; } else { size = rb_to_int(argv[0]); } argc = 0; } } else { rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); rb_warn("Enumerator.new without a block is deprecated; use Object#to_enum"); recv = *argv++; if (--argc) { meth = *argv++; --argc; } } return enumerator_init(obj, recv, meth, argc, argv, 0, size); }
static VALUE t_set_pending_connect_timeout (VALUE self, VALUE signature, VALUE timeout) { float ti = RFLOAT_VALUE(timeout); if (evma_set_pending_connect_timeout (NUM2ULONG (signature), ti)); return Qtrue; return Qfalse; }
octave_value OR_Variable::to_octave() { if (ruby_val == Qtrue) { return true; } else if (ruby_val == Qfalse) { return false; } else if (ruby_val == Qnil) { return octave_NaN; } else if (rb_type(ruby_val) == T_FLOAT) { return RFLOAT_VALUE(ruby_val); } else if (rb_type(ruby_val) == T_FIXNUM) { return FIX2LONG(ruby_val); } else if (rb_obj_is_kind_of(ruby_val, rb_path2class("Array")) == Qtrue) { return OR_Array(ruby_val).to_octave(); } else if (rb_obj_is_kind_of(ruby_val, rb_path2class("Hash")) == Qtrue) { return OR_Hash(ruby_val).to_octave(); } else if (rb_obj_is_kind_of(ruby_val, rb_path2class("Octave::StructMatrix")) == Qtrue) { return OR_StructMatrix(ruby_val).to_octave(); } else if (rb_obj_is_kind_of(ruby_val, rb_path2class("Octave::CellMatrix")) == Qtrue) { return OR_CellMatrix(ruby_val).to_octave(); } else if (rb_obj_is_kind_of(ruby_val, rb_path2class("Octave::Matrix")) == Qtrue) { return OR_Matrix(ruby_val).to_octave(); } else { return OR_String(ruby_val).to_octave(); } }
octave_value OR_Matrix::to_octave() { int row_index, column_index; int number_of_rows = FIX2INT(rb_iv_get(ruby_val, "@m")); int number_of_columns = FIX2INT(rb_iv_get(ruby_val, "@n")); VALUE cells = rb_iv_get(ruby_val, "@cells"); VALUE row, cell; Matrix matrix = Matrix(number_of_rows, number_of_columns); for (row_index = 0; row_index < number_of_rows; row_index++) { row = RARRAY_PTR(cells)[row_index]; for (column_index = 0; column_index < number_of_columns; column_index++) { cell = RARRAY_PTR(row)[column_index]; if (rb_type(cell) == T_FLOAT) { matrix(row_index, column_index) = RFLOAT_VALUE(cell); } else if (rb_type(cell) == T_FIXNUM) { matrix(row_index, column_index) = FIX2LONG(cell); } else { matrix(row_index, column_index) = octave_NaN; } } } return matrix; }
static VALUE math_cosh(VALUE obj, VALUE x) { Need_Float(x); return DBL2NUM(cosh(RFLOAT_VALUE(x))); }
static VALUE math_tan(VALUE obj, VALUE x) { Need_Float(x); return DBL2NUM(tan(RFLOAT_VALUE(x))); }
VALUE rb_IupPPlotInsert(VALUE node,VALUE v_index,VALUE v_sample_index,VALUE v_x,VALUE v_y) { float x,y; int index = NUM2INT(v_index); int sample_index = NUM2INT(v_sample_index); VALUE v_ih = rb_iv_get(node,"@ihandle"); if(TYPE(v_x)==T_FIXNUM) x = NUM2INT(v_x); else x = RFLOAT_VALUE(v_x); if(TYPE(v_y)==T_FIXNUM) y = NUM2INT(v_y); else y = RFLOAT_VALUE(v_y); IupPPlotInsert((Ihandle*)NUM2ULONG(v_ih),index,sample_index,x,y); return Qnil; }
static VALUE t_set_comm_inactivity_timeout (VALUE self UNUSED, VALUE signature, VALUE timeout) { float ti = RFLOAT_VALUE(timeout); if (evma_set_comm_inactivity_timeout(NUM2BSIG(signature), ti)) { return Qtrue; } return Qfalse; }
static VALUE t_set_pending_connect_timeout (VALUE self UNUSED, VALUE signature, VALUE timeout) { float ti = RFLOAT_VALUE(timeout); if (evma_set_pending_connect_timeout(NUM2BSIG(signature), ti)) { return Qtrue; } return Qfalse; }
/* * call-seq: * flo.arg -> 0 or float * flo.angle -> 0 or float * flo.phase -> 0 or float * * Returns 0 if the value is positive, pi otherwise. */ static VALUE float_arg(VALUE self) { if (isnan(RFLOAT_VALUE(self))) return self; if (f_tpositive_p(self)) return INT2FIX(0); return rb_const_get(rb_mMath, id_PI); }
inline static VALUE f_signbit(VALUE x) { if (RB_TYPE_P(x, T_FLOAT)) { double f = RFLOAT_VALUE(x); return f_boolcast(!isnan(f) && signbit(f)); } return f_negative_p(x); }
/* * call-seq: * flo.denominator -> integer * * Returns the denominator (always positive). The result is machine * dependent. * * See numerator. */ static VALUE float_denominator(VALUE self, SEL sel) { double d = RFLOAT_VALUE(self); if (isinf(d) || isnan(d)) return INT2FIX(1); return ZERO; // return rb_call_super(0, 0); }
/** * lua_SetVarToStack( lua_State *L, VALUE vVarValue ) * * Desc: Positionne une valeur Ruby dans la pile Lua */ static void lua_SetVarToStack( lua_State *L, VALUE vVarValue ) { int iStackSize; long iRbArrayLength; int iCpt; VALUE keys; switch( TYPE(vVarValue) ) { case T_NIL: lua_pushnil( L ); break; case T_TRUE: lua_pushboolean( L, 1 ); break; case T_FALSE: lua_pushboolean( L, 0 ); break; case T_STRING: lua_pushlstring( L, RSTRING_PTR(vVarValue), RSTRING_LEN(vVarValue) ); break; case T_FIXNUM: lua_pushnumber( L, FIX2INT(vVarValue) ); break; case T_BIGNUM: lua_pushnumber( L, NUM2DBL(vVarValue) ); break; case T_FLOAT: lua_pushnumber( L, (lua_Number)RFLOAT_VALUE(vVarValue) ); break; case T_ARRAY: lua_newtable( L ); iStackSize = lua_gettop( L ); iRbArrayLength = RARRAY_LEN( vVarValue ); lua_pushstring( L, "n" ); lua_pushnumber( L, iRbArrayLength ); lua_settable( L, -3 ); for( iCpt = 0; iCpt < iRbArrayLength; iCpt++ ) { lua_SetVarToStack( L, RARRAY_PTR(vVarValue)[iCpt] ); lua_rawseti( L, iStackSize, iCpt + 1 ); } break; case T_HASH: lua_newtable( L ); keys = rb_funcall( vVarValue, rb_intern("keys"), 0 ); for( iCpt = 0; iCpt <= RARRAY_LEN(keys)-1; iCpt++ ){ VALUE vHashKey; vHashKey = *(RARRAY_PTR(keys)+iCpt); lua_pushlstring( L, RSTRING_PTR(vHashKey), RSTRING_LEN(vHashKey) ); lua_SetVarToStack( L, rb_hash_aref(vVarValue, vHashKey) ); lua_settable( L, -3 ); } break; default: lua_pushlightuserdata( L,(void*)vVarValue ); /* saves ruby object */ break; } }