Hi there,
is there another way of accessing Infinity or -Infinity other than doing
1.0/0 or -1.0/0? I could not find any… TIA!
Regards
robert
Hi there,
is there another way of accessing Infinity or -Infinity other than doing
1.0/0 or -1.0/0? I could not find any… TIA!
Regards
robert
Robert Klemme wrote:
Hi there,
is there another way of accessing Infinity or -Infinity other than doing
1.0/0 or -1.0/0? I could not find any… TIA!
I couldn’t find anything either, so I do this sometimes:
class Float
Infinity = 1.0/0
end
if -3.0/0 == Float::Infinity
puts “infinite”
end
Is it reasonable to compare infinities? I would tend to use
Float#finite? (there’s also Float#nan? for not-a-numbers) so that I
could say
if not (-3.0/0).finite?
puts ‘infinite’
end
Mike
In article 3E286515.4000301@path.berkeley.edu, Joel VanderWerf wrote:
Robert Klemme wrote:
Hi there,
is there another way of accessing Infinity or -Infinity other than doing
1.0/0 or -1.0/0? I could not find any… TIA!I couldn’t find anything either, so I do this sometimes:
class Float
Infinity = 1.0/0
endif -3.0/0 == Float::Infinity
puts “infinite”
end
–
mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
Mike Stok wrote:
In article 3E286515.4000301@path.berkeley.edu, Joel VanderWerf wrote:
Robert Klemme wrote:
Hi there,
is there another way of accessing Infinity or -Infinity other than doing
1.0/0 or -1.0/0? I could not find any… TIA!I couldn’t find anything either, so I do this sometimes:
class Float
Infinity = 1.0/0
endif -3.0/0 == Float::Infinity
puts “infinite”
endIs it reasonable to compare infinities? I would tend to use
Float#finite? (there’s also Float#nan? for not-a-numbers) so that I
could sayif not (-3.0/0).finite?
puts ‘infinite’
endMike
Well, Ruby’s floating point number system does include two infinities,
so I guess it’s reasonable to compare with them…
I think I defined the constant so I could use it in case statements, but
you’re right, #finite? is cleaner.
Joel VanderWerf wrote:
Well, Ruby’s floating point number system does include two infinities,
so I guess it’s reasonable to compare with them…
This might sound a bit pedantic but:
Ruby doesn’t provide the two infinitive numbers. The underlying floating
point routines do. So you don’t have two infinities, since any Float
object can be infinite. You can have an infinite number of infinities.
$ irb
irb(main):001:0> a = 1.0/0
Infinity
irb(main):002:0> b = 1.0/0
Infinity
irb(main):003:0> a.id
537929428
irb(main):004:0> b.id
537915848
irb(main):005:0> a.equal? b
false
So the only use for the Infinity constants I can see, is for clarity
(and space conserving) when you explicitly assign Infinity to more than
one variable. Creating them for comparison is just plain wasteful, as it
is one more object in ObjectSpace and a more complicated comparison,
having to look into two objects instead of one.
IMHO
–
([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
Kent Dahl wrote:
Joel VanderWerf wrote:
Well, Ruby’s floating point number system does include two infinities,
so I guess it’s reasonable to compare with them…This might sound a bit pedantic but:
Ruby doesn’t provide the two infinitive numbers. The underlying floating
point routines do. So you don’t have two infinities, since any Float
object can be infinite. You can have an infinite number of infinities.$ irb
irb(main):001:0> a = 1.0/0
Infinity
irb(main):002:0> b = 1.0/0
Infinity
irb(main):003:0> a.id
537929428
irb(main):004:0> b.id
537915848
irb(main):005:0> a.equal? b
false
But…
(1.2).equal?(1.2) # ==> false
(10100).equal?(10100) # ==> false
Does that mean there are an infinity of googols?
I don’t think #equal? is the mathematically correct comparison for
numbers that may be implemented as heap allocated entities. That’s the
job of #==.
So the only use for the Infinity constants I can see, is for clarity
(and space conserving) when you explicitly assign Infinity to more than
one variable. Creating them for comparison is just plain wasteful, as it
is one more object in ObjectSpace and a more complicated comparison,
having to look into two objects instead of one.
That’s why I define a constant… (and also so I can use it in case
statements, though I’m not sure I ever have, so I will shut up now ![]()
Hi all,
thank you all for your remarks! However, comparison was not my point but
initialization. Sorry, that I did not mention this.
“Kent Dahl” kentda@stud.ntnu.no schrieb im Newsbeitrag
news:3E2874A8.86141CA4@stud.ntnu.no…
Joel VanderWerf wrote:
Well, Ruby’s floating point number system does include two infinities,
so I guess it’s reasonable to compare with them…This might sound a bit pedantic but:
Ruby doesn’t provide the two infinitive numbers. The underlying floating
point routines do. So you don’t have two infinities, since any Float
object can be infinite. You can have an infinite number of infinities.
“This might sound a bit pedantic but:” No, you can’t! I bet, there is only
finite memory in your system. :-))))
So the only use for the Infinity constants I can see, is for clarity
(and space conserving) when you explicitly assign Infinity to more than
one variable.
Or if you want to assign them at all. “x = Float::Infinity” is much cleaner
and better to read than “x = 1.0/0.0”.
Creating them for comparison is just plain wasteful, as it
is one more object in ObjectSpace and a more complicated comparison,
having to look into two objects instead of one.
I totally agree with that one.
Regards
robert
Joel VanderWerf wrote:
But…
(1.2).equal?(1.2) # ==> false
(10100).equal?(10100) # ==> falseDoes that mean there are an infinity of googols?
Maybe, I’m no math buff, but I was thinking more about the fact that
they are heap allocated entities. When I read “two infinities” before
“reasonable to compare with them”, I thought you might be under the
illusion that the infinity objects are shared single instances or
immediate values. My bad, and good to see you aren’t.
I don’t think #equal? is the mathematically correct comparison for
numbers that may be implemented as heap allocated entities. That’s the
job of #==.
And checking for infinity isn’t the job of neither of those, but
Float#infinite? since it is such a special value. Using #== is a bad
idea for Floats in general due to imprecision, and chucking them up for
infinity goes against code clarity, IMHO.
–
([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)
Hi,
From: “Robert Klemme” bob.news@gmx.net
Sent: Monday, January 20, 2003 6:13 PM
Or if you want to assign them at all. “x = Float::Infinity” is much cleaner
and better to read than “x = 1.0/0.0”.
I used kind of “1.0/0.0” workarounds in soap4r. It’s for mapping
XML Schema instance to Ruby’s object, not for comparing.
I’m happy with Float::Infinity but there must be few person
who use it…
And, other than Float::Infinity, what are good names for
-Infinity, +0, -0, and NaN?
NegativeInfinity, Zero, NegativeZero, NaN?
Regards,
// NaHi