Sign in to follow this  
Antheus

Accurate math library

Recommended Posts

Antheus    2409
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 this post


Link to post
Share on other sites
Wan    1366
Haskell's lazy evaluation could do it, but that's probably not what you're looking for.

Using GHCi:
Prelude> let a = 1 / 3
Prelude> let b = a * pi
Prelude> let c = b / pi
Prelude> c == a
True

Share this post


Link to post
Share on other sites
gsg    157
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 this post


Link to post
Share on other sites
TheUnbeliever    963
Quote:
Original post by gsg
Representing 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this