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-Status: No, score=-2.6 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no 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 6EC281F4B4 for ; Mon, 5 Oct 2020 11:16:11 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id AA71612090C; Mon, 5 Oct 2020 20:15:31 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id E74841208EF for ; Mon, 5 Oct 2020 20:15:29 +0900 (JST) Received: by filterdrecv-p3mdw1-5dd6bc5999-fr7kl with SMTP id filterdrecv-p3mdw1-5dd6bc5999-fr7kl-18-5F7B0071-1B 2020-10-05 11:16:01.145021912 +0000 UTC m=+1171045.264243564 Received: from herokuapp.com (unknown) by geopod-ismtpd-5-0 (SG) with ESMTP id 2C6dAQPtSwOWIRc2QhEFXg for ; Mon, 05 Oct 2020 11:16:01.068 +0000 (UTC) Date: Mon, 05 Oct 2020 11:16:01 +0000 (UTC) From: foldes.laszlo2@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 76141 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Bug X-Redmine-Issue-Id: 17214 X-Redmine-Issue-Author: karatedog X-Redmine-Sender: karatedog 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?t66dDZPAGiguEggrw=2Fv18A8dtNA=2FBMqMQU0gzuOTOuN7JD5yDJbBO18wVsi+4U?= =?us-ascii?Q?+xxjIIhIaKQ4Aps3+jPLTG2cZw+biGjdvjeS5xy?= =?us-ascii?Q?LFK6rbii+WeYPktbRZ8AD7REhRFt1xnbet5A2LC?= =?us-ascii?Q?z9StCWIfVVf0OWkku04xFOJ=2FsTKQjyNfyrbpqFk?= =?us-ascii?Q?ka83VKNpJ4unr1oyA8+UnrGKd09veg6buAoAz+q?= =?us-ascii?Q?2XiDk=2Fh=2FRURlFNq9A=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 100299 Subject: [ruby-core:100299] [Ruby master Bug#17214] BigDecimal exponentiation gives incorrect results 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 #17214 has been updated by karatedog (F=F6ldes L=E1szl=F3). nobu (Nobuyoshi Nakada) wrote in #note-2: > `BigDecimal` instances hold the precision, and it is not enough in that c= ase. > You need to override the default precision in some operations. > ```ruby > BigDecimal(2222).power(3.5, Float::DIG).to_i #=3D> 517135308457 > ``` If I increase the precision of the two arguments ("2222", "3.5"), but leave= the operation's precision on default, the result still will be wrong. So the good result relies only on the precision of the operation which is n= ot straightforward immediately for the developer (because it will return a = value). To sum up: 1. Only #power can be used for proper exponentiation, as #** cannot be give= n any argument and thus will give almost always wrong results if used with = large numbers. Existing code might need to be rewritten, changing class fro= m Float to BigDecimal will not be idempotent. 1. BigDecimal is advertised as an "arbitrary precision library" yet using t= he defaults it is less precise than Float. This is misleading, it will bite= newcomers in the back. I think this should be documented and/or fine-tuned, the above two violates= the Principle of Least Surprise. ---------------------------------------- Bug #17214: BigDecimal exponentiation gives incorrect results https://bugs.ruby-lang.org/issues/17214#change-87884 * Author: karatedog (F=F6ldes L=E1szl=F3) * Status: Rejected * Priority: Normal * ruby -v: ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- This is an incorrect value: ```ruby (BigDecimal("2222") ** BigDecimal("3.5")).to_i # =3D> 517135311000 ``` This is the correct value (within Float precision): ```ruby 2222 ** 3.5 # =3D> 517135308457.25256 ``` As the Base gets larger, the problem is more visible. Wrong value, number o= f trailing zeroes increase: ```ruby (BigDecimal("22222") ** BigDecimal("3.5")).to_i # =3D> 1635840670000000 ``` Nearing maximum Float precision: ```ruby 22222 ** 3.5 # =3D> 1635840670214066.5 ``` -- = https://bugs.ruby-lang.org/ Unsubscribe: