## Feature #2347

### Math::INFINITY

**Description**

=begin

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);

=end

### History

#### #1 Updated by Yuki Sonoda almost 7 years ago

=begin

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)

=end

#### #2 Updated by Yuki Sonoda almost 7 years ago

=begin

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)

=end

#### #3 Updated by Usaku NAKAMURA almost 7 years ago

=begin

Hello,

In message " [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

=end

#### #4 Updated by Shugo Maeda almost 7 years ago

=begin

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 , and it was discussed

in the thread

and a thread from . 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

=end

#### #5 Updated by Yukihiro Matsumoto almost 7 years ago

=begin

Hi,

In message "Re: 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.

=end

#### #6 Updated by Marc-Andre Lafortune almost 7 years ago

=begin

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.

=end

#### #7 Updated by Shugo Maeda almost 7 years ago

=begin

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

alreadygenerates 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

=end

#### #10 Updated by Yui NARUSE over 6 years ago

**Status**changed from*Assigned*to*Closed***% Done**changed from*0*to*100*

=begin

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.

=end

#### #11 [ruby-core:46720] Updated by Adam Carpenter about 4 years ago

naruse (Yui NARUSE) wrote:

=begin

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.=end

thank you Marc for figuring out this bug - helpful code

~Adam

http://ville-web.ch