J Fabian Meier
J Fabian Meier

Reputation: 35805

Keep track of maximal error in double arithmetic

A double a represents a given rational/real number up to a maximal error of epsilon. If I add two doubles, the maximal error is also added up. For multiplication I can also give estimates for the maximal error.

Is there a reasonable way to keep track of this arithmetic errors? I would like to know the level of preciness with which I know a given number.

I thought I construct a datatype of two rational numbers (main number and error) and define operations on this datatype, but maybe I am just reinventing something which already exists, or my concept is too complicated.

Upvotes: 2

Views: 66

Answers (1)

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

There are two solutions that need only existing libraries.

If your numbers are always rational, you can use a rational arithmetic library. They store each number as the ratio of a pair of integers. Rational number arithmetic, given only rational calculations, should be exact. Wikipedia's List of arbitrary-precision arithmetic software lists several Java libraries that support rationals.

BigDecimal, which has been mentioned in comments, can be used to get increased precision, but is only exact for finite length decimal fractions. You can exactly calculate 1/3 in rational arithmetic, but only approximate it in BigDecimal.

Rational arithmetic depends for exactness on all numbers being exactly represented by a rational. That can break down on computing e.g. a square root. Interval arithmetic is an alternative that does not give you exactness but does let you know the range of possible rounding error. Each variable is represented by an interval that is known to contain the real arithmetic value.

Upvotes: 1

Related Questions