From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-4.1 required=3.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 6C5FC1F453 for ; Tue, 30 Apr 2019 13:31:01 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 93074120A16; Tue, 30 Apr 2019 22:30:56 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 9A75C120A0A for ; Tue, 30 Apr 2019 22:30:54 +0900 (JST) Received: by filter0064p3iad2.sendgrid.net with SMTP id filter0064p3iad2-14308-5CC84DCC-30 2019-04-30 13:29:48.957681794 +0000 UTC m=+406287.752023561 Received: from herokuapp.com (unknown [3.86.82.84]) by ismtpd0033p1mdw1.sendgrid.net (SG) with ESMTP id EIx8QGDxTAqbqEiSM_3k7g for ; Tue, 30 Apr 2019 13:29:48.926 +0000 (UTC) Date: Tue, 30 Apr 2019 13:29:48 +0000 (UTC) From: nobu@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67977 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15811 X-Redmine-Issue-Author: yennguyenh X-Redmine-Sender: nobu X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?q8Dly+pU2+3ektTtZVXgZtbJPXwqo7p86jCsvYTW4BzywxEKH2L2sImKsX9ZgF?= =?us-ascii?Q?zRdHq8CpDIFHkg58bmLZ+JDdUWs+TVA5r+Ve+Jt?= =?us-ascii?Q?gWP+Vw2P5Ab31lx5waD8QiOZpudF8KEygDdJt=2FN?= =?us-ascii?Q?fZXJWNrk0lMfoBA8igJCQVeKPM=2FWefxk9znzO=2FS?= =?us-ascii?Q?lUdGFZ3oeP9MWqjUT0wOVjnHCL8cCYXit=2FA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92494 Subject: [ruby-core:92494] [Ruby trunk Feature#15811] Propsing new method for comparing equality of 2 (float) numbers relatively X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #15811 has been updated by nobu (Nobuyoshi Nakada). duerst (Martin D=FCrst) wrote: > Ruby is an object-oriented language. So I think this should be something = like: > ``` > a.close_to?(b, abs_tolerance: t) > ``` > or so, not a function with two main numbers. If it is an instance method, the relative tolerance feels relative to the a= bsolute value of the receiver, not the larger one. ---------------------------------------- Feature #15811: Propsing new method for comparing equality of 2 (float) num= bers relatively https://bugs.ruby-lang.org/issues/15811#change-77855 * 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 proje= ct! This method gives the approximation for the equal comparison based on t= wo 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 tole= rance of 5%, pass tol=3D0.05. The default tolerance is 1E-9, which assures = that the two values are the same within about 9 decimal digits. rel_tol mus= t 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 t= o 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 t= o be set in case of near-zero numbers. # Discussion There are some test cases available for the method which has approved the a= ccuracy of the method. BigNumbers and integers are also tested. However, mo= re test cases are still needed to assure even better the accuracy of the me= thod. # Gist = Relative equal comparison https://gist.github.com/yennguyenh/63d5e7a11f354f796b43ada037c4b2c5 Test cases https://gist.github.com/yennguyenh/2e81dc72b310cb9d886a82faf3d536ef -- = https://bugs.ruby-lang.org/ Unsubscribe: