Created
September 25, 2019 13:10
-
-
Save arnobaer/c2b9ddef9e8470bc76deeb0f67dfedbd to your computer and use it in GitHub Desktop.
Yet another fixed point fractional prescale calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fixed point fractional prescale calculation using add and sub | |
# Proposed by Andrea Bocci, Sept. 2019 | |
import argparse | |
class PrescaleCounter(object): | |
precision = 1 | |
def __init__(self, prescale): | |
self.step = 10**self.precision | |
self.counter = 0 | |
self.prescale = int(round(prescale * self.step)) | |
def next(self): | |
result = False | |
self.counter += self.step | |
if self.counter >= self.prescale: | |
self.counter -= self.prescale | |
result = True | |
return (self.counter / self.step), result | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--prescale', type=float, default=2.3) | |
parser.add_argument('--bx-range', type=int, default=256) | |
args = parser.parse_args() | |
counter = PrescaleCounter(args.prescale) | |
for i in range(args.bx_range): | |
count, result = counter.next() | |
print('{:04} | {} | {}'.format(i, count, int(result))) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment