Infinity and -Infinity

J

Jon A. Lambert

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
 
D

daz

Jon said:
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
 
J

Jon A. Lambert

daz said:
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.
 
T

Trans

Jon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top