Re: Exact real numbers

*To*: mathgroup at smc.vnet.net*Subject*: [mg50548] Re: [mg50501] Exact real numbers*From*: Murray Eisenberg <murray at math.umass.edu>*Date*: Thu, 9 Sep 2004 05:19:31 -0400 (EDT)*Organization*: Mathematics & Statistics, Univ. of Mass./Amherst*References*: <200409080915.FAA08526@smc.vnet.net>*Reply-to*: murray at math.umass.edu*Sender*: owner-wri-mathgroup at wolfram.com

Is this the sort of thing you want? roundReal[x_, n_] := 10.^(-n) Round[10^n x] roundReal[1.56012309843, 3] 1.56 NumberForm[%, 20] 1.56 Ross Sean Civ AFRL/DELO 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 kirtland.af.mil. I no longer subscribe to > mathgroup. > > Dr. Sean Ross > AFRL/DELO > 3550 Aberdeen Ave SE, Building 761 > Kirtland AFB, NM 87117 > > phone: 505-846-9148 > fax: 505-853-0485 > > -- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305

**References**:**Exact real numbers***From:*Ross Sean Civ AFRL/DELO <sean.ross@kirtland.af.mil>