Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 133 lines (113 sloc) 3.137 kb
9595725 update copyrights to 2011
Laurent Sansonetti authored
1 /*
2 * This file is covered by the Ruby license. See COPYING for more details.
7d7d3e8 @ferrous26 Change ownership to The MacRuby Team and update copyrights
ferrous26 authored
3 *
4 * Copyright (C) 2012, The MacRuby Team. All rights reserved.
9595725 update copyrights to 2011
Laurent Sansonetti authored
5 * Copyright (C) 2007-2011, Apple Inc. All rights reserved.
6 * Copyright (C) 1993-2007 Yukihiro Matsumoto
7 */
9c1d230 committing experimental branch content
Laurent Sansonetti authored
8
d0898dd include/ruby/macruby.h -> macruby_internal.h
Laurent Sansonetti authored
9 #include "macruby_internal.h"
9c1d230 committing experimental branch content
Laurent Sansonetti authored
10
11 VALUE rb_mPrecision;
12
13 static ID prc_pr, prc_if;
14
15
16 /*
17 * call-seq:
18 * num.prec(klass) => a_class
19 *
20 * Converts _self_ into an instance of _klass_. By default,
21 * +prec+ invokes
22 *
23 * klass.induced_from(num)
24 *
25 * and returns its value. So, if <code>klass.induced_from</code>
26 * doesn't return an instance of _klass_, it will be necessary
27 * to reimplement +prec+.
28 */
29
30 static VALUE
31 prec_prec(VALUE x, SEL sel, VALUE klass)
32 {
33 return rb_funcall(klass, prc_if, 1, x);
34 }
35
36 /*
37 * call-seq:
38 * num.prec_i => Integer
39 *
40 * Returns an +Integer+ converted from _num_. It is equivalent
41 * to <code>prec(Integer)</code>.
42 */
43
44 static VALUE
45 prec_prec_i(VALUE x, SEL sel)
46 {
47 VALUE klass = rb_cInteger;
48
49 return rb_funcall(x, prc_pr, 1, klass);
50 }
51
52 /*
53 * call-seq:
54 * num.prec_f => Float
55 *
56 * Returns a +Float+ converted from _num_. It is equivalent
57 * to <code>prec(Float)</code>.
58 */
59
60 static VALUE
61 prec_prec_f(VALUE x, SEL sel)
62 {
63 VALUE klass = rb_cFloat;
64
65 return rb_funcall(x, prc_pr, 1, klass);
66 }
67
68 /*
69 * call-seq:
70 * Mod.induced_from(number) => a_mod
71 *
72 * Creates an instance of mod from. This method is overridden
73 * by concrete +Numeric+ classes, so that (for example)
74 *
75 * Fixnum.induced_from(9.9) #=> 9
76 *
77 * Note that a use of +prec+ in a redefinition may cause
78 * an infinite loop.
79 */
80
81 static VALUE
82 prec_induced_from(VALUE module, SEL sel, VALUE x)
83 {
84 rb_raise(rb_eTypeError, "undefined conversion from %s into %s",
85 rb_obj_classname(x), rb_class2name(module));
86 return Qnil; /* not reached */
87 }
88
89 /*
90 * call_seq:
91 * included
92 *
93 * When the +Precision+ module is mixed-in to a class, this +included+
94 * method is used to add our default +induced_from+ implementation
95 * to the host class.
96 */
97
98 static VALUE
99 prec_included(VALUE module, SEL sel, VALUE include)
100 {
101 switch (TYPE(include)) {
102 case T_CLASS:
103 case T_MODULE:
104 break;
105 default:
106 Check_Type(include, T_CLASS);
107 break;
108 }
109 rb_objc_define_method(*(VALUE *)include, "induced_from", prec_induced_from, 1);
110 return module;
111 }
112
113 /*
114 * Precision is a mixin for concrete numeric classes with
115 * precision. Here, `precision' means the fineness of approximation
116 * of a real number, so, this module should not be included into
117 * anything which is not a subset of Real (so it should not be
118 * included in classes such as +Complex+ or +Matrix+).
119 */
120
121 void
122 Init_Precision(void)
123 {
124 rb_mPrecision = rb_define_module("Precision");
125 rb_objc_define_method(*(VALUE *)rb_mPrecision, "included", prec_included, 1);
126 rb_objc_define_method(rb_mPrecision, "prec", prec_prec, 1);
127 rb_objc_define_method(rb_mPrecision, "prec_i", prec_prec_i, 0);
128 rb_objc_define_method(rb_mPrecision, "prec_f", prec_prec_f, 0);
129
130 prc_pr = rb_intern("prec");
131 prc_if = rb_intern("induced_from");
132 }
Something went wrong with that request. Please try again.