MathGroup Archive 2004

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

Search the Archive

Re: Exact real numbers


roundReal[n_Integer?Positive][r_Real] := Round[10^n r]/10.^n

1.56012309843 // roundReal[3]

Often, with Mathematica, you will have to write routines to serve your
particular needs. One might think that Mathematica would have a built-in
routine to do rounding of Reals but I don't think it does.

When working on a subject, make a Routines Section at the front of your
notebook and as you work along you can accumulate useful routines there. You
will almost always have to program some.

David Park
djmp at

From: Ross Sean Civ AFRL/DELO [mailto:sean.ross at]
To: mathgroup at

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

Dr. Sean Ross
3550 Aberdeen Ave SE, Building 761
Kirtland AFB, NM 87117

phone: 505-846-9148
fax: 505-853-0485

  • Prev by Date: Re: sorry, but more q's on random numbers
  • Next by Date: Re: Exact real numbers
  • Previous by thread: Re: Exact real numbers
  • Next by thread: Re: Exact real numbers