I'm trying to format my code in such a way that a chosen number of significant digits are saved, throughout the whole code. Basically, I'm looking for something that does the same as the function round(x, d, 'significant'), but will retain this effect during calculations. Any ideas?
Asked
Active
Viewed 96 times
0
-
2You are aware that the round off error is going to accumulate, right? e.g. if you are only interested in integers then `2.5 + 3.5` would be interpreted as `2 + 3 == 5` whereas the real result is `6` and (obviously) `5 ~= floor(6)` so the point at which you round is important. – Nicky Mattsson Nov 19 '18 at 10:03
-
Since the OP specifically asks for this effect to be retained in calculations, I'd say it is safe to assume that they're aware of rounding issues, Nicky. On topic: check out the documentation on [digits](https://ch.mathworks.com/help/symbolic/digits.html) and [vpa](https://ch.mathworks.com/help/symbolic/vpa.html) if you have access to the symbolic math toolbox. Otherwise, I would not know of a solution besides changing the precision from the standard `double` to, for instance, [`single`](https://ch.mathworks.com/help/matlab/ref/single.html). – Floris Nov 19 '18 at 14:20
-
Thanks for the quick answers. Yes, I'm aware of accumulative error. This is part of a school exercise, the target is to see how the error is affected by choosing different numbers of significant digits. From what I've read about digits and vpa, it gives the exact same result as round, but I'll be sure to try it out. – Netta Gal Nov 20 '18 at 11:25