<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Calculating Logarithms</title>
	<atom:link href="http://programmingpraxis.com/2009/12/18/calculating-logarithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/12/18/calculating-logarithms/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Pat</title>
		<link>http://programmingpraxis.com/2009/12/18/calculating-logarithms/#comment-3008</link>
		<dc:creator><![CDATA[Pat]]></dc:creator>
		<pubDate>Mon, 09 May 2011 23:51:02 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1779#comment-3008</guid>
		<description><![CDATA[I note that the approved solution calculates (- (* x x) x2)) twice. Is there some reason you don&#039;t use a temporary?

(define (newton x2)
    (let loop ((x 1.0))
      (let ((t (- (* x x) x2)))
        (if (&lt; (abs t) epsilon) x
            (loop (- x (/ t (+ x x))))))))]]></description>
		<content:encoded><![CDATA[<p>I note that the approved solution calculates (- (* x x) x2)) twice. Is there some reason you don&#8217;t use a temporary?</p>
<p>(define (newton x2)<br />
    (let loop ((x 1.0))<br />
      (let ((t (- (* x x) x2)))<br />
        (if (&lt; (abs t) epsilon) x<br />
            (loop (- x (/ t (+ x x))))))))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Calculating Sines &#171; Programming Praxis</title>
		<link>http://programmingpraxis.com/2009/12/18/calculating-logarithms/#comment-895</link>
		<dc:creator><![CDATA[Calculating Sines &#171; Programming Praxis]]></dc:creator>
		<pubDate>Tue, 12 Jan 2010 09:01:58 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1779#comment-895</guid>
		<description><![CDATA[[...] calculated the value of pi, and logarithms to seven digits, in two previous exercises. We continue that thread in the current exercise with a function that calculates [...]]]></description>
		<content:encoded><![CDATA[<p>[...] calculated the value of pi, and logarithms to seven digits, in two previous exercises. We continue that thread in the current exercise with a function that calculates [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jaime</title>
		<link>http://programmingpraxis.com/2009/12/18/calculating-logarithms/#comment-851</link>
		<dc:creator><![CDATA[Jaime]]></dc:creator>
		<pubDate>Fri, 18 Dec 2009 12:31:34 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1779#comment-851</guid>
		<description><![CDATA[Mine, in Python

[sourcecode lan=&quot;python&quot;]
import math

#Maximum error
DELTA = 0.0001

def cmp_estimation(estimation, true):
    if (true - DELTA) &lt;= estimation &lt;= (true + DELTA):
        return True
    else:
        return False


def newton(number):
    &#039;&#039;&#039;
    Returns the square root using Newton formula
    &#039;&#039;&#039;

    old_estimation = 0
    estimation = number / 2
            
    #check until next iteration gives us less precission than DELTA
    while not cmp_estimation(old_estimation, estimation):
        old_estimation = estimation
        estimation = estimation \
                    - (float(estimation ** 2 - number) / (2 * estimation))
    
    return estimation

def euler(number, base):
    &#039;&#039;&#039;
    Calculate logarithms using Euler formula
    &#039;&#039;&#039;
    
    #initialize the numbers getting the closest powers
    upper = 1
    while base ** upper &lt;= number:
        lower = upper
        upper += 1
                
    prev_arith_mean = -1
    arith_mean = 0
    geo_mean = upper
    
    while not cmp_estimation(arith_mean, prev_arith_mean):
        prev_arith_mean = arith_mean
                
        #Calculate geometric and arithmetic mean
        arith_mean = float(lower + upper) / 2
        geo_mean = newton((base ** lower) * (base ** upper))
        
        #Narrow the interval
        if geo_mean &lt; number:
            lower = arith_mean
        else:
            upper = arith_mean
            
    return arith_mean

#Some test to ensure everything is fine
if __name__ == &#039;__main__&#039;:
    for i in range(2, 100):
        assert cmp_estimation(newton(i), math.sqrt(i))

    for base in range(2, 11):
        for i in range(base, 100):
            assert cmp_estimation(euler(i, base), math.log(i, base))
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Mine, in Python</p>
<pre class="brush: plain;">
import math

#Maximum error
DELTA = 0.0001

def cmp_estimation(estimation, true):
    if (true - DELTA) &lt;= estimation &lt;= (true + DELTA):
        return True
    else:
        return False


def newton(number):
    '''
    Returns the square root using Newton formula
    '''

    old_estimation = 0
    estimation = number / 2
            
    #check until next iteration gives us less precission than DELTA
    while not cmp_estimation(old_estimation, estimation):
        old_estimation = estimation
        estimation = estimation \
                    - (float(estimation ** 2 - number) / (2 * estimation))
    
    return estimation

def euler(number, base):
    '''
    Calculate logarithms using Euler formula
    '''
    
    #initialize the numbers getting the closest powers
    upper = 1
    while base ** upper &lt;= number:
        lower = upper
        upper += 1
                
    prev_arith_mean = -1
    arith_mean = 0
    geo_mean = upper
    
    while not cmp_estimation(arith_mean, prev_arith_mean):
        prev_arith_mean = arith_mean
                
        #Calculate geometric and arithmetic mean
        arith_mean = float(lower + upper) / 2
        geo_mean = newton((base ** lower) * (base ** upper))
        
        #Narrow the interval
        if geo_mean &lt; number:
            lower = arith_mean
        else:
            upper = arith_mean
            
    return arith_mean

#Some test to ensure everything is fine
if __name__ == '__main__':
    for i in range(2, 100):
        assert cmp_estimation(newton(i), math.sqrt(i))

    for base in range(2, 11):
        for i in range(base, 100):
            assert cmp_estimation(euler(i, base), math.log(i, base))
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2009/12/18/calculating-logarithms/#comment-850</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Fri, 18 Dec 2009 12:16:17 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1779#comment-850</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2009/12/18/programming-praxis-calculating-logarithms/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
eps :: Double
eps = 1e-7

sqrt&#039; :: Double -&gt; Double
sqrt&#039; n = head . filter (\x -&gt; abs (x^2 - n) &lt; eps) $
    iterate (\x -&gt; x - (x^2 - n) / (2*x)) 1

log&#039; :: Double -&gt; Double -&gt; Double
log&#039; b n = f 0 0 where
    f lo hi &#124; b ** hi &lt; n             = f lo (hi + 1)
            &#124; abs (lo / hi - 1) &lt; eps = arit
            &#124; geom &lt; n                = f arit hi
            &#124; otherwise               = f lo arit
            where geom = sqrt&#039; (b ** lo * b ** hi)
                  arit = (lo + hi) / 2
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2009/12/18/programming-praxis-calculating-logarithms/" rel="nofollow">http://bonsaicode.wordpress.com/2009/12/18/programming-praxis-calculating-logarithms/</a> for a version with comments):</p>
<pre class="brush: css;">
eps :: Double
eps = 1e-7

sqrt' :: Double -&gt; Double
sqrt' n = head . filter (\x -&gt; abs (x^2 - n) &lt; eps) $
    iterate (\x -&gt; x - (x^2 - n) / (2*x)) 1

log' :: Double -&gt; Double -&gt; Double
log' b n = f 0 0 where
    f lo hi | b ** hi &lt; n             = f lo (hi + 1)
            | abs (lo / hi - 1) &lt; eps = arit
            | geom &lt; n                = f arit hi
            | otherwise               = f lo arit
            where geom = sqrt' (b ** lo * b ** hi)
                  arit = (lo + hi) / 2
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Calculating Logarithms &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2009/12/18/calculating-logarithms/#comment-849</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Calculating Logarithms &#171; Bonsai Code]]></dc:creator>
		<pubDate>Fri, 18 Dec 2009 12:15:59 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1779#comment-849</guid>
		<description><![CDATA[[...] Praxis &#8211; Calculating&#160;Logarithms By Remco Niemeijer  In today&#8217;s Programming Praxis problem we have to implement Euler&#8217;s method for calculating logarithms [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Calculating&nbsp;Logarithms By Remco Niemeijer  In today&#8217;s Programming Praxis problem we have to implement Euler&#8217;s method for calculating logarithms [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

