[ 
https://issues.apache.org/jira/browse/SPARK-2599?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14068113#comment-14068113
 ] 

Doris Xin edited comment on SPARK-2599 at 7/21/14 2:06 AM:
-----------------------------------------------------------

Found this in-depth article discussing the different considerations for 
comparing floating point numbers: 
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

My suggestion is the following (a blend of absolute and relative epsilon):

{code}
def almostEquals(y: Double, epsilon: Double = 1E-10): Boolean = {
      if(x == y || math.abs(x - y) < epsilon) {
        true
      } else if(math.abs(x) > math.abs(y)) {
        math.abs(x - y) / math.abs(x) < epsilon
      } else {
        math.abs(x - y) / math.abs(y) < epsilon
      }
  }
{code}

Not the most rigorous but covers most use cases I'd imagine (small numbers get 
caught by the first condition while large numbers with large absolute 
difference but small relative difference would still be considered equal by the 
subsequent conditions).


was (Author: dorx):
Found this in-depth article discussing the different considerations for 
comparing floating point numbers: 
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

My suggestion is the following (a blend of absolute and relative epsilon):

def almostEquals(y: Double, epsilon: Double = 1E-10): Boolean = {
      if(x == y || math.abs(x - y) < epsilon) {
        true
      } else if(math.abs(x) > math.abs(y)) {
        math.abs(x - y) / math.abs(x) < epsilon
      } else {
        math.abs(x - y) / math.abs(y) < epsilon
      }
  }

Not the most rigorous but covers most use cases I'd imagine (small numbers get 
caught by the first condition while large numbers with large absolute 
difference but small relative difference would still be considered equal by the 
subsequent conditions).

> almostEquals mllib.util.TestingUtils does not behave as expected when 
> comparing against 0.0
> -------------------------------------------------------------------------------------------
>
>                 Key: SPARK-2599
>                 URL: https://issues.apache.org/jira/browse/SPARK-2599
>             Project: Spark
>          Issue Type: Bug
>          Components: MLlib
>            Reporter: Doris Xin
>            Priority: Minor
>
> DoubleWithAlmostEquals.almostEquals, when used to compare a number with 0.0, 
> would always produce an epsilon of 1 >> 1e-10, causing false failure when 
> comparing very small numbers with 0.0.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to