clang-format stereography c files

This commit is contained in:
queue-miscreant 2025-07-22 00:16:00 -05:00
parent 5fbf8970c2
commit 32294879c9
3 changed files with 232 additions and 198 deletions

View File

@ -0,0 +1,9 @@
BasedOnStyle: llvm
IndentWidth: 4
AlignAfterOpenBracket: BlockIndent
BinPackParameters: OnePerLine
BreakBeforeBraces: Custom
BraceWrapping:
AfterFunction: true
PointerAlignment: Left

View File

@ -1,19 +1,18 @@
#include <complex.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <complex.h>
#define STRRED "\x1b[31m"
#define STRGREEN "\x1b[32m"
#define STRNORM "\x1b[m"
#define SECONDS_PER_NANOSECOND 1000000000
#define NUM_LOOPS 100000
double complex complex_turn(double turn)
{
return cexp(I*M_PI*turn);
}
double complex complex_turn(double turn) { return cexp(I * M_PI * turn); }
double complex approx_turn(double turn)
{
@ -28,10 +27,12 @@ double complex approx_turn(double turn)
return (c * c - s * s) + I * (2 * c * s);
}
void print_errors(const double *inputs,
void print_errors(
const double* inputs,
const double complex* ideals,
const double complex* approxs,
int n)
int n
)
{
double c_error, s_error;
double largest_c_error, largest_s_error;
@ -70,41 +71,48 @@ void print_errors(const double *inputs,
total_c_error /= (double)n;
total_s_error /= (double)n;
printf("Squared error in cosines: \n"\
"\tAverage: %f (%f%% error)\n""\tLargest: %f (%f%% error)" \
"\n\t\tInput:\t\t%f\n\t\tValue:\t\t%f\n\t\tApproximation:\t%f\n"
, total_c_error, sqrt(total_c_error) * 100
, largest_c_error, sqrt(largest_c_error) * 100
, inputs[largest_c_index]
, creal(ideals[largest_c_index]), creal(approxs[largest_c_index]));
printf("Squared error in sines: \n"\
"\tAverage: %f (%f%% error)\n\tLargest: %f (%f%% error)" \
"\n\t\tInput:\t\t%f\n\t\tValue:\t\t%f\n\t\tApproximation:\t%f\n"
, total_s_error, sqrt(total_s_error) * 100
, largest_s_error, sqrt(largest_s_error) * 100
, inputs[largest_c_index]
, cimag(ideals[largest_s_index]), cimag(approxs[largest_s_index]));
printf(
"Squared error in cosines: \n"
"\tAverage: %f (%f%% error)\n"
"\tLargest: %f (%f%% error)"
"\n\t\tInput:\t\t%f\n\t\tValue:\t\t%f\n\t\tApproximation:\t%f\n",
total_c_error, sqrt(total_c_error) * 100, largest_c_error,
sqrt(largest_c_error) * 100, inputs[largest_c_index],
creal(ideals[largest_c_index]), creal(approxs[largest_c_index])
);
printf(
"Squared error in sines: \n"
"\tAverage: %f (%f%% error)\n\tLargest: %f (%f%% error)"
"\n\t\tInput:\t\t%f\n\t\tValue:\t\t%f\n\t\tApproximation:\t%f\n",
total_s_error, sqrt(total_s_error) * 100, largest_s_error,
sqrt(largest_s_error) * 100, inputs[largest_c_index],
cimag(ideals[largest_s_index]), cimag(approxs[largest_s_index])
);
}
// time the length of the computation `f` in nanoseconds
long time_computation( double complex (*f)(double),
long time_computation(
double complex (*f)(double),
const double* inputs,
double complex* results,
int n)
int n
)
{
size_t i;
long tick;
long tick_s;
long tick_ns;
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
tick = tp.tv_nsec;
tick_ns = tp.tv_nsec;
tick_s = tp.tv_sec;
for (i = 0; i < n; i++) {
results[i] = f(inputs[i]);
}
//this isn't quite proper, since the clock may have ticked over a second
clock_gettime(CLOCK_MONOTONIC, &tp);
return tp.tv_nsec - tick;
return SECONDS_PER_NANOSECOND * (tp.tv_sec - tick_s) +
(tp.tv_nsec - tick_ns);
}
int main(int argn, char** args)
@ -130,12 +138,17 @@ int main(int argn, char **args)
double frac_speed;
if (diff > 0) {
frac_speed = rat_time / (double)trig_time;
printf(STRRED "math.h" STRNORM " faster, speedup: %ldns (%2.2fx)\n",
diff, frac_speed);
printf(
STRRED "math.h" STRNORM " faster, speedup: %ldns (%2.2fx)\n", diff,
frac_speed
);
} else {
frac_speed = trig_time / (double)rat_time;
printf(STRGREEN "Approximation" STRNORM " faster, speedup: %ldns (%2.2fx)\n",
-diff, frac_speed);
printf(
STRGREEN "Approximation" STRNORM
" faster, speedup: %ldns (%2.2fx)\n",
-diff, frac_speed
);
print_errors(rands, trigs, rats, NUM_LOOPS);
}

View File

@ -1,5 +1,5 @@
#include <stdio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
@ -33,7 +33,8 @@ void rational(double turn, struct circle *ret)
ret->s = 2 * c * s;
}
double errors(int n, const struct circle *circles1, const struct circle *circles2)
double
errors(int n, const struct circle* circles1, const struct circle* circles2)
{
double c_error, s_error;
double largest_c_error, largest_s_error;
@ -67,12 +68,18 @@ double errors(int n, const struct circle *circles1, const struct circle *circles
total_c_error /= (double)n;
total_s_error /= (double)n;
printf("Squared error in cosines: \n\tAverage: %f (%f%% error)\n\tLargest: %f (%f%% error)\n"
, total_c_error, sqrt(total_c_error) * 100
, largest_c_error, sqrt(largest_c_error) * 100);
printf("Squared error in sines: \n\tAverage: %f (%f%% error)\n\tLargest: %f (%f%% error)\n"
, total_s_error, sqrt(total_s_error) * 100
, largest_s_error, sqrt(largest_s_error) * 100);
printf(
"Squared error in cosines: \n\tAverage: %f (%f%% error)\n\tLargest: %f "
"(%f%% error)\n",
total_c_error, sqrt(total_c_error) * 100, largest_c_error,
sqrt(largest_c_error) * 100
);
printf(
"Squared error in sines: \n\tAverage: %f (%f%% error)\n\tLargest: %f "
"(%f%% error)\n",
total_s_error, sqrt(total_s_error) * 100, largest_s_error,
sqrt(largest_s_error) * 100
);
return 0;
}
@ -117,12 +124,17 @@ int main(int argn, char **args)
long linSpeed = rat_time - trig_time;
if (linSpeed > 0) {
fracSpeed = rat_time / (double)trig_time;
printf(STRRED "math.h" STRNORM " faster, speedup: %ldns (%2.2fx)\n",
linSpeed, fracSpeed);
printf(
STRRED "math.h" STRNORM " faster, speedup: %ldns (%2.2fx)\n",
linSpeed, fracSpeed
);
} else {
fracSpeed = trig_time / (double)rat_time;
printf(STRGREEN "Approximation" STRNORM " faster, speedup: %ldns (%2.2fx)\n",
-linSpeed, fracSpeed);
printf(
STRGREEN "Approximation" STRNORM
" faster, speedup: %ldns (%2.2fx)\n",
-linSpeed, fracSpeed
);
errors(10000, rats, trigs);
}
}