Re: Input accuracy
- To: mathgroup at yoda.ncsa.uiuc.edu
- Subject: Re: Input accuracy
- From: jacobson at cello.hpl.hp.com
- Date: Fri, 09 Nov 90 09:10:49 PST
Keith Slavin, writes:
> I have recently been trying to get mathematica to accept an input of GIVEN
> accuracy, ie: Foo[1.3,25] implies 1.300000000000000000000000, ie: 25 digit
> precision according to the Precision[] function, otherwise the
> OUTPUT accuracy suffers due to the 'machine accuracy' assumed for
> '1.3'.
> ...
> Any ideas?
Yes, this does it. (I've called it PNum instead of Foo.)
The first definition accepts a string input, so you would write:
PNum["1.3",25]. For easier input when rationalize would work, I added
the second definition, which takes a number and calls Rationalize.
The third is just there so it works on integers as well.
PNum[st_String, n_] := \
SetPrecision[ToExpression[StringJoin[st,
Apply[StringJoin, Table["0", {Max[n, Precision[1.] + 5]}]]]], n]
PNum[x_Real, n_] := \
SetPrecision[N[Rationalize[x], Max[n, Precision[1.] + 5]], n]
PNum[x_Integer, n_] := SetPrecision[N[x, Max[n, Precision[1.] + 5]], n]
Big floats are extremely dangerous in Mathematica. Caveat Emptor!
(There are many iterative calculations that ratchet down the precision
by 1 each iteration, and Mathematica uses the precision to determine
the number of bits in the representation. So when you are done you
have a totally meaningless result. Mathematica also has some bugs
that let the precision get too big, so don't trust what you get. If
you can live with their accuracy/precision, machine numbers are much
safer.)
-- David Jacobson