# Infinity and -Infinity

**URL:** <https://rubytalk.org/t/infinity-and-infinity/22193>\
**Category:** ruby-talk\
**Created:** [5 November 2005 18:12 UTC](https://rubytalk.org/t/infinity-and-infinity/22193 "2005-11-05T18:12:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_A\_Lambert2](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@Jon\_A\_Lambert2](https://rubytalk.org/u/Jon_A_Lambert2)\
**Post date:** [5 November 2005 18:12 UTC](https://rubytalk.org/t/infinity-and-infinity/22193/1 "2005-11-05T18:12:42Z")

</div>

Are there predefined constants somewhere for these floating point values?

=\> ["EPSILON", "MIN\_10\_EXP", "MANT\_DIG", "MAX", "MAX\_EXP", "RADIX", "MIN", "MIN\_EXP", "ROUNDS", "MAX\_10\_EXP", "DIG"]  
irb(main):011:0\> Math.constants  
=\> ["E", "PI"]

irb(main):016:0\> Float::MAX  
=\> 1.79769313486232e+308  
irb(main):017:0\> Float::MIN  
=\> 2.2250738585072e-308  
irb(main):009:0\> (+1.0/0.0)  
=\> Infinity  
irb(main):012:0\> (-1.0/0.0)  
=\> -Infinity

irb(main):021:0\> x = Inifinity  
NameError: uninitialized constant Inifinity

---

<div class="post-metadata">

**Author:** ![daz](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daz](https://rubytalk.org/u/daz)\
**Post date:** [6 November 2005 00:02 UTC](https://rubytalk.org/t/infinity-and-infinity/22193/2 "2005-11-06T00:02:23Z")

</div>

Jon A. Lambert wrote:

> Are there predefined constants somewhere for these floating point values?
> 
> =\> ["EPSILON", "MIN\_10\_EXP", "MANT\_DIG", "MAX", "MAX\_EXP", "RADIX", "MIN",  
> "MIN\_EXP", "ROUNDS", "MAX\_10\_EXP", "DIG"]  
> irb(main):011:0\> Math.constants  
> =\> ["E", "PI"]
> 
> irb(main):016:0\> Float::MAX  
> =\> 1.79769313486232e+308  
> irb(main):017:0\> Float::MIN  
> =\> 2.2250738585072e-308

That's a vexing question 'cos you just gave a comprehensive list of them.

Perhaps numeric.c is the answer you wanted ?

> irb(main):009:0\> (+1.0/0.0)  
> =\> Infinity  
> irb(main):012:0\> (-1.0/0.0)  
> =\> -Infinity
> 
> irb(main):021:0\> x = Inifinity  
> NameError: uninitialized constant Inifinity

( In\<i\>finity spelling irrelevant )

Infinity is simulated all the way AFAICT - so, with your help,  
I think this shows that your definition usably sticks:

&nbsp;&nbsp;&nbsp;negi = (-1.0/0.0)  
&nbsp;&nbsp;&nbsp;p negi # -Infinity  
&nbsp;&nbsp;&nbsp;Infinity = -negi  
&nbsp;&nbsp;&nbsp;p Infinity # Infinity  
&nbsp;&nbsp;&nbsp;p Infinity.class # Float  
&nbsp;&nbsp;&nbsp;p Infinity == (+1.0/0.0)-1 # true

Without all that unnecessary fuss:

&nbsp;&nbsp;&nbsp;Infinity = (+1.0/0.0) # set constant

&nbsp;&nbsp;&nbsp;x = -Infinity-3  
&nbsp;&nbsp;&nbsp;p x # -Infinity

daz

---

<div class="post-metadata">

**Author:** ![Jon\_A\_Lambert2](https://avatars.discourse-cdn.com/v4/letter/j/43a26b/32.png) [@Jon\_A\_Lambert2](https://rubytalk.org/u/Jon_A_Lambert2)\
**Post date:** [6 November 2005 04:05 UTC](https://rubytalk.org/t/infinity-and-infinity/22193/3 "2005-11-06T04:05:46Z")

</div>

daz wrote:

> Without all that unnecessary fuss:
> 
> &nbsp;&nbsp;Infinity = (+1.0/0.0) # set constant
> 
> &nbsp;&nbsp;x = -Infinity-3  
> &nbsp;&nbsp;p x # -Infinity

Okay thanks. I'm going to set my own constant.  
The math looks good too, although I'm just using for boundary conditions.

I was suprised not to find min or max functions either

looking for ...

someclass#min(x,y)  
or  
x.min(y)

I ended up using  
x \< y ? x : y

Then I stumbled upon...Enumerable's  
[x, y].min

So I ended up using a mix of both as I was looping through arrays in some cases to get the min/max.  
I'm presuming the latter is slightly slower for just a check of two numbers.

> **···**
>
> --  
> J. Lambert

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [6 November 2005 04:47 UTC](https://rubytalk.org/t/infinity-and-infinity/22193/4 "2005-11-06T04:47:10Z")

</div>

Jon A. Lambert wrote:

> I was suprised not to find min or max functions either
> 
> looking for ...
> 
> someclass#min(x,y)  
> or  
> x.min(y)
> 
> I ended up using  
> x \< y ? x : y

It would be nice if there were, but there's a problem. Enumerables can  
be comparable too. So #min and #max would name clash with Enumerable's  
methods, and also with Date#min (for munutes). It still might be  
possible to do if one took arity into account though.

As It is Florian Gross provided Facets with #at\_least and #at\_most,  
though to me those seem long winded for such a function. So I also  
offer #clip and #cap.

&nbsp;&nbsp;4.cap(5) #=\> 4  
&nbsp;&nbsp;4.cap(3) #=\> 3

&nbsp;&nbsp;4.clip(5) #=\> 5  
&nbsp;&nbsp;4.clip(3) #=\> 4

&nbsp;&nbsp;4.clip(5,7) #=\> 5  
&nbsp;&nbsp;4.clip(3,5) #=\> 4  
&nbsp;&nbsp;4.clip(1,3) #=\> 3

T.
