logo       


Choosing A Webhost:
A web hosting service is a type of Internet hosting service that allows individuals and organizations to provide their own website accessible via the World Wide Web. Web hosts are companies that provide space on a server they own for use by their clients as well as providing Internet connectivity, typically in a data center. Web hosts can also provide data center space and connectivity to the Internet for servers they do not own to be located in their data center, called colocation. more...

[ruby-dev:28530] Re: Rational: msg#00032

Subject: [ruby-dev:28530] Re: Rational
ふなばです。

rational/rationalize の話をしたので、原さんの rational 1.00 の
Rational(aFloat) と比べてみました。

原さんの Rational 1.00 ではこうなりますが、

  Rational(0.3) => 2702159776422297/9007199254740992

いくつかの Common Lisp や Haskell 処理系などで、浮動小数点数の書式が同
じ場合、rational や toRational の結果はちゃんと一致しているようです。
というか、そうじゃないものはみつけられませんでした。

  (rational 0.3f0) => 5033165/16777216
  (rational 0.3d0) => 5404319552844595/18014398509481984

  toRational (0.3::Float) => 5033165 % 16777216
  toRational (0.3::Double) => 5404319552844595 % 18014398509481984

  (inexact->exact 0.3) => 5404319552844595/18014398509481984
  0.3 asFraction => (5404319552844595/18014398509481984)

など。

とりあえず、原さんの Rational 1.00 も rb_rat_flo_to_rat() をつぎのよう
にすると、同様になりそうです。

static VALUE
rb_rat_flo_to_rat(klass, x)
     double x;
{
    double f;
    int e;
    f = frexp(x, &e);
#if 1 /* dbl */
    f = ldexp(f, DBL_MANT_DIG);
    e -= DBL_MANT_DIG;
#else /* flt */
    f = (float)ldexp(f, FLT_MANT_DIG);
    e -= FLT_MANT_DIG;
#endif

    return RT_S_NEW2(klass, RT_MUL(rb_dbl2big(f),
                            RT_POW(rb_int2big(2), INT2FIX(e))), INT2FIX(1));
}

# コード上の数字 2 は、FLT_RADIX にすべきかも。

CL の仕様書によれば「浮動小数点数は (数学的には) s*f*b^(e-p) の形式の
有理数である」らしいので。これに従えば、問題なく中身が取り出せているな
らば、結果も定まるように思います。

今のところ、どうすべきか、ということはないのですが、なぜ違うのかはちょっ
と疑問です。

rationalize のほうは、モノにもよりますが、rational の10倍前後遅いみた
いですね。なんか難しいことをしているのでしょうか。これを to_r にするの
は少し厳しいのかも。とりあえず、僕がみたかぎりでは、rational のほうが
普通っぽいようです。




Ruby Jobs
Java Jobs
Jobs in California
more...
what
job title, keywords
where
city, state, zip
jobs by job search
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
db.firebase.por...    text.xml.xalan....    qnx.openqnx.dev...    user-groups.zar...    internationaliz...    kde.devel.konve...    finance.e-gold....    emacs.latex.pre...    gis.therion/200...    web.webmin.gene...    yellowdog.gener...    vserver/2003-08...    redhat.release....    sysutils.tivoli...    xfree86.expert/...    mail.becky.user...    hardware.netapp...    netbsd.ports.xe...    python.distutil...    boot-loaders.gr...    culture.interne...    java.springfram...    activedir/2006-...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe