summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--LICENSE24
-rw-r--r--Makefile20
-rw-r--r--README.md15
-rw-r--r--libs/libpoly.c7
-rw-r--r--libs/libsin.c7
-rw-r--r--libs/libsq.c4
-rw-r--r--main.c97
7 files changed, 51 insertions, 123 deletions
diff --git a/LICENSE b/LICENSE
deleted file mode 100644
index fdddb29..0000000
--- a/LICENSE
+++ /dev/null
@@ -1,24 +0,0 @@
-This is free and unencumbered software released into the public domain.
-
-Anyone is free to copy, modify, publish, use, compile, sell, or
-distribute this software, either in source code form or as a compiled
-binary, for any purpose, commercial or non-commercial, and by any
-means.
-
-In jurisdictions that recognize copyright laws, the author or authors
-of this software dedicate any and all copyright interest in the
-software to the public domain. We make this dedication for the benefit
-of the public at large and to the detriment of our heirs and
-successors. We intend this dedication to be an overt act of
-relinquishment in perpetuity of all present and future rights to this
-software under copyright law.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
-OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-
-For more information, please refer to <https://unlicense.org>
diff --git a/Makefile b/Makefile
index fdb2c0c..3b59d66 100644
--- a/Makefile
+++ b/Makefile
@@ -1,18 +1,6 @@
+montecarlo: main.c
+ cc $^ -o $@
-all: main libs
-
-# the main program
-main: main.c
- cc -o main main.c
-
-# available libraries
-.PHONY: libs
-libs: libs/*
- cc -shared libs/libsq.c -lm -lc -o libsq
- cc -shared libs/libsin.c -lm -lc -o libsin
- cc -shared libs/libpoly.c -lm -lc -o libpoly
-
-# clear build
.PHONY: clean
-clean:
- rm -f main libsq libsin libpoly
+clean:
+ rm -rf montecarlo
diff --git a/README.md b/README.md
deleted file mode 100644
index 8dc07f1..0000000
--- a/README.md
+++ /dev/null
@@ -1,15 +0,0 @@
-# Файлы
-* main.c: главный файл
-* Makefile: система сброки GNU Make
-* libs/ : папка с библиотеками для теста. а каждом файле там по 1 функции.
-
-# Как протестировать
-## Требования
-* ОС: Unix
-* gnu make
-* командная строка
-## Сборка
-* cd <путь к этому файлу>
-* make all
-## Запуск
-`./main ./libsq `. вместо libsq может быть libsin, libpoly
diff --git a/libs/libpoly.c b/libs/libpoly.c
deleted file mode 100644
index 2daa972..0000000
--- a/libs/libpoly.c
+++ /dev/null
@@ -1,7 +0,0 @@
-
-#include <math.h>
-
-// calculate polynomial
-double f(double x) {
- return pow(x, 3) - 2*x*x - 10*x + 69;
-}
diff --git a/libs/libsin.c b/libs/libsin.c
deleted file mode 100644
index 34200af..0000000
--- a/libs/libsin.c
+++ /dev/null
@@ -1,7 +0,0 @@
-
-#include <math.h>
-
-// calculate sine
-double f(double x) {
- return sin(x);
-}
diff --git a/libs/libsq.c b/libs/libsq.c
deleted file mode 100644
index 2b93fb4..0000000
--- a/libs/libsq.c
+++ /dev/null
@@ -1,4 +0,0 @@
-// calculate square
-double f(double x) {
- return x*x;
-}
diff --git a/main.c b/main.c
index 76afcef..b9bebbb 100644
--- a/main.c
+++ b/main.c
@@ -1,59 +1,56 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
-/*
- * Сотов Константин Алексеевич, 117 группа
- * sotov@twistea.su +7 914 329 50 01
- *
- * Данный код загружает динамическую библиотеку,
- * имя которой передано в аргументах командной строки,
- * и считает интеграл функции "f", если она есть
- * в этой динамической библиотеке.
- */
+#define _USE_MATH_DEFINES
+#include <math.h>
-#include <stdio.h> // printf, scanf
-#include <stdlib.h> // exit
-#include <dlfcn.h> // dlopen, dlsym, dlclose
+struct point {
+ float x, y;
+};
+struct circle {
+ // center not needed for this task
+ /*struct point center;*/
+ float rad;
+};
-// Integrates function f from a to b with <sums> steps.
-// Uses "naive" and straightforward integration technique
-// Assumes a < b
-double integral(double (*f)(double), double a, double b, int sums) {
- double result = 0;
- const double dx = (b-a)/sums;
- for (double i = a; i < b; i += dx)
- result += dx * f(i);
- return result;
+char in_circle(const struct circle *c, const struct point p) {
+ /*p.x -= c->x;*/
+ /*p.y -= c->y;*/
+ return p.x * p.x + p.y * p.y < c->rad*c->rad;
}
-int main(int argc, char* argv[]) {
- if (argc != 2) {
- printf("Недостаточно аргументов. использование:\n"
- "%s <путь к динамической библиотеке>\n", argv[0]);
- exit(EXIT_FAILURE);
+float calculate_pi(int iterations) {
+ int in = 0, out = 0;
+ int *arr[2] = {&out, &in};
+ const struct circle c = {.rad = 1.0f};
+ for (int i = 0; i < iterations; ++i) {
+ struct point p = {
+ .x = rand() % 2000000u / 1e6f - 1,
+ .y = rand() % 2000000u / 1e6f - 1
+ };
+ ++*arr[in_circle(&c, p)];
}
- // Open the dynamic library
- void* handle = dlopen(argv[1], RTLD_NOW);
- if (handle == NULL) {
- printf("ERROR: %s\n", dlerror());
- exit(EXIT_FAILURE);
- }
- // Locate the function
- void* f = dlsym(handle, "f");
- if (f == NULL) {
- printf("ERROR: %s", dlerror());
- dlclose(handle);
- exit(EXIT_FAILURE);
- }
-
- // Input-output block
- const char* fullname
- = "Сотов Константин Алексеевич";
- printf("Автор: %s\n", fullname);
- double a, b;
- puts("Введите нижнюю и верхнюю границы интегрирования через пробел");
- scanf("%lf %lf", &a, &b);
- printf("Интеграл f от %lf до %lf = %lf\n",
- a, b, integral(f, a, b, 10000));
+ return (float)in/iterations * 4;
+}
- exit(EXIT_SUCCESS);
+int main(int argc, char *argv[]) {
+ srand(time(NULL));
+ int iters;
+ if (argc == 2) {
+ iters = atoi(argv[1]);
+ } else {
+ fputs("Iterations: ", stdout);
+ if (!scanf("%i", &iters)) {
+ fputs("Input a valid number\n", stderr);
+ return EXIT_FAILURE;
+ }
+ }
+ float pi = calculate_pi(iters);
+ float err = fabs(M_PI-pi)/M_PI;
+ printf( "calculated pi: %.4f, actual pi: %.4f\n"
+ "error: %.2f%%\n",
+ pi, M_PI, err * 100);
+ return EXIT_SUCCESS;
}