Highest voted questions tagged round - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-21T14:46:37Z https://mathematica.stackexchange.com/feeds/tag?tagnames=round&sort=votes https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/203419 7 Count number of occurences of particular numbers in list Vocis https://mathematica.stackexchange.com/users/66793 2019-08-07T13:12:30Z 2019-08-07T19:08:27Z <p>I have a list of numbers which I have rounded and now all numbers are divisible by 5. </p> <pre><code> napajecky=Round[{0.227, 0.155, 0.184, 0.178, 0.248, 0.106, 0.295, 0.286, 0.126, 0.109, 0.172, 0.364, 0.131, 0.268, 0.3, 0.172, 0.324, 0.359, 0.52, 0.277, 0.169, 0.303, 0.169, 0.097, 0.265, 0.063, 0.262, 0.19, 0.095, 0.059, 0.035, 0.051, 0.163, 0.099, 0.038, 0.093, 0.16, 0.1454, 0.3803, 0.2437, 0.0359, 0.0959, 0.0346, 0.136, 0.275, 0.003, 0.004, 0.003, 0.003, 0.005, 0.1686, 0.1031}*100, 5] </code></pre> <p>Now, I need to make a new list which would count number of occurences of numbers divisible by 5 in the rounded list and link it with that particular number. So the new list would look like this e.g.: newlist={number of occurence of number 5 is 7, number of occurences of number 10 is 8,number of occurences of number 15 is 12, ... etc.}</p> <p>I tried to define function that would count these occurences in certain range (5 to 80) but it does´t work:</p> <pre><code>f[x_]:=Count[x,Range[5,80,5] res=f/@napajecky </code></pre> <p>Can you help me to find the best way? Thank you for advice.</p> https://mathematica.stackexchange.com/q/88156 6 Round vs. Floor vs. Ceiling for non-integers non-orders-of-magnitude ThomasH https://mathematica.stackexchange.com/users/14780 2015-07-13T21:47:51Z 2015-07-14T07:42:33Z <p>I understand that <code>Round</code> give the nearest even integer for cases where the number is between two integers, i.e. <code>Round[2.5] = 2</code> and <code>Round[3.5] = 4</code> (see this <a href="https://mathematica.stackexchange.com/questions/29122/why-do-numberform-and-round-apparently-use-different-tie-breaking-methods/33939#33939">question</a>). This carries over to rounding to non-integers that are not orders of magnitude as well:</p> <pre><code>Round[1.3, 0.2] = 1.2 Round[1.5, 0.2] = 1.6 Round[Range[1, 2, 0.1], 0.2] = {1., 1.2, 1.2, 1.2, 1.4, 1.6, 1.6, 1.6, 1.8, 1.8, 2.} </code></pre> <p>However, the <code>Floor</code> function seems to behave oddly in a similar situation:</p> <pre><code>Floor[Range[1, 2, 0.1], 0.2] = {1., 1., 1., 1.2, 1.2, 1.4, 1.6, 1.6, 1.8, 1.8, 2.} </code></pre> <p>but <code>Ceiling</code> does what I would expect it to do:</p> <pre><code>Ceiling[Range[1, 2, 0.1], 0.2] = {1., 1.2, 1.2, 1.4, 1.4, 1.6, 1.6, 1.8, 1.8, 2., 2.} </code></pre> <p>My question therefore is why does <code>Floor[1.2, 0.2] = 1.</code>? I understand the behavior of <code>Round</code> for this test set, but I do not understand <code>Floor</code>. Then, with <code>Floor</code> giving something I don't expect, I am very surprised that <code>Ceiling</code> gives exactly what I would expect.</p> <hr> <h2>Background for my Problem</h2> <p>I have a regular array of 2D data (<code>{x, y, probability}</code>) that I want to bin by summing, so <code>Round</code> doesn't work because different numbers of elements are included in consecutive bins because it rounds to evens, but I expected <code>Floor</code> to work.</p> <pre><code>gathered = GatherBy[predictedProbabilityDist, { Round[#[], newGridSpacing], Round[#[], newGridSpacing] } &amp;]; </code></pre> <p>versus</p> <pre><code>gathered = GatherBy[predictedProbabilityDist, { Floor[#[], newGridSpacing], Floor[#[], newGridSpacing] } &amp;]; </code></pre> <p>or the nearly equivalent expression with <code>Ceiling</code>, followed by something like</p> <pre><code>predictedProbDist = Map[ { Min[#[[All, 1]]], Min[#[[All, 2]]], Total[#[[All, 3]]] } &amp;, gathered]; </code></pre> <p>Based on the testing with the simple <code>Range</code> above, it looks like I need to use <code>Ceiling</code>, but I don't know why for sure that is the case.</p> https://mathematica.stackexchange.com/q/72428 6 Is the divergence between Round and setting an explicit precision intentional or a bug? Jinxed https://mathematica.stackexchange.com/users/24763 2015-01-24T20:13:52Z 2015-01-25T19:23:46Z <p><strong>Introductory remark:</strong> I take it (from <em>Mathematica's</em> <a href="http://reference.wolfram.com/language/tutorial/NumericalPrecision.html" rel="nofollow">online documentation</a>), that <code>Precision</code> is effectively the number of significant digits, as seen by the system.</p> <ul> <li>For literals, precision is set using backquotes, e.g. <code>2.5011</code>, which yields a <code>3.</code>. Calling <code>Precision</code> on this result gives <code>1</code>, as expected.</li> <li><p>On the other hand, there is <code>Round</code>, which, well, rounds according to the unbiased next-to-even-scheme, e.g.: </p> <p>$\quad \quad$<code>Round[{2.501, 3.501, 2.401, 3.401, 2.5, 3.5, 2.4, 3.4}]</code></p> <p>gives the integers</p> <p>$\quad \quad$<code>{3, 4, 2, 3, 2, 4, 2, 3}</code></p></li> </ul> <p><strong>But:</strong></p> <p>$\quad \quad$<code>{2.5011, 3.5011, 2.4011, 3.4011, 2.51, 3.51, 2.41, 3.41}</code><br /></p> <p>yields an unexpected difference at the 5th position:<br /></p> <p>$\quad \quad$<code>{3., 4., 2., 3., 3., 4., 2., 3.}</code></p> <p>So, specifying a precision explicitly (which should be a number of significant digits, according to documentation), gives different results than rounding the same literals to the same number of significant digits.</p> <p>While the explicit precision statement <code></code> does show the number rounded, its method is not next-to-nearest-even, but the biased next-to-larger-absolute (i.e. "up-from-0.5").</p> <p><strong>My question therefore is:</strong></p> <p>Did I miss some of the intricacies of the assorted norms or should <em>Mathematica</em> round the same, regardless of how the number of significant digits is specified (<code>Round</code> or <code>1 (* or more, according to magnitude before the decimal separator *)</code>)?</p> <p><strong>Further findings</strong></p> <p><code>2.51*2.1</code> results in a displayed <code>0</code> with <code>Precision</code> yielding not <code>1</code>, as expected, but <code>0.69897</code>. Checking further using</p> <pre><code>{#, Round[#], Accuracy[#], Precision[#]}&amp;/@{2.51, 2.1, 2.51+2.1, 2.51*2.1} </code></pre> <p>which leads to</p> <p>$$\begin{array}{cccc} \bf input&amp; \bf output&amp; \bf Round&amp;\bf Accuracy&amp;\bf Precision\\ 2.51&amp; 2. &amp; 2 &amp; 0.60206 &amp; 1. \\ 2.1&amp; 2. &amp; 2 &amp; 0.69897 &amp; 1. \\ 2.51+2.1&amp; 4. &amp; 4 &amp; 0.346787 &amp; 1. \\ 2.51*2.1&amp; 0. &amp; 5 &amp; 0. &amp; 0.69897 \\ \end{array}$$</p> <p>adds to my confusion about the semantics of significant digits in <em>Mathematica</em>.</p> <p>Reference: <a href="http://reference.wolfram.com/language/tutorial/NumericalPrecision.html" rel="nofollow"><code>NumericalPrecision</code></a></p> <hr> <blockquote> <p><strong>Extension/Background for the Question</strong></p> <p>The reason for me asking this question is, that I wanted to implement calculations with <em>Mathematica</em>, which obey the rule, that any (final) results shall be displayed with the minimum number of significant digits of all values/measurements used as input.</p> <p>After consulting the <a href="http://reference.wolfram.com/language/tutorial/NumericalPrecision.html" rel="nofollow">documentation</a>, I thought, that setting an input value's precision explicitly would have been the key to this.</p> <p>Now, however, I am at a loss, since</p> <ol> <li>the rounding behavior of the frontend differs from <code>Round</code></li> <li>arithmetic operations like multiplications change the precision of the result in forseeable, but practically less usable ways.</li> </ol> <p>Obviously, I will have to turn to the hard way and track the number of significant digits myself and round as appropriate using <code>Round</code>.</p> </blockquote> https://mathematica.stackexchange.com/q/107812 4 Opposite Collatz Conjecture Dops https://mathematica.stackexchange.com/users/37858 2016-02-19T20:46:24Z 2016-02-20T07:08:20Z <p>I'm trying to explore what would happen if you change the collatz conjecture to flip the equations for odds and evens. So if you have an even number you multiply by 3, add 1, and divide by 2. If you have an odd number, divide it by 2. I understand that this will leave you with some decimal answers so I'm deciding to round up. I'm trying to write this program in <em>Mathematica</em> however am running into trouble. Here is my code: </p> <pre><code>flipCollatz[n_] := If[EvenQ[n], ((3 n + 1)/2), (n/2)] flipOrbit[n_] := Module[{flipVals = {n}, n0 = n}, While[n0 &gt; 1, n0 = flipCollatz[n0]; If[IntegerQ[n0], n0 = n0, n0 = (n0 + 0.5)]; flipVals = Append[flipVals, n0]; ]; flipVals ] flipOrbit </code></pre> <p>It's giving me the wrong output. I should be getting 10,16,25,13,7,4,7,4,...</p> <p>Any ideas?</p> <p>Thanks much!</p> https://mathematica.stackexchange.com/q/192843 3 Problems with rounding giving too many digits Wombles https://mathematica.stackexchange.com/users/52818 2019-03-07T23:29:08Z 2019-03-08T05:41:53Z <p>My current code is:</p> <pre><code>round = .1; f[x_] := x + 4 - Sqrt[3 x^2 - 5] xx = Solve[f[x] == 0, Reals]; StringForm["x=1", If[Length[xx] &gt;= 1, Round[Max[xx[[All, 1, 2]]], round], "none"]] </code></pre> <blockquote> <p>x=5.800000000000001</p> </blockquote> <p>which i want to give me an output of "x = 5.8" instead.</p> <p>Any advice would be appreciated.</p> https://mathematica.stackexchange.com/q/71359 2 Rounding to the nearest decimal and pasting such output into an Input cell David G. Stork https://mathematica.stackexchange.com/users/9735 2015-01-08T20:14:10Z 2015-01-09T01:44:48Z <p>This question is very closely related to two earlier ones:</p> <p><a href="https://mathematica.stackexchange.com/questions/32597/rounding-to-the-nearest-decimal">Rounding to the nearest decimal</a></p> <p>and</p> <p><a href="https://mathematica.stackexchange.com/questions/7871/how-do-you-round-numbers-so-that-it-affects-computation">How do you round numbers so that it affects computation?</a></p> <p>but confronts an additional issue not addressed by those answers. (Incidentally, the only way you can appreciate my question is by running code on your machine... merely inspecting the below does not suffice.)</p> <p>I want to generate a fixed list of random numbers (from a distribution, but the below illustrates the problem), but for space and readability, I want to keep only three digits to the right of the decimal point. This code</p> <pre><code>Round[#, .001] &amp; /@ RandomReal[{1, 10}, 3] </code></pre> <p>gives the following output (which I typed in by hand):</p> <p>(* {9.41, 7.26, 4.298} *)</p> <p>This looks fine. However, if you simply copy everything from that output cell and it paste into an input cell, you get "" markers after each number, and in some cases many more digits than the three desired.</p> <p>I've tried all manner of <code>Round</code>, <code>Ceiling</code> and <code>Floor</code>, such as:</p> <pre><code>N@(Round[1000 #] &amp; /@ RandomReal[{1, 10}, 3])/1000 </code></pre> <p>and</p> <pre><code>myRound[x_, n_] := Ceiling[10^n x]/10^n // N; myRound[#, 3] &amp; /@ RandomReal[{1, 10}, 3] </code></pre> <p>and can get appropriate outputs, which show three digits, as desired. Nevertheless, in every case when I cut such output and paste it into an input cell, the "" markers or extraneous digits appear.</p> <p>How do I get lists of "true" fixed-digit numbers for input cells?</p> https://mathematica.stackexchange.com/q/193412 2 Efficient Round edition with different rounding direction matheorem https://mathematica.stackexchange.com/users/4742 2019-03-17T11:54:38Z 2019-03-17T12:33:15Z <p>As pointed out in <a href="https://mathematica.stackexchange.com/q/2116/4742">this post</a>, Mathematica has a special version of <code>Round</code> that </p> <blockquote> <p>Round rounds numbers of the form x.5 toward the nearest even integer.</p> </blockquote> <p>A comment by David G suggest that why not have differnt options <code>Direction → {"HalfDown","HalfUp","HalfEven","HalfOdd","Stochastic"}</code></p> <p>These days I need a version of Round to HalfUp. I write a quite ugly and slow function as below</p> <pre><code>myRound[x_, d_] := Module[{}, c1 = (1./d)*10; c2 = 1./d; theDigit = Last@IntegerDigits@IntegerPart[x*c1]; If[theDigit &gt;= 5, InternalStringToDouble@ToString@N[(IntegerPart[x*c2] + 1)/c2], InternalStringToDouble@ToString@N[(IntegerPart[x*c2])/c2]]] </code></pre> <p>speed test</p> <pre><code>In:= myRound[#, 0.01] &amp; /@ RandomReal[1., 1000000]; // AbsoluteTiming Out= {30.7072, Null} In:= Round[#, 0.01] &amp; /@ RandomReal[1., 1000000]; // AbsoluteTiming Out= {0.285921, Null} </code></pre> <p>So I am wondering if someone on this site already have developed an efficient toolkit for round matters?</p> https://mathematica.stackexchange.com/q/108374 2 Create an accumulator array from a function EinsL https://mathematica.stackexchange.com/users/20097 2016-02-25T21:35:36Z 2016-02-25T23:01:44Z <p>I am making some images for a project about the hough transform line detection, I am showing the slope-intercept form , the normal form and their accumulator arrays to show the peaks. I am kind of new to Mathematica.</p> <pre><code>Data = Table[{x, x + 1}, {x, -5, 5}]; Show[ ListPlot[Data, PlotMarkers -&gt; {Automatic, 11}, Joined -&gt; False] ] pSIF[m_, x_, y_] := -x*m + y pNF[t_, x_, y_] := x*Cos[t] + y*Sin[t] Show[ Plot[Apply[pSIF[m, #1, #2] &amp;, Data, {1}], {m, -2.5, 2.5}, Evaluated -&gt; True, PlotRange -&gt; {Automatic, Automatic}] ] Show[ Plot[Apply[pNF[m, #1, #2] &amp;, Data, {1}], {m, 0, Pi}, Evaluated -&gt; True, PlotRange -&gt; {{0, Pi}, Automatic}] ] </code></pre> <p>I have done the slope-intercept and normal form plots, but I don't know how to quantize the plot and make the accumulator array. I am trying to round it and move it to a table but without success.</p> <pre><code>Show[ Plot[Round[Apply[pNF[m, #1, #2] &amp;, Data, {1}]], {m, 0, Pi}, Evaluated -&gt; True, PlotRange -&gt; {{0, Pi}, Automatic}] ] </code></pre> https://mathematica.stackexchange.com/q/127142 2 Why does Mathematica round numbers in this way where the fractional part is 0.5? [duplicate] Mats Granvik https://mathematica.stackexchange.com/users/328 2016-09-25T07:43:05Z 2016-09-25T15:24:28Z <div class="question-status question-originals-of-duplicate"> <p>This question already has an answer here:</p> <ul> <li> <a href="/questions/2116/why-round-to-even-integers" dir="ltr">Why round to even integers?</a> <span class="question-originals-answer-count"> 4 answers </span> </li> </ul> </div> <p>I was trying to comment on sequence <a href="https://oeis.org/A047229" rel="nofollow">https://oeis.org/A047229</a> in the OEIS, when one of the editors pointed out that the rounding is done wrong in this Mathematica one-liner:</p> <pre><code>Table[Round[(3*n - 3)/2], {n, 1, 57}] </code></pre> <p>In Mathematica I get the following results:</p> <pre><code>Round[1.5] = 2 Round[4.5] = 4 Round[7.5] = 8 Round[10.5] = 10 </code></pre> <p>In school I was taught that you should round fractions equal to 0.5 up and that the correct answers are:</p> <pre><code>Round[1.5] = 2 Round[4.5] = 5 Round[7.5] = 8 Round[10.5] = 11 </code></pre> <p>Is this a bug or a feature?</p> https://mathematica.stackexchange.com/q/92485 1 Keep leading/trailing zeroes (like Defer except with numbers) in a given number hedgepig https://mathematica.stackexchange.com/users/32652 2015-08-27T21:53:33Z 2015-08-29T02:53:29Z <p>I'm trying to write a function that counts the number of significant figures a given number has so I can ensure my calculations are rounded correctly; however, I am having a lot of trouble preventing the formatting engine from changing my inputs. For instance,<code>Defer[00001.343200]</code> returns <code>1.3432</code> when I would like a way for the output to keep the raw input of a number as the original <code>00001.343200</code> or, failing that, <code>1.343200</code>. <code>ToString</code> does the same thing. Phrased differently, I would like to suppress whatever is converting my input with leading and or trailing zeroes to an output that lacks leading and trailing zeroes.</p> <p>Does anyone know how to do this, or find an equivalent solution?</p> https://mathematica.stackexchange.com/q/188478 1 Rounding a number to decimal places and keeping a trailing zero Q.P. https://mathematica.stackexchange.com/users/27119 2018-12-27T22:42:58Z 2018-12-30T19:10:59Z <p>It's often useful for me to round numbers to some decimal place for printing on a graph or figure. I usually do this by:</p> <pre><code>RoundedNumber = Round[NumberToRound, 0.001] </code></pre> <p>But if the resultant rounding is say <code>1.2</code> from <code>1.19</code>, I would like to keep the trailing zero; so <code>1.20</code>. The reason being is that I represent by errors in bracketed notation -- where the number in brackets represents the error in the last digit. E.g. <code>1.20(2)</code> -- so as you can see it is important to keep the trailing zero as <code>1.2(2)</code> has a different meaning to <code>1.20(0)</code></p> <p>Does anyone a way I can do this?</p> https://mathematica.stackexchange.com/q/137103 1 How can I Influence the number of digits when printing a polynomial expression or when I want to make it a String? Adalbert Hanßen https://mathematica.stackexchange.com/users/21390 2017-02-06T16:59:03Z 2017-02-06T20:53:12Z <p>I have an expession (it is the result of Fit) but I want to print or display an abbreviated form with all numbers in it rounded to say 6 decimal digits. </p> <p><code>Print[N[polynomial, 6]]</code> does not what I want and ToString also has no option to limit the number of digits.</p> <p>I rather get an expression with <code>0.0008834432170369743*d</code> in it (the other coefficients also have physically meaningless digits which I would like to get rid of).</p> <p><strong>How can I transform an expression to a string limiting all numerical constants in it to some given number of relevant digits?</strong></p> <p>I further want to use such a string for fit in a constuct like this:</p> <blockquote> <pre><code> Show[{ Graphics[myPlot] , Graphics[{Black , Text[fit , {2, 1} , {-1, 0} ] } ] } ] </code></pre> </blockquote> https://mathematica.stackexchange.com/q/110853 1 Chop not working [closed] Laura McMullen https://mathematica.stackexchange.com/users/36067 2016-03-23T20:40:15Z 2016-03-23T23:27:21Z <p>I am running a function that has a large output in matrix format with small, complex parts added to the numbers- basically a rounding error. I am trying to use Chop to obtain use-able results, but when I highlight Chop[%] and choose Evaluate Cells from the drop down, the program just beeps and highlights the last original output bracket in neon green. Does anyone have any ideas on this? Thanks much,</p> <p>Laura<a href="https://i.stack.imgur.com/YqKXJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YqKXJ.png" alt="screenshot"></a></p> https://mathematica.stackexchange.com/q/91420 0 Why doesn't the Round function work on decimals and how to fix it or work around it [duplicate] Tom Mozdzen https://mathematica.stackexchange.com/users/32343 2015-08-12T00:51:49Z 2015-08-12T03:14:58Z <div class="question-status question-originals-of-duplicate"> <p>This question already has an answer here:</p> <ul> <li> <a href="/questions/65298/how-to-prevent-round-with-hided-fractions" dir="ltr">How to prevent Round with hided fractions</a> <span class="question-originals-answer-count"> 2 answers </span> </li> <li> <a href="/questions/72454/permanently-rounding-numbers-values-to-a-given-number-of-significant-figures" dir="ltr">Permanently rounding numbers/values to a given number of significant figures</a> <span class="question-originals-answer-count"> 2 answers </span> </li> </ul> </div> <p>I'm trying to specify yaxis tick labels from 0 to 1.0 in steps of 0.1. This is what I get:</p> <pre><code>ygrids=Range[0,1.0,0.1]; 0.,0.1,0.2,0.30000000000000004,0.4,0.5,0.6000000000000001,0.7000000000000001,0.8,0.9,1.} </code></pre> <p>Round[ygrids,0.01] doesn't fix it. (0.001 fixes all but the 7.0000x)</p> <p>This is too simple to be this hard!</p> <p>Help appreciated (version 10.1)</p> https://mathematica.stackexchange.com/q/188607 0 Return the precision of the first non zero digit in a number for measurement error representation function Q.P. https://mathematica.stackexchange.com/users/27119 2018-12-30T16:51:57Z 2018-12-31T11:54:41Z <p>I'd like to return the precision of the first non zero digit in a number. The motivation is so that I can quickly construct my error notation which is the bracketed notation, e.g. <span class="math-container">$x = 1.234$</span> with associated error <span class="math-container">$\delta x = 0.0015$</span> I would represent the measurement as <span class="math-container">$1.23(2)$</span>. I want to automate this so I would:</p> <ul> <li><p>Determine the precision of the error -- round up by the next digit. So in pseudo code:</p> <p><code>DeltaX = 0.0015</code> <code>DeterminePrecissionValue[DeltaX] (*Would return: 3*)</code> <code>NumberForm[Numb, {DeltaX, 3}]</code> <code>NumberForm[Numb, {X, 3}]</code></p></li> <li><p>Based on the above result I would then round the measurement value to the precision of the error result and then format accordingly.</p></li> </ul>