ViolaJones/cpp/test.cpp
2023-05-07 20:15:55 +02:00

64 lines
1.9 KiB
C++

#include <iostream>
#include <iomanip>
#include "data.hpp"
#include "toolbox.hpp"
#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
#define PBWIDTH 60
void printProgress(const float64_t& percentage) noexcept {
const uint64_t val = static_cast<uint64_t>(percentage * 100);
const int lpad = static_cast<int>(percentage * PBWIDTH);
const int rpad = PBWIDTH - lpad;
printf("%3lu%% [%.*s%*s]\r", val, lpad, PBSTR, rpad, "");
fflush(stdout);
}
void clearProgress() noexcept {
// Progress bar width + space before + num space + space after
printf("%*c\r", PBWIDTH + 1 + 3 + 3, ' ');
}
template<typename T>
void test(const uint64_t& N) noexcept {
#ifdef __DEBUG
printf("DETERMINISTIC for N=%s of %s sized %s\n", thousand_sep(N).c_str(), typeid(T).name(), format_byte_size(sizeof(T)).c_str());
print("Estimating memory footprint at : " + format_byte_size(3 * N * sizeof(T)));
#endif
T *a = new T[N], *b = new T[N], *c = new T[N];
T mean = static_cast<T>(0.0);
const size_t percent = N / 100;
for(size_t i = 0; i < N; ++i){
if (i % percent == 0) printProgress(static_cast<float64_t>(i) / N);
a[i] = static_cast<T>(i < N>>1 ? 0.1 : 1.0);
b[i] = static_cast<T>(1.0);
c[i] = a[i] * b[i];
mean += c[i];
}
mean /= static_cast<T>(N);
clearProgress();
std::cout << mean << std::endl;
delete[] a, delete[] b, delete[] c;
}
void test_float() noexcept {
std::cout << std::setprecision(1<<8);
const uint64_t N = static_cast<uint64_t>(1)<<28;
test<float128_t>(N);
test<float64_t>(N);
test<float32_t>(N);
//printf("%.128af\n", static_cast<float64_t>(1) / 3);
//std::cout << static_cast<float64_t>(1) / 3 << std::endl;
//std::cout << std::hexfloat << static_cast<float64_t>(1) / 3 << std::endl;
//printf("%.128Lf\n", static_cast<long float64_t>(1) / 3);
//printf("%.128lf\n", static_cast<float64_t>(1) / 3);
//printf("%.128f\n", static_cast<float>(1) / 3);
}