MathGroup Archive 2004

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

Search the Archive

Re: rounding

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49526] Re: [mg49469] rounding
  • From: Ramesh Raju Mudunuri <rmudunuri at uh.edu>
  • Date: Thu, 22 Jul 2004 02:46:58 -0400 (EDT)
  • References: <200407211039.GAA07808@smc.vnet.net>
  • Reply-to: Ramesh Raju Mudunuri <rmudunuri at uh.edu>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,
With a little modification, your code will do what you wanted. Due to 
chopping and rounding errors your code does not work properly. 
You can use

round[x_, y_] := (Round[x 10^y] 10^(-y)) // N

where we first rationalize and then ask for the numerical value. You get the
answer you are looking for through round[mylist,2]. A more elaborate way
would be to define 
round2[x_?NumericQ, 
y_Integer?NonNegative] := ((Round[x*(10^y)])(10^(-y)) // N) /; ! IntegerQ[x]
round2[x_?NumericQ, y_Integer?NonNegative] := x /; IntegerQ[x]

and use Map as in

Map[round2[#, 2] &, mylist] 

to round mylist (Conditions have been used to define round2, to let integers
be as they are). Similar functions can be written to chop a real number to a 
given number of decimals, where IntegerPart is used instead of Round.


Ramesh



  ----- Original Message ----- 
  From: Amit Gandhi 
To: mathgroup at smc.vnet.net
  To: mathgroup at smc.vnet.net 
  Sent: Wednesday, July 21, 2004 5:39 AM
  Subject: [mg49526] [mg49469] rounding




  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.

--Boundary_(ID_hdYnexqwD6R1NGQI2YKgiw)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Verdana size=2>Hi,</FONT></DIV>
<DIV><FONT face=Verdana size=2>With a little modification, your code will do 
what you wanted. Due to </FONT></DIV>
<DIV><FONT face=Verdana size=2>chopping and rounding errors your code does not 
work properly. </FONT></DIV>
<DIV><FONT face=Verdana size=2>You can use</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>round[x_, y_] := (Round[x 10^y] 10^(-y)) // N</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Verdana size=2>where we first rationalize and then ask for the 
numerical value. You get the</FONT></DIV>
<DIV><FONT face=Verdana size=2>answer you are looking for through 
round[mylist,2]. A more elaborate way</FONT></DIV>
<DIV><FONT face=Verdana size=2>would be to define </FONT></DIV>
<P>round2[x_?NumericQ, <BR>y_Integer?NonNegative] := ((Round[x*(10^y)])(10^(-y)) 
// N) /; ! IntegerQ[x]<BR>round2[x_?NumericQ, y_Integer?NonNegative] := x /; 
IntegerQ[x]</P>
<DIV>and use Map as in</DIV>
<DIV>&nbsp;</DIV>
<DIV>Map[round2[#, 2] &amp;, mylist] </DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Verdana size=2>to round mylist (Conditions have been used to 
define round2, to let integers</FONT></DIV>
<DIV><FONT face=Verdana size=2>be as they are). </FONT><FONT face=Verdana 
size=2>Similar functions can be written to chop a real number </FONT><FONT 
face=Verdana size=2>to a </FONT></DIV>
<DIV><FONT face=Verdana size=2>given number of decimals, where IntegerPart is 
used instead of Round.</FONT></DIV>
<DIV><FONT face=Verdana size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Verdana size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Verdana size=2>Ramesh</FONT></DIV>
<DIV><FONT face=Verdana size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Verdana size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Verdana size=2></FONT>&nbsp;</DIV>
<BLOCKQUOTE 
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV 
  style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B> 
To: mathgroup at smc.vnet.net
  <A title=akgandhi at uchicago.edu href="mailto:akgandhi at uchicago.edu">Amit 
  Gandhi</A> </DIV>
  <DIV style="FONT: 10pt arial"><B>To:</B> <A title=mathgroup at smc.vnet.net 
  href="mailto:mathgroup at smc.vnet.net">mathgroup at smc.vnet.net</A> </DIV>
  <DIV style="FONT: 10pt arial"><B>Sent:</B> Wednesday, July 21, 2004 5:39 
  AM</DIV>
  <DIV style="FONT: 10pt arial"><B>Subject:</B> [mg49469] rounding</DIV>
  <DIV><BR></DIV><BR><BR>Hello Group:<BR>I was wondering if anyone might be able 
  to explain why mathematica <BR>does the following -<BR>In Mathematica I create 
  a list of numbers as follows:<BR><BR>mylist - Table[i, {i, 1., 8., 
  .2}];<BR><BR>This produces<BR><BR>{1., 1.2, 1.4, 1.6, 1.8, 2.05, 2.2, 2.4, 
  2.6, 2.8, 3., 3.2, 3.4, <BR>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, <BR>6.4, 6.6, 6.8, 7., \ 7.2, 7.4, 7.6, 7.8, 
  8.}<BR><BR>However if I try editing this output, it shows me that the full 
  form <BR>of this list is:<BR><BR>{1.`, 1.2`, 1.4000000000000001`, 1.6`, 1.8`, 
  2.05`, 2.2`, 2.4`, <BR>2.6`, \ 2.8000000000000003`, 3.`, 3.2`, 3.4`, 3.6`, 
  <BR>3.8000000000000003`, 4.`, 4.2`, \ 4.4`, 4.6000000000000005`, 4.8`, 
  <BR>5.`, 5.2`, 5.4`, 5.6000000000000005`, 5.8`, \ 6.`, 6.2`, 6.4`, 
  <BR>6.6000000000000005`, 6.8`, 7.`, 7.2`, 7.4`, \ 7.6000000000000005`, 
  <BR>7.8`, 8.`}<BR><BR>I would like this bottom list to be rounded so that the 
  full form is <BR>equivalent to the first list. So I wrote a round function 
  that <BR>rounds a real number to a specified number of decimal 
  places:<BR><BR>round[x_, place_] := 
  (Round[x*(10.^place)])/(10.^place);<BR><BR>so for instance round[3.456, 2] = 
  3.46.<BR><BR>Now when I do round[mylist, 2], the full form of the numbers in 
  the <BR>list continue to be<BR><BR>{1.`, 1.2`, 1.4000000000000001`, 1.6`, 
  1.8`, 2.05`, 2.2`, 2.4`, <BR>2.6`, \ 2.8000000000000003`, 3.`, 3.2`, 3.4`, 
  3.6`, <BR>3.8000000000000003`, 4.`, 4.2`, \ 4.4`, 4.6000000000000005`, 4.8`, 
  <BR>5.`, 5.2`, 5.4`, 5.6000000000000005`, 5.8`, \ 6.`, 6.2`, 6.4`, 
  <BR>6.6000000000000005`, 6.8`, 7.`, 7.2`, 7.4`, \ 7.6000000000000005`, 
  <BR>7.8`, 8.`}<BR><BR>Can anyone explain to me how to properly round numbers 
  in mathematica.<BR></BLOCKQUOTE></BODY></HTML>

--Boundary_(ID_hdYnexqwD6R1NGQI2YKgiw)--


  • References:
    • rounding
      • From: Amit Gandhi <akgandhi@uchicago.edu>
  • Prev by Date: RE: Plotting a function and its derivative
  • Next by Date: Fundamental theorem problem
  • Previous by thread: Re: rounding
  • Next by thread: Re: rounding