LibBF – a small library to handle arbitrary precision floating point numbers
11–20 of 63 posts
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#12Mostly off topic but maybe this post attracts people who know the answer to a question that I ran into when I implemented a big integer based rational number library some time ago. The decimal representation of a rational number p / q has the general form ± . ( ), for example 41,111,111 / 333,000 equals 123.456(789). Is there an efficient algorithm to determine the lengths of the fractional parts or one that does at…
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#13Mostly off topic but maybe this post attracts people who know the answer to a question that I ran into when I implemented a big integer based rational number library some time ago. The decimal representation of a rational number p / q has the general form ± . ( ), for example 41,111,111 / 333,000 equals 123.456(789). Is there an efficient algorithm to determine the lengths of the fractional parts or one that does at…
For a simple answer, the lengths are bounded above by Euler's totient function [1] (the sum of factors) of the denominator. Any tighter bounds depend on the specific primes in the factorisation.
Note that Euler's totient function is "always 'nearly n'" in magnitude (Hardy and Littlewood), so that representation will be very expensive for most fractions of large denominator.
[0]: https://en.wikipedia.org/wiki/Repeating_decimal [1]: https://en.wikipedia.org/wiki/Euler%27s_totient_function
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#14For Mac users trying this at home, I made the following changes to build and run the tinypi tests on Sierra: 1. Comment out the malloc.h include in tinypi.c. 2. Add -Wno-unused-function to CFLAGS. 3. Use shasum, rather than sha1sum, in the test target.
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#15This is perfect for jq[0]. [0] https://github.com/stedolan/jq
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#16This is perfect for jq[0]. [0] https://github.com/stedolan/jq
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#17Mostly off topic but maybe this post attracts people who know the answer to a question that I ran into when I implemented a big integer based rational number library some time ago. The decimal representation of a rational number p / q has the general form ± . ( ), for example 41,111,111 / 333,000 equals 123.456(789). Is there an efficient algorithm to determine the lengths of the fractional parts or one that does at…
While I’m sure you know this, when computing the fractional part you will have at most q-1 digits in your repeating cycle. When you divide by q, you have at most q unique remainders (0 to q-1). Also when computing the fractional part, if you end up with the same remainder as you’ve previously seen then you’ve hit the cycle.
And unfortunately q is just way to loose as a bound if it even deserves the name bound in this case. I initially started looking into this because I wanted to print something like »0.577 215 664 « but in that case q is essentially useless because you either have to really search the cycle which quickly becomes impractical once q reaches millions or billions or you have to live with »0.577 215 664 «.
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#18Mostly off topic but maybe this post attracts people who know the answer to a question that I ran into when I implemented a big integer based rational number library some time ago. The decimal representation of a rational number p / q has the general form ± . ( ), for example 41,111,111 / 333,000 equals 123.456(789). Is there an efficient algorithm to determine the lengths of the fractional parts or one that does at…
Look at the Wikipedia page on repeated decimals and you can see many of the repeated sequences have length (q-1) ((q-1) is phi(q) when q is prime) [2].
There's a SO answer that talks about this [3]. I think I can derive it here, though take it with a grain of salt as I might have screwed something up.
Say you have a rational number p / q, and w.l.o.g., p
p/q = R sum_{k=1}^{\infty} 10^{-s k} = R / ( 10^s - 1 )
rearranging terms: p (10^s - 1) - q R = 0
Let's just assume for now 10 and q are relatively prime. I think I have to do a little hand waiving at this point and just assume the above has a solution. If the above is true, this means 10^s = 1 (%q) for s equal to the order of 10 mod q, which is a fancy way of saying 10^s = C q + 1 for some integer C. One choice that will always work is taking s = phi(q) though this might be smaller (specifically a divisor or phi(q)) depending on what the order of 10 is to q.So to make the above equation true, let's be coarse and choose s = phi(q) in the below:
.. p (10^{ phi(q) } - 1 ) - q R = 0
-> p ( C q ) - q R = 0
-> q ( p C - R ) = 0
And there you go. Assuming that I haven't bungled anything, this also gives an 'algorithm' to find the actual repeated sequence: R = p C = p 10^{phi(q)} . Choosing s, the length of R in base 10, to be phi(q) will always "work", but, again, s might be able to be chosen smaller, depending on what divisors of phi(q) there are and what order 10 is to q, so the real answer of the length of the repeated sequence is 10's order in the multiplicative group mod q.There's also the case of when 10 and q aren't relatively prime that needs to be worked out.
You can also see how this argument could be modified to work with other bases.
[1] https://softwareengineering.stackexchange.com/a/192077
[2] https://en.wikipedia.org/wiki/Repeating_decimal#Decimal_expa...
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#19This is perfect for jq[0]. [0] https://github.com/stedolan/jq
Why does jq need arbitrary precision floating-point numbers?
Re: LibBF – a small library to handle arbitrary precision floating point numbers
#20This is perfect for jq[0]. [0] https://github.com/stedolan/jq
Why does jq need arbitrary precision floating-point numbers?
(JSON also does not permit infinities or NaN, which JavaScript does permit.)