From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id E50DF17D56FD for ; Thu, 30 Jan 2014 15:37:01 +0900 (JST) Received: from funfun.nagaokaut.ac.jp (smtp.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 78C48B5D884 for ; Thu, 30 Jan 2014 15:24:05 +0900 (JST) Received: from funfun.nagaokaut.ac.jp (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 2A88B97A82B for ; Thu, 30 Jan 2014 15:24:07 +0900 (JST) X-Virus-Scanned: amavisd-new at nagaokaut.ac.jp Authentication-Results: funfun.nagaokaut.ac.jp (amavisd-new); dkim=fail (1024-bit key) reason="fail (message has been altered)" header.d=sendgrid.me Received: from funfun.nagaokaut.ac.jp ([127.0.0.1]) by funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vo0eXrqZPyQT for ; Thu, 30 Jan 2014 15:24:07 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id E286797A820 for ; Thu, 30 Jan 2014 15:24:06 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id F2A1C95243E for ; Thu, 30 Jan 2014 15:24:04 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 455B812073C; Thu, 30 Jan 2014 15:17:31 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o2.heroku.sendgrid.net (o2.heroku.sendgrid.net [67.228.50.55]) by neon.ruby-lang.org (Postfix) with SMTP id 946361206BC for ; Thu, 30 Jan 2014 15:17:06 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=rWsEB8TApG7vfPTJUZ4V1NKMc48=; b=t7XL+qW/YuT+UjRxN4 +SWef3ub43bz/71XDo4ba79XRwL/DNWEXeTAEB7/T8tk6forj975tdMSKI1/UkR8 Kan2KuOA55Cl/AAQtDJUfNWtNVmIVhgqOeKaUWgxdKMG8XL3ufV0BmplgM5/x/hZ HeL5aV6LxNQloQfHS8gcA8mG0= Received: by mf260.sendgrid.net with SMTP id mf260.24802.52E9EE616 Thu, 30 Jan 2014 06:17:05 +0000 (UTC) Received: from herokuapp.com (ec2-54-196-154-30.compute-1.amazonaws.com [54.196.154.30]) by ismtpd-023 (SG) with ESMTP id 143e1cb2c99.3534.2b1320 Thu, 30 Jan 2014 06:17:05 +0000 (GMT) Date: Thu, 30 Jan 2014 06:17:05 +0000 From: shibata.hiroshi@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 34065 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 8542 X-Redmine-Issue-Author: GSnyder X-Redmine-Issue-Assignee: mrkn X-Redmine-Sender: hsbt X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: OOF Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS5HKf8tIXKlIFHK7RIhXdDMXuVBPFLFn/+CQsAcTP8pe9M+IJPRb/5f4ccHFt9S7Gv/5kyW4NgvO63xnLu5D5SN8rsNpTjHtNx9/5O1zKCAj/gthQScPiAqzd4FJ1wjGt0= X-ML-Name: ruby-core X-Mail-Count: 60298 Subject: [ruby-core:60298] [ruby-trunk - Bug #8542] BigMath::exp modifies its first argument 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: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #8542 has been updated by Hiroshi SHIBATA. Target version changed from 2.1.0 to current: 2.2.0 ---------------------------------------- Bug #8542: BigMath::exp modifies its first argument https://bugs.ruby-lang.org/issues/8542#change-44783 * Author: Garth Snyder * Status: Assigned * Priority: Normal * Assignee: Kenta Murata * Category: ext * Target version: current: 2.2.0 * ruby -v: ruby 2.0.0p195 (2013-05-14 revision 40734) [i386-solaris2.11] * Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN ---------------------------------------- =begin I noticed this when creating the patch at https://github.com/ruby/ruby/pull/332. I believe it affects everything from ruby 1.8 up. BigMath::exp is implemented for negative numbers according to the identity E ** -x == 1 / (E ** x). At bigdecimal.c:2742 (in GitHub commit 258acf3, in the implementation of BigMath_s_exp), the code flips the sign on the input argument for negative numbers. Later, it returns the reciprocal of the result. Problem: When the first argument is a BigDecimal (or, presumably, Bignum), the original argument is modified, so its sign bit remains flipped. Hence: $ irb 2.0.0-p195 :001 > require 'bigdecimal/math' => true 2.0.0-p195 :002 > x = BigDecimal(-1) => # 2.0.0-p195 :003 > BigMath.exp(x, 20) => # 2.0.0-p195 :004 > x => # Here, x is modified when used as an argument to BigMath.exp AND the answer is wrong. I have already submitted the patch mentioned above for the latter problem, but I'm not sure what the appropriate fix would be for the sign modification. Just resetting the sign bit on exit would be easy but not thread safe; BigMath.exp really shouldn't be modifying the argument at all. But copying the whole argument is potentially wasteful if the precision is high. I suspect that the special calculation track for negative values is actually not needed at all. Without the patch I just submitted, BigMath.exp is reliably returning the reciprocal of the correct answer, which means that it's properly calculating the correct answer by using the (negative) VALUE x passed in as the original argument -- at least for immediate values. So perhaps the basic iteration loop is just as valid for negative exponents as it is already implemented. =end -- http://bugs.ruby-lang.org/