計算物理学 Rubin H. Landau 参考文献・docker登録
https://qiita.com/kaizen_nagoya/items/af924f5ac71ff5cc8c89
docker/ubuntu
# cd /home/cp/HCPcodes
# clang pond.c -o pond
pond.c:19:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {
^
1 warning generated.
# ./pond
data stored in pond.dat
# cat pond.dat
1 4.000000
2 4.000000
...
998 3.130261
999 3.131131
1000 3.132000
# gcc pond.c
pond.c:19:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
main() {
^~~~
main関数の前にint をつければよい。
vond.c
/* From "COMPUTATIONAL PHYSICS", 3rd Ed, Enlarged Python eTextBook
by RH Landau, MJ Paez, and CC Bordeianu
Copyright Wiley-VCH Verlag GmbH & Co. KGaA, Berlin; Copyright R Landau,
Oregon State Unv, MJ Paez, Univ Antioquia, C Bordeianu, Univ Bucharest, 2015.
Support by National Science Foundation
*/
//https://physics.oregonstate.edu/~landaur/Books/CPbook/Codes/HPCodes/pond.c
// pond.c: *Monte-Carlo integration to determine pi (stone throwing
#include <stdio.h>
#include <stdlib.h>
// if you don't have drand48 uncomment the following two lines
// #define drand48 1.0/RAND_MAX*rand
// #define srand48 srand
#define max 1000 // number of stones to be thrown
#define seed 68111 // seed for number generator
int main() {
int i, pi = 0;
double x, y, area;
FILE *output; // save data in pond.dat
output = fopen("pond.dat","w");
srand48(seed); // seed the number generator
for (i = 1; i<= max; i++) {
x = drand48()*2-1; // creates floats between
y = drand48()*2-1; // 1 and -1
if ((x*x + y*y)<1) pi++; // stone hit the pond
area = 4*(double)pi/i; // calculate area
fprintf(output, "%i\t%f\n", i, area);
}
printf("data stored in pond.dat\n");
fclose(output);
return i;
}
<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>
This article is an individual impression based on the individual's experience. It has nothing to do with the organization or business to which I currently belong.
文書履歴(document history)
ver. 0.01 初稿 20190818
ver. 0.04 URL追記 20230302
最後までおよみいただきありがとうございました。
いいね 💚、フォローをお願いします。
Thank you very much for reading to the last sentence.
Please press the like icon 💚 and follow me for your happy life.