Comments on: Rounding Results https://blogs.mathworks.com/loren/2009/09/03/rounding-results/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Mon, 11 Jun 2018 16:19:35 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Loren https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-32086 Tue, 08 Mar 2011 20:06:05 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-32086 Mark-

I suspect it’s never been added because it’s not too hard to write the code, and I don’t think very many people have requested it. If you would like to see a version shipping with MATLAB, use the link on the right of my blog to contact technical support and put in your plea for an enhancement.

Finally, you can check out the File Exchange. When I searched for rounding or rounding significant, I found quite a few possible solutions (with some other stuff also mixed in.

–Loren

]]>
By: Mark https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-32085 Tue, 08 Mar 2011 19:46:37 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-32085 Why does MatLab not have a floating point rounding function to round to 1 – 6 places to the right of the decimal point?

I had to write my own and using the basic (x*10^n/10^n) where n is the number of decimal places does not work reliably (round numbers like 3.44545 to one decimal place 3.5)

]]>
By: Loren https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30933 Mon, 04 Jan 2010 14:05:27 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30933 Drazick, OysterEngineer, and possibly others,

Because of the nature of floating point arithmetic, getting exactly zero every time is not possible unless perhaps you can control every aspect of the calculations, order, whether on the stack, etc. In which case, MATLAB is possibly not the best tool if you override all operations.

–Loren

]]>
By: Drazick https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30919 Mon, 28 Dec 2009 18:10:05 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30919 Loren,
I agree with OysterEngineer.
Sometimes, because of finite accuracy of Matlab I get results such as 3E-16. I want it to be exactly 0. It would be nice to such function.

Though it can be done by round.

Thanks.

]]>
By: Loren https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30710 Wed, 28 Oct 2009 11:43:02 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30710 OysterEngineer-

I recommend you use the support link on the right to enter your suggestion for round. Suggestions directly from customers tend to carry more weight. Thanks.

–Loren

]]>
By: OysterEngineer https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30708 Tue, 27 Oct 2009 20:49:30 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30708 The illustrates why MatLab needs a smarter round function, much like even lowly Excel has, where the user can specify the number of digits to which you want to round to.

]]>
By: Loren https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30647 Mon, 28 Sep 2009 15:55:18 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30647 Craig-

I’m glad rounding works for you. Are you rounding to tenths for your work? Is your target value always in in tenths? Another possibility is to check if the calculated value is within a certain distance from the spec (e.g., abs(spec-calc) < SomeTolerance). --Loren

]]>
By: Craig Miller https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30608 Mon, 14 Sep 2009 19:39:20 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30608 Loren,
I use rounding for calculations and comparisions with specification limits. For example if the lower spec limit is 1.8 and the calculated value is 1.78254343 I want this value to pass because it rounds to 1.8 which is not less than the specification limit. This may not be ‘correct’ but it’s the way we’ve done our spec checking since time immemorial (in computer terms anyway).
Craig

]]>
By: Loren https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30576 Tue, 08 Sep 2009 11:07:43 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30576 Ben-

Obviously you still need to be aware of numerical round-off in the calculations done before rounding in the first place. And using floor instead of round may not be what you want to do.

–Loren

]]>
By: Ben https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30575 Tue, 08 Sep 2009 02:12:29 +0000 https://blogs.mathworks.com/loren/2009/09/03/rounding-results/#comment-30575 >> floor(22*(15/22)) ans = 14 >> floor(49*(1/49)) ans = 0 ]]>