月度归档:2009年12月

[复习] 三角函数 和差化积,积化和差

[转载] —— 和差化积,积化和差的推导和记忆
原文: http://sherring.blog.sohu.com/101104952.html

首先,我们知道
sin(a+b)=sina*cosb+cosa*sinb,
sin(a-b)=sina*cosb-cosa*sinb

我们把两式相加就得到
sin(a+b)+sin(a-b)=2sina*cosb

所以,
sina*cosb=(sin(a+b)+sin(a-b))/2

同理,若把两式相减,就得到
cosa*sinb=(sin(a+b)-sin(a-b))/2

同样的,我们还知道
cos(a+b)=cosa*cosb-sina*sinb,
cos(a-b)=cosa*cosb+sina*sinb

所以,把两式相加,我们就可以得到
cos(a+b)+cos(a-b)=2cosa*cosb

所以我们就得到,
cosa*cosb=(cos(a+b)+cos(a-b))/2

同理,两式相减我们就得到
sina*sinb=-(cos(a+b)-cos(a-b))/2

这样,我们就得到了积化和差的四个公式:
sina*cosb=(sin(a+b)+sin(a-b))/2
cosa*sinb=(sin(a+b)-sin(a-b))/2
cosa*cosb=(cos(a+b)+cos(a-b))/2
sina*sinb=-(cos(a+b)-cos(a-b))/2

好,有了积化和差的四个公式以后,我们只需一个变形,就可以得到和差化积的四个公式.我们把上述四个公式中的a+b设为x,a-b设为y,那么a=(x+y)/2,b=(x-y)/2

把a,b分别用x,y表示就可以得到和差化积的四个公式:
sinx+siny=2sin((x+y)/2)*cos((x-y)/2)
sinx-siny=2cos((x+y)/2)*sin((x-y)/2)
cosx+cosy=2cos((x+y)/2)*cos((x-y)/2)
cosx-cosy=-2sin((x+y)/2)*sin((x-y)/2)

php-cgi莫名其妙的崩溃

homeserver通过FastCGI协议与php-cgi进程进行通信来执行php脚本文件。
一直以来都发现php-cgi进程不是很稳定,可能是我在实现协议上有不当的地方。运行一段时间后,例如一两天,phpcgi进程就会自动结束,也不留下任何相关信息。
目前,我通过写批处理文件,在进程结束后自动建立新进程来临时解决这个问题。之前在windows上是写一个批处理文件,然后copy几万行执行命令,这样就相当于无止境地执行。
在Linux上做的更简单,可能是我不知道在windows的批处理里怎么写循环。

#!/bin/bash
while true
    echo “Starting php server …”
    php-cgi -b 127.0.0.1:8080
    echo “Server exited. Restart it …t”
    sleep 10s
done

为啥要sleep 10s呢?因为php-cgi进程异常结束了,socket还在占用着8080端口,如果不睡眠一下,那么会耗尽CPU资源,知道端口能够使用。这样可能导致日志文件的体积疯狂增长,直到硬盘资源枯竭而止。。。

test

这么好的id,真不知道怎么使用好呢~~

唉,最近被那些事儿烦死了,总不能静下来做些自己喜欢的东西。看到iceboy在写自己的MagicGate™,看到气泡熊在破解别人的加密算法,看到FB天天在做算法题,看到TualatriX在玩Chrome了,看到martin已经有自己的公司了,看到HD开源了,看到“C”在设计Vision的UI了。而我呢?好像很忙的样子,却又无所事事。还真希望快点到军训的日子,快点到临近放假的日子。。。

小虾啊小虾~~


(图由iceboy提供。。。)

一首Lisp的赞歌,听听吧

Eternal Flame (song parody)

This is a parody of the song
“God Lives on Terra” by Julia Ecklar.
For more information about this song,
visit Songworm.

I was taught assembler
in my second year of school.
It’s kinda like construction work —
with a toothpick for a tool.
So when I made my senior year,
I threw my code away,
And learned the way to program
that I still prefer today.

Now, some folks on the Internet
put their faith in C++.
They swear that it’s so powerful,
it’s what God used for us.
And maybe it lets mortals dredge
their objects from the C.
But I think that explains
why only God can make a tree.

For God wrote in Lisp code
When he filled the leaves with green.
The fractal flowers and recursive roots:
The most lovely hack I’ve seen.
And when I ponder snowflakes,
never finding two the same,
I know God likes a language
with its own four-letter name.

Now, I’ve used a SUN under Unix,
so I’ve seen what C can hold.
I’ve surfed for Perls, found what Fortran’s for,
Got that Java stuff down cold.
Though the chance that I’d write COBOL code
is a SNOBOL’s chance in Hell.
And I basically hate hieroglyphs,
so I won’t use APL.

Now, God must know all these languages,
and a few I haven’t named.
But the Lord made sure, when each sparrow falls,
that its flesh will be reclaimed.
And the Lord could not count grains of sand
with a 32-bit word.
Who knows where we would go to
if Lisp weren’t what he preferred?

And God wrote in Lisp code
Every creature great and small.
Don’t search the disk drive for man.c,
When the listing’s on the wall.
And when I watch the lightning burn
Unbelievers to a crisp,
I know God had six days to work,
So he wrote it all in Lisp.

Yes, God had a deadline.
So he wrote it all in Lisp.

这首歌是一位研究AV的朋友好几年前介绍的。某吉他男(程序员)写的一首关于God使用Lisp的歌曲,蛮幽默滑稽的。在GNU的ftp上可以下载。我后来不知什么时候做了一个歌词文件,也是我到目前做的唯一一个LRC文件。。。

The song:
eternal-flame.ogg
The lyric:
eternal-flame. lrc

String Reverse in C++

今夜帮沛公改了个C++程序,反转输出一个句子。觉得他写的程序过于麻烦,既使用了string又使用了char数组。
于是下了下面的程序:

#include iostream
#include cstring
int main()
{
char s[10000];
gets(s);
for(char*p; p=strrchr(s,’ ‘); *p=’\0’)
std::cout–(p+1)–” “;
std::cout–s–std::endl;
return 0;
}

但沛公喜欢简洁一点,就简化一下,如下:
#include
int main(char*p, char s[10000])
{
for(gets(s); p=strrchr(s,’ ‘); *p=’\0’)
std::cout–(p+1)–” “;
std::cout–s–std::endl;
}

(*^__^*) 嘻嘻…… 还可以试试非主流C++ , 如下: