simon-git: spigot (master): Simon Tatham
Commits to Tartarus hosted VCS
tartarus-commits at lists.tartarus.org
Tue Oct 10 21:06:44 BST 2017
TL;DR:
f71b87a Python: make a spigot from an iterator giving digits.
Repository: https://git.tartarus.org/simon/spigot.git
On the web: https://git.tartarus.org/?p=simon/spigot.git
Branch updated: master
Committer: Simon Tatham <anakin at pobox.com>
Date: 2017-10-10 21:06:44
commit f71b87ae1cc8262c286c1f671cf8b94d6675e317
web diff https://git.tartarus.org/?p=simon/spigot.git;a=commitdiff;h=f71b87ae1cc8262c286c1f671cf8b94d6675e317;hp=08dcb92ee88964181628e9b1ff8a3c86728769ee
Author: Simon Tatham <anakin at pobox.com>
Date: Tue Oct 10 19:48:06 2017 +0100
Python: make a spigot from an iterator giving digits.
Similarly to the continued-fraction creation function in the previous
commit, this lets you write an iterator that generates the decimal
digits of a number (or in some other base if you prefer), and pass
them to the spigot module which will give you back a Spigot object
representing the resulting real number.
The first 'digit' you return is the integer part, and the handling of
negative numbers is slightly fiddly: if you yield (say) -1,2,3,4 then
that means -1 *plus* 0.234, i.e. -0.766. To generate the digits of a
negative number in the obvious way, you instead start by yielding the
special prefix object spigot.BASE_NEG, so if you wanted -1.234 you
could yield spigot.BASE_NEG and then 1,2,3,4.
You can also yield spigot.BASE_CHANGE(b) part way through to switch to
interpreting subsequent digits in a different base. I demo this with
my usual favourite example of the Champernowne constant; another set
of good examples would be numbers like e, 1/e or sin(1) expressed in a
factorial base.
Internally to the Python side of the implementation, this is done
using an 'interval source' system in which what is really passed to
the C core is triples of the form (n0,n1,d) which mean 'narrow the
interval [0,1] to [n0/d,n1/d]', or rather, scale that operation to the
current bracketing interval for the output number. I may yet decide to
expose that feature externally, since it allows more general ways of
constructing a number, such as half-valued digits (which you could use
to avoid some exactness hazards); but for the moment I've left it as
an internal implementation detail because the handling of
_terminating_ sequences of those triples is not intuitive.
python/demo.py | 50 ++++++++++++++++++++++++++++++++
python/pyspig.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++
python/spig/__init__.py | 31 ++++++++++++++++++++
python/testsuite.py | 36 +++++++++++++++++++++++
4 files changed, 194 insertions(+)
More information about the tartarus-commits
mailing list