MathGroup Archive 2004

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

Search the Archive

rounding

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49469] rounding
  • From: Amit Gandhi <akgandhi at uchicago.edu>
  • Date: Wed, 21 Jul 2004 06:39:39 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com


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.


  • Prev by Date: AW: 7 equations and 7 unknowns (non-linear equations)
  • Next by Date: Plotting a function and its derivative
  • Previous by thread: AW: 7 equations and 7 unknowns (non-linear equations)
  • Next by thread: Re: rounding