[Haskell-cafe] Canned routines for the first say thousand digits of pi, e, sqrt 2, etc?
will.yager at gmail.com
Fri Dec 18 21:30:12 UTC 2015
A trick I use when I need lots of digits is to calculate the number using a
If you calculate it using e.g. Rational, you can then get arbitrary
precision and print an arbitrary number of digits.
Here are a few examples. I've calculated both e and pi, accurate to 1000
digits. It takes about 2.3 seconds to run on my laptop.
It's also very easy to adapt to non-decimal bases.
On Fri, Dec 18, 2015 at 2:20 PM, Jeffrey Brown <jeffbrown.the at gmail.com>
> Thanks everybody! It turns out WolframAlpha will do the computation
> <https://www.reddit.com/r/piano/comments/3uz8oj/the_melody_of_pi_226_digits_chromatic_%CF%80_base_12/cy39b5g> for
> me. (It won't let me copy the digits, so I have to transcribe them by hand,
> but given how much time I'm spending reviewing the notes anyway, that is a
> small part of the overall labor cost.)
> On Fri, Dec 18, 2015 at 4:31 AM, Olaf Klinke <olf at aatal-apotheke.de>
>> As it happens, I am just studying a presentation  Martin Escardo gave
>> to students at the University of Birmingham. It contains Haskell code for
>> exact real number computation. Among other things, there is a function that
>> computes a signed digit representation of pi/32. It computes several
>> thousand digits in a few seconds.
>> I did not try it yet, but many irrational numbers are fixed points of
>> simple arithmetical expressions. For example, the golden ratio is the fixed
>> point of \x -> 1+1/x. Infinite streams of digits should be a type where
>> such a fixed point is computable. Or you could use a sufficiently precise
>> rational approximation and convert that do decimal in the usual way.
>> import Data.Ratio
>> import Data.List (iterate)
>> -- one step of Heron's algorithm for sqrt(a)
>> heron :: (Fractional a) => a -> a -> a
>> heron a x = (x+a/x)/2
>> -- infinite stream of approximations to sqrt(a)
>> approx :: (Fractional a) => a -> [a]
>> approx a = iterate (heron a) 1
>> -- Find an interval with rational end-points
>> -- for a signed-digit real number
>> type SDReal = [Int] -- use digits [-1,0,1]
>> interval :: Int -> SDReal -> (Rational,Rational)
>> interval precision x = let
>> f = foldr (\d g -> (a d).g) id (take precision x))
>> a d = \x -> ((fromIntegral d)+x)/2
>> in (f(-1),f(1))
>>  www.cs.bham.ac.uk/~mhe/.talks/phdopen2013/realreals.lhs
> Jeffrey Benjamin Brown
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe