# Accurate math library

This topic is 3625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anyone know of a perfectly accurate math library? Something that would be capable of the following:
a = 1/3;
b = a * Pi;
c = b / Pi;

c == a; // true
Obviously, it would need internal and external representation, where evaluating a would result in 0.3333333, and b = 1.047198, but would preserve adequate information internally to be able to retain precision loss for as long as needed. Needless to say, internal storage would likely need to contain full expression. Just curious, don't have any pressing need for this.

##### Share on other sites
Haskell's lazy evaluation could do it, but that's probably not what you're looking for.

Using GHCi:
Prelude> let a = 1 / 3Prelude> let b = a * piPrelude> let c = b / piPrelude> c == aTrue

##### Share on other sites
What you're probably after is a Computer Algebra System. There's a few different ones linked from Wikipedia: http://en.wikipedia.org/wiki/Computer_algebra_system

##### Share on other sites
Representing irrationals like pi exactly is not possible, so no. Libraries and languages that support arbitrary precision arithmetic will let you do similar things with ratios, though.

For instance, in lisp:

(let ((n (/ 17 (expt 13 99))))  (print n)  (print (* n (expt 13 99))))17/190718085458920964116236375748835779710674959067303165370168392262012207679844273858329666379998629245551661077 17

This does not work for floats.

[Edited by - gsg on February 20, 2008 8:14:52 AM]

##### Share on other sites
GNU Multiple Precision Library is one.

One thing to bear in mind with these libraries, is that efficiency can be poor. Certain calculations necessarily cause the number's precision to grow and grow, which makes the operations slower and slower.

##### Share on other sites
Quote:
 Original post by gsgRepresenting irrationals like pi exactly is not possible, so no.

Yes it is. You just did it – 'pi'. A symbolic maths system would deal fine with this. It would, of course, need to be converted to some alternative representation to be used outside of the system (either some finite-precision form, or some graphical or string representation of the symbolic form) but it would certainly allow examples like the original to work correctly. In fact, this is almost exactly what the OP specifies.