Received: by 10.66.72.165 with SMTP id e5mr1796636pav.4.1343679321081; Mon, 30 Jul 2012 13:15:21 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!usenet.stanford.edu!q4no1278888pbi.1!news-out.google.com!p10ni99964539pbh.1!nntp.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 30 Jul 2012 15:15:20 -0500 Date: Mon, 30 Jul 2012 13:15:16 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: What is the command to do a power of a value References: <5016CF47.55648.calajapr@time.synchro.net> <5016CF47.55649.calajapr@time.synchro.net> In-Reply-To: <5016CF47.55649.calajapr@time.synchro.net> Message-ID: Lines: 43 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.192.108 X-Trace: sv3-lReHuZezgp8FTU68hbBx35W44xD/XPc9TrDDcnNzVXRjw+uzvIxvhnZzhFhWUp3DojFn//Pq1wQdDTr!dFXfVhQ7ZiIGdjyKZw8LsDZtZXTOnEWJsfzZT7bDj3AIMZNieUgzLg0phkGYOHBvEMVtZRP7Z2O7!0Y44LErUFB9ZQcXRiZ58D8G0areVComdOiMn3rfaWPBSThY= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2963 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Xref: csiph.com comp.lang.java.programmer:16681 On 7/30/2012 12:00 PM, Patricia Shanahan wrote: > To: Andreas Leitgeb > From: Patricia Shanahan > > On 7/30/2012 6:22 AM, Andreas Leitgeb wrote: >> On Sun, 29 Jul 2012 21:28:11 -0700 (PDT), xvictoryeohx@gmail.com wrote: >>> C=L(1+i/100)power of n >> >> x ^ n = exp ( log(x) * n ) | x = (1 + i/100) >> = exp ( log( 1 + i/100 ) * n) >> = exp ( log1p ( i/100 ) * n) >> >> If you're doing more calculations with same interest-rate but >> different periods, then you may want to calculate >> double logBase = Math.log1p( i / 100 ); >> once, and use that for the individual calculations: >> C = L * Math.exp( logBase * n ) >> >> The gist of this response is, that for the kind of base (1+i/100), >> you better separate the pow operation out into log and exp, and >> actually use log1p on (i/100) instead of log on (1+i/100) for >> efficiency's and precision's sake. >> >> For "Math.log1p" see: >> http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#log1p%28double%2 > 9 >> > > I am curious about why you expect this to be more precise than: "The computed > result must be within 1 ulp of the exact result. Results must be > semi-monotonic." (From the pow description). Or do you know of cases where > Math.pow gets an over-large rounding error? > > Note that I am not disagreeing with your method for calculating a power of a > number slightly greater than 1, just questioning whether doing it explicitly > gets more precise answers than using Math.pow. I've thought about this some more, and I think I get it now. If i is reasonably small compared to 100, the 1 + i/100 addition loses several bits of precision. Patricia