<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Parolski.com &#187; BigDecimal</title>
	<atom:link href="http://www.parolski.com/tag/bigdecimal/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parolski.com</link>
	<description>Faith, Solaris, and Chicken Korma, by Anton Parol</description>
	<lastBuildDate>Mon, 26 Jul 2010 20:34:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Half a half: Part 2</title>
		<link>http://www.parolski.com/2008/12/13/half-a-half-part-2/</link>
		<comments>http://www.parolski.com/2008/12/13/half-a-half-part-2/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 21:12:26 +0000</pubDate>
		<dc:creator>Anton Parol</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[BigDecimal]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[problem]]></category>

		<guid isPermaLink="false">http://www.parolski.com/?p=158</guid>
		<description><![CDATA[Ten days ago we had a look at Java&#8217;s ability to deal with numbers which had alot of decimal places. Liam pointed out that having to loop through a variety of operations is longer than performing a more efficient operation once. With Liams magical formula, we get the following Java, with no loop: 1 2 [...]]]></description>
			<content:encoded><![CDATA[<p>Ten days ago we had a look at Java&#8217;s ability to deal with numbers which had alot of decimal places. Liam pointed out that having to loop through a variety of operations is longer than performing a more efficient operation once.</p>
<p>With Liams magical formula, we get the following Java, with no loop:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigDecimal</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> The_proper_half <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Integer</span> times_to_loop<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param args the number of times you want to run the loop
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        calculate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> times_to_loop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//The Number we want to half</span>
        <span style="color: #003399;">BigDecimal</span> number_to_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The result of doing the half</span>
        <span style="color: #003399;">BigDecimal</span> the_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The number we want to divide by</span>
        <span style="color: #003399;">BigDecimal</span> divider <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        the_half <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>number_to_half.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span>divider.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>times_to_loop<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The last number has &quot;</span> <span style="color: #339933;">+</span>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> number_to_half.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span>the_half<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; decimal places: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>number_to_half.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span>the_half<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, we only perform the opertation once (line 23), but admitadly, to get the correct number at the end, we still do the subtraction at the end!</p>
<p>Regarding optimisation, there are huge benefits. It turns out that the old way if hugely costly for thousands of iterations. These are the results for 10000 iterations, the old way:</p>
<pre>anton@anton-laptop classes $ time java Half_of_a_half 10000 &gt; /dev/null

real    5m22.714s
user    5m21.320s
sys    0m1.052s</pre>
<p>The results for the new way are much better:</p>
<pre>anton@anton-laptop classes $ time java The_proper_half 10000 &gt; /dev/null

real    0m1.019s
user    0m1.248s
sys    0m0.052s</pre>
<p>So it went from over five minutes, down to just a second. That was just by changing from a loop a single statement. Thats pretty cool stuff!</p>
<p>I have to admit here, the difficult bit is probably not the code. Its the abillity to see how a problem may be translated from one way of expressing it to another. Its that bit of genius thats really done the work here. Credit to Liam for the genius!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parolski.com/2008/12/13/half-a-half-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Half a number, half it again and add it to the first half.</title>
		<link>http://www.parolski.com/2008/12/03/half-a-number-half-it-again-and-add-it-to-the-first-half/</link>
		<comments>http://www.parolski.com/2008/12/03/half-a-number-half-it-again-and-add-it-to-the-first-half/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 20:37:17 +0000</pubDate>
		<dc:creator>Anton Parol</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[BigDecimal]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.parolski.com/?p=136</guid>
		<description><![CDATA[This is a common riddle, so if you&#8217;ve heard it, don&#8217;t shout out the answer (people might think your strange for shouting at the internet&#8230;.) Take a number. Then, half that number. Then half that half and add it to the first half. Before that gives people a headache, lets do a little example: Lets [...]]]></description>
			<content:encoded><![CDATA[<p>This is a common riddle, so if you&#8217;ve heard it, don&#8217;t shout out the answer (people might think your strange for shouting at the internet&#8230;.)</p>
<p>Take a number. Then, half that number. Then half that half and add it to the first half. Before that gives people a headache, lets do a little example:</p>
<p>Lets use 10 . Then half of ten gives us 5. Then, half five (2.5) and add it to the first half, 5. This gives us 7.5</p>
<p>Continue to half the last half, adding it to the sum of the previous halves. Phew, I hope thats explained properly.i.e. half two point five, and add it the the 7.5 we already have, which give us 8.25</p>
<p>Okay, so the question is this. If we keeping adding on the halves, will we ever reach 10 again? Well, I like Java, so lets get all geeky and model the problem. For this, the double and float data types won&#8217;t give us enough decimal places. For this, we&#8217;ll need: math.BigDecimal . The Big Decimal. Sounds ominous!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigDecimal</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Half_of_a_half <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Integer</span> times_to_loop<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param args the number of times you want to run the loop
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        calculate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> times_to_loop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//The Number we want to half</span>
        <span style="color: #003399;">BigDecimal</span> number_to_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The result of doing the half</span>
        <span style="color: #003399;">BigDecimal</span> the_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The number we want to divide by</span>
        <span style="color: #003399;">BigDecimal</span> divider <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> times_to_loop<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            number_to_half <span style="color: #339933;">=</span> number_to_half.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span>divider<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            the_half <span style="color: #339933;">=</span> the_half.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>number_to_half<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>the_half<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The last number has &quot;</span> <span style="color: #339933;">+</span>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> the_half.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; decimal places&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This code takes a single parameter on the command line: the number of times to half by</p>
<p>e.g.<br />
<code><br />
anton@anton-laptop classes $ java Half_of_a_half 10<br />
5<br />
7.5<br />
8.75<br />
9.375<br />
9.6875<br />
9.84375<br />
9.921875<br />
9.9609375<br />
9.98046875<br />
9.990234375<br />
The last number has 9 decimal places<br />
</code><br />
So as we can see, the decimal places just get long and longer. The additions are never big enough to get us close enough to the original number. Problem solved!</p>
<p>The fun bit is realising the significance of the calculation, or rather, the speed with which it was done. Performing 1000 divisions and additions takes less that 0.2 of a second! Maybe thats not so fast by todays computing ability, but by human standards thats pretty extreme.<br />
<code><br />
anton@anton-laptop classes $ time java Half_of_a_half 1000 &gt; /dev/null<br />
</code><br />
<code><br />
real    0m1.387s<br />
user    0m1.916s<br />
sys    0m0.036s<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.parolski.com/2008/12/03/half-a-number-half-it-again-and-add-it-to-the-first-half/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
