1 /**********************************************************************
6 created at: Tue Jan 26 02:40:41 2000
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
16 static ID prc_pr
, prc_if
;
21 * num.prec(klass) => a_class
23 * Converts _self_ into an instance of _klass_. By default,
26 * klass.induced_from(num)
28 * and returns its value. So, if <code>klass.induced_from</code>
29 * doesn't return an instance of _klass_, it will be necessary
30 * to reimplement +prec+.
34 prec_prec(VALUE x
, VALUE klass
)
36 return rb_funcall(klass
, prc_if
, 1, x
);
41 * num.prec_i => Integer
43 * Returns an +Integer+ converted from _num_. It is equivalent
44 * to <code>prec(Integer)</code>.
50 VALUE klass
= rb_cInteger
;
52 return rb_funcall(x
, prc_pr
, 1, klass
);
59 * Returns a +Float+ converted from _num_. It is equivalent
60 * to <code>prec(Float)</code>.
66 VALUE klass
= rb_cFloat
;
68 return rb_funcall(x
, prc_pr
, 1, klass
);
73 * Mod.induced_from(number) => a_mod
75 * Creates an instance of mod from. This method is overridden
76 * by concrete +Numeric+ classes, so that (for example)
78 * Fixnum.induced_from(9.9) #=> 9
80 * Note that a use of +prec+ in a redefinition may cause
85 prec_induced_from(VALUE module
, VALUE x
)
87 rb_raise(rb_eTypeError
, "undefined conversion from %s into %s",
88 rb_obj_classname(x
), rb_class2name(module
));
89 return Qnil
; /* not reached */
96 * When the +Precision+ module is mixed-in to a class, this +included+
97 * method is used to add our default +induced_from+ implementation
102 prec_included(VALUE module
, VALUE include
)
104 switch (TYPE(include
)) {
109 Check_Type(include
, T_CLASS
);
112 rb_define_singleton_method(include
, "induced_from", prec_induced_from
, 1);
117 * Precision is a mixin for concrete numeric classes with
118 * precision. Here, `precision' means the fineness of approximation
119 * of a real number, so, this module should not be included into
120 * anything which is not a subset of Real (so it should not be
121 * included in classes such as +Complex+ or +Matrix+).
128 #define rb_intern(str) rb_intern_const(str)
130 rb_mPrecision
= rb_define_module("Precision");
131 rb_define_singleton_method(rb_mPrecision
, "included", prec_included
, 1);
132 rb_define_method(rb_mPrecision
, "prec", prec_prec
, 1);
133 rb_define_method(rb_mPrecision
, "prec_i", prec_prec_i
, 0);
134 rb_define_method(rb_mPrecision
, "prec_f", prec_prec_f
, 0);
136 prc_pr
= rb_intern("prec");
137 prc_if
= rb_intern("induced_from");