Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Infinity and -Infinity

Reply
Thread Tools

Infinity and -Infinity

 
 
Jon A. Lambert
Guest
Posts: n/a
 
      11-05-2005
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




 
Reply With Quote
 
 
 
 
daz
Guest
Posts: n/a
 
      11-06-2005

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



 
Reply With Quote
 
 
 
 
Jon A. Lambert
Guest
Posts: n/a
 
      11-06-2005
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




 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      11-06-2005

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Infinity + Infinity (or NegInfinity - NegInfinity) Skybuck Flying C Programming 40 01-25-2012 12:25 AM
atoi and INFINITY migurus C Programming 1 04-15-2008 09:09 PM
Reading and Writing float value of infinity to file. Jim Langston C++ 15 10-23-2007 08:43 AM
Portably generating infinity and NaN Michael Hoffman Python 18 04-16-2007 09:59 AM
pickle and infinity Bart Ogryczak Python 6 11-30-2006 02:31 PM



Advertisments