[#28509] Rational — Tadayoshi Funaba <tadf@...>

ふなばです。

49 messages 2006/04/05
[#28510] Re: Rational — keiju@... (石塚圭樹) 2006/04/05

けいじゅ@いしつかです.

[#28512] Re: Rational — Tadayoshi Funaba <tadf@...> 2006/04/05

ふなばです。

[#28513] Re: Rational — Shin-ichiro HARA <sinara@...> 2006/04/05

原です。

[#28514] Re: Rational — keiju@... (石塚圭樹) 2006/04/05

けいじゅ@いしつかです.

[#28517] Re: Rational — Yukihiro Matsumoto <matz@...> 2006/04/06

まつもと ゆきひろです

[#28520] Re: Rational — keiju@... (石塚圭樹) 2006/04/06

けいじゅ@いしつかです.

[#28521] Re: Rational — Yukihiro Matsumoto <matz@...> 2006/04/06

まつもと ゆきひろです

[#28525] Re: Rational — keiju@... (石塚圭樹) 2006/04/06

けいじゅ@いしつかです.

[#28527] Re: Rational — Shin-ichiro HARA <sinara@...> 2006/04/06

原です。

[#28536] Re: Rational — Shin-ichiro HARA <sinara@...> 2006/04/10

原です。

[#28537] Re: Rational — keiju@... (石塚圭樹) 2006/04/10

けいじゅ@いしつかです.

[#28589] Float#div and Float#divmod [AGAIN] — Shin-ichiro HARA <sinara@...>

原です。

16 messages 2006/04/23

[ruby-dev:28532] Re: Rational

From: Yukihiro Matsumoto <matz@...>
Date: 2006-04-09 16:44:11 UTC
List: ruby-dev #28532
In message "Re: [ruby-dev:28531] Re: Rational"
    on Mon, 10 Apr 2006 01:26:10 +0900, Shin-ichiro HARA <sinara@blade.nagaokaut.ac.jp> writes:
|
|原です。
|
|>ふなばです。
|>
|>rational/rationalize の話をしたので、原さんの rational 1.00 の
|>Rational(aFloat) と比べてみました。
|
|おお、ありがとうございます。ふなばさんのコードは参考にさせて
|いただきます。
|
|>rationalize のほうは、モノにもよりますが、rational の10倍前後遅いみた
|>いですね。なんか難しいことをしているのでしょうか。これを to_r にするの
|>は少し厳しいのかも。とりあえず、僕がみたかぎりでは、rational のほうが
|>普通っぽいようです。
|
|すいません、rationalize というのを知らないのですが、
|これは何を求める関数なのでしょう?

CommonLispには浮動小数点数を有理数に変換する関数が二つあって、
rationalは高速だが不正確(というかなんというか)で、
rationalizeは低速だがより直観的(というかなんというか)な結果
を返すそうです。以下、CLtL2からの引用です。

---
rational number
rationalize number

Each of these functions converts any non-complex number to a rational number. If the
argument is already rational, it is returned. The two functions differ in their treatment
of floating-point numbers.

rational assumes that the floating-point number is completely accurate and returns a
rational number mathematically equal to the precise value of the floating-point number.

rationalize assumes that the floating-point number is accurate only to the precision of the
floating-point representation and may return any rational number for which the
floating-point number is the best available approximation of its format; in doing this it
attempts to keep both numerator and denominator small.

It is always the case that

(float (rational x) x) == x

and

(float (rationalize x) x) == x

That is, rationalizing a floating-point number by either method and then converting it back
to a floating-point number of the same format produces the original number. What
distinguishes the two functions is that rational typically has a simple, inexpensive
implementation, whereas rationalize goes to more trouble to produce a result that is more
pleasant to view and simpler to compute with for some purposes.

In This Thread