Re: [Haskell-cafe] Canned routines for the first say thousand digits of pi, e, sqrt 2, etc?

As it happens, I am just studying a presentation [1] 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)) Cheers, Olaf [1] www.cs.bham.ac.uk/~mhe/.talks/phdopen2013/realreals.lhs

Thanks everybody! It turns out WolframAlpha will do the computation
https://www.reddit.com/r/piano/comments/3uz8oj/the_melody_of_pi_226_digits_c...
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
As it happens, I am just studying a presentation [1] 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))
Cheers, Olaf
[1] www.cs.bham.ac.uk/~mhe/.talks/phdopen2013/realreals.lhs
-- Jeffrey Benjamin Brown

A trick I use when I need lots of digits is to calculate the number using a
series expansion.
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.
https://gist.github.com/wyager/33dcc26d1e867c462808
It's also very easy to adapt to non-decimal bases.
Will
On Fri, Dec 18, 2015 at 2:20 PM, Jeffrey Brown
Thanks everybody! It turns out WolframAlpha will do the computation https://www.reddit.com/r/piano/comments/3uz8oj/the_melody_of_pi_226_digits_c... 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
wrote: As it happens, I am just studying a presentation [1] 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))
Cheers, Olaf
[1] www.cs.bham.ac.uk/~mhe/.talks/phdopen2013/realreals.lhs
-- Jeffrey Benjamin Brown
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (3)
-
Jeffrey Brown
-
Olaf Klinke
-
William Yager