<?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>Numbergrinder</title>
	<atom:link href="http://www.numbergrinder.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.numbergrinder.com</link>
	<description>Take your data and Grind it down...</description>
	<lastBuildDate>Mon, 21 Mar 2011 00:04:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Testing post</title>
		<link>http://www.numbergrinder.com/2011/02/testing-post/</link>
		<comments>http://www.numbergrinder.com/2011/02/testing-post/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 05:58:17 +0000</pubDate>
		<dc:creator>agilis</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=6</guid>
		<description><![CDATA[Gonna test a ton of posts! GWAR!]]></description>
			<content:encoded><![CDATA[<p>Gonna test a ton of posts! GWAR!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2011/02/testing-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;m an Engineer, Not a Compiler</title>
		<link>http://www.numbergrinder.com/2009/02/im-an-engineer-not-a-compiler/</link>
		<comments>http://www.numbergrinder.com/2009/02/im-an-engineer-not-a-compiler/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 06:12:27 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=121</guid>
		<description><![CDATA[Recently I had a phone interview where I got asked a variety of Java questions. This kind of thing is standard, and most of the questions were somewhat standard: What is polymorphism? What&#8217;s the difference between a List and a &#8230; <a class="more-link" href="http://www.numbergrinder.com/2009/02/im-an-engineer-not-a-compiler/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently I had a phone interview where I got asked a variety of Java questions.  This kind of thing is standard, and most of the questions were somewhat standard:<br />
<span id="more-121"></span></p>
<ul>
<li /> What is polymorphism?
<li /> What&#8217;s the difference between a List and a Set?  When would you use one over the other?
<li /> When might you see deadlock?
<li /> What is the difference between strong typing and weak typing?
</ul>
<p>These are mostly fair game.  I dislike the polymorphism one, simply because it is so closely tied to most OO languages and inheritance that most people&#8211;when overriding or overloading a method, for instance&#8211;use it without ever thinking &#8220;oh! this is polymorphism in action!&#8221;  Instead I might ask &#8220;what is inheritance and where do you use it,&#8221; which generally has a keyword or pattern in OO languages.  But this is a personal preference and I can see the argument.</p>
<p>The strong and weak typing one was a bit unusual, because he was actually referring to <a href="http://en.wikipedia.org/wiki/Type_system#Type_checking">type checking</a> rather than <a href="http://en.wikipedia.org/wiki/Strongly-typed_programming_language">type strength</a> and got a little confused when I described C as weak-static, Java as strong-static, and Python as strong-dynamic (I think JavaScript is weak-dynamic, but I didn&#8217;t mention it).</p>
<p>What came next, however, were nano-questions:</p>
<ul>
<li /> What package is List in?
<li /> What package is File in?
<li /> What keyword do you use to inherit with?
</ul>
<p>(We also got the &#8220;standard interview questions&#8221; such as &#8220;where do you see yourself in 5 years,&#8221; etc.)</p>
<p>Russ Olsen <a href="http://www.jroller.com/rolsen/entry/building_a_software_team_five">mentions two consequences</a> of asking nano-questions:</p>
<blockquote><p>
Aside from telling you not much at all, the tiny question has two real costs: first it takes up the time that you could be spending getting to know this person, finding out if they are smart enough, if they have the right background, if they will fit into your group. The second cost of this kind of question is that it tends to tick off those smart, well rounded people that you really do want to hire.
</p></blockquote>
<p>I got the nano questions listed here right, but let me throw on a third consequence:  Asking nano-questions can lead to false negatives by weeding out otherwise great fits.</p>
<p>A good engineer thinks abstractly in terms of designing and building <em>systems</em>, they think in terms of algorithms, components, and engineering design.  They do not necessarily know all of the details of syntax of a given language, especially if they are used to a good IDE which does it for them (I use Eclipse: I type <em>List</em> and then control-space to load <em>java.util.List</em>).  It is more important that I recognize which package I want than that I be able to name it off the top of my head.</p>
<p>Similarly, it is more important that I be able to tell you when and where I should use inheritance, and when and where I should use polymorphism, than to be able to spit off the definition.</p>
<p>Basically: Any question that takes 5 seconds to answer with Google is not a good question.   My favorite phone interview question? &#8220;What&#8217;s your favorite language?&#8221; followed by &#8220;What are it&#8217;s weaknesses?&#8221;</p>
<p>Yet a lot of interviews and a lot of exams basically test you to see how well you would substitute for a compiler.  Even the Java Certification Exams tend to focus on questions of syntax and compilation as opposed to either how well you can actually program or how well you can design a system. </p>
<p>I am a good engineer, I am not a good compiler.  I cannot look at a block of code and necessarily tell you that the problem with it is that it won&#8217;t catch the <em> ClassNotFoundException</em>, and a modern compiler will tell me that this is a problem.  If not immediately, than at least when I attempt to compile.  IDE dependency? Perhaps, but that isn&#8217;t necessarily a bad thing since that is representative of the tools they will be using in the office.</p>
<p>In Short: Finding a good fit for your team is far too important to risk it with a game of Trivial Pursuit: Geek Edition.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/02/im-an-engineer-not-a-compiler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Time + Computation + Thinking = Constant</title>
		<link>http://www.numbergrinder.com/2009/01/time-computation-thinking-constant/</link>
		<comments>http://www.numbergrinder.com/2009/01/time-computation-thinking-constant/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 16:51:18 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=119</guid>
		<description><![CDATA[Working on Project Euler, there is a problem involving dice probabilities. The forum shows a variety of different solutions, some of them involving entirely too much typing (several people literally iterated through every possible combination of dice faces), and some &#8230; <a class="more-link" href="http://www.numbergrinder.com/2009/01/time-computation-thinking-constant/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Working on <a href="http://projecteuler.net/">Project Euler</a>,  there is a <a href="http://projecteuler.net/index.php?section=problems&#038;id=205">problem involving dice probabilities</a>.  The forum shows a variety of different solutions, some of them involving entirely too much typing (several people literally iterated through every possible combination of dice faces), and some of them were somewhat more considered.</p>
<p>Reading the comments, it reminded me of something my Russian mathematical modeling professor used to say back in school:</p>
<blockquote><p>
time + computation + thinking = constant
</p></blockquote>
<p><span id="more-119"></span></p>
<p>One solution, in C#, took 56 seconds.  After some review and evaluation, he got it down to 126 ms. Another one in Java took &#8220;about a minute.&#8221;  Several of the solutions, however, even written in Python without use of <a href="http://psyco.sourceforge.net/">Psyco</a> took the barest fraction of that time.  My solution took 64 ms in Python, and another person&#8217;s took less than a 6th the time that mine did on the same hardware. </p>
<p>What made such a radical difference? Thinking about the problem a little differently leading to a different algorithm, <a href="http://en.wikipedia.org/wiki/Memoization">memoization</a>, precomputing , and things along those lines.  There was no magic involved: some people spent more time considering the algorithm and implementation, other people just wrote the easiest code possible and let the computer do the work.</p>
<p>In a Numerical Methods class we did projects on numeric solutions to PDEs.  My group did an elliptic PDE and we approached it using three different methods: two numerical approaches to the problem, one analytic for comparison.  I did the analytic solution, and the final solution took around 10 pages of typeset equations to illustrate every step.   The solution, however, was very robust, accurate, and extremely fast once implemented in software.    It was easy to test, vary the edge conditions or the problem domain, and took only a few seconds to execute even for relatively complex problems.  There were a few limitations, but most of  those were not crippling and only effected accuracy in edge cases.</p>
<p>The purely numeric solutions, on the other hand, were extremely difficult to work with.  The simplest solution had a variety of restrictions on the structure of the problem and took over two orders of magnitude of time longer.  On the other hand, the numeric solutions were (for the most part) easier to describe and to implement.  It seemed that the more complex the numeric solution and the more thinking and work it took before implementing, the faster it ran once finally implemented.</p>
<p>At a previous job a coworker implemented a solution where algorithm he came up with ran in $\Theta(n^3)$ time. Given the nature of the application, this was entirely too slow and so we put it up on the whiteboard to examine it.  With some thinking, we got it down to $\Theta(n)$, which for a couple of thousand items is hugely significant. </p>
<p>This is a fundamental concept in Software Engineering, because frequently in optimization the first thing to check are the algorithms.  Small efficiencies&#8211;which are generally relatively easy&#8211;rarely help significantly and can hurt long-run maintenance.  A change to the algorithm&#8211;or apply techniques such as caching&#8211;can improve the execution time by several orders of magnitude <em>and</em> help later code maintenance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/01/time-computation-thinking-constant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding an Application Icon</title>
		<link>http://www.numbergrinder.com/2009/01/adding-an-application-icon/</link>
		<comments>http://www.numbergrinder.com/2009/01/adding-an-application-icon/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 22:28:26 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=117</guid>
		<description><![CDATA[One of the final steps in developing your app before submission is adding an icon. This is a straightforward process: Create a 57&#215;57 pixel PNG, name it &#8220;Icon.png&#8221;. In must be exactly 57&#215;57 pixels and it must also be a &#8230; <a class="more-link" href="http://www.numbergrinder.com/2009/01/adding-an-application-icon/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the final steps in developing your app before submission is adding an icon. This is a straightforward process:</p>
<ul>
<li /> Create a 57&#215;57 pixel PNG, name it &#8220;Icon.png&#8221;.  In must be <em>exactly</em> 57&#215;57 pixels and it must also be a PNG.
<li /> Add the file to your project&#8217;s resources.
<li /> Go into the Info.plist file and fill in the Value for &#8220;Icon file&#8221; to Icon.png, making sure that it matches the spelling and capitalization exactly.
</ul>
<p> Build and launch your app normally, the icon should be added and visible on your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/01/adding-an-application-icon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>KGS Rating Graph</title>
		<link>http://www.numbergrinder.com/2009/01/kgs-rating-graph/</link>
		<comments>http://www.numbergrinder.com/2009/01/kgs-rating-graph/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 16:21:01 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=136</guid>
		<description><![CDATA[The game of Go involves a ranking system to allow people at different levels of strength to play fair games. The system is widely employed in both in-person and online games, and some variation of it is used by every &#8230; <a class="more-link" href="http://www.numbergrinder.com/2009/01/kgs-rating-graph/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://witticism.org/numbergrinder2/wp-content/uploads/2011/03/RScreenSnapz002.png"><img class="alignnone size-full wp-image-133" title="RScreenSnapz002" src="http://witticism.org/numbergrinder2/wp-content/uploads/2011/03/RScreenSnapz002.png" alt="" width="549" height="390" /></a></p>
<p>The game of <a href="http://en.wikipedia.org/wiki/Go_(game)">Go</a> involves a ranking system to allow people at different levels of strength to play fair games.     The system is widely employed in both in-person and online games, and some variation of it is used by every major Go server and organization.</p>
<p>The ranks go from 30k to 1k, and then 1d to 9d.  1d is 1 rank (or &#8220;stone&#8221;) from 1k, and 5k is 4 stones from 9k.  There are some problems with this system (e.g., in reality the ratings are neither linear nor transitive), but they provide a good way of estimating someone&#8217;s strength before you play them for the first time.</p>
<p>In this essay we are going to examine the KGS <a href="http://senseis.xmp.net/?KGSRankHistogram%2FData">rating data</a> from 16 March 2008 for confident ranks.   One of the key skills in data analysis is an understanding that <a href="http://numbergrinder.com/node/18">data is data</a>, and so it is a useful exercise to examine arbitrary data sets to see what you can discover.  With this in mind, let&#8217;s examine the data and see what falls out.<br />
<span id="more-136"></span><br />
The values here are rounded <a href="http://numbergrinder.com/node/27">interval data</a>: The zero point is arbitrary and while differences are useful (a 5k is 3 ranks away from a 2k), summation isn&#8217;t (a 3k + a 2k has no meaning).  4 stones is a bit of an interesting number, because the majority of games take place within a 4 stone handicap.</p>
<p>Before we can leap in and perform our analysis, however, it pays to look at the histogram to see the distribution of the data.  We should see a few different things:</p>
<ul>
<li> A negative <a href="http://en.wikipedia.org/wiki/Skewness">skew</a> (-0.69, for the curious).</li>
<li> A positive <a href="http://en.wikipedia.org/wiki/Kurtosis">kurtosis</a> (0.53).</li>
</ul>
<p>What this means is that our distribution isn&#8217;t really normal, so we should be very careful about using the arithmetic mean, since the skew will bias our result.   We <em>could legitimately calculate it</em> with this data, but it would be tempting to make errant conclusions, so we will stick to using quantiles.</p>
<p>I&#8217;ve marked on here the 10%, 25%, 50%, 75%, and 90% quantiles representing how many people fall below each value.  The values for these are:</p>
<ul>
<li> 10%: 12k</li>
<li> 25%: 8k</li>
<li> 50%: 4k (Median)</li>
<li> 75%: 1k</li>
<li> 90%: 2d</li>
</ul>
<p>This feels very compressed: over 25% of the players are between 4k and 1k.  It also means that only 10% of KGS is below 12k in strength, and over 50% of the players are between 8k and 1k.  Someone who is around 4k is within 4 stones of the majority of the server.</p>
<p>There are a couple of different reasons that this could be, and we&#8217;ll explore the reasons more when we look at the data for the American Go Association.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/01/kgs-rating-graph/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bush Approval Rating: 8 Years in Review</title>
		<link>http://www.numbergrinder.com/2009/01/bush-approval-rating-8-years-in-review/</link>
		<comments>http://www.numbergrinder.com/2009/01/bush-approval-rating-8-years-in-review/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 10:53:24 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=138</guid>
		<description><![CDATA[A graph of Bush&#8217;s approval rating over the last 8 years. The line represents a 5-day rolling median for his rating, which ranged between 90 and 23.]]></description>
			<content:encoded><![CDATA[<p><a href="http://witticism.org/numbergrinder2/wp-content/uploads/2011/03/RScreenSnapz001.png"><img src="http://witticism.org/numbergrinder2/wp-content/uploads/2011/03/RScreenSnapz001.png" alt="" title="RScreenSnapz001" width="598" height="533" class="alignnone size-full wp-image-135" /></a></p>
<p>A graph of Bush&#8217;s approval rating over the last 8 years.  The line represents a 5-day rolling median for his rating, which ranged between 90 and 23. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/01/bush-approval-rating-8-years-in-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking Malloc History</title>
		<link>http://www.numbergrinder.com/2009/01/checking-malloc-history/</link>
		<comments>http://www.numbergrinder.com/2009/01/checking-malloc-history/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 19:28:57 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=115</guid>
		<description><![CDATA[Sometimes it is useful to see what malloc operations have been performed by a given application. Doing this is a fairly straightforward process that can be facilitated with the command malloc_history, but requires a little bit of setup for iPhone-specific &#8230; <a class="more-link" href="http://www.numbergrinder.com/2009/01/checking-malloc-history/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is useful to see what malloc operations have been performed by a given application.   Doing this is a fairly straightforward process that can be facilitated with the command malloc_history, but requires a little bit of setup for iPhone-specific apps since the console cannot be used directly.<br />
<span id="more-115"></span></p>
<ol>
<li /> Go under the Executables directory of your project in XCode.
<li /> &#8220;Get Info&#8221; on your application.
<li /> Click on the &#8220;Arguments&#8221; tab
<li /> Under Environment variables set <em>MallocStackLogging and </em><em>NSZombieEnabled</em> to <em>YES</em>.  The former allows for malloc_history to be used, the latter logs deallocated memory, which can help find the memory address.
<li /> Run the app using the debugger (Command-Option-Y)
<li /> Check the console or probe around with the debugger to find the memory address you want to check.
<li /> Go to top of the Debugger Console and look for a line that looks like this:
</ol>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">launchd(28861) malloc: stack logs being written into /tmp/stack-logs.28861.launchd.7lhXSV</pre></div></div>

<ol>
<li /> Taking the pid from that statement (28861) in this case and the memory address you picked up earlier (from, for example, a debug output line that looks something like this: <em>-[NGTableViewDelegate tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x104f650</em> or from otherwise looking around with the debugger).
<li /> Type <em>malloc_history
<pid> <memory address></memory></pid></em> the command line or <text>shell malloc_history
<pid> <memory address></memory></pid></text> from inside of GDB.
</ol>
<p>This can help track down tricky problems with delegate objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/01/checking-malloc-history/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Where do I&#8230;</title>
		<link>http://www.numbergrinder.com/2009/01/where-do-i/</link>
		<comments>http://www.numbergrinder.com/2009/01/where-do-i/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 18:16:20 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=113</guid>
		<description><![CDATA[&#8230;sign up to get paid tons of money to make statistical fallacies? There is a fallacy I like to refer to as the &#8220;clifford fallacy&#8221; that the news media seems particularly prone to making: &#8220;If this dog keeps growing at &#8230; <a class="more-link" href="http://www.numbergrinder.com/2009/01/where-do-i/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://junkfoodscience.blogspot.com/2008/08/jfs-special-report-obesity.html">&#8230;sign up to get paid tons of money to make statistical fallacies?</a></p>
<p>There is a fallacy I like to refer to as the &#8220;clifford fallacy&#8221; that the news media seems particularly prone to making: &#8220;If this dog keeps growing at its present rate, in five years it will be the size of <a href="http://en.wikipedia.org/wiki/Clifford_the_Big_Red_Dog">Clifford</a>!&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/01/where-do-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wrappers for Numbers: NSNumber and Collections of Integers</title>
		<link>http://www.numbergrinder.com/2009/01/wrappers-for-numbers-nsnumber-and-collections-of-integers-2/</link>
		<comments>http://www.numbergrinder.com/2009/01/wrappers-for-numbers-nsnumber-and-collections-of-integers-2/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 05:24:35 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=125</guid>
		<description><![CDATA[In the Java programming language there are both raw and wrapper objects to represent numbers. For example, int and Integer, double and Double, etc. If one wanted to add a numeric object to a Collection object, it required using the &#8230; <a class="more-link" href="http://www.numbergrinder.com/2009/01/wrappers-for-numbers-nsnumber-and-collections-of-integers-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the Java programming language there are both raw and wrapper objects to represent numbers.  For example, <em>int</em> and <em>Integer</em>, <em>double</em> and <em>Double</em>, etc.  If one wanted to add a numeric object to a <em>Collection</em> object, it required using the wrapper object rather than the raw type.</p>
<p>With the introduction of <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html">autoboxing in Java 1.5</a>, moving between these types would largely be handled by the compiler, but there is still a distinction. </p>
<p>Just as with <a href="http://numbergrinder.com/node/49">null types</a>, Objective-C also requires that you wrap raw types before inserting them into objects or <a href="http://numbergrinder.com/node/35">using them in variable argument lists</a> where the type <em>id</em> is expected.  To help us with this, cocoa provides <em>NSNumber</em>.</p>
<p><span id="more-125"></span></p>
<p>There is a great deal of confusion among newcomers on the <a href="https://devforums.apple.com/">Apple developer forums</a> and <a href="http://www.iphonedevsdk.com/">other developer-driven communities</a>  because of typedefs such as <em>NSInteger</em> and <em>NSUInteger</em>.  They expect that these would work like <java>Integer</java> or <java>Long</java> and function as wrapper objects.</p>
<p>In actuality these are just ways of following the rule &#8220;avoid raw C types.&#8221;  You can find their definition in <em>NSObjCRuntime.h</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #6e371a;">#if __LP64__ || (TARGET_OS_EMBEDDED &amp;&amp; !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">long</span> NSInteger;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">long</span> NSUInteger;
<span style="color: #6e371a;">#else</span>
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">int</span> NSInteger;
<span style="color: #a61390;">typedef</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> NSUInteger;
<span style="color: #6e371a;">#endif</span></pre></td></tr></table></div>

<p>These are there for convenience, especially when combined with <em>NSIntegerMax</em>, <em>NSIntegerMin</em>, etc.</p>
<p>When we want to create a collection of <em>NSInteger</em>s (or <em>double</em>s, <em>long</em>s, etc), we have three main options options:</p>
<h2>Options</h2>
<ol>
<li /> Create a &#8220;raw&#8221; C/C++ array
<li /> <a href="http://numbergrinder.com/node/50">Use the Standard Template Library</a>
<li /> Wrap the value with <em>NSNumber</em> and use a Cocoa collection
</ol>
<h3>Raw C Arrays</h3>
<p>Many times all that the developer needs is an array of numbers that can be processed quickly.  In these cases it can be best just to use a straight C/C++ array.  For example, this code creates an array of <em>NSInteger</em> and then stores the square of the index:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">&nbsp;
size_t size <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>
&nbsp;
NSInteger <span style="color: #339933;">*</span>array <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NSInteger <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>calloc<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>NSInteger<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>NSInteger i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">*</span>i<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
free<span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Standard Template Library</h3>
<p>After <a href="http://numbergrinder.com/node/50">setting up to use the STL</a> the STL can be used the same way it would be in C++, just using <em>NSInteger</em> in the template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>NSInteger<span style="color: #000080;">&gt;</span> instance<span style="color: #008080;">;</span>
&nbsp;
instance.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
instance.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
instance.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>This approach is significantly more robust than using straight arrays and has the advantage of that everything does not need to be wrapped.  On the other hand, it does not provide the convenience of working with Objective-C types and still needs to be converted to interact with Cocoa libraries.</p>
<h3>Using <em>NSNumber</em> as a Wrapper</h3>
<p>The final option, and the most robust, is to use Cocoa&#8217;s built-in libraries and wrap the <em>NSInteger</em> values in a <em>NSNumber</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInteger<span style="color: #002200;">:</span>myInt<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p><em>NSNumber</em> is a subclass of <em>NSValue</em> and it can store any of the basic numeric types.  It should also be noted that <a href="http://numbergrinder.com/node/47">NSDecimalNumber is a subclass of NSNumber</a>. </p>
<p><em>NSNumber</em> contains a variety of useful functions, but it does not actually contain the facilities for basic mathematical operations.  To work with the numbers you will need to &#8220;unwrap&#8221; them once they get to their destination.</p>
<h2>Further Reading</h2>
<ul>
<li /> <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html">NSNumber Class Reference</a>
<li /> <a href="http://iPhoneDeveloperTips.com/cocoa/nsnumber-and-nsinteger.html">NSNumber and NSInteger</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/01/wrappers-for-numbers-nsnumber-and-collections-of-integers-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the STL in Objective-C</title>
		<link>http://www.numbergrinder.com/2009/01/using-the-stl-in-objective-c/</link>
		<comments>http://www.numbergrinder.com/2009/01/using-the-stl-in-objective-c/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 04:47:21 +0000</pubDate>
		<dc:creator>nachtrabe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://witticism.org/numbergrinder2/?p=109</guid>
		<description><![CDATA[One of the really powerful libraries available to C++ programmers is the Standard Template Library (STL). While Cocoa has fantastic built-in classes, sometimes it is beneficial&#8211;especially when working with algorithms where raw numbers are being used and speed is more &#8230; <a class="more-link" href="http://www.numbergrinder.com/2009/01/using-the-stl-in-objective-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of the really powerful libraries available to C++ programmers is the <a href="http://en.wikipedia.org/wiki/Standard_template_library">Standard Template Library (STL)</a>.  While Cocoa has fantastic built-in classes, sometimes it is beneficial&#8211;especially when working with algorithms where raw numbers are being used and speed is more important&#8211;to use the STL.</p>
<p>Using the STL in Objective-C requires that you switch the file to use <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_14_section_1.html#//apple_ref/doc/uid/TP30001163-CH10-SW1" class="broken_link" rel="nofollow">Objective-C++</a> by changing the file ending to <em>.mm</em>. After that you can import the various STL libraries normally: <em>#import <vector></vector></em>.</p>
<p>See Also:</p>
<ul>
<li /> <a href="http://www.sgi.com/tech/stl/">Standard Template Library Programmer&#8217;s Guide</a>
<li /> <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_14_section_1.html#//apple_ref/doc/uid/TP30001163-CH10-SW1" class="broken_link" rel="nofollow">The Objective-C 2.0 Programming Language: Using C++ With Objective-C</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.numbergrinder.com/2009/01/using-the-stl-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

