<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169196</link>
    <title>MATLAB Central Newsreader - decimal precision</title>
    <description>Feed for thread: decimal precision</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 13 May 2008 18:31:08 -0400</pubDate>
      <title>decimal precision</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169196#431871</link>
      <author>Young Ryu</author>
      <description>Hi-&lt;br&gt;
&lt;br&gt;
I'd like to round at 4th decimal point. For example, &lt;br&gt;
&lt;br&gt;
1.41536664&lt;br&gt;
=&amp;gt; 1.415&lt;br&gt;
&lt;br&gt;
how can I round at specific precision? There is no syntax  &lt;br&gt;
in "round" function.&lt;br&gt;
&lt;br&gt;
Thanks!&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 13 May 2008 18:43:03 -0400</pubDate>
      <title>Re: decimal precision</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169196#431875</link>
      <author>jay vaughan</author>
      <description>"Young Ryu" &amp;lt;ryuyr77@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;g0cmpc$gjg$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi-&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'd like to round at 4th decimal point. For example, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 1.41536664&lt;br&gt;
&amp;gt; =&amp;gt; 1.415&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; how can I round at specific precision? There is no syntax  &lt;br&gt;
&amp;gt; in "round" function.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks!&lt;br&gt;
&lt;br&gt;
n = 1.41536665;&lt;br&gt;
% you could multiply, round, then divide&lt;br&gt;
num_dig = 4;&lt;br&gt;
n_rounded = round(n*(10^num_dig))/(10^num_dig);&lt;br&gt;
&lt;br&gt;
% or for display purposes only you can use&lt;br&gt;
num2str(n,'%.4f')&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 13 May 2008 18:57:24 -0400</pubDate>
      <title>Re: decimal precision</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169196#431879</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;g0cmpc$gjg$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
Young Ryu &amp;lt;ryuyr77@gmail.com&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;I'd like to round at 4th decimal point. For example, &lt;br&gt;
&lt;br&gt;
&amp;gt;1.41536664&lt;br&gt;
&amp;gt;=&amp;gt; 1.415&lt;br&gt;
&lt;br&gt;
&amp;gt;how can I round at specific precision?&lt;br&gt;
&lt;br&gt;
It is not (generally) possible to round fractions at a decimal precision,&lt;br&gt;
The exceptions have to do with rounding to integers divided by exact powers&lt;br&gt;
of 2, such as 1/2, 1/4, 3/4, and so on.&lt;br&gt;
&lt;br&gt;
There is no exact representation in binary floating point for 0.1 (1/10),&lt;br&gt;
just as there is no exact representation in base 10 decimal expression&lt;br&gt;
for 1/3. If your number does not happen to round to one of those&lt;br&gt;
power-of-2 fractions I mentioned before, then there will always be&lt;br&gt;
something left over -- it might be 15 decimal places further down, but&lt;br&gt;
it will be there.&lt;br&gt;
&lt;br&gt;
You -can-, though, convert numbers to character strings that&lt;br&gt;
have specific numbers of decimal places, for "presentation purposes"&lt;br&gt;
as the other poster mentioned.&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;"He wove a great web of knowledge, linking everything together, &lt;br&gt;
&amp;nbsp;&amp;nbsp;and sat modestly at a switchboard at the center, eager to help."&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-- Walter Kerr&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 13 May 2008 19:22:03 -0400</pubDate>
      <title>Re: decimal precision</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169196#431885</link>
      <author>Roger Stafford</author>
      <description>"Young Ryu" &amp;lt;ryuyr77@gmail.com&amp;gt; wrote in message &amp;lt;g0cmpc$gjg&lt;br&gt;
