Handling calculations with many decimal places

  • Hi,

    I am writing an implementation of the Brent-Salamin algorithm for
    calculating pi. I want to be able to display potentially a large
    number of decimal places in the result (hundreds at least). However I
    am finding that even using long double values in my calculation gives
    me only the first 50 or so decimal places. Is this a limitation of
    long double that I am running into here?

    Should I be using NSDecimal instead to do this kind of calculation? If
    so, can anyone point me at a good example of its use as I am
    struggling with how to use it?

    Thanks,

    Ian.
    --
  • On 04/11/2009, at 1:18 AM, Ian Piper wrote:

    > I am writing an implementation of the Brent-Salamin algorithm for
    > calculating pi. I want to be able to display potentially a large
    > number of decimal places in the result (hundreds at least). However
    > I am finding that even using long double values in my calculation
    > gives me only the first 50 or so decimal places. Is this a
    > limitation of long double that I am running into here?

    Yes, remarkably enough, something of finite size has limits on its
    precision.

    > Should I be using NSDecimal instead to do this kind of calculation?
    > If so, can anyone point me at a good example of its use as I am
    > struggling with how to use it?

    NSDecimal may help, but it's also of finite size, so it won't help
    trying to print out the digits of pi to arbitrary precision, since pi
    is infinitely long. Algorithms to print the digits of pi usually emit
    the digits one at a time, so they are not storing the whole value as a
    single number. You'll probably need to resort to this sort of approach
    if you're just trying to calculate pi. To use the resulting value of
    pi in a calculation is going to be very difficult, and probably serves
    no practical purpose.

    --Graham
  • On 04/11/2009, at 1:29 AM, Graham Cox wrote:
    > NSDecimal may help, but it's also of finite size, so it won't help
    > trying to print out the digits of pi to arbitrary precision, since
    > pi is infinitely long. Algorithms to print the digits of pi usually
    > emit the digits one at a time, so they are not storing the whole
    > value as a single number. You'll probably need to resort to this
    > sort of approach if you're just trying to calculate pi. To use the
    > resulting value of pi in a calculation is going to be very
    > difficult, and probably serves no practical purpose.

    You could also try using code like this, which should be fairly easy
    to port to Mac:

    http://www.tc.umn.edu/~ringx004/mapm-main.html

    --Graham
  • On Nov 3, 2009, at 06:18, Ian Piper wrote:

    > I am writing an implementation of the Brent-Salamin algorithm for
    > calculating pi. I want to be able to display potentially a large
    > number of decimal places in the result (hundreds at least). However
    > I am finding that even using long double values in my calculation
    > gives me only the first 50 or so decimal places. Is this a
    > limitation of long double that I am running into here?
    >
    > Should I be using NSDecimal instead to do this kind of calculation?
    > If so, can anyone point me at a good example of its use as I am
    > struggling with how to use it?

    Clearly you cannot *directly calculate* a value correct to hundreds of
    significant digits unless you can *represent* the value correct to
    hundreds of significant digits.

    Clearly long double and NSDecimal cannot represent that many
    significant digits.

    Therefore you cannot do this with a direct implementation of the Brent-
    Salamin algorithm. You're going to have to do one of:

    1. Come up with your own representation of the value, correct to the
    desired number of significant digits, and write math routines for
    adding, subtracting, multiplying, dividing and taking square roots of
    numbers in your new representation.

    or

    2. Modify the algorithm so that it produces the digits sequentially
    without relying on high precision.

    Both of those sound extremely difficult, at least if you want an
    execution time shorter than your lifetime.

    I really don't think Cocoa has anything that can help you here. It's a
    pure computer science project.
  • You may find using binary coded decimal is a useful.

    If you google for          binary coded decimal library for osx
    you get a number of libraries.  I thought this one was interesting.

    http://mac.softpedia.com/get/Development/Libraries/libpbcd.shtml

    Peter
  • Ian Piper wrote:

    > I want to be able to display potentially a large number of decimal
    > places in the result (hundreds at least).

    Explanation and many links to libs:

    http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

    Any lib written in C or with C bindings would work in Objective-C.

    Or look at the source for 'dc':

    http://en.wikipedia.org/wiki/Dc_(computer_program)

      -- GG
  • On 3 Nov 2009, at 21:22, Greg Guerin wrote:

    > Ian Piper wrote:
    >
    >> I want to be able to display potentially a large number of decimal
    >> places in the result (hundreds at least).
    >
    > Explanation and many links to libs:
    >
    > http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic
    >
    > Any lib written in C or with C bindings would work in Objective-C.
    >
    >
    > Or look at the source for 'dc':
    >
    > http://en.wikipedia.org/wiki/Dc_(computer_program)

    Thanks - interesting stuff. Definitely beyond my capabilities at the
    moment! I have taken a look at the MAPM homepage (http://www.tc.umn.edu/~ringx004/mapm-main.html
    ) and if I have some time I may try to see if I can compile that
    library on my Mac.

    For now, I think I will rethink my ambitions in this area!

    Ian.
    --
previous month november 2009 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
Go to today