Here's the code:

  def IsApproximatelyEqual(x, y, epsilon):
    # Check absolute precision.
    if -epsilon <= x - y <= epsilon:
      return True

    # Are x or y too close to zero?
    if -epsilon <= x <= epsilon or -epsilon <= y <= epsilon:
      return False

    # Check relative precision.
    return (-epsilon <= (x - y) / x <= epsilon
         or -epsilon <= (x - y) / y <= epsilon)

As you can see, the "relative" part is basically abs(x-y)/max(x,y) <=
epsilon.  The previous two clauses are for absolute differences and to make
sure we don't get huge numbers when we divide by x and y.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to