<?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>taichino.com</title>
	<atom:link href="http://taichino.com/feed" rel="self" type="application/rss+xml" />
	<link>http://taichino.com</link>
	<description>永遠のネバーランド</description>
	<lastBuildDate>Mon, 08 Mar 2010 11:26:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQLが出力するCSVファイルをPythonで読みたい件</title>
		<link>http://taichino.com/programming/1624</link>
		<comments>http://taichino.com/programming/1624#comments</comments>
		<pubDate>Mon, 08 Mar 2010 11:26:05 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=1624</guid>
		<description><![CDATA[ケースとしてはレアな気がしますが、MySQLのSELECT 〜 INTO OUTFILEで出力したCSVに対して、処理をしたい時があります。僕の場合はpythonでやるわけですが、MySQLが出力するCSVの形式がpythonデフォルトの物と違うようでハマったのでメモしておきます。

MySQLには以下のようにデータを入れておきます。JSON形式のテキストを1レコードのみ準備しています。

#!/usr/bin/python
# -*- coding: utf-8 -*-
&#160;
import MySQLdb
import simplejson as json
&#160;
def main&#40;&#41;:
  data = &#91;'あ', 'い', 'う'&#93;
  cur = MySQLdb.connect&#40;host='localhost', db='dbname', user='user', passwd='pass'&#41;.cursor&#40;&#41;
  if not cur.execute&#40;'SELECT * FROM csv_test'&#41;:
    cur.execute&#40;'INSERT INTO csv_test(json) VALUES(%s)', &#40;json_data&#41;&#41;
&#160;
if __name__ == '__main__': main&#40;&#41;

このデータをSQLでCSVに吐き出します。注意したいのはターミネータにタブを指定している事です。カンマがJSONデータに含まれている為か、後でパースする時に、デフォルトのままだと正常にパースされません。

SELECT title, json FROM csv_test LIMIT 2 INTO OUTFILE '/tmp/test.csv' FIELDS [...]]]></description>
			<content:encoded><![CDATA[<p>ケースとしてはレアな気がしますが、MySQLのSELECT 〜 INTO OUTFILEで出力したCSVに対して、処理をしたい時があります。僕の場合はpythonでやるわけですが、MySQLが出力するCSVの形式がpythonデフォルトの物と違うようでハマったのでメモしておきます。</p>
<p><span id="more-1624"></span></p>
<p>MySQLには以下のようにデータを入れておきます。JSON形式のテキストを1レコードのみ準備しています。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> MySQLdb
<span style="color: #ff7700;font-weight:bold;">import</span> simplejson <span style="color: #ff7700;font-weight:bold;">as</span> json
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  data = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'あ'</span>, <span style="color: #483d8b;">'い'</span>, <span style="color: #483d8b;">'う'</span><span style="color: black;">&#93;</span>
  cur = MySQLdb.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>host=<span style="color: #483d8b;">'localhost'</span>, db=<span style="color: #483d8b;">'dbname'</span>, <span style="color: #dc143c;">user</span>=<span style="color: #483d8b;">'user'</span>, passwd=<span style="color: #483d8b;">'pass'</span><span style="color: black;">&#41;</span>.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> cur.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SELECT * FROM csv_test'</span><span style="color: black;">&#41;</span>:
    cur.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'INSERT INTO csv_test(json) VALUES(%s)'</span>, <span style="color: black;">&#40;</span>json_data<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>: main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>このデータをSQLでCSVに吐き出します。注意したいのはターミネータにタブを指定している事です。カンマがJSONデータに含まれている為か、後でパースする時に、デフォルトのままだと正常にパースされません。</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> title<span style="color: #66cc66;">,</span> json <span style="color: #993333; font-weight: bold;">FROM</span> csv_test <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">2</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #993333; font-weight: bold;">OUTFILE</span> <span style="color: #ff0000;">'/tmp/test.csv'</span> <span style="color: #993333; font-weight: bold;">FIELDS</span> TERMINATED <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span>;</pre></div></div>

<p>するとCSVファイルの内容は以下のようになります。２列目がunicode表現になっています。これから元の文字列を含んだjsonを復元したいんですが、unicode表現のまま復元されてしまいハマりました。</p>

<div class="wp_syntax"><div class="code"><pre class="csv" style="font-family:monospace;">テストデータ	[&quot;\\u3042&quot;, &quot;\\u3044&quot;, &quot;\\u3046&quot;]</pre></div></div>

<p>結論を書くとこのデータをパースするには以下のようにします。ポイントはCSVファイルの形式にあわせて、csv.Dialectのサブクラスを作ってパース時に指定する事です。Dialect使わずに、CSVファイル側を微調整しようとしても巧く行きませんでした(CSVの仕様を完全に把握してたら出来るんだと思います。)。また、escapecharacterを指定しないとスラッシュがunescapeされずにjsonデータを復元できません。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">csv</span>
<span style="color: #ff7700;font-weight:bold;">import</span> simplejson <span style="color: #ff7700;font-weight:bold;">as</span> json
<span style="color: #ff7700;font-weight:bold;">from</span> prettyprint <span style="color: #ff7700;font-weight:bold;">import</span> pp
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MySQLQuote<span style="color: black;">&#40;</span><span style="color: #dc143c;">csv</span>.<span style="color: black;">Dialect</span><span style="color: black;">&#41;</span>:
  delimiter      = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span>
  quotechar      = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span>'</span>
  lineterminator = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>'</span>
  quoting        = <span style="color: #dc143c;">csv</span>.<span style="color: black;">QUOTE_ALL</span>
  escapechar     = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span>'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  reader = <span style="color: #dc143c;">csv</span>.<span style="color: black;">reader</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'test.csv'</span>, <span style="color: #483d8b;">'rb'</span><span style="color: black;">&#41;</span>, MySQLQuote<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> reader:
    pp<span style="color: black;">&#40;</span>json.<span style="color: black;">loads</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
  main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>僕はGAEを使っているので、データのアップロード時に使うCSVをこれで処理しています。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/1624/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pythonで日本語を含んだリストと辞書をpretty printしたい件</title>
		<link>http://taichino.com/programming/1599</link>
		<comments>http://taichino.com/programming/1599#comments</comments>
		<pubDate>Fri, 05 Mar 2010 00:00:49 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=1599</guid>
		<description><![CDATA[Pythonでコード書いてると、１回は残念だなぁと思うポイントとして表題の件があると思います。具体的には以下です。

# リストも辞書も出力がお世辞にも良いとは言えない。。
&#62;&#62;&#62; print &#91;'あ', 'い', 'う'&#93;
&#91;'\xe3\x81\x82', '\xe3\x81\x84', '\xe3\x81\x86'&#93;
&#62;&#62;&#62; print &#123;'title':'ねじまき鳥', 'author':'村上春樹'&#125;
&#123;'author': '\xe6\x9d\x91\xe4\xb8\x8a\xe6\x98\xa5\xe6\xa8\xb9', 'title': '\xe3\x81\xad\xe3\x81\x98\xe3\x81\xbe\xe3\x81\x8d\xe9\xb3\xa5'&#125;

日本語がバイト表現な上、全要素が１行で表示されています。これではちょっとprintデバッグするにも萎えますよね。複雑な辞書を出力した場合なんかは、出力された文字列の整形にかなりのパワーを裂かれること請け合いです。

前々から何とかならないかと、悶々とした日々を過ごしていたのですが、先ほど駄目元で以下のコードを試してみたところ無事pretty printに成功しました。一旦json形式にした文字列を、evalでunicode文字列として再評価しています。そうする事でエスケープ表記されたunicode文字からunicodeオブジェクトを作っています。無理矢理感は否めませんが、背に腹は代えられないでしょう！

#!/usr/bin/python
# -*- coding: utf-8 -*-
&#160;
import simplejson as json
&#160;
def pp&#40;obj&#41;:
  if isinstance&#40;obj, list&#41; or isinstance&#40;obj, dict&#41;:
    orig = json.dumps&#40;obj, indent=4&#41;
    print eval&#40;&#34;u'''%s'''&#34; % orig&#41;.encode&#40;'utf-8'&#41;
  else:
    print obj

これで例えば以下の様な出力が得られます。これで安心してリストと辞書を使えるようになりました。

&#62;&#62;&#62; pp&#40;&#91;'あ', 'い', 'う'&#93;&#41;
&#91;
  [...]]]></description>
			<content:encoded><![CDATA[<p>Pythonでコード書いてると、１回は残念だなぁと思うポイントとして表題の件があると思います。具体的には以下です。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># リストも辞書も出力がお世辞にも良いとは言えない。。</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">'あ'</span>, <span style="color: #483d8b;">'い'</span>, <span style="color: #483d8b;">'う'</span><span style="color: black;">&#93;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>e3<span style="color: #000099; font-weight: bold;">\x</span>81<span style="color: #000099; font-weight: bold;">\x</span>82'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>e3<span style="color: #000099; font-weight: bold;">\x</span>81<span style="color: #000099; font-weight: bold;">\x</span>84'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>e3<span style="color: #000099; font-weight: bold;">\x</span>81<span style="color: #000099; font-weight: bold;">\x</span>86'</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'title'</span>:<span style="color: #483d8b;">'ねじまき鳥'</span>, <span style="color: #483d8b;">'author'</span>:<span style="color: #483d8b;">'村上春樹'</span><span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span><span style="color: #483d8b;">'author'</span>: <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>e6<span style="color: #000099; font-weight: bold;">\x</span>9d<span style="color: #000099; font-weight: bold;">\x</span>91<span style="color: #000099; font-weight: bold;">\x</span>e4<span style="color: #000099; font-weight: bold;">\x</span>b8<span style="color: #000099; font-weight: bold;">\x</span>8a<span style="color: #000099; font-weight: bold;">\x</span>e6<span style="color: #000099; font-weight: bold;">\x</span>98<span style="color: #000099; font-weight: bold;">\x</span>a5<span style="color: #000099; font-weight: bold;">\x</span>e6<span style="color: #000099; font-weight: bold;">\x</span>a8<span style="color: #000099; font-weight: bold;">\x</span>b9'</span>, <span style="color: #483d8b;">'title'</span>: <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>e3<span style="color: #000099; font-weight: bold;">\x</span>81<span style="color: #000099; font-weight: bold;">\x</span>ad<span style="color: #000099; font-weight: bold;">\x</span>e3<span style="color: #000099; font-weight: bold;">\x</span>81<span style="color: #000099; font-weight: bold;">\x</span>98<span style="color: #000099; font-weight: bold;">\x</span>e3<span style="color: #000099; font-weight: bold;">\x</span>81<span style="color: #000099; font-weight: bold;">\x</span>be<span style="color: #000099; font-weight: bold;">\x</span>e3<span style="color: #000099; font-weight: bold;">\x</span>81<span style="color: #000099; font-weight: bold;">\x</span>8d<span style="color: #000099; font-weight: bold;">\x</span>e9<span style="color: #000099; font-weight: bold;">\x</span>b3<span style="color: #000099; font-weight: bold;">\x</span>a5'</span><span style="color: black;">&#125;</span></pre></div></div>

<p>日本語がバイト表現な上、全要素が１行で表示されています。これではちょっとprintデバッグするにも萎えますよね。複雑な辞書を出力した場合なんかは、出力された文字列の整形にかなりのパワーを裂かれること請け合いです。</p>
<p><span id="more-1599"></span></p>
<p>前々から何とかならないかと、悶々とした日々を過ごしていたのですが、先ほど駄目元で以下のコードを試してみたところ無事pretty printに成功しました。一旦json形式にした文字列を、evalでunicode文字列として再評価しています。そうする事でエスケープ表記されたunicode文字からunicodeオブジェクトを作っています。無理矢理感は否めませんが、背に腹は代えられないでしょう！</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> simplejson <span style="color: #ff7700;font-weight:bold;">as</span> json
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> pp<span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>obj, <span style="color: #008000;">list</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>obj, <span style="color: #008000;">dict</span><span style="color: black;">&#41;</span>:
    orig = json.<span style="color: black;">dumps</span><span style="color: black;">&#40;</span>obj, indent=<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;u'''%s'''&quot;</span> <span style="color: #66cc66;">%</span> orig<span style="color: black;">&#41;</span>.<span style="color: black;">encode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'utf-8'</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> obj</pre></div></div>

<p>これで例えば以下の様な出力が得られます。これで安心してリストと辞書を使えるようになりました。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> pp<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'あ'</span>, <span style="color: #483d8b;">'い'</span>, <span style="color: #483d8b;">'う'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span>
    <span style="color: #483d8b;">&quot;あ&quot;</span>, 
    <span style="color: #483d8b;">&quot;い&quot;</span>, 
    <span style="color: #483d8b;">&quot;う&quot;</span>
<span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> pp<span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'title'</span>:<span style="color: #483d8b;">'ねじまき鳥'</span>, <span style="color: #483d8b;">'author'</span>:<span style="color: #483d8b;">'村上春樹'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">&quot;author&quot;</span>: <span style="color: #483d8b;">&quot;村上春樹&quot;</span>, 
    <span style="color: #483d8b;">&quot;title&quot;</span>: <span style="color: #483d8b;">&quot;ねじまき鳥&quot;</span>
<span style="color: black;">&#125;</span></pre></div></div>

<p>たった４行ですが、達成感があります:) なので今は凄く良い気になっているのですが、常識かもっといい方法があったら恥ずかしいので教えて下さい。</p>
<p><strong>2010.3.8 追記</strong><br />
個人的には割と使用頻度が高いので、prettyprintモジュールにしてpypiに登録しました。インストールはeasy_installから。ソースは<a href="http://github.com/taichino/prettyprint">github</a>から。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ easy_install prettyprint</pre></div></div>

<p>出力するppメソッドと、整形文字列を返すpp_strが定義されただけの酷く簡単なモジュールです。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> prettyprint <span style="color: #ff7700;font-weight:bold;">import</span> pp, pp_str
target = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'order'</span>:<span style="color: #483d8b;">'綺麗に表示せよ'</span><span style="color: black;">&#125;</span>
pp<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> pp_str<span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/1599/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>gitのリポジトリ作成メモ</title>
		<link>http://taichino.com/memo/1587</link>
		<comments>http://taichino.com/memo/1587#comments</comments>
		<pubDate>Mon, 01 Mar 2010 18:36:26 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[メモ]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=1587</guid>
		<description><![CDATA[基本的にはgithubを使えば良いとは思うんですが、privateなリポジトリを作りたい時もあります。で、たまーにリポジトリ作るんですが、毎回方法をすっかり忘れてしまうのでメモ書きです。

大まかな手順は、以下の通りです。

リモートリポジトリを作成
 ローカルリポジトリを作成
 ローカルリポジトリをリモートリポジトリに紐付け

以下が作業ログですが、umaskの設定を忘れなければハマりどころは少ない気がします。

# gitユーザとグループ作成
&#91;admin@remote&#93;$ adduser git
&#91;admin@remote&#93;$ usermod -G git taichino  # 自分
&#91;admin@remote&#93;$ usermod -G git dev1      # 他の開発者
&#91;admin@remote&#93;$ su - git
&#91;git@remote&#93;$ vim ~/.bashrc # umaskを002に設定する
&#160;
# リモートリポジトリ作成
&#91;git@remote&#93;$ mkdir -p /home/git/hoge.git  # リモートリポジトリ名には.gitを付ける
&#91;git@remote&#93;$ cd /home/git/hoge.git
&#91;git@remote&#93;$ git --bare init
&#160;
# ローカルリポジトリ作成とリモートリポジトリへの登録(by taichino)
&#91;taichino@local&#93;$ mkdir hoge
&#91;taichino@local&#93;$ cd hoge
&#91;taichino@local&#93;$ git init
&#91;taichino@local&#93;$ echo &#34;Hello git project&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>基本的にはgithubを使えば良いとは思うんですが、privateなリポジトリを作りたい時もあります。で、たまーにリポジトリ作るんですが、毎回方法をすっかり忘れてしまうのでメモ書きです。</p>
<p><span id="more-1587"></span></p>
<p>大まかな手順は、以下の通りです。</p>
<ol>
<li>リモートリポジトリを作成</li>
<li> ローカルリポジトリを作成</li>
<li> ローカルリポジトリをリモートリポジトリに紐付け</li>
</ol>
<p>以下が作業ログですが、umaskの設定を忘れなければハマりどころは少ない気がします。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># gitユーザとグループ作成</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>admin<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ adduser git
<span style="color: #7a0874; font-weight: bold;">&#91;</span>admin<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ usermod <span style="color: #660033;">-G</span> git taichino  <span style="color: #666666; font-style: italic;"># 自分</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>admin<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ usermod <span style="color: #660033;">-G</span> git dev1      <span style="color: #666666; font-style: italic;"># 他の開発者</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>admin<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">su</span> - git
<span style="color: #7a0874; font-weight: bold;">&#91;</span>git<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">vim</span> ~<span style="color: #000000; font-weight: bold;">/</span>.bashrc <span style="color: #666666; font-style: italic;"># umaskを002に設定する</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># リモートリポジトリ作成</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>git<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>git<span style="color: #000000; font-weight: bold;">/</span>hoge.git  <span style="color: #666666; font-style: italic;"># リモートリポジトリ名には.gitを付ける</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>git<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>git<span style="color: #000000; font-weight: bold;">/</span>hoge.git
<span style="color: #7a0874; font-weight: bold;">&#91;</span>git<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git <span style="color: #660033;">--bare</span> init
&nbsp;
<span style="color: #666666; font-style: italic;"># ローカルリポジトリ作成とリモートリポジトリへの登録(by taichino)</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> hoge
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> hoge
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git init
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Hello git project&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> readme
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git add readme
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git commit <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;start project with git&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git remote add origin <span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>taichino<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>git<span style="color: #000000; font-weight: bold;">/</span>hoge.git
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git push origin master
&nbsp;
<span style="color: #666666; font-style: italic;"># cloneとpush (by dev1)</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>dev1<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git clone <span style="color: #c20cb9; font-weight: bold;">ssh</span>:<span style="color: #000000; font-weight: bold;">//</span>dev1<span style="color: #000000; font-weight: bold;">@</span>remote<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>git<span style="color: #000000; font-weight: bold;">/</span>hoge.git
<span style="color: #7a0874; font-weight: bold;">&#91;</span>dev1<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #7a0874; font-weight: bold;">cd</span> hoge
<span style="color: #7a0874; font-weight: bold;">&#91;</span>dev1<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ <span style="color: #c20cb9; font-weight: bold;">vim</span> readme  <span style="color: #666666; font-style: italic;"># 適当に編集</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>dev1<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git add readme
<span style="color: #7a0874; font-weight: bold;">&#91;</span>dev1<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git commit <span style="color: #660033;">-m</span> <span style="color: #ff0000;">'modified for pull test'</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>dev1<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git push origin master
&nbsp;
<span style="color: #666666; font-style: italic;"># pull (by taichino)</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>taichino<span style="color: #000000; font-weight: bold;">@</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>$ git pull origin master</pre></div></div>

<p>リモートリポジトリを作る際に&#8211;bareオプションを指定すると、管理データのみ作られて編集用データは置かれません。別に必須ではないと思いますが、まぁリモートリポジトリに編集データは不要なので普通は指定しとけば良いと思います。</p>
<p>あと本当はssh経由では無くてgitプロトコルでアクセスしたいところですが、別に１人で使う分には問題ないのでほったらかしています。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/memo/1587/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[objective-c] FMDatabaseの使い方メモ</title>
		<link>http://taichino.com/programming/1574</link>
		<comments>http://taichino.com/programming/1574#comments</comments>
		<pubDate>Sun, 28 Feb 2010 12:51:23 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[fmdatabase]]></category>
		<category><![CDATA[objective-c]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=1574</guid>
		<description><![CDATA[ここ２、３週間久々にiphoneを弄っているのですが、GCが入ってない環境でのプログラミングはやらないと衰えるんですね。LLに慣れると辛いです。細かい事にまで気を使う必要があるので疲れますね。ブログ書きながらリハビリしようと思います。
さてiPhoneではバックエンドにSQLiteを使えるんですが、そのラッパーのFMDatabaseの使い方をまとめておきます。

インストールとプロジェクト設定はこちらのページを参考にさせて頂きました。ソースをコピーして、プロジェクトにライブラリを追加しましょう。
今回このインターフェースを使って行う処理は、概ね以下のSQLの通りです。テーブル作成後、CRUD操作を一通り行っています。

-- CREATE TABLES
CREATE TABLE authors&#40;
    id   INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
&#41;;
&#160;
CREATE TABLE books&#40;
    id        INTEGER PRIMARY KEY AUTOINCREMENT,
    title     TEXT,
    author_id INTEGER,
    score [...]]]></description>
			<content:encoded><![CDATA[<p>ここ２、３週間久々にiphoneを弄っているのですが、GCが入ってない環境でのプログラミングはやらないと衰えるんですね。LLに慣れると辛いです。細かい事にまで気を使う必要があるので疲れますね。ブログ書きながらリハビリしようと思います。<br />
さてiPhoneではバックエンドにSQLiteを使えるんですが、そのラッパーの<a href="http://code.google.com/p/flycode/source/browse/trunk/fmdb/src/FMDatabase.h">FMDatabase</a>の使い方をまとめておきます。</p>
<p><span id="more-1574"></span></p>
<p>インストールとプロジェクト設定は<a href="http://d.hatena.ne.jp/uzulla/20081001#p1">こちらのページ</a>を参考にさせて頂きました。ソースをコピーして、プロジェクトにライブラリを追加しましょう。</p>
<p>今回このインターフェースを使って行う処理は、概ね以下のSQLの通りです。テーブル作成後、CRUD操作を一通り行っています。</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- CREATE TABLES</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> authors<span style="color: #66cc66;">&#40;</span>
    id   INTEGER <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> AUTOINCREMENT<span style="color: #66cc66;">,</span>
    name TEXT
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> books<span style="color: #66cc66;">&#40;</span>
    id        INTEGER <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> AUTOINCREMENT<span style="color: #66cc66;">,</span>
    title     TEXT<span style="color: #66cc66;">,</span>
    author_id INTEGER<span style="color: #66cc66;">,</span>
    score     INTEGER
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- INSERT </span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> authors<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'村上春樹'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> authors<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ポールグレアム'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> books<span style="color: #66cc66;">&#40;</span>author_id<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'１Ｑ８４'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> books<span style="color: #66cc66;">&#40;</span>author_id<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ねじまき鳥クロニクル'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> books<span style="color: #66cc66;">&#40;</span>author_id<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ハッカーと画家'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> books<span style="color: #66cc66;">&#40;</span>author_id<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'On Lisp'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- SELECT / UPDATE</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> books<span style="color: #66cc66;">,</span> authors <span style="color: #993333; font-weight: bold;">WHERE</span> books<span style="color: #66cc66;">.</span>author_id <span style="color: #66cc66;">=</span> authors<span style="color: #66cc66;">.</span>id;
<span style="color: #993333; font-weight: bold;">UPDATE</span> books <span style="color: #993333; font-weight: bold;">SET</span> score <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span> <span style="color: #993333; font-weight: bold;">WHERE</span> title <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'１Ｑ８４'</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> books <span style="color: #993333; font-weight: bold;">SET</span> score <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">4</span> <span style="color: #993333; font-weight: bold;">WHERE</span> title <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'ハッカーと画家'</span>;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> books<span style="color: #66cc66;">,</span> authors <span style="color: #993333; font-weight: bold;">WHERE</span> books<span style="color: #66cc66;">.</span>author_id <span style="color: #66cc66;">=</span> authors<span style="color: #66cc66;">.</span>id;
&nbsp;
<span style="color: #808080; font-style: italic;">-- DELETE</span>
<span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> authors;
<span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> books;</pre></div></div>

<p>早速実装してみたコードが以下になります。必要な部分のみ抜粋しています。(ソースがそのまま動作可能ではないのも、LLに比べてストレスがたまりますね。。) CRUD操作をそれぞれ別のメソッドとしています。またインスタンスを取得(正確にはファイルパスの取得)するのが割と面倒なので、_getDBとして括りだしています。</p>
<p>特に他のラッパーライブラリと比べても、変わったところはなく抵抗無く使えると思いますが、僕がハマった点としてはexecuteQuery, executeUpdateで使用するプレースホルダーに入れる値は、オブジェクト型だと言う事です。Cの文字列を指定してしまい、BAD_ACCESSでしばらく悩みました。</p>
<p>あと、SELECT COUNT(*)を使った時のカラム名は、COUNT(*)になります。他の集約関数を使った時もSQL中に書いた表現がそのままカラム名になります。醜いのでASを使った方が良いかと思います。</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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;FMDatabase.h&quot;</span>
<span style="color: #6e371a;">#import &quot;FMDatabaseAdditions.h&quot;</span>
<span style="color: #6e371a;">#define DBFILE     @&quot;fmdb_test.db&quot;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// データベースインスタンスを返す</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>FMDatabase<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_getDB<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dbName <span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span>  pathArray <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> docdir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathArray objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> dbpath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>docdir stringByAppendingPathComponent<span style="color: #002200;">:</span>dbName<span style="color: #002200;">&#93;</span>;
	FMDatabase<span style="color: #002200;">*</span> db <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>FMDatabase databaseWithPath<span style="color: #002200;">:</span>dbpath<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>db open<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">@throw</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSException</span> exceptionWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DBOpenException&quot;</span> reason<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;couldn't open specified db file&quot;</span> userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> db;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// テーブル作成</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>createTable <span style="color: #002200;">&#123;</span>
    FMDatabase<span style="color: #002200;">*</span> db <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self _getDB<span style="color: #002200;">:</span>DBFILE<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> tables <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CREATE TABLE authors(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)&quot;</span>,
        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;authors&quot;</span>,
        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CREATE TABLE books(id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author_id INTEGER, score INTEGER)&quot;</span>,
        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;books&quot;</span>,
        <span style="color: #a61390;">nil</span>
    <span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> tableName <span style="color: #a61390;">in</span> tables<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>db tableExists<span style="color: #002200;">:</span>tableName<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>db executeUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>tables objectForKey<span style="color: #002200;">:</span>tableName<span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR: %d: %@&quot;</span>, <span style="color: #002200;">&#91;</span>db lastErrorCode<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>db lastErrorMessage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
            <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// table already exists error を起こしてみる</span>
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> tableName <span style="color: #a61390;">in</span> tables<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> 
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>db executeUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>tables objectForKey<span style="color: #002200;">:</span>tableName<span style="color: #002200;">&#93;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR: %d: %@&quot;</span>, <span style="color: #002200;">&#91;</span>db lastErrorCode<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>db lastErrorMessage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// error codeは1</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>db close<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// レコードの挿入</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>insert <span style="color: #002200;">&#123;</span>
    FMDatabase<span style="color: #002200;">*</span> db <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self _getDB<span style="color: #002200;">:</span>DBFILE<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> fixture <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>
                             <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
                                           <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;村上春樹&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;author&quot;</span>,
                                           <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;id&quot;</span>,
                                           <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;１Ｑ８４&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ねじまき鳥クロニクル&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
                                           <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;books&quot;</span>,
                                           <span style="color: #a61390;">nil</span>
                             <span style="color: #002200;">&#93;</span>,
                             <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span>
                                           <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ポールグレアム&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;author&quot;</span>,
                                           <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithInt<span style="color: #002200;">:</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;id&quot;</span>,
                                           <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ハッカーと画家&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;On Lisp&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>,
                                           <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;books&quot;</span>,
                                           <span style="color: #a61390;">nil</span>
                             <span style="color: #002200;">&#93;</span>,
                             <span style="color: #a61390;">nil</span>
        <span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> item <span style="color: #a61390;">in</span> fixture<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSNumber</span><span style="color: #002200;">*</span> author_id <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;id&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> author    <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;author&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span>  books     <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;books&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>db executeUpdate<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;INSERT INTO authors(id, name) VALUES (?, ?)&quot;</span> , author_id, author<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> book <span style="color: #a61390;">in</span> books<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #11740a; font-style: italic;">// its too ugly, COUNT(*) as cnt should be used in SQL</span>
                FMResultSet<span style="color: #002200;">*</span> rs <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>db executeQuery<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;SELECT COUNT(*) FROM books WHERE author_id = ? and title = ?&quot;</span>, author_id, book<span style="color: #002200;">&#93;</span>;
                <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>rs next<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#91;</span>rs intForColumn<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;COUNT(*)&quot;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                    <span style="color: #002200;">&#91;</span>db executeUpdate<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;INSERT INTO books(title, author_id) VALUES (?, ?)&quot;</span> , book, author_id<span style="color: #002200;">&#93;</span>;
                <span style="color: #002200;">&#125;</span>
                <span style="color: #002200;">&#91;</span>rs close<span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">// if duplicated, error code 19 will return (Constraint Error)</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR: %d: %@&quot;</span>, <span style="color: #002200;">&#91;</span>db lastErrorCode<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>db lastErrorMessage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>db close<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// レコードの取得</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>select <span style="color: #002200;">&#123;</span>
    FMDatabase<span style="color: #002200;">*</span> db <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self _getDB<span style="color: #002200;">:</span>DBFILE<span style="color: #002200;">&#93;</span>;
    FMResultSet<span style="color: #002200;">*</span> rs <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>db executeQuery<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;SELECT * FROM authors, books WHERE authors.id = books.author_id&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>rs next<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d %@ %d &quot;</span>,
              <span style="color: #002200;">&#91;</span>rs intForColumn<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;id&quot;</span><span style="color: #002200;">&#93;</span>,
              <span style="color: #002200;">&#91;</span>rs stringForColumn<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span><span style="color: #002200;">&#93;</span>,
              <span style="color: #002200;">&#91;</span>rs intForColumn<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;score&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>rs close<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>db close<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// レコードの更新</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>update <span style="color: #002200;">&#123;</span>
    FMDatabase<span style="color: #002200;">*</span> db <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self _getDB<span style="color: #002200;">:</span>DBFILE<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>db executeUpdate<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;UPDATE books SET score = 5 WHERE title = ?&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;１Ｑ８４&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR: %d: %@&quot;</span>, <span style="color: #002200;">&#91;</span>db lastErrorCode<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>db lastErrorMessage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>db executeUpdate<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;UPDATE books SET score = 4 WHERE title = ?&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ハッカーと画家&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR: %d: %@&quot;</span>, <span style="color: #002200;">&#91;</span>db lastErrorCode<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>db lastErrorMessage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>db close<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// レコードの削除</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>delete <span style="color: #002200;">&#123;</span>
    FMDatabase<span style="color: #002200;">*</span> db <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self _getDB<span style="color: #002200;">:</span>DBFILE<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> tables <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;authors&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;books&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> table <span style="color: #a61390;">in</span> tables<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>db executeUpdate<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DELETE FROM %s&quot;</span>, <span style="color: #002200;">&#91;</span>table cStringUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ERROR: %d: %@&quot;</span>, <span style="color: #002200;">&#91;</span>db lastErrorCode<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>db lastErrorMessage<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>db close<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>あと仕方が無いと言えば無いのですが、objective-cの辞書と配列の使いにくさは非常にストレスを感じます。例えば、insertで作っている辞書と配列の混合物(51〜67行目)をpythonで書くと以下になります。</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">fixture = <span style="color: black;">&#91;</span>
  <span style="color: black;">&#123;</span><span style="color: #483d8b;">'author'</span>: <span style="color: #483d8b;">'村上春樹'</span>,
   <span style="color: #483d8b;">'books'</span> : <span style="color: black;">&#91;</span><span style="color: #483d8b;">'１Ｑ８４'</span>, <span style="color: #483d8b;">'ねじまき鳥クロニクル'</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span>,
  <span style="color: black;">&#123;</span><span style="color: #483d8b;">'author'</span>: <span style="color: #483d8b;">'ポールグレアム'</span>,
   <span style="color: #483d8b;">'books'</span> : <span style="color: black;">&#91;</span><span style="color: #483d8b;">'ハッカーと画家'</span>, <span style="color: #483d8b;">'On Lisp'</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span><span style="color: black;">&#93;</span></pre></div></div>

<p>この差は結構重要で、LLだったら取りあえず辞書に入れといて、というところでもわざわざ扱うデータ専用のクラスを作るところから始める必要があり、LLってホンマに開発効率良かったんやというのを実感している次第であります。あとブログ幅も足りないですね。近いうちにテーマ修正しようと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/1574/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>浮動小数点の情報落ちを追って見た</title>
		<link>http://taichino.com/programming/1546</link>
		<comments>http://taichino.com/programming/1546#comments</comments>
		<pubDate>Thu, 18 Feb 2010 17:55:42 +0000</pubDate>
		<dc:creator>taichino</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[algorithm]]></category>

		<guid isPermaLink="false">http://taichino.com/?p=1546</guid>
		<description><![CDATA[今日topcoderの問題を解いてて、浮動小数点の誤差ではまりました。問題を単純化したコードが以下になります。

#include ＜iostream＞
using namespace std;
&#160;
int main&#40;int argc, char** argv&#41; &#123;
    double small = 1e-18;
    double big   = 1;
    double added = small + big;
    cout ＜＜ added ＜＜ endl;  // 1が出力される。(small分が切り捨てられてる)
&#125;

加算を行った結果、小数点以下の値が切り捨てられています。一瞬何が起こったのか解らなかったのですが、折角なので追ってみる事にしました。

まずdoubleの内部表現は次のようになります。(詳細はwikipediaを参照)

符号部(sign)が1bit、指数部(exponential)が11bit, 仮数部(significand)が52bitです。
実際に確認してみます。gdbで確認するには以下のようにします。(他に簡単な確認方法があれば教えて下さい。)

$ g++ -g test.cpp
$ gdb ./a.out
&#40;gdb&#41; x/g &#38;small
0xbfffee88:	0x3c32725dd1d243ac
&#40;gdb&#41; x/g &#38;big
0xbfffee90:	0x3ff0000000000000
&#40;gdb&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>今日topcoderの問題を解いてて、浮動小数点の誤差ではまりました。問題を単純化したコードが以下になります。</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include ＜iostream＞</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span><span style="color: #000040;">**</span> argv<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> small <span style="color: #000080;">=</span> <span style="color:#800080;">1e-18</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> big   <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">double</span> added <span style="color: #000080;">=</span> small <span style="color: #000040;">+</span> big<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> ＜＜ added ＜＜ endl<span style="color: #008080;">;</span>  <span style="color: #666666;">// 1が出力される。(small分が切り捨てられてる)</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>加算を行った結果、小数点以下の値が切り捨てられています。一瞬何が起こったのか解らなかったのですが、折角なので追ってみる事にしました。</p>
<p><span id="more-1546"></span></p>
<p>まずdoubleの内部表現は次のようになります。(詳細は<a href="http://ja.wikipedia.org/wiki/浮動小数点数">wikipediaを参照</a>)<br />
<img src='http://taichino.com/wp-content/plugins/wp-latexrender/pictures/ec2ed113f3e1c88825d107509b199200.gif' title='(-1)^{sign} \times 2^{(exponential-1023)} \times (1+significand)' alt='(-1)^{sign} \times 2^{(exponential-1023)} \times (1+significand)' align=absmiddle><br />
符号部(sign)が1bit、指数部(exponential)が11bit, 仮数部(significand)が52bitです。</p>
<p>実際に確認してみます。gdbで確認するには以下のようにします。(他に簡単な確認方法があれば教えて下さい。)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">g++</span> <span style="color: #660033;">-g</span> test.cpp
$ <span style="color: #c20cb9; font-weight: bold;">gdb</span> .<span style="color: #000000; font-weight: bold;">/</span>a.out
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> x<span style="color: #000000; font-weight: bold;">/</span>g <span style="color: #000000; font-weight: bold;">&amp;</span>small
0xbfffee88:	0x3c32725dd1d243ac
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> x<span style="color: #000000; font-weight: bold;">/</span>g <span style="color: #000000; font-weight: bold;">&amp;</span>big
0xbfffee90:	0x3ff0000000000000
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">gdb</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> x<span style="color: #000000; font-weight: bold;">/</span>g <span style="color: #000000; font-weight: bold;">&amp;</span>added
0xbfffee98:	0x3ff0000000000000</pre></div></div>

<p>見ての通り、0&#215;3c32725dd1d243acと0&#215;3ff0000000000000の加算結果が0&#215;3ff0000000000000となっています。</p>
<p>まずbigの値を見てみると、符号部は0, 指数部は3ff, 仮数部は0です。これを先ほどの式に当てはめます。<br />
<img src='http://taichino.com/wp-content/plugins/wp-latexrender/pictures/e833edef2f7b2416105d2708f8807cfa.gif' title='(-1)^{0} \times 2^{(1023-1023)} \times (1.0) = 1.0' alt='(-1)^{0} \times 2^{(1023-1023)} \times (1.0) = 1.0' align=absmiddle></p>
<p>上で見た式の通りにデータが構成されていますね！同様にsmallについても見てみましょう。<br />
<img src='http://taichino.com/wp-content/plugins/wp-latexrender/pictures/b207980a01ccc01da64fd8118fe225ba.gif' title='(-1)^{0} \times 2^{(963-1023)} \times (1.2725dd1d243ac) = (-1)^{0} \times 2^{-60} \times (1.2725dd1d243ac)' alt='(-1)^{0} \times 2^{(963-1023)} \times (1.2725dd1d243ac) = (-1)^{0} \times 2^{-60} \times (1.2725dd1d243ac)' align=absmiddle></p>
<p>これで大分と事情が飲み込めますね。加算の際は指数部の桁をあわせる必要がありますが、smallとbigでは指数部に2^60倍の差があるため、smallの仮数部が60bit分右にシフトします。仮数部は全部で52bitしか無いので、結果的にすべて0になってしまい、smallの値は切り捨てられることになります。</p>
<p>このような現象は情報落ちと呼ばれていて、僕も学校で習った気はしますが、きちんと考えたのは今回が初めてでした。倍精度の浮動小数点といっても、絶対値の大きく違う数を同時に扱おうとすると簡単に誤差が発生してしまう事が解りました。まだ感覚は掴みきれていませんが、気をつけて行こうと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://taichino.com/programming/1546/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 2.549 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-03-10 23:29:27 -->
