* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / prec.c
blob84de958be8fec9e8ccf46e9a8f2e0bf7323db674
1 /**********************************************************************
3 prec.c -
5 $Author$
6 created at: Tue Jan 26 02:40:41 2000
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
14 VALUE rb_mPrecision;
16 static ID prc_pr, prc_if;
20 * call-seq:
21 * num.prec(klass) => a_class
23 * Converts _self_ into an instance of _klass_. By default,
24 * +prec+ invokes
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+.
33 static VALUE
34 prec_prec(VALUE x, VALUE klass)
36 return rb_funcall(klass, prc_if, 1, x);
40 * call-seq:
41 * num.prec_i => Integer
43 * Returns an +Integer+ converted from _num_. It is equivalent
44 * to <code>prec(Integer)</code>.
47 static VALUE
48 prec_prec_i(VALUE x)
50 VALUE klass = rb_cInteger;
52 return rb_funcall(x, prc_pr, 1, klass);
56 * call-seq:
57 * num.prec_f => Float
59 * Returns a +Float+ converted from _num_. It is equivalent
60 * to <code>prec(Float)</code>.
63 static VALUE
64 prec_prec_f(VALUE x)
66 VALUE klass = rb_cFloat;
68 return rb_funcall(x, prc_pr, 1, klass);
72 * call-seq:
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
81 * an infinite loop.
84 static VALUE
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 */
93 * call_seq:
94 * included
96 * When the +Precision+ module is mixed-in to a class, this +included+
97 * method is used to add our default +induced_from+ implementation
98 * to the host class.
101 static VALUE
102 prec_included(VALUE module, VALUE include)
104 switch (TYPE(include)) {
105 case T_CLASS:
106 case T_MODULE:
107 break;
108 default:
109 Check_Type(include, T_CLASS);
110 break;
112 rb_define_singleton_method(include, "induced_from", prec_induced_from, 1);
113 return module;
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+).
124 void
125 Init_Precision(void)
127 #undef rb_intern
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");