2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1999-08-13 09:45:20 +04:00
|
|
|
|
1999-09-16 13:40:33 +04:00
|
|
|
prec.c -
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
2000-01-05 07:41:21 +03:00
|
|
|
created at: Tue Jan 26 02:40:41 2000
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2003-01-16 10:34:03 +03:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
|
|
|
|
VALUE rb_mPrecision;
|
|
|
|
|
|
|
|
static ID prc_pr, prc_if;
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* num.prec(klass) => a_klass
|
|
|
|
*
|
|
|
|
* Converts _self_ into an instance of _klass_. By default,
|
|
|
|
* +prec+ invokes
|
|
|
|
*
|
|
|
|
* klass.induced_from(num)
|
|
|
|
*
|
|
|
|
* and returns its value. So, if <code>klass.induced_from</code>
|
|
|
|
* doesn't return an instance of _klass_, it will be necessary
|
|
|
|
* to reimplement +prec+.
|
|
|
|
*/
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
prec_prec(x, klass)
|
|
|
|
VALUE x, klass;
|
|
|
|
{
|
|
|
|
return rb_funcall(klass, prc_if, 1, x);
|
|
|
|
}
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* num.prec_i => Integer
|
|
|
|
*
|
|
|
|
* Returns an +Integer+ converted from _num_. It is equivalent
|
|
|
|
* to <code>prec(Integer)</code>.
|
|
|
|
*/
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
prec_prec_i(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
|
|
|
VALUE klass = rb_cInteger;
|
|
|
|
|
|
|
|
return rb_funcall(x, prc_pr, 1, klass);
|
|
|
|
}
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* num.prec_f => Integer
|
|
|
|
*
|
|
|
|
* Returns an +Float+ converted from _num_. It is equivalent
|
|
|
|
* to <code>prec(Float)</code>.
|
|
|
|
*/
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
prec_prec_f(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
|
|
|
VALUE klass = rb_cFloat;
|
|
|
|
|
|
|
|
return rb_funcall(x, prc_pr, 1, klass);
|
|
|
|
}
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Mod.induced_from(number) => a_mod
|
|
|
|
*
|
|
|
|
* Creates an instance of mod from. This method is overridden
|
|
|
|
* by concrete +Numeric+ classes, so that (for example)
|
|
|
|
*
|
|
|
|
* Fixnum.induced_from(9.9) #=> 9
|
|
|
|
*
|
|
|
|
* Note that a use of +prec+ in a redefinition may cause
|
|
|
|
* an infinite loop.
|
|
|
|
*/
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
prec_induced_from(module, x)
|
2001-01-15 10:01:00 +03:00
|
|
|
VALUE module, x;
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
|
|
|
rb_raise(rb_eTypeError, "undefined conversion from %s into %s",
|
2003-01-31 07:00:17 +03:00
|
|
|
rb_obj_classname(x), rb_class2name(module));
|
2001-03-28 12:43:25 +04:00
|
|
|
return Qnil; /* not reached */
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
/*
|
|
|
|
* call_seq:
|
|
|
|
* included
|
|
|
|
*
|
|
|
|
* When the +Precision+ module is mixed-in to a class, this +included+
|
|
|
|
* method is used to add our default +induced_from+ implementation
|
|
|
|
* to the host class.
|
|
|
|
*/
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
2001-08-29 10:28:51 +04:00
|
|
|
prec_included(module, include)
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE module, include;
|
|
|
|
{
|
|
|
|
switch (TYPE(include)) {
|
|
|
|
case T_CLASS:
|
|
|
|
case T_MODULE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Check_Type(include, T_CLASS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rb_define_singleton_method(include, "induced_from", prec_induced_from, 1);
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2003-12-30 19:38:32 +03:00
|
|
|
/*
|
|
|
|
* Precision is a mixin for concrete numeric classes with
|
|
|
|
* precision. Here, `precision' means the fineness of approximation
|
|
|
|
* of a real number, so, this module should not be included into
|
* array.c, enum.c, eval.c, file.c, io.c, numeric.c, object.c, prec.c,
process.c, re.c, string.c: typos in RDoc comments. [ruby-core:02783]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-14 08:06:52 +04:00
|
|
|
* anything which is not a subset of Real (so it should not be
|
2003-12-30 19:38:32 +03:00
|
|
|
* included in classes such as +Complex+ or +Matrix+).
|
|
|
|
*/
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
Init_Precision()
|
|
|
|
{
|
|
|
|
rb_mPrecision = rb_define_module("Precision");
|
2001-08-29 10:28:51 +04:00
|
|
|
rb_define_singleton_method(rb_mPrecision, "included", prec_included, 1);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_method(rb_mPrecision, "prec", prec_prec, 1);
|
|
|
|
rb_define_method(rb_mPrecision, "prec_i", prec_prec_i, 0);
|
|
|
|
rb_define_method(rb_mPrecision, "prec_f", prec_prec_f, 0);
|
|
|
|
|
|
|
|
prc_pr = rb_intern("prec");
|
|
|
|
prc_if = rb_intern("induced_from");
|
|
|
|
}
|