[Date Index]
[Thread Index]
[Author Index]
Re: Exact real numbers
*To*: mathgroup at smc.vnet.net
*Subject*: [mg50551] Re: Exact real numbers
*From*: Paul Abbott <paul at physics.uwa.edu.au>
*Date*: Thu, 9 Sep 2004 05:19:43 -0400 (EDT)
*Organization*: The University of Western Australia
*References*: <chmntb$9sf$1@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
In article <chmntb$9sf$1 at smc.vnet.net>,
Ross Sean Civ AFRL/DELO <sean.ross at kirtland.af.mil> 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 .
You could achieve this using, say,
myRound[x_?NumericQ, n_:3] := SetPrecision[ Round[10^n x]/10^n, n + 1]
to round, by default, to 3 decimal places, but this would invoke
arbitrary precision arithmetic, which is usually undesirable.
> 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.
There is no need to use Round if your goal is to test for equality. Use
Chop instead. For example,
Compare[a_, b_, n_:3] := Chop[a - b, 5 10^(-n-1)] == 0
Compare[1.56012309843, 1.560]
True
Compare[1.56012309843, 1.561]
False
Compare[1.56012309843, 1.561, 2]
True
This avoids the need for arbitrary precision arithmetic.
Cheers,
Paul
--
Paul Abbott Phone: +61 8 9380 2734
School of Physics, M013 Fax: +61 8 9380 1014
The University of Western Australia (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009 mailto:paul at physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul
Prev by Date:
**Re: sorry, but more q's on random numbers**
Next by Date:
**Re: How to solve a simple Trig cofunction?**
Previous by thread:
**Re: Exact real numbers**
Next by thread:
**Re: Exact real numbers**
| |