ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: hai-yen.nguyen@creditshelf.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92612] [Ruby trunk Feature#15811] Propsing new method for comparing equality of 2 (float) numbers relatively
Date: Thu, 09 May 2019 08:09:16 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77972.20190509080915.babf3ee00f43387b@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15811.20190429105809@ruby-lang.org

Issue #15811 has been updated by yennguyenh (yen nguyen).


nobu (Nobuyoshi Nakada) wrote:
> I think it should be under `Math` or `Float`, and a independent gem could be a good first step.

I have updated the first gist. It is under Math! I just forget to put it in gist . Thank you for reminding anyway :D 

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

* 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/

  parent reply	other threads:[~2019-05-09  8:09 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15811.20190429105809@ruby-lang.org>
2019-04-29 10:58 ` [ruby-core:92475] [Ruby trunk Feature#15811] Propsing new method for comparing equality of 2 (float) numbers relatively hai-yen.nguyen
2019-04-29 11:03 ` [ruby-core:92476] " hai-yen.nguyen
2019-04-29 11:35 ` [ruby-core:92477] " manga.osyo
2019-04-29 11:55 ` [ruby-core:92478] " shevegen
2019-04-29 14:55 ` [ruby-core:92479] " wishdev
2019-04-30  7:23 ` [ruby-core:92488] " nobu
2019-04-30  8:16 ` [ruby-core:92489] " duerst
2019-04-30 10:50 ` [ruby-core:92491] " sawadatsuyoshi
2019-04-30 13:29 ` [ruby-core:92494] " nobu
2019-05-09  8:09 ` hai-yen.nguyen [this message]
2019-05-09 10:22 ` [ruby-core:92613] " hai-yen.nguyen
2019-07-29  6:54 ` [ruby-core:93966] [Ruby master " ko1
2019-07-29  7:34 ` [ruby-core:93975] " wishdev
2019-07-29  7:35 ` [ruby-core:93976] " muraken
2019-07-29  7:49 ` [ruby-core:93981] " wishdev
2019-07-29 16:40 ` [ruby-core:93999] " wishdev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-77972.20190509080915.babf3ee00f43387b@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).