2009/08/23

raw Komatsuna

http://cookpad.com/category/626

ある夜、無性にサラダが食べたくなってそこらへんにあった小松菜と胡瓜を生で切り刻んで食べたのだが、、、小松菜の後味が微妙に苦くてよろしくなかった。
ちょろっと調べてみたら炒め物やおひたしで利用するのが通常らしく、生で食うもんじゃないらしい(´ー`; )

2009/08/21

if statement without bracket

C言語で書かれた以下のコードは合法だし動くんだけど、正直やめてほしい。わかるんだけど気持ち悪くてしょうがない。好みの問題かしら、、(汗
#include <stdio.h>
int main(int argc, char *argv[])
{
int a = 0;
// displays true!
if (a == 0) printf("true!\n");
else {
printf("false!\n");
}
return 0;
}
view raw gistfile1.h hosted with ❤ by GitHub

こんなコードが某デスクトップの某巨大ライブラリには浴びるほどあるわけです。全体的な傾向としてはわかりやすいし好きなコードなんだけど、上記だけは僕的にいただけないです。

2009/08/19

[memo] make is NOT always GNU make

http://www.freebsd.org/cgi/man.cgi?query=make&sektion=1

当たり前のことではあるが、makeコマンドで実行される make が必ず GNU Make とは限らない。FreeBSD における make は BSD make であり、GNU make は gmake コマンドである。

Linux, Mac OS X においては、make は GNU make と見て差し支えない。Mac OS X での BSD make は bsdmake である。くれぐれも GNU脳にならないように>自分

Solaris とかだとどうなるんだろうね。

----

※GNU脳になってしまっていてよくひっかかる別のコマンドとしては、tar コマンドや cp コマンドが挙げられる。特に後者は Mac OS X の cp に -a オプションがないことに驚愕した苦い思い出がある。

2009/08/12

[memo] thread safe and portable random function?

スレッドセーフで移植性が高いrandom関数って書けないかしらん。と少し悩んで以下のように書いた。
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
//
// returns random number from zero to max.
// to compile this, run the following command.
// $ gcc -Wall -lm -lrt filename
//
double getRand(struct drand48_data *data, int max)
{
double rand = 0;
drand48_r(data, &rand);
rand = floor(rand * max);
return rand;
}
int initRand(struct drand48_data *data)
{
struct timespec time;
int result = clock_gettime(CLOCK_REALTIME, &time);
if (result == -1) {
perror("clock_gettime");
return -1;
}
srand48_r(time.tv_nsec, data);
return 0;
}
int main(int argc, char *argv[])
{
int c, result;
struct drand48_data data;
result = initRand(&data);
if (result == -1) {
return result;
}
for (c = 0; c < 100; c++) {
printf("%.0f\n", getRand(&data, 500));
}
return 0;
}


型の問題を差し引いたとしても、残念なことに上記は移植性がない。関数 [d|s]rand48_r が GNU による拡張だからである。うまい方法を御存知の方がいらっしゃったら教えてくださいませ(´ー`; )

[ Update August 13th 0:03 JST by m ]

pascal君から指摘があったので全面的に書き直しました。古いバージョンは一度だけ呼ぶべき srand48_r関数 を毎回呼んでいました。指摘さんくすです。