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 7C15F17CFF3F for ; Wed, 19 Jun 2013 08:53:50 +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 4A915EA6A6B for ; Wed, 19 Jun 2013 08:27:36 +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 52DD397A839 for ; Wed, 19 Jun 2013 08:27:36 +0900 (JST) X-Virus-Scanned: amavisd-new at nagaokaut.ac.jp 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 HKTK7tW6pIYl for ; Wed, 19 Jun 2013 08:27:36 +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 17BA597A82B for ; Wed, 19 Jun 2013 08:27:36 +0900 (JST) Received: from carbon.ruby-lang.org (carbon.ruby-lang.org [221.186.184.68]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id BD112952408 for ; Wed, 19 Jun 2013 08:27:34 +0900 (JST) Received: from beryllium.ruby-lang.org (beryllium.ruby-lang.org [127.0.0.1]) by carbon.ruby-lang.org (Postfix) with ESMTP id DEA703C21EBBF; Wed, 19 Jun 2013 08:27:31 +0900 (JST) Received: from fluorine.ruby-lang.org (www.rubyist.net [210.251.121.216]) by carbon.ruby-lang.org (Postfix) with ESMTP id 047653C21ED7E for ; Wed, 19 Jun 2013 08:27:31 +0900 (JST) Received: from ruby-lang.org (localhost [127.0.0.1]) by fluorine.ruby-lang.org (Postfix) with ESMTP id E081B3F810 for ; Wed, 19 Jun 2013 08:27:30 +0900 (JST) Delivered-To: ruby-core@ruby-lang.org Date: Wed, 19 Jun 2013 08:27:31 +0900 Posted: Wed, 19 Jun 2013 08:27:30 +0900 From: "GSnyder (Garth Snyder)" Reply-To: ruby-core@ruby-lang.org Subject: [ruby-core:55550] [ruby-trunk - Bug #8542][Open] BigMath::exp modifies its first argument To: ruby-core@ruby-lang.org Message-Id: X-ML-Name: ruby-core X-Mail-Count: 55550 X-MLServer: fml [fml 4.0.3 release (20011202/4.0.3)]; post only (only members can post) X-ML-Info: If you have a question, send e-mail with the body "help" (without quotes) to the address ruby-core-ctl@ruby-lang.org; help= X-Mailer: Redmine X-Redmine-Issue-Author: GSnyder X-Auto-Response-Suppress: OOF X-Redmine-Issue-Id: 8542 X-Redmine-Mailinglistintegration-Message-Ids: 28953 X-Redmine-Project: ruby-trunk Auto-Submitted: auto-generated X-Redmine-Site: Ruby Issue Tracking System X-Redmine-Host: bugs.ruby-lang.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Precedence: bulk List-Id: ruby-core.ruby-lang.org List-Software: fml [fml 4.0.3 release (20011202/4.0.3)] List-Post: List-Owner: List-Help: List-Unsubscribe: Issue #8542 has been reported by GSnyder (Garth Snyder). ---------------------------------------- Bug #8542: BigMath::exp modifies its first argument https://bugs.ruby-lang.org/issues/8542 Author: GSnyder (Garth Snyder) Status: Open Priority: Normal Assignee: Category: Target version: 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/