Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

prec.c

Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   prec.c -
00004 
00005   $Author: nobu $
00006   $Date: 2004/04/14 04:06:52 $
00007   created at: Tue Jan 26 02:40:41 2000
00008 
00009   Copyright (C) 1993-2003 Yukihiro Matsumoto
00010 
00011 **********************************************************************/
00012 
00013 #include "ruby.h"
00014 
00015 VALUE rb_mPrecision;
00016 
00017 static ID prc_pr, prc_if;
00018 
00019 
00020 /*
00021  *  call-seq:
00022  *   num.prec(klass)   => a_klass
00023  *
00024  *  Converts _self_ into an instance of _klass_. By default,
00025  *  +prec+ invokes 
00026  *
00027  *     klass.induced_from(num)
00028  *
00029  *  and returns its value. So, if <code>klass.induced_from</code>
00030  *  doesn't return an instance of _klass_, it will be necessary
00031  *  to reimplement +prec+.
00032  */
00033 
00034 static VALUE
00035 prec_prec(x, klass)
00036     VALUE x, klass;
00037 {
00038     return rb_funcall(klass, prc_if, 1, x);
00039 }
00040 
00041 /*
00042  *  call-seq:
00043  *    num.prec_i  =>  Integer
00044  *
00045  *  Returns an +Integer+ converted from _num_. It is equivalent 
00046  *  to <code>prec(Integer)</code>.
00047  */
00048 
00049 static VALUE
00050 prec_prec_i(x)
00051     VALUE x;
00052 {
00053     VALUE klass = rb_cInteger;
00054 
00055     return rb_funcall(x, prc_pr, 1, klass);
00056 }
00057 
00058 /*
00059  *  call-seq:
00060  *    num.prec_f  =>  Integer
00061  *
00062  *  Returns an +Float+ converted from _num_. It is equivalent 
00063  *  to <code>prec(Float)</code>.
00064  */
00065 
00066 static VALUE
00067 prec_prec_f(x)
00068     VALUE x;
00069 {
00070     VALUE klass = rb_cFloat;
00071 
00072     return rb_funcall(x, prc_pr, 1, klass);
00073 }
00074 
00075 /*
00076  * call-seq:
00077  *   Mod.induced_from(number)  =>  a_mod
00078  * 
00079  * Creates an instance of mod from. This method is overridden
00080  * by concrete +Numeric+ classes, so that (for example)
00081  *
00082  *   Fixnum.induced_from(9.9)   #=>  9
00083  *
00084  * Note that a use of +prec+ in a redefinition may cause
00085  * an infinite loop.
00086  */
00087 
00088 static VALUE
00089 prec_induced_from(module, x)
00090     VALUE module, x;
00091 {
00092     rb_raise(rb_eTypeError, "undefined conversion from %s into %s",
00093             rb_obj_classname(x), rb_class2name(module));
00094     return Qnil;                /* not reached */
00095 }
00096 
00097 /*
00098  * call_seq:
00099  *   included
00100  *
00101  * When the +Precision+ module is mixed-in to a class, this +included+
00102  * method is used to add our default +induced_from+ implementation
00103  * to the host class.
00104  */
00105 
00106 static VALUE
00107 prec_included(module, include)
00108     VALUE module, include;
00109 {
00110     switch (TYPE(include)) {
00111       case T_CLASS:
00112       case T_MODULE:
00113        break;
00114       default:
00115        Check_Type(include, T_CLASS);
00116        break;
00117     }
00118     rb_define_singleton_method(include, "induced_from", prec_induced_from, 1);
00119     return module;
00120 }
00121 
00122 /*
00123  * Precision is a mixin for concrete numeric classes with
00124  * precision.  Here, `precision' means the fineness of approximation
00125  * of a real number, so, this module should not be included into
00126  * anything which is not a subset of Real (so it should not be
00127  * included in classes such as +Complex+ or +Matrix+).
00128 */
00129 
00130 void
00131 Init_Precision()
00132 {
00133     rb_mPrecision = rb_define_module("Precision");
00134     rb_define_singleton_method(rb_mPrecision, "included", prec_included, 1);
00135     rb_define_method(rb_mPrecision, "prec", prec_prec, 1);
00136     rb_define_method(rb_mPrecision, "prec_i", prec_prec_i, 0);
00137     rb_define_method(rb_mPrecision, "prec_f", prec_prec_f, 0);
00138 
00139     prc_pr = rb_intern("prec");
00140     prc_if = rb_intern("induced_from");
00141 }
00142 

Generated on Wed Jan 18 23:32:05 2006 for Ruby by doxygen 1.3.5