Continuing logarithmic arithmetic: floor operator on encoded length conditions

I am trying to implement basic arithmetic on Bill Gosper with continued logarithms , which are a β€œmutation” of continued fractions, allowing us to use the term co -proutines to emit and consume very small messages even on very large or very small numbers.

Reverse arithmetic, such as {+, -, *, /}, is quite simple described by Gosper, at least in a unary representation, but it’s difficult for me to implement the modulo operator, which effectively truncates information from the division operation.

I realized that the modulo operator can be basically defined using operations that I already have:

a mod b == a - b * floor (a / b)

leaving my only gender issue.

I also read that the encoded length format for continuous logarithms effectively describes

'... the integer part of the base of the journal is the 2nd number remaining described.'

Thus, getting the first term immediately (pass) gives the correct result so far, but leaves a significant part, which, I assume, requires a kind of transfer mechanism.

I wrote the following code to test the input terms and the expected output terms, but mostly I'm looking for ideas of a high-level algorithm behind a realistic floor.

An example of input (1234/5) into an output pair is

Input: [7, 0, 3, 0, 0, 0, 0, 1, 3, 3, 1]

Output: [7, 0, 3, 1, 4, 2, 1, 1]

from fractions import Fraction def const(frac): """ CL bistream from a fraction >= 1 or 0. """ while frac: if frac >= 2: yield 1 frac = Fraction(frac, 2) else: yield 0 frac -= 1 frac = Fraction(1, frac) if frac else 0 def rle(bit_seq): """ Run-length encoded CL bitstream. """ s = 0 for bit in bit_seq: s += bit if not bit: yield s s = 0 def floor(rle_seq): """ RLE CL terms of the greatest integer less than rle_seq. """ #pass yield from output """ Sample input/output pairs for floor(). """ num = Fraction(1234) for den in range(1, int(num)+1): input = list(rle(const(num / den))) output = list(rle(const(num // den))) # Integer division! print("> ", input) print(">> ", output) print(">>*", list(floor(input))) print() assert(list(floor(input)) == output) 

How can I implement the floor operator in the spirit of continuing the arithmetic of the fraction by consuming the terms only if necessary and immediately emitting the terms and, especially, using the run length of the encoded format (in binary form) rather than the unary extension Gosper tends to describe.

+2
source share

All Articles