Issue #15811 has been updated by shevegen (Robert A. Heiler).


Not sure if the name .close? is a good name to indicate a relative comparison. Note that
I have no real pro or con opinion, just pointing out that the name may not be ideal. I
don't have a good alternative proposal either; perhaps something a bit longer, two words?

Perhaps you could add a more specific example might be useful. Will this method reside
on Math, for example? E. g.:

    Math.close?(a, b, rel_tol, abs_tol)

Again, not having any preference myself, just asking to see more details added to the
suggestion. The ruby team is open to adding/discussing changes/modifications based on
use cases; in my opinion, the more details can be added (when they are important), the
better.

To make it easier for others, I copy/pasted a part of your gist sample (excluding the
documents), just to make it simpler for people to read the code here; you can indent
via 4 ' ' and then the bugtracker here will correctly highlight the ruby code:

    def self.close?(a, b, rel_tol: RELATIVE_TOLERANCE, abs_tol: ABSOLUTE_TOLERANCE)
      raise ArgumentError.new('Arguments must be numeric') unless (a.is_a?(Numeric) && b.is_a?(Numeric))
      raise ArgumentError.new('Error tolerance must positive') if (rel_tol < 0.0 || abs_tol < 0.0)

      # short-cut exact equality
      return true if a == b

      # check if any attribute is Infinite
      return false if a.infinite? || b.infinite?

      # weak comparition - the tolerance is scaled by the larger of 2 values
      abs_diff = (a - b).abs
      ((abs_diff <= (rel_tol * b).abs) ||
        (abs_diff <= (rel_tol * a).abs) ||
        (abs_diff <= abs_tol))
    end


----------------------------------------
Feature #15811: Propsing new method for comparing equality of 2 (float) numbers relatively
https://bugs.ruby-lang.org/issues/15811#change-77824

* Author: yennguyenh (yen nguyen)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
#Background

Equal comparison method between 2 float numbers returns unexpected results sometimes. Therefore, a relative comparison method is needed!

#Proposal

A relative equal comparison method has been written based on a Python project! This method gives the approximation for the equal comparison based on two values: realative tolerance and absolute tolerance. Near zero value will also be considered carefully!

#Implementation

The function for that would be called close?
close?(a, b, rel_tol, abs_tol)

a and b: are the two values to be tested to relative closeness

rel_tol: is the relative tolerance -- it is the amount of error allowed, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass tol=0.05. The default tolerance is 1E-9, which assures that the two values are the same within about 9 decimal digits. rel_tol must be greater than 0.0

abs_tol: is a minimum absolute tolerance level -- useful for comparisons near zero.

#Evaluation of your implementation

By default, relative tolerance is 1E-9 which is relatively precise enough to compare two float numbers. However it can also be adjusted in case higher accuracy is requested. The absolute tolerance is by default 0.0 and need to be set in case of near-zero numbers.

#Discussion

There are some test cases available for the method which has approved the accuracy of the method. BigNumbers and integers are also tested. However, more test cases are still needed to assure even better the accuracy of the method.

#Gist 

Relative equal comparison

https://gist.github.com/yennguyenh/63d5e7a11f354f796b43ada037c4b2c5

Test cases

https://gist.github.com/yennguyenh/2e81dc72b310cb9d886a82faf3d536ef



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>