Answered by:
How to implement arbitrary precision calculator

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.
- GNU Multi-Precision Library (and MPFR): C bignum libraries
- Java.math: BigInteger and BigDecimal classes for Java
- decNumber: C package for decimal bignums
- Computable Real Numbers: arbitrary-precision library for Common Lisp
- LargeInteger - JScience Library
- Math::BigFloat is an arbitrary-precision module for Perl
- BigDigits is an arbitrary-precision library in C
- Crypto++ includes arbitrary-precision arithmetic in C++
- apfloat arbitrary-precision library for Java and C++
- LGPL-licensed integer bignum source code written in C
- MAPM A Portable Arbitrary Precision Math Library in C. (Also includes bindings for C++ and Lua.)
- LiDIA A C++ Library For Computational Number Theory for C and C++
- NTL NTL: A Library for doing Number Theory for C and C++
- CLN A Class Library for Numbers (arbitrary integers and floating point numbers for C and C++)
Stand-alone application software that supports arbitrary precision computations:
- PARI/GP, an open source computer algebra system that supports arbitrary precision.
- Mathematica, Maple computer algebra system, and Macsyma: computer algebra software including arbitrary-precision arithmetic
- BCMath: the PHP binary calculator functions
- bc programming language: the POSIX / GNU basic calculator
- dc programming language: the POSIX desk calculator
- The Python programming language: the built-in long integer type is of arbitrary precision. The Decimal built-in module has user definable precision.
- REXX: programming language (including Open Object Rexx and NetRexx)
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.
- GNU Multi-Precision Library (and MPFR): C bignum libraries
- Java.math: BigInteger and BigDecimal classes for Java
- decNumber: C package for decimal bignums
- Computable Real Numbers: arbitrary-precision library for Common Lisp
- LargeInteger - JScience Library
- Math::BigFloat is an arbitrary-precision module for Perl
- BigDigits is an arbitrary-precision library in C
- Crypto++ includes arbitrary-precision arithmetic in C++
- apfloat arbitrary-precision library for Java and C++
- LGPL-licensed integer bignum source code written in C
- MAPM A Portable Arbitrary Precision Math Library in C. (Also includes bindings for C++ and Lua.)
- LiDIA A C++ Library For Computational Number Theory for C and C++
- NTL NTL: A Library for doing Number Theory for C and C++
- CLN A Class Library for Numbers (arbitrary integers and floating point numbers for C and C++)
Stand-alone application software that supports arbitrary precision computations:
- PARI/GP, an open source computer algebra system that supports arbitrary precision.
- Mathematica, Maple computer algebra system, and Macsyma: computer algebra software including arbitrary-precision arithmetic
- BCMath: the PHP binary calculator functions
- bc programming language: the POSIX / GNU basic calculator
- dc programming language: the POSIX desk calculator
- The Python programming language: the built-in long integer type is of arbitrary precision. The Decimal built-in module has user definable precision.
- REXX: programming language (including Open Object Rexx and NetRexx)
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 logicSunday, June 24, 2007 5:32 AM