* Sean Russell (ser / germane-software.com) wrote:
> Martin DeMello wrote:
> 
> > Sean Russell <ser / germane-software.com> wrote:
> > Well, simplifying a little you could say that integer::0 == 0, but
> > float::0 is a number in the general region of 0 (no strict equality test
> 
> In computer science, this is the most /intuitive/ thing I have ever
> seen.  0 != 0.0.
> 
> In the recent discussion about whether computer science is more
> science or art, I should have used this as an example of why it is
> art.
> 
> When I say 0.0, I /mean/ 0.0, not some number in the general vacinity
> of 0.0.  If I want "almost" 0.0, I should be able to say ~0.0.
> 
>         do(something) if x ~= 0.0

This should do what you're looking for:

class Numeric
  def like?(value, threshold = 0.0001)
      self > value - threshold && self < value + threshold
  end
end

irb(main):001:0> require './like.rb'
true
irb(main):002:0> 5.0.like? 5
true
irb(main):003:0> 5.like? 5.00
true
irb(main):004:0> 5.0.like? 5.0000000001
true

> I understand that 0.0 != 0, and that 0/0 != 0.0/0.0 because of how
> floating point numbers are implemented in the underlying logic.
> However, I /still/ think it is funny that, after all this time, we are
> redefining math because of limitations in processors.  Don't you?
> Isn't it hilarious?  The idea that computers can't do basic math

Neither 0/0 nor 0.0/0.0 are defined.  You can't test the equality of
infinite values, and you can't test the equality of NaNs either.  That
doesn't have anything to do with the underlying logic; that's basic
mathematics.

As for 0/0 != 0.0/0.0, see below.

> /properly/?  Try explaining that to a mathematician who's never dealt
> with a computer at that level, if you can find one.

The mathematician would probably slap you around a bit for trying to do
something silly (see below).

> BTW, what I'm peeved about isn't that -1.0/0.0 = -Infinity, but that
> -1/0 isn't /also/ -Infinity.  If 0 == 0.0, then it should follow that
> 0/0 == 0.0/0.0.

In physics, chemistry, business, or any other profession where precision
matters, 0 may or may not be equal to 0.0.  Personally, I prefer the
current system; equality comparisons between integer and floating point
values are a minor inconvenience (see the Numeric#like? method above).

The implementation makes sense if you look at it from a differential
calculus perspective.  The {+,-}Infinity definitions come from the limit
of equations as they approach 1.0/0.0 and -1.0/0.0 over a continuous
domain.  Limits only make sense over a continuous range (eg a continuous
set of real numbers).  Calculating the limit of a series of natural
numbers or integers doesn't make a whole lot of sense.

-- 
Paul Duncan <pabs / pablotron.org>        pabs in #gah (OPN IRC)
http://www.pablotron.org/               OpenPGP Key ID: 0x82C29562