locked
How to implement arbitrary precision calculator RRS feed

  • Question

  • Hi all,
    I'm trying to implement an arbitrary precision calculator using C (if you know linux, I'm trying to clone dc). Since it is going to be arbitrary precision, I need to store the input and intermediate results in something other than simple doubles. I'm thinking of 2 options:
    * strings containing the numbers as it is:for eg. 3.14 will be "3.14". But this will need converting back and forth between string and numeric every time a computation is done.
    * a linked list of long's: for eg., 3.14159265358979 might be stored as 3, 1415926, 5358979 with an external variable holding the position of the decimal point.
        Is either of these a good choice, or is there a better way (one with a good tradeoff between ease of programming and efficiency) ?


    Monday, June 11, 2007 7:59 PM

Answers

  • Arbitrary precision is used in applications where arithmetic performance is not a limiting factor, or where precise results or exact integer arithmetic with very large numbers is required. It is also useful for checking the results of fixed-precision calculations. Hence, for APA, we need to look at the accuracy of the results and not at the efficiency.

    As for ur query regarding storage of the value of "pi", rational numbers can be stored as a pair of two integers for the numerator and denominator, in a fixed-point format with a fixed denominator, or in a floating-point format as a significand multiplied by an arbitrary exponent.
    For further help, you can have a look at these softwares.

    Arbitrary-precision software

    Arbitrary-precision arithmetic in most computer software is implemented by calling an external library that provides datatypes and subroutines to store numbers with the requested precision and to perform computations.

    Stand-alone application software that supports arbitrary precision computations:

    Tuesday, June 12, 2007 7:10 AM

All replies

  • Arbitrary precision is used in applications where arithmetic performance is not a limiting factor, or where precise results or exact integer arithmetic with very large numbers is required. It is also useful for checking the results of fixed-precision calculations. Hence, for APA, we need to look at the accuracy of the results and not at the efficiency.

    As for ur query regarding storage of the value of "pi", rational numbers can be stored as a pair of two integers for the numerator and denominator, in a fixed-point format with a fixed denominator, or in a floating-point format as a significand multiplied by an arbitrary exponent.
    For further help, you can have a look at these softwares.

    Arbitrary-precision software

    Arbitrary-precision arithmetic in most computer software is implemented by calling an external library that provides datatypes and subroutines to store numbers with the requested precision and to perform computations.

    Stand-alone application software that supports arbitrary precision computations:

    Tuesday, June 12, 2007 7:10 AM
  • Thank you Arijit. The decNumber and GNU MPL library links were very helpful.. I shall read through them and try to use their algorithms.
    Tuesday, June 12, 2007 5:53 PM
  • Really good resources man. Thanks a lot.
    Thursday, June 14, 2007 2:51 PM
  • even i'm trying to build calculator but in java.
    most of the work is done.
    but the operators which i'm considering are +, -,unary - & *.
    though this can be expanded.
    actually the code which i'm trying to develop is for web application.
    the context is very different.
    the expression you will get will be a string.
    if you want to try, give it a try & can also share it here.
    soon, i'll post here my logic
    Sunday, June 24, 2007 5:32 AM