# Infinity and -Infinity

Discussion in 'Ruby' started by Jon A. Lambert, Nov 5, 2005.

1. ### Jon A. LambertGuest

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

Jon A. Lambert, Nov 5, 2005

2. ### dazGuest

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:

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

Without all that unnecessary fuss:

Infinity = (+1.0/0.0) # set constant

x = -Infinity-3
p x # -Infinity

daz

daz, Nov 6, 2005

3. ### Jon A. LambertGuest

daz wrote:
>
> Without all that unnecessary fuss:
>
> Infinity = (+1.0/0.0) # set constant
>
> x = -Infinity-3
> 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

Jon A. Lambert, Nov 6, 2005
4. ### TransGuest

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.

4.cap(5) #=> 4
4.cap(3) #=> 3

4.clip(5) #=> 5
4.clip(3) #=> 4

4.clip(5,7) #=> 5
4.clip(3,5) #=> 4
4.clip(1,3) #=> 3

T.

Trans, Nov 6, 2005