Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 142 lines (118 sloc) 3.107 kB
817a62d 2000-05-01
matz authored
1 /**********************************************************************
07bb9f2 1.4.0
matz authored
2
21d0b07 1.4.1
matz authored
3 prec.c -
07bb9f2 1.4.0
matz authored
4
5 $Author$
6 $Date$
56e60da 20000105
matz authored
7 created at: Tue Jan 26 02:40:41 2000
07bb9f2 1.4.0
matz authored
8
42bc5d2 Updated Copyrights of Matz to 2003.
michal authored
9 Copyright (C) 1993-2003 Yukihiro Matsumoto
07bb9f2 1.4.0
matz authored
10
817a62d 2000-05-01
matz authored
11 **********************************************************************/
07bb9f2 1.4.0
matz authored
12
13 #include "ruby.h"
14
15 VALUE rb_mPrecision;
16
17 static ID prc_pr, prc_if;
18
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
19
20 /*
21 * call-seq:
22 * num.prec(klass) => a_klass
23 *
24 * Converts _self_ into an instance of _klass_. By default,
25 * +prec+ invokes
26 *
27 * klass.induced_from(num)
28 *
29 * and returns its value. So, if <code>klass.induced_from</code>
30 * doesn't return an instance of _klass_, it will be necessary
31 * to reimplement +prec+.
32 */
33
07bb9f2 1.4.0
matz authored
34 static VALUE
35 prec_prec(x, klass)
36 VALUE x, klass;
37 {
38 return rb_funcall(klass, prc_if, 1, x);
39 }
40
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
41 /*
42 * call-seq:
43 * num.prec_i => Integer
44 *
45 * Returns an +Integer+ converted from _num_. It is equivalent
46 * to <code>prec(Integer)</code>.
47 */
48
07bb9f2 1.4.0
matz authored
49 static VALUE
50 prec_prec_i(x)
51 VALUE x;
52 {
53 VALUE klass = rb_cInteger;
54
55 return rb_funcall(x, prc_pr, 1, klass);
56 }
57
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
58 /*
59 * call-seq:
68935b1 * prec.c (prec_prec_f): documentation patch from
matz authored
60 * num.prec_f => Float
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
61 *
68935b1 * prec.c (prec_prec_f): documentation patch from
matz authored
62 * Returns a +Float+ converted from _num_. It is equivalent
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
63 * to <code>prec(Float)</code>.
64 */
65
07bb9f2 1.4.0
matz authored
66 static VALUE
67 prec_prec_f(x)
68 VALUE x;
69 {
70 VALUE klass = rb_cFloat;
71
72 return rb_funcall(x, prc_pr, 1, klass);
73 }
74
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
75 /*
76 * call-seq:
77 * Mod.induced_from(number) => a_mod
78 *
79 * Creates an instance of mod from. This method is overridden
80 * by concrete +Numeric+ classes, so that (for example)
81 *
82 * Fixnum.induced_from(9.9) #=> 9
83 *
84 * Note that a use of +prec+ in a redefinition may cause
85 * an infinite loop.
86 */
87
07bb9f2 1.4.0
matz authored
88 static VALUE
89 prec_induced_from(module, x)
0c27272 Mon Jan 15 16:00:07 2001 Yukihiro Matsumoto <matz@ruby-lang.org>
matz authored
90 VALUE module, x;
07bb9f2 1.4.0
matz authored
91 {
92 rb_raise(rb_eTypeError, "undefined conversion from %s into %s",
a0cf2fd * variable.c (rb_obj_classname): new function.
matz authored
93 rb_obj_classname(x), rb_class2name(module));
3b94766 * object.c (rb_str2cstr): warn if string contains \0 and length
matz authored
94 return Qnil; /* not reached */
07bb9f2 1.4.0
matz authored
95 }
96
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
97 /*
98 * call_seq:
99 * included
100 *
101 * When the +Precision+ module is mixed-in to a class, this +included+
102 * method is used to add our default +induced_from+ implementation
103 * to the host class.
104 */
105
07bb9f2 1.4.0
matz authored
106 static VALUE
73a5993 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
107 prec_included(module, include)
07bb9f2 1.4.0
matz authored
108 VALUE module, include;
109 {
110 switch (TYPE(include)) {
111 case T_CLASS:
112 case T_MODULE:
113 break;
114 default:
115 Check_Type(include, T_CLASS);
116 break;
117 }
118 rb_define_singleton_method(include, "induced_from", prec_induced_from, 1);
119 return module;
120 }
121
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
122 /*
123 * Precision is a mixin for concrete numeric classes with
124 * precision. Here, `precision' means the fineness of approximation
125 * of a real number, so, this module should not be included into
cf9c8f9 @nobu * array.c, enum.c, eval.c, file.c, io.c, numeric.c, object.c, prec.c,
nobu authored
126 * anything which is not a subset of Real (so it should not be
23f64f4 Add RDoc for kernel functions, and tidy up
dave authored
127 * included in classes such as +Complex+ or +Matrix+).
128 */
07bb9f2 1.4.0
matz authored
129
130 void
131 Init_Precision()
132 {
133 rb_mPrecision = rb_define_module("Precision");
73a5993 * parse.y (yylex): ternary ? can be followed by newline.
matz authored
134 rb_define_singleton_method(rb_mPrecision, "included", prec_included, 1);
07bb9f2 1.4.0
matz authored
135 rb_define_method(rb_mPrecision, "prec", prec_prec, 1);
136 rb_define_method(rb_mPrecision, "prec_i", prec_prec_i, 0);
137 rb_define_method(rb_mPrecision, "prec_f", prec_prec_f, 0);
138
139 prc_pr = rb_intern("prec");
140 prc_if = rb_intern("induced_from");
141 }
Something went wrong with that request. Please try again.