Skip to content

Instantly share code, notes, and snippets.

@arnobaer
Created September 8, 2022 11:56
Show Gist options
  • Save arnobaer/e26689c1cfd5bfba92e19f41a4339fa4 to your computer and use it in GitHub Desktop.
Save arnobaer/e26689c1cfd5bfba92e19f41a4339fa4 to your computer and use it in GitHub Desktop.
Prescale counter for fractional prescales with fixed precision.
// Prescale counter for fractional prescales with fixed precision.
#include <iostream>
#include <cmath>
#include <limits>
struct PrescaleCounter {
const size_t prescale_count;
const size_t single_step;
size_t trigger_counter = 0;
PrescaleCounter(float prescale, size_t prec)
: prescale_count(std::round(prescale * std::pow(10, prec))),
single_step(std::pow(10, prec))
{}
bool operator()()
{
trigger_counter += single_step;
if (trigger_counter >= prescale_count)
{
trigger_counter -= prescale_count;
return true;
}
return false;
}
};
int main()
{
const float prescale = 2.48;
const size_t prec = 2;
auto prescale_counter = PrescaleCounter(prescale, prec);
// testing
const size_t bx_count = 1000;
size_t counter = 0;
for (size_t i = 0; i < bx_count; ++i)
{
const bool result = prescale_counter();
if (result) ++counter;
std::cout << "[" << i << "] " << result << std::endl;
}
const float calculated_prescale = (1. / counter) * bx_count;
std::cout << "prescale: " << prescale << std::endl;
std::cout << "calc. : " << calculated_prescale << " (" << counter << " triggers in " << bx_count << " BX)" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment