MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Exact real numbers

Don't get distracted by the binary representation vs. decimal notation debate. If you want to identify points in space exactly, so that equality and inequality are precise, then Rationalize your inputs. There was a recent thread on computing the smallest circle that enclosed a set of points, in which Rationalize was very useful. (Google for "FindMinimum and the minimum-radius circle".)

Rather than the point {1.563,2.4,1.7856} use the point

Rationalize[{1.563, 2.4, 1.7856}]
{1563/1000, 12/5, 1116/625}


Round[1000*{1.563, 2.4, 1.7856}]/1000
{1563/1000, 12/5, 893/500}


Rationalize[{1.563, 2.4, 1.7856}, 10^(-4)]
{186/119, 12/5, 1116/625}

Choose the method you like best. You can quibble over which, if any, of those is "rounded to 3 decimals", but it really doesn't matter. Your stated purpose is to know, unambiguously, whether two input numbers or points are the same. Rationalize them the same way, and there should be no doubt.

For showing results, use NumberForm.

One caveat: exact arithmetic (in any computer algebra system or language, binary or decimal, packed decimal or whatever) is much slower than floating point arithmetic. If you do a LOT of computations with Rational numbers, you can end up with denominators bigger than your head and things will slow down. Such is life!

It wasn't an issue in the smallest-circle problem, and maybe it won't be in your problem, either.


On Wed, 8 Sep 2004 05:15:01 -0400 (EDT), Ross Sean Civ AFRL/DELO <sean.ross at> wrote:

> I have been having an on-going problem with real numbers in Mathematica that
> has come up again.  In short, I can find no way of "rounding" a number in
> Mathematica.  To me, when I say, round 1.56012309843 to three decimal
> places, I want the result to be 1.560 exactly.  No hanging 00000000001's .
> The reason for this is that I need to do 3-D logic involving the location of
> points in space.  I don't like getting faked out by numbers that ought to be
> the same but are slighly unequal due to rounding errors.
> Round only applies to integers.  Multiplying the results of Round by machine
> precision reals results in all the hanging digits I want to get rid of.
> SetPrecision and SetAccuracy don't seem to do what I want.
> SetPrecision[1.5000287965,5] returns a result with more than 5 digits to the
> right of the decimal as does SetAccuracy.
> RealDigits is great in that I can actually get a list of all digits and
> manipulate them, however, its "inverse" FromDigits returns an F**ing
> fraction.  I don't want a fraction.  I want an exact decimal.  I also prefer
> function pairs that are exact inverses to one another.
> One strange assymetry of Mathematica is that I can type in the exact decimal
> number 1.50027` and Mathematica knows that is an exact decimal.  FullForm
> reveals that the only decimal digits in that number are what I specified.
> However, I know of no way to create an exact decimal from a machine
> precision one.
> Does anyone know of a way to do true decimal rounding in Mathematica?
> Please respond to sean.ross at  I no longer subscribe to
> mathgroup.
> Dr. Sean Ross
> 3550 Aberdeen Ave SE, Building 761
> Kirtland AFB, NM 87117
> phone: 505-846-9148
> fax: 505-853-0485

DrBob at

  • Prev by Date: Re: Re: plot thousands(?) of trajectories in single graph.
  • Next by Date: Re: plot thousands(?) of trajectories in single graph.
  • Previous by thread: Re: Exact real numbers
  • Next by thread: Re: Exact real numbers