# round() function does not work as expected

10 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

John D'Errico
on 3 Dec 2023

Edited: John D'Errico
on 3 Dec 2023

Sorry, but it is often the case that your vector X is not what you thought it was, or you have written a function named round.

X = [16.9900000000000 + 0.00000000000000i 9.49188888257731 + 1.38777878078145e-16i 1.55759614170397 - 1.11022302462516e-16i 3.03346810679636 - 2.77555756156289e-16i 8.03389530551741 + 4.16333634234434e-17i]

Y = round(real(X),1)

If you got something else, then perhaps you have written a function round of your own that you have forgotten about, or the vector X is not what you claim it to be. There are essentially no other options. (Ok, maybe you have downloaded a function by that name, but I would put that under second case, where you have your own round function.) You can check for the existence of a different round function by doing this:

which round -all

If you have your own function named round on top of the search path, then rename it, as round is a terribly useful tool.

##### 2 Comments

Voss
on 3 Dec 2023

The rounded output is equal to the expected output.

x_in = [5.84000000000000 -0.0700000000000000 -0.730000000000000 1.18000000000000 0.490000000000000 -0.170000000000000 0.550000000000000 0.120000000000000 -0.0900000000000000 0.740000000000000 0.570000000000000 0.420000000000000 -0.520000000000000 0.0700000000000000 0.830000000000000 0.360000000000000 0.420000000000000 0.860000000000000 0.460000000000000 -0.0600000000000000 0.0800000000000000 -0.220000000000000 -0.0600000000000000 0.580000000000000 -0.510000000000000 0.110000000000000 0.460000000000000 -0.510000000000000 0.140000000000000 -1.10000000000000 0.800000000000000 0.440000000000000 -0.130000000000000 0.440000000000000 0.800000000000000 -1.10000000000000 0.140000000000000 -0.510000000000000 0.460000000000000 0.110000000000000 -0.510000000000000 0.580000000000000 -0.0600000000000000 -0.220000000000000 0.0800000000000000 -0.0600000000000000 0.460000000000000 0.860000000000000 0.420000000000000 0.360000000000000 0.830000000000000 0.0700000000000000 -0.520000000000000 0.420000000000000 0.570000000000000 0.740000000000000 -0.0900000000000000 0.120000000000000 0.550000000000000 -0.170000000000000 0.490000000000000 1.18000000000000 -0.730000000000000 -0.0700000000000000];

X_out_expected = [17 9.50000000000000 1.60000000000000 3 8 2.50000000000000 3 3.50000000000000 10 0 2 2 7 7 8.50000000000000 8 0 9.50000000000000 8 15 9 8 8.50000000000000 13.5000000000000 0 8.50000000000000 0 3.50000000000000 7 2 7.50000000000000 0 6 0 7.50000000000000 2 7 3.50000000000000 0 8.50000000000000 0 13.5000000000000 8.50000000000000 8 9 15 8 9.50000000000000 0 8 8.50000000000000 7 7 2 2 0 10 3.50000000000000 3 2.50000000000000 8 3 1.60000000000000 9.50000000000000];

X_out = FFT(x_in);

X_out_rounded = round(X_out,1);

isequal(X_out_rounded, X_out_expected)

function X = FFT(x)

N = length(x);

if N <= 1

X = x;

return;

end

X_even = FFT(x(1:2:end));

X_odd = FFT(x(2:2:end));

factor = exp(-2j * pi * (0:N/2-1) / N);

X = [X_even + factor .* X_odd , X_even - factor .* X_odd];

end

John D'Errico
on 3 Dec 2023

Edited: John D'Errico
on 3 Dec 2023

Surely you could have tested this yourself right here?

x = [5.84000000000000 -0.0700000000000000 -0.730000000000000 1.18000000000000 0.490000000000000 -0.170000000000000 0.550000000000000 0.120000000000000 -0.0900000000000000 0.740000000000000 0.570000000000000 0.420000000000000 -0.520000000000000 0.0700000000000000 0.830000000000000 0.360000000000000 0.420000000000000 0.860000000000000 0.460000000000000 -0.0600000000000000 0.0800000000000000 -0.220000000000000 -0.0600000000000000 0.580000000000000 -0.510000000000000 0.110000000000000 0.460000000000000 -0.510000000000000 0.140000000000000 -1.10000000000000 0.800000000000000 0.440000000000000 -0.130000000000000 0.440000000000000 0.800000000000000 -1.10000000000000 0.140000000000000 -0.510000000000000 0.460000000000000 0.110000000000000 -0.510000000000000 0.580000000000000 -0.0600000000000000 -0.220000000000000 0.0800000000000000 -0.0600000000000000 0.460000000000000 0.860000000000000 0.420000000000000 0.360000000000000 0.830000000000000 0.0700000000000000 -0.520000000000000 0.420000000000000 0.570000000000000 0.740000000000000 -0.0900000000000000 0.120000000000000 0.550000000000000 -0.170000000000000 0.490000000000000 1.18000000000000 -0.730000000000000 -0.0700000000000000];

X = FFT(x)

Xr = round(real(X),1)

function X = FFT(x)

N = length(x);

if N <= 1

X = x;

return;

end

X_even = FFT(x(1:2:end));

X_odd = FFT(x(2:2:end));

factor = exp(-2j * pi * (0:N/2-1) / N);

X = [X_even + factor .* X_odd , X_even - factor .* X_odd];

end

If you have gotten something different using that exact code, then you are not using the same input vector that you think you are using, or your code is not what you claim it to be.

I have even seen people swear literally on a stack of bibles (and this was just recently) that they did not have a function by some specific name, when in fact they had written one, squirreled away on their search path. So I would check again. As well, I would verify that the function FFT is exactly as you have shown here. Be very careful, as some people save their own functions in the MATLAB supplied search path itself. (A terribly bad idea.) Changes to those functions are not seen until you force MATLAB to rehash the file cache. That does not happen usually until you restart MATLAB.

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!