summaryrefslogtreecommitdiffhomepage
path: root/misc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-03-28 19:29:05 +0200
committerTyge Løvset <[email protected]>2023-03-28 19:29:05 +0200
commit59d74d181e44dd05a8570b42fc6284745e225664 (patch)
treeb693ec9f5ef0d529fd2e6edbe3b53cf0f1eb8c2d /misc
parent26cd0a73422cdbcd4998170e179fa0f3ce48e9a5 (diff)
downloadSTC-modified-59d74d181e44dd05a8570b42fc6284745e225664.tar.gz
STC-modified-59d74d181e44dd05a8570b42fc6284745e225664.zip
Example changes. Added crand.h possible replacement for crandom.h
Diffstat (limited to 'misc')
-rw-r--r--misc/examples/gauss2.c11
-rw-r--r--misc/examples/prime.c5
-rw-r--r--misc/examples/shape.c19
3 files changed, 16 insertions, 19 deletions
diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c
index be514c12..ce29f786 100644
--- a/misc/examples/gauss2.c
+++ b/misc/examples/gauss2.c
@@ -11,14 +11,15 @@
int main()
{
- enum {N = 10000000};
- const double Mean = -12.0, StdDev = 6.0, Scale = 74;
+ enum {N = 5000000};
+ uint64_t seed = (uint64_t)time(NULL);
+ stc64_t rng = stc64_new(seed);
+ const float Mean = round(stc64_randf(&rng)*98.f - 49.f), StdDev = stc64_randf(&rng)*10.f + 1.f, Scale = 74.f;
printf("Demo of gaussian / normal distribution of %d random samples\n", N);
+ printf("Mean %f, StdDev %f\n", Mean, StdDev);
// Setup random engine with normal distribution.
- uint64_t seed = (uint64_t)time(NULL);
- stc64_t rng = stc64_new(seed);
stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev);
// Create and init histogram map with defered destruct
@@ -32,7 +33,7 @@ int main()
// Print the gaussian bar chart
c_forpair (index, count, csmap_int, hist) {
- int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N);
+ int n = (int)round((float)*_.count * StdDev * Scale * 2.5f / (float)N);
if (n > 0) {
cstr_resize(&bar, n, '*');
printf("%4d %s\n", *_.index, cstr_str(&bar));
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index 9ffb2f53..34d64f10 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -42,9 +42,8 @@ int main(void)
puts("\n");
puts("Show the last 50 primes using a temporary crange generator:");
- crange R = crange_make(n - 1, 0, -2);
- c_forfilter (i, crange, R,
- cbits_test(&primes, *i.ref>>1) &&
+ c_forfilter (i, crange, crange_object(n - 1, 0, -2),
+ cbits_test(&primes, *i.ref/2) &&
c_flt_take(i, 50)
){
printf("%lld ", *i.ref);
diff --git a/misc/examples/shape.c b/misc/examples/shape.c
index e24f7fd7..d7116039 100644
--- a/misc/examples/shape.c
+++ b/misc/examples/shape.c
@@ -4,7 +4,7 @@
#include <stdio.h>
#include <stc/ccommon.h>
-#define c_dyn_cast(T, s) \
+#define DYN_CAST(T, s) \
(&T##_api == (s)->api ? (T*)(s) : (T*)0)
// Shape definition
@@ -53,15 +53,14 @@ typedef struct {
extern struct ShapeAPI Triangle_api;
-Triangle Triangle_from(Point a, Point b, Point c)
-{
- Triangle t = {.shape={.api=&Triangle_api}, .p={a, b, c}};
+Triangle Triangle_from(Point a, Point b, Point c) {
+ Triangle t = {{&Triangle_api}, {a, b, c}};
return t;
}
static void Triangle_draw(const Shape* shape)
{
- const Triangle* self = c_dyn_cast(Triangle, shape);
+ const Triangle* self = DYN_CAST(Triangle, shape);
printf("Triangle : (%g,%g), (%g,%g), (%g,%g)\n",
self->p[0].x, self->p[0].y,
self->p[1].x, self->p[1].y,
@@ -88,9 +87,8 @@ typedef struct {
extern struct ShapeAPI Polygon_api;
-Polygon Polygon_init(void)
-{
- Polygon p = {.shape={.api=&Polygon_api}, .points=PointVec_init()};
+Polygon Polygon_init(void) {
+ Polygon p = {{&Polygon_api}, {0}};
return p;
}
@@ -101,15 +99,14 @@ void Polygon_addPoint(Polygon* self, Point p)
static void Polygon_drop(Shape* shape)
{
- Polygon* self = c_dyn_cast(Polygon, shape);
+ Polygon* self = DYN_CAST(Polygon, shape);
printf("poly destructed\n");
PointVec_drop(&self->points);
- Shape_drop(shape);
}
static void Polygon_draw(const Shape* shape)
{
- const Polygon* self = c_dyn_cast(Polygon, shape);
+ const Polygon* self = DYN_CAST(Polygon, shape);
printf("Polygon :");
c_foreach (i, PointVec, self->points)
printf(" (%g,%g)", i.ref->x, i.ref->y);