$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi-&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'd like to round at 4th decimal point. For example, &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 1.41536664&lt;br&gt;
&amp;gt; =&amp;gt; 1.415&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; how can I round at specific precision? There is no syntax  &lt;br&gt;
&amp;gt; in "round" function.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks!&lt;br&gt;
----------&lt;br&gt;
&amp;nbsp;&amp;nbsp;You haven't made it clear whether it is only the display of a number or the &lt;br&gt;
actual number matlab is using in its computations that is to be altered.  There &lt;br&gt;
is an important difference between the two, though new users often do not &lt;br&gt;
distinguish between the two concepts.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;If you want to display a number to four significant figures, do this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;fprintf('The number n equals: %12.4e\n',n)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;On the other hand, if you want to change the number actually stored in &lt;br&gt;
matlab in this manner, do this:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;m = 4; % Number of significant decimals&lt;br&gt;
&amp;nbsp;k = fix(log10(abs(n)))-m+1;&lt;br&gt;
&amp;nbsp;n2 = round(10^k*n)/10^k;&lt;br&gt;
&amp;nbsp;fprintf('The altered number is: %30.20e\n',n2)&lt;br&gt;
&amp;nbsp;&amp;nbsp;= 1.41500000000000003600e+00&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;You will note that if you display the altered number to a sufficient accuracy, &lt;br&gt;
it is unlikely to be exactly the fraction you requested.  That is because &lt;br&gt;
matlab's 'double' format is unable to exactly represent most decimal &lt;br&gt;
fractions.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Wed, 14 May 2008 00:24:01 -0400</pubDate>
      <title>Re: decimal precision</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/169196#431927</link>
      <author>Roger Stafford</author>
      <description>"Roger Stafford" &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in &lt;br&gt;
message &amp;lt;g0cpor$h3v$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;   You haven't made it clear whether it is only the display of a number or the &lt;br&gt;
&amp;gt; actual number matlab is using in its computations that is to be altered.  &lt;br&gt;
There &lt;br&gt;
&amp;gt; is an important difference between the two, though new users often do not &lt;br&gt;
&amp;gt; distinguish between the two concepts.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   If you want to display a number to four significant figures, do this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  fprintf('The number n equals: %12.4e\n',n)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   On the other hand, if you want to change the number actually stored in &lt;br&gt;
&amp;gt; matlab in this manner, do this:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  m = 4; % Number of significant decimals&lt;br&gt;
&amp;gt;  k = fix(log10(abs(n)))-m+1;&lt;br&gt;
&amp;gt;  n2 = round(10^k*n)/10^k;&lt;br&gt;
&amp;gt;  fprintf('The altered number is: %30.20e\n',n2)&lt;br&gt;
&amp;gt;   = 1.41500000000000003600e+00&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   You will note that if you display the altered number to a sufficient &lt;br&gt;
accuracy, &lt;br&gt;
&amp;gt; it is unlikely to be exactly the fraction you requested.  That is because &lt;br&gt;
&amp;gt; matlab's 'double' format is unable to exactly represent most decimal &lt;br&gt;
&amp;gt; fractions.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Roger Stafford&lt;br&gt;
------------&lt;br&gt;
&amp;nbsp;&amp;nbsp;I was hasty in my earlier response and made three errors.  I should have &lt;br&gt;
said:&lt;br&gt;
&lt;br&gt;
1) To display n to four significant decimals, do:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;fprintf('The number n equals: %10.3e\n',n)&lt;br&gt;
&lt;br&gt;
2) To actually alter the number n to the nearest four place decimal, do:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;m = 4; % Number of significant decimals&lt;br&gt;
&amp;nbsp;k = floor(log10(abs(n)))-m+1;&lt;br&gt;
&amp;nbsp;n2 = round(n/10^k)*10^k;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;a) The most serious error I made was multiplying n by 10^k instead of &lt;br&gt;
dividing by it.  That really makes a mess of it!&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;b) However, using 'fix' instead of 'floor' is also an error, giving one too few &lt;br&gt;
significant decimal places for numbers less than 1.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;c) In 1) with %12.4e it would result in 5, not 4, significant decimals being &lt;br&gt;
displayed.&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Sorry about all that!  Perhaps I stayed up too late last night.&lt;br&gt;
&lt;br&gt;
Roger Stafford&lt;br&gt;
&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
