[Date Index]
[Thread Index]
[Author Index]
Re: rounding
*To*: mathgroup at smc.vnet.net
*Subject*: [mg49519] Re: [mg49469] rounding
*From*: DrBob <drbob at bigfoot.com>
*Date*: Thu, 22 Jul 2004 02:46:36 -0400 (EDT)
*References*: <200407211039.GAA07808@smc.vnet.net>
*Reply-to*: drbob at bigfoot.com
*Sender*: owner-wri-mathgroup at wolfram.com
The number 1.4, for instance, isn't exactly representable as a machine precision binary number, so what you're asking is impossible. What you've done is close enough for any reasonable numerical purpose, however; it's only the way you're displaying numbers that makes you believe there's a problem. If you only want to SEE one place after the decimal, use NumberForm:
NumberForm[data, 2]
NumberForm[data, {16, 1}]
NumberForm[roundedData = Round[10data]/10., {16, 1}]
The first two statements display the numbers without changing them in way; the last rounds them the way you already hit upon, and then displays them so that the precision problem is not made obvious. Something like the third method is best if you want to do computations with the rounded form (roundedData).
The rounded form of 1.4 will not be exactly 1.4, however. For that, you need something like:
roundedData = Rationalize[data]
{1, 6/5, 7/5, 8/5, 9/5, 41/20, 11/5, 12/5, 13/5,
14/5, 3, 16/5, 17/5, 18/5, 19/5, 4, 21/5, 22/5, 23/5,
24/5, 5, 26/5, 27/5, 28/5, 29/5, 6, 31/5, 32/5, 33/5,
34/5, 7, 36/5, 37/5, 38/5, 39/5, 8}
NumberForm[N[roundedData],{16, 1}]
NumberForm[{1., 1.2, 1.4, 1.6, 1.8, 2.05, 2.2, 2.4,
2.6, 2.8, 3., 3.2, 3.4, 3.6, 3.8, 4., 4.2, 4.4,
4.6, 4.8, 5., 5.2, 5.4, 5.6, 5.8, 6., 6.2, 6.4,
6.6, 6.8, 7., 7.2, 7.4, 7.6, 7.8, 8.}, {16, 1}]
Bobby
On Wed, 21 Jul 2004 06:39:39 -0400 (EDT), Amit Gandhi <akgandhi at uchicago.edu> wrote:
>
>
> Hello Group:
> I was wondering if anyone might be able to explain why mathematica
> does the following -
> In Mathematica I create a list of numbers as follows:
>
> mylist - Table[i, {i, 1., 8., .2}];
>
> This produces
>
> {1., 1.2, 1.4, 1.6, 1.8, 2.05, 2.2, 2.4, 2.6, 2.8, 3., 3.2, 3.4,
> 3.6, 3.8, \ 4., 4.2, 4.4, 4.6, 4.8, 5., 5.2, 5.4, 5.6, 5.8, 6., 6.2,
> 6.4, 6.6, 6.8, 7., \ 7.2, 7.4, 7.6, 7.8, 8.}
>
> However if I try editing this output, it shows me that the full form
> of this list is:
>
> {1.`, 1.2`, 1.4000000000000001`, 1.6`, 1.8`, 2.05`, 2.2`, 2.4`,
> 2.6`, \ 2.8000000000000003`, 3.`, 3.2`, 3.4`, 3.6`,
> 3.8000000000000003`, 4.`, 4.2`, \ 4.4`, 4.6000000000000005`, 4.8`,
> 5.`, 5.2`, 5.4`, 5.6000000000000005`, 5.8`, \ 6.`, 6.2`, 6.4`,
> 6.6000000000000005`, 6.8`, 7.`, 7.2`, 7.4`, \ 7.6000000000000005`,
> 7.8`, 8.`}
>
> I would like this bottom list to be rounded so that the full form is
> equivalent to the first list. So I wrote a round function that
> rounds a real number to a specified number of decimal places:
>
> round[x_, place_] := (Round[x*(10.^place)])/(10.^place);
>
> so for instance round[3.456, 2] = 3.46.
>
> Now when I do round[mylist, 2], the full form of the numbers in the
> list continue to be
>
> {1.`, 1.2`, 1.4000000000000001`, 1.6`, 1.8`, 2.05`, 2.2`, 2.4`,
> 2.6`, \ 2.8000000000000003`, 3.`, 3.2`, 3.4`, 3.6`,
> 3.8000000000000003`, 4.`, 4.2`, \ 4.4`, 4.6000000000000005`, 4.8`,
> 5.`, 5.2`, 5.4`, 5.6000000000000005`, 5.8`, \ 6.`, 6.2`, 6.4`,
> 6.6000000000000005`, 6.8`, 7.`, 7.2`, 7.4`, \ 7.6000000000000005`,
> 7.8`, 8.`}
>
> Can anyone explain to me how to properly round numbers in mathematica.
>
>
>
--
DrBob at bigfoot.com
www.eclecticdreams.net
**References**:
**rounding**
*From:* Amit Gandhi <akgandhi@uchicago.edu>
Prev by Date:
**RE: Plotting a contour plot with cylindrical co-ordinates**
Next by Date:
**RE: Plotting a function and its derivative**
Previous by thread:
**rounding**
Next by thread:
**Re: rounding**
| |