Feature #2347
Math::INFINITY
| Status: | Closed | Start date: | 11/09/2009 | |
|---|---|---|---|---|
| Priority: | Low | Due date: | ||
| Assignee: | % Done: | 100% |
||
| Category: | core | |||
| Target version: | 1.9.2 |
Description
It is easy to get an infinity in Ruby (ex: 1.0/0) but that may not be obvious to everyone.
Could we have Math::INFINITY which would make code using it cleaner?
Thanks
diff --git a/math.c b/math.c
index 51caf35..653a239 100644
--- a/math.c
+++ b/math.c
@@ -764,6 +764,7 @@ Init_Math(void)
#else
rb_define_const(rb_mMath, "E", DBL2NUM(exp(1.0)));
#endif
+ rb_define_const(rb_mMath, "INFINITY", DBL2NUM(1.0/0.0));
rb_define_module_function(rb_mMath, "atan2", math_atan2, 2);
rb_define_module_function(rb_mMath, "cos", math_cos, 1);
Associated revisions
Add Float::INFINITY and Float::NAN.
* numeric.c (Init_Numeric): Add Float::INFINITY and Float::NAN.
[ruby-dev:1657] [ruby-dev:4760] [ruby-list:7023] [ruby-list:46690]
[ruby-core:26632] [ruby-talk:41352] [ruby-talk:203333]
* include/ruby/defines.h (INFINITY): defined.
* include/ruby/defines.h (NAN): defined.
* include/ruby/util.h (ruby_div0): removed.
* numeric.c (fix_pow): use INFINITY and NAN instead of ruby_div0(1.0).
* marshal.c (r_object0): ditto.
* bignum.c (big_fdiv): ditto.
History
Updated by yugui (Yuki Sonoda) over 2 years ago
2009/11/9 Marc-Andre Lafortune <redmine@ruby-lang.org>: > It is easy to get an infinity in Ruby (ex: 1.0/0) but that may not be obvious to everyone. > > Could we have Math::INFINITY which would make code using it cleaner? As my understand, Ruby has not had Math::INFINITY because C does not have it. Introducing infinity might decrease portability of Ruby. But I think actually it is not a problem. Now Ruby assumes the environment has native threading and US-ASCII compatible locale. Here are a problem. Is there an environment which satisfies the conditions but does not have infinity? Thanks, -- Yuki Sonoda (Yugui)
Updated by yugui (Yuki Sonoda) over 2 years ago
2009/11/9 Tanaka Akira <akr@fsij.org>: >> Here are a problem. Is there an environment which satisfies the >> conditions but does not have infinity? > > NetBSD on VAX, I guess. VAX! OK. I understand. I'm against for introducing Math::INFINITY. -- Yuki Sonoda (Yugui)
Updated by usa (Usaku NAKAMURA) over 2 years ago
Hello, In message "[ruby-core:26632] [Feature #2347] Math::INFINITY" on Nov.09,2009 04:53:15, <redmine@ruby-lang.org> wrote: > + rb_define_const(rb_mMath, "INFINITY", DBL2NUM(1.0/0.0)); FYI, some strict compilers doesn't permit to divide by zero literal. Regards, -- U.Nakamura <usa@garbagecollect.jp>
Updated by shugo (Shugo Maeda) over 2 years ago
Hi, 2009/11/9 Yugui <yugui@yugui.jp>: >> Could we have Math::INFINITY which would make code using it cleaner? > > As my understand, Ruby has not had Math::INFINITY because C does not have it. > Introducing infinity might decrease portability of Ruby. But I think > actually it is not a problem. I proposed Float::INFINITY in [ruby-list:7023], and it was discussed in the thread and a thread from [ruby-dev:1657]. 1.0 / 0.0 now produces an infinity, but it raised ZeroDivisionError at that time. I don't remember the reason why Float::INFINITY was rejected, but we have already Float#infinite?, so Math::INFINITY or Float::INFINITY which are provided only on the platform supportng IEEE 754 may not be so bad. Apart from portability issues, someone proposed a class whose instance represents a mathematical infinity at that time, not a IEEE 754 infinity. It may be worth considering. -- Shugo Maeda
Updated by matz (Yukihiro Matsumoto) over 2 years ago
Hi, In message "Re: [ruby-core:26644] Re: [Feature #2347] Math::INFINITY" on Mon, 9 Nov 2009 12:17:41 +0900, Shugo Maeda <shugo@ruby-lang.org> writes: |I don't remember the reason why Float::INFINITY was rejected, but we |have already Float#infinite? It's not because infinity is not defined by Ruby's number system, but because there's no portable way to generate infinity from C. 1.0/0.0 may not be compiled on some compiler. matz.
Updated by marcandre (Marc-Andre Lafortune) over 2 years ago
I'm quite ignorant about the differences between compilers and implementations (and I wouldn't mind remaining this way! ;-) The fact is, Ruby _already_ generates infinities. Line 2471 of numeric.c: #define infinite_value() ruby_div0(1.0) It's used in fix_pow. I think there is a C macro called INFINITY also: http://www.gnu.org/software/libtool/manual/libc/Infinity-and-NaN.html If it is still an imperative that Ruby supports non IEEE architectures, then maybe that constant could be conditional on that? I do not want to judge the decision to support an old platforms or not. I think it would be sad if Ruby was kept back because of supporting them though.
Updated by shugo (Shugo Maeda) over 2 years ago
Hi,
2009/11/10 Marc-Andre Lafortune <redmine@ruby-lang.org>:
> I'm quite ignorant about the differences between compilers and implementations (and I wouldn't mind remaining this way! ;-)
>
> The fact is, Ruby _already_ generates infinities. Line 2471 of numeric.c:
>
> #define infinite_value() ruby_div0(1.0)
FYI, I heard that some compilers doesn't permit zero divisions by
literals, but the following
code can be used to avoid the check:
tmp = 1.0;
inf = 1.0 / (tmp - tmp);
GNU Octave has more complicated code to generate an infinity:
volatile double tmp_inf;
#if defined (SCO)
volatile double tmp = 1.0;
tmp_inf = 1.0 / (tmp - tmp);
#elif defined (__alpha__) && defined (__osf__)
extern unsigned int DINFINITY[2];
tmp_inf = (*(X_CAST(double *, DINFINITY)));
#else
double tmp = 1e+10;
tmp_inf = tmp;
for (;;)
{
tmp_inf *= 1e+10;
if (tmp_inf == tmp)
break;
tmp = tmp_inf;
}
--
Shugo Maeda
Updated by ujihisa (ujihisa .) over 2 years ago
- Status changed from Open to Assigned
Updated by naruse (Yui NARUSE) over 2 years ago
- Priority changed from Normal to Low
Updated by naruse (Yui NARUSE) over 2 years ago
- Status changed from Assigned to Closed
- % Done changed from 0 to 100
This issue was solved with changeset r26197. Marc-Andre, thank you for reporting this issue. Your contribution to Ruby is greatly appreciated. May Ruby be with you.