X-Recipient: archive-cygwin AT delorie DOT com DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:subject :content-type:content-transfer-encoding; q=dns; s=default; b=HyC GjrPK26J6OT66SapxGNRObGt7h1jy6cMqmhFdML1PrF4SoQ7u39QuG5NKCfChw46 p5/nbQY2BpiCn9rydF0qfLv9qP29eelKukYHf5S4xqrjKv/g2quMZ+zD/7cnbYCW MFrUrLMnQoYY9ruLWyXiwtgXxSzz8ImIr3Pi7ccY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:subject :content-type:content-transfer-encoding; s=default; bh=+VmPFGYth 3m3/TjnIchxDfBLyvA=; b=KKWuMxB8PVqfoEVloYJAe6rFjqCTJ0KRn/+r5Md1q H6Vi0aphS1UxsCOtLNKc4uXGS6I3Sd7KlQRzi4I5FuZGRsPKeNKlow2b18WInieZ w2xeUUfjGaWDzzldWjR58YTZszsR9UxTgfVuodIi8n0Sst4BWzzDUOYDoI54VXZE d8= Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-HELO: smtp4-g21.free.fr Message-ID: <53A3715B.8000904@free.fr> Date: Fri, 20 Jun 2014 01:25:15 +0200 From: =?ISO-8859-15?Q?Falk_Tannh=E4user?= User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: cygwin AT cygwin DOT com Subject: log2() function from C standard library is inaccurate Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes I noticed that the log2() function in GNU Octave returns inaccurate results for many arguments that are exactly representable integer powers of 2. After discussion on the Octave mailing list https://savannah.gnu.org/bugs/?42583 it occurs that Octave doesn't have this problem on most other platforms (Linux, OS X) and that it is due to the fact that Octave usually uses log2() from the local C library. The following C++ program exposes the error: _________________________________________________________________ // Compilation: g++ -Wall -Wextra -O3 -s testlog2.cxx -o testlog2 #include #include #if 0 ///////////////////////////////////////////////////////// inline double log2(double x) { double result; asm ("fyl2x" : "=t" (result) : "0" (x), "u" (1.0) : "st(1)"); return result; } #else ///////////////////////////////////////////////////////// #include #endif //////////////////////////////////////////////////////// int main() { int n = std::numeric_limits::min_exponent - std::numeric_limits::digits; std::cout << "n = " << n << " ... " << std::numeric_limits::max_exponent - 1 << '\n'; int errcnt = 0; for(double x = std::numeric_limits::denorm_min(); x < std::numeric_limits::infinity(); x *= 2, ++n) { if(n != log2(x)) { std::cout << n << '\n'; ++errcnt; } } std::cout << "Found " << errcnt << " errors.\n"; return 0; } _________________________________________________________________ With GCC 4.9.0 or 4.8.3 for x86_64-pc-cygwin, erroneous results are found for 441 values. This seems to be due to the fact that log2 is implemented in terms of log (as something like log(x)/log(2)). OTOH, when using the assembler version of log2 (just changing '#if 0' to '#if 1' in above code); accurate results are always returned. Falk -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